4. Register Users

User Registration

The Zecrey account is represented by a public key, which is generated with the L1 private key.User registration consists of two parts:

  1. Generating Zecrey L2 user account for the L1 account

  2. Registering the L1 to L2 correspondence

This enables the following functionality:

  • A user with an L1 account now has an associated L2 account on Zecrey.

  • L1 users can deposit L1 assets into their L2 accounts.

  • L2 users can perform transactions on Zecrey (create orders, trade assets, transfer assets, etc.).

  • L2 users can withdraw their L2 assets to their L1 accounts.

On-chain registration

  1. First, register a user on the contract related to Zecrey L1.

  2. Then Zecrey L2 synchronizes with the L1 registration of the user. All transactions executed by the user on L2 will be recorded under this registered L1/L2 user.

What happens during the user registration process?

  • Register an account in the L1 contract and match the unique name of the tagged account to this account.

  • Link the account to L2 by submitting the associated information.

  • The L2 program recognizes the associated information submitted by the user, confirms it, saves it in the database, and the registration of the account is complete.

This is an overview of the user registration process:

Register Account With Private Key

  • Since sending a transaction to L1 will consume a certain amount of gas fee, to ensure successful registration, the user needs to ensure that he or she has BNB in the address.

  1. RegisterAccountWithPrivateKey will submit a registration transaction to L1 for registration, and this function will internally determine whether the account has been registered. If it has been registered, will directly return to the client.

    Note: IfAccountRegistered can determine if the account has been registered.

  2. If the RegisterAccountWithPrivateKey function determines internally that the user has not registered on L1, the first step will be executed with the L1 private key, accountName and other information input by the user.

Example

  • Register an account with the name "Alice"

    • Prepare accountName, L1Addr and privateKey.

    • Register an account on L1.

    • If you have not registered before, wait for about 30 seconds and then check whether the registration is successful.

      Note: it takes some time for L1 to confirm the transaction and L2 to synchronize the transaction with L1.

package main

import (
   "encoding/json"
   "fmt"
   "time"

   "github.com/Zecrey-Labs/zecrey-marketplace-go-sdk/sdk"
)

func main() {
   accountName := "Alice"
   l1Addr := "0xD207262DEA01aE806fA2dCaEdd489Bd2f5FABcFE"                             //the address of l1 account
   privateKey := "0x1a061a8e74cee1ce2e2ddd29f5afea99ecfbaf1998b6d349a8c09a368e637b8e" //the private key of l1
   client, err := sdk.RegisterAccountWithPrivateKey(accountName, l1Addr, privateKey)
   if err != nil {
      panic(err)
   }
   isRegister, err := sdk.IfAccountRegistered(accountName)
   if err != nil {
      panic(err)
   }


   if !isRegister{
      time.Sleep(time.Second * 30) //wait for l1 to verify the transaction
      isRegister, err = sdk.IfAccountRegistered(accountName)
      if err != nil {
         panic(err)
      }
      
      data, _ := json.Marshal(isRegister)
      fmt.Println("GetAccountIsRegistered:", string(data))

      accountName, l2pk, seed := client.GetMyInfo() //get information about your account through GetMyInfo()
      fmt.Println("accountName:", accountName)      //account name
      fmt.Println("l2pk:", l2pk)                    //l2 publick key
      fmt.Println("seed:", seed)                    //l2 private key
   }

}

Example result

GetAccountIsRegistered:true
accountName:Alice
l2pk:22fc6f5d74c8639245462a0af6b5c931bd209c04034b28421a60336635ab85950a3163e68ec29319ca200fac009408369b0a1f75200a118aded920cd240e1358
seed:0x7ea589236ac7e6034a40ad31f27a6ea1bbaeb7746ba5e8d3408a3abb480a8688

Last updated