Introduction to Conflux network address

Conflux initially uses the same hex40 format address as Ethereum, but limits the first address. Only 0x0, 0x1 and 0x8 prefixes are legal, and these three prefixes can be used to distinguish the types of addresses:

  • 0x0 built in contract address
  • 0x1 general external account address
  • 0x8 contract address

However, because the address formats are similar and can not be completely used with each other (because Conflux limits the fixed prefix), it is very easy to mix with Ethereum addresses, resulting in asset loss. To solve this problem, Conflux uses CIP-37 A new Base32 format address is introduced.
This article will introduce the Conflux address in detail.

Ethereum hex40 address

The Ethereum account private key is a 256 bit string (32 bytes), usually generated randomly.

18e14a7b6a307f426a94f8114701e7c8e774e7f9a47e2c2035db29a206321725

Then the X and Y values corresponding to the private key are calculated by elliptic curve algorithm. 04 + X + Y is the public key (65 bytes)

04
50863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352
2cd470243453a299fa9e77237716103abc11a1df38855ed6f2ee187e9c582ba6

Finally, Keccak-256 calculates the public key (excluding 04 prefix) to obtain the hash value, and takes the last 20 bytes as the Ethereum address

1016f75c54c607f082ae6b0881fac0abeda21781

That is 0x1016f75c54c607f082ae6b0881fac0abeda21781

And Ethereum passed EIP-55 An address format with verification is introduced. Its implementation is very simple, that is, make a keccak256 hash of the address, then align it by bits, and turn the letters with hash value > = 8 into uppercase:

It should be noted that in the solid smart contract, the address in checkSum format must be used

Conflux Base32 address

The address generation rule of Conflux is the same as that of Ethereum, except that the first address is forcibly changed to 0x1 after address generation

  • 0x7defad05b632ba2cef7ea20731021657e20a7596
  • 0x1defad05b632ba2cef7ea20731021657e20a7596

Then base32 code the address beginning with 0x1 to get the base32 format address currently used by Conflux:

cfx:aarc9abycue0hhzgyrr53m6cxedgccrmmyybjgh4xg

The addresses of the two formats are only encoded differently, but the information expressed is the same and can be converted to each other

Specific coding rules

The encoding uses 32 characters: abcdefghjkmnprstuvwxyz0123456789 (i, l, o, q removed)

network prefix

Determine the network prefix according to networkId:

  • 1: cfxtest
  • 1029: cfx
  • N: netN

payload

The fixed 0 is used as the version byte, and then the byte data of hex40 address is added to construct the payload, and then the payload is base32 encoded.

checksum

Construct the data to calculate the checksum:

  • The lower 5 digits of each character of the network prefix "cfx..." Corresponding to 0x03, 0x06, 0x18
  • One 0 (5 0 bits) as connector
  • payload
  • 8 zeros (as the template of checksum)

Use the following method to calculate a 40 bit number and convert it into 8 base32 characters as the checksum

uint64_t PolyMod(const data &v) {
    uint64_t c = 1;
    for (uint8_t d : v) {
        uint8_t c0 = c >> 35;
        c = ((c & 0x07ffffffff) << 5) ^ d;

        if (c0 & 0x01) c ^= 0x98f2bc8e61;
        if (c0 & 0x02) c ^= 0x79b76d99e2;
        if (c0 & 0x04) c ^= 0xf33e5fb3c4;
        if (c0 & 0x08) c ^= 0xae2eabe2a8;
        if (c0 & 0x10) c ^= 0x1e4f43e470;
    }
    return c ^ 1;
}

Finally connect all data together:

netprefix + ":" + payload + checksum

Optional address type information

The base32 address can contain an optional address type information, which does not participate in the checksum calculation. There are four address types:

  • type.contract general contract
  • type.user ordinary user
  • type.builtin - built in contract
  • type.null zero address type

Generally, addresses with type information are represented in uppercase letters

CFX:TYPE.USER:AAKPBX01FZM1XP89CB7URF6YVYXH3GGX5E9HZ07B38

For specific address specifications, see CIP-37 specification

Base32 usage scenario

