logo

Addresses

Address#

Overview#

Changes within the aelf blockchain occur through the execution of transactions. An address within aelf identifies a participant in a transaction, either as the sender or the recipient. The sender is denoted as From, while the recipient is denoted as To.

From can represent:

  • User Address: Identifies an individual participant.
  • Contract Address: Identifies a smart contract.
  • Virtual Address: Represents a virtual entity within the blockchain.
  • To is exclusively a Contract Address, indicating that the transaction sender intends to execute a specific method within that smart contract.

    For further details on each type of address in the aelf blockchain, please see below.

    User Address#

    A User Address in aelf is generated from a unique key pair owned by a real user of the blockchain. Here’s how it works:

    Key Pair Generation:#
  • A User Address is derived from a key pair using the IAElfAsymmetricCipherKeyPair interface.
  • This interface includes
  • 1
    public interface IAElfAsymmetricCipherKeyPair
    2
    {
    3
    byte[] PrivateKey { get; }
    4
    byte[] PublicKey { get; }
    5
    }

    Currently, aelf blockchain utilizes ECKeyPair for this purpose, similar to many other blockchain systems.

    Generating a Key Pair:#
  • Users can use the aelf-command tool to create a valid ECKeyPair:
  • 1
    aelf-command create
  • You will need to provide a valid password. The tool generates a .json file containing the public and private keys, encrypted with your password.
  • Using dApp SDK:#

    For dApp developers, the aelf JavaScript SDK js-sdk` offers a method based on bip39 for deterministic key pair generation:

    1
    import Aelf from 'aelf-sdk';
    2
    Aelf.wallet.createNewWallet();

    This method returns an object with the mnemonic, key pair, and address encoded in base58.

    Generating an Address#

    When you create a new wallet using the aelf-sdk, it will return an object with three important parts:

  • Mnemonic: A phrase used to generate the key pair.
  • Key Pair: Contains the public and private keys.
  • Address: The unique identifier for the wallet.
  • In aelf, we usually encode the address in a format called base58. The address is derived from the public key by taking the first 30 bytes of its double SHA-256 hash. Here's how you can get the address from the public key using the aelf-sdk:

    1
    import Aelf from 'aelf-sdk';
    2
    const address = Aelf.wallet.getAddressFromPubKey(pubKey);
    Address Representation:#
  • The User Address in aelf is represented using Protobuf message Address
  • 1
    option csharp_namespace = "AElf.Types";
    2
    message Address
    3
    {
    4
    bytes value = 1;
    5
    }

    In summary, a User Address in aleft blockchain is fundamental for identifying users and interacting with the blockchain securely and effectively.

    Contract Address#

    A Contract Address in aelf blockchain uniquely identifies a Smart Contract. Here’s how it’s created:

  • Calculation Method:
  • A Contract Address is determined during the deployment of a Smart Contract.
  • It’s calculated using a combination of the blockchain's chain id and a *serial number associated with the contract.
  • Implementation:
  • Here’s how you can build a Contract Address in aelf:

    1
    private static Address BuildContractAddress(Hash chainId, long serialNumber)
    2
    {
    3
    var hash = HashHelper.ConcatAndCompute(chainId, HashHelper.ComputeFrom(serialNumber));
    4
    return Address.FromBytes(hash.ToByteArray());
    5
    }
    6
    public static Address BuildContractAddress(int chainId, long serialNumber)
    7
    {
    8
    return BuildContractAddress(HashHelper.ComputeFrom(chainId), serialNumber);
    9
    }
  • HashHelper.ConcatAndCompute: Combines the chain id and serial number hashes.
  • Address.FromBytes: Converts the resulting hash into a readable Address format.
  • Usage:
  • Developers deploying Smart Contracts on aelf blockchain use these methods to generate unique Contract Addresses dynamically.
  • In essence, a Contract Address in aelf blockchain ensures each Smart Contract can be uniquely identified and interacted with securely across the network.

    Contract Virtual Address#

    In the aelf blockchain, every contract has the capability to create additional virtual addresses based on its main Address. These virtual addresses are known as Virtual Addresses.

  • Creation Process:
  • Virtual Addresses are generated by applying a hash function to the main Address of the contract.
  • This process allows contracts to create multiple unique identifiers dynamically.
  • Use Case:
  • For instance, in aelf blockchain, when transferring tokens using the MultiToken contract, both sender and recipient are identified by their respective Addresses.
  • Virtual Addresses extend this functionality by enabling the creation of unique identifiers for specific transactions or actions within a contract.
  • Creation Process:
  • Virtual Addresses are controlled exclusively by the primary contract.
  • This capability allows contracts to manage transactions and funds independently for each user, ensuring secure custody.
  • Utility:
  • Virtual Addresses serve as reliable identifiers generated through business actions on the contract.
  • They are particularly useful for token transfers and other interactions where unique identification is crucial.
  • In summary, Virtual Addresses in aelf blockchain enhance the flexibility and security of contract interactions by providing unique identifiers derived from the main contract Address.

    Edited on: 14 July 2024 05:07:57 GMT+0