Show / Hide Table of Contents

Lock Contract

A lock contract specifies a certain timestamp, before which no one is allowed to withdraw assets from the contract. Once the time specified is reached, the contract owners can then withdraw the assets.

The current time obtained through the contract is the time of the latest block in the blockchain (the error is about 15 seconds). For details, refer to Blockchain class , Header class .

This section describes how to deploy a lock contract in the blockchain, so that it can be called by others.

Before start, you need to download the latest client Neo GUI from GitHub and run the test net .

Creating a wallet

  1. In Neo-GUI, click Wallet -> New Wallet Database to create a wallet.

  2. Right-click on the account, view the private key, and copy the public key from the second line

Converting the public key into byte array

Here we write a local program to convert the public key into a byte array, as follows:

namespace ConsoleApp1
    class Program
        static void Main(string[] args)
            // Replace the string with the public key copied in the last step
            byte[] b = HexToBytes("0285eab65f4a0126e4b85b4e5d8b7e303aff7efb360d595f2e3189bb90487ad5aa");
            foreach (var item in b)
                Console.Write($"{item}, ");

        static byte[] HexToBytes(string hexString)
            hexString = hexString.Trim();
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;

After running it, the screen will display the byte array created from the public key. Copy this down as we will be using it later.

Writing a smart contract

In Visual Studio create a smart contract project and write the following smart contract.

using Neo.SmartContract.Framework.Services.Neo;

namespace Neo.SmartContract
    public class Lock : Framework.SmartContract
        public static bool Main(uint timestamp, byte[] pubkey, byte[] signature)
            Header header = Blockchain.GetHeader(Blockchain.GetHeight());
            if (header.Timestamp < 1588327800) // 2020-5-1 18:10
                return false;
            // Paste the public key byte array here
            return VerifySignature(signature,new byte[] { 2, 133, 234, 182, 95, 74, 1, 38, 228, 184, 91, 78, 93, 139, 126, 48, 58, 255, 126, 251, 54, 13, 89, 95, 46, 49, 137, 187, 144, 72, 122, 213, 170 });

You need to change these two variables in the code: the public key and the lock time.

  • public key: Paste the previous copy of the public key byte array.

  • lock time: Change the lock time in the sample code, which is a Unix timestamp. You can calculate it yourself or use an online tool Unix timestamp online conversion .

After replacing the two variables, compile the contract into the file Lock.avm.

Obtaining the contract script

You can choose one of the following ways to obtain the contract script:

  • Use the C# code below to read the .avm to get the bytecode.

byte[] bytes = System.IO.File.ReadAllBytes("Lock.avm");
for (int i = 0; i < bytes.Length; i++)
  • Use Neo-GUI to obtain the script:

    1. Click Advanced -> Deploy Contract .

    2. click the Load button on the bottom right corner. Choose the Lock.avm file generated earlier.

    3. Copy the contract script displayed in the Code box, as shown below.

Creating a contract address

  1. Right-click on the address area and select Create Contract Add -> Custom to create a contract address with the contract script generated before.

  2. In the Import Custom Contract dialog, specify the following:

    1. Parameter List: Because our contract has a parameter for signature, you should fill in 00 . For details, refer to Parameter .

    2. Script: enter the contract script copied from previous step.

    3. Private Key: Optional. When the contract needs to be signed, specify the private key used for signing.

  3. After clicking OK , the smart contract verification account is created successfully.


Next you can do a test of the smart contract verification account. The testing process is to first transfer some assets into the account address, then transfer them out using Neo-GUI.

In order to ensure the accuracy of the test, it is best not to have any other assets in the wallet, as you may not know if the assets are coming from a standard address or a contract address, unless you understand the client's change finding algorithm and know which transaction is coming from the contract address.

If your operation is correct, the following happens when the asset is transferred:

When the current time is less than the lockout time, the transfer is not confirmed, ie. the transfer fails.

After clicking Rebuild Index about 5 minutes, the unconfirmed transfer will disappear and the assets will return to the previous state.

If the current time is greater than the lock time, the transfer will be successful.