Conflux trust from v1.0 1.1 at the beginning, all RPC methods involving addresses only accept addresses in base32 format. Portal has also been upgraded synchronously. By default, addresses in the new format are displayed and accepted. New addresses are required for all major scenarios:

  • RPC interaction
  • Wallet transfer
  • Developing applications using SDK

At present, only one scenario needs to use the hex40 checksum address, that is, when developing a solid smart contract, only the hex40 checksum address can be used in the solid code (limited by the solid compiler)

In addition, when interacting with contract methods, where parameters are passed or results are returned, ABI encoded hex40 addresses are also required. However, when using the SDK of Conflux for interaction, the SDK will automatically convert the address format. Therefore, it can be said that this scenario also uses the address in base32 format.

SDK address method and Scan address translation tool

Each language SDK of Conflux provides two format address conversion methods. Officially developed JS, Go, Java, Python language SDKs, and community developed c + + SDKs.
Take JS SDK as an example:

const {format} = require('js-conflux-sdk');

const hexAddress = "0x12c0ced72d5579b3df107b0697948d267c98d3d9";
const base32Address = "cfx:aakpbx01fzm1xp89cb7urf6yvyxh3ggx5e9hz07b38";
const netId = 1029;

// hex to base32
format.address(hexAddress, netId);
// cfx:aakpbx01fzm1xp89cb7urf6yvyxh3ggx5e9hz07b38
format.hexAddress(base32Address);
// 0x12c0ced72d5579b3df107b0697948d267c98d3d9

In addition, JS-SDK also provides multiple address related utilities methods

const {address} = require('js-conflux-sdk');
/*
{
  encodeCfxAddress: [Function: encode],
  decodeCfxAddress: [Function: decode],
  ethChecksumAddress: [Function: ethChecksumAddress],
  ethAddressToCfxAddress: [Function: ethAddressToCfxAddress],
  ADDRESS_TYPES: {
    USER: 'user',
    CONTRACT: 'contract',
    BUILTIN: 'builtin',
    NULL: 'null'
  },
  isValidCfxAddress: [Function: isValidCfxAddress],
  verifyCfxAddress: [Function: verifyCfxAddress],
  hasNetworkPrefix: [Function: hasNetworkPrefix],
  simplifyCfxAddress: [Function: simplifyCfxAddress],
  shortenCfxAddress: [Function: shortenCfxAddress],
  isZeroAddress: [Function: isZeroAddress],
  isInternalContractAddress: [Function: isInternalContractAddress],
  isValidHexAddress: [Function: isValidHexAddress],
  isValidCfxHexAddress: [Function: isValidCfxHexAddress]
}
*/

address.encodeCfxAddress(hexAddress, netId, true);
// CFX:TYPE.USER:AAKPBX01FZM1XP89CB7URF6YVYXH3GGX5E9HZ07B38
address.decodeCfxAddress(base32Address);
/*
{
  hexAddress: <Buffer 12 c0 ce d7 2d 55 79 b3 df 10 7b 06 97 94 8d 26 7c 98 d3 d9>,
  netId: 1029,
  type: 'user'
}
*/

In addition, the Scan browser also provides a Address translation tool , which is convenient for the address conversion between the two formats

bip44 coin number

Like Ethereum and bitcoin, Conflux also follows the account system introduced by bip32, bip44 and bip39. That is, you can use mnemonics to generate HD wallets.

For example, the wallet derivation path of Ethereum is m/44'/60'/0'/0/0, where 60 is the coin code of ETH.

Conflux The Coin code is 503 , the derived path is: m/44'/503'/0'/0/0

0x1 compatible address generation tool

At present, only addresses beginning with 0x1 in Ethereum network can also be used in Conflux network. If you want to use one address in two networks, you can try to generate some addresses beginning with 0x1. You can use the cli tool cfxjs in JS Conflux SDK to generate:

$ npx cfxjs genEthCMPTaccount  
PrivateKey:  0x6c6faf9644eafe16211ad6a222f7e2a22eb3b10f3145a6226ef0b4c9ef618413
Address:  0x15d4b49ffd7a75a86901cdfce54d85548d3698dd

There's another one Web tools , you can quickly find the index and private key of an 0x1 account from the mnemonics. For safety, please disconnect the Internet and turn on privacy mode

Keywords: Blockchain

Added by stearnol on Thu, 16 Dec 2021 05:50:04 +0200