英文原文 : How does Conflux Java SDK adapt CIP37(https://github.com/Conflux-Chain/java-conflux-sdk/blob/master/docs/cfx-address.md)

As a new generation of public links, Conflux not only has super high performance, but also maintains compatibility with the Ethereum ecosystem: using format compatible addresses; EVM compatible VMS are implemented. The benefits of maintaining compatibility are lower costs and barriers to migration. However, similar addresses also bring a lot of problems. For example, when cross-chain operations are carried out through Shuttleflow, similar addresses are often mixed and assets are lost. Asset loss is a very serious problem. In order to optimize cross-chain experience and reduce the problem of incorrect address use, Conflux in CIP37 (https://github.com/Conflux-Chain/CIPs/blob/master/CIPs/cip-37.md) in the attempt to introduce a new address format: base32Check.

Before CIP37

Conflux initially followed roughly the address format of Ethereum, i.e., a hex40 address of length 40 in hexadecimal code, but divided the address into common addresses starting with 0x1, contract addresses starting with 0x8, and built-in contract addresses starting with 0x0. Only hex40 addresses beginning with these three characters are valid Conflux addresses. Some Ethereum addresses (addresses beginning with 0x1) are also valid on Conflux, and conversely, Conflux addresses have a 1/16 chance of being available on Ethereum.

Currently, Conflux has the following three addresses:

  • General address:0x1386b4185a223ef49592233b69291bbe5a80c527
  • Contract Address:0x8269f0add11b4915d78791470d091d25cff73ee5
  • Built-in contract:0x0888000000000000000000000000000000000002

It is because of the incompatibility of addresses that the wrong chain address is used when crossing the chain, resulting in the loss of assets. Other etheric lane in EIP55 (https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md) introduced a checksum with address codes, which will address to meet certain conditions to uppercase characters, thus preventing address input error. Conflux also introduces a verification change rule.

  • Non-checksum address: 0x1386b4185a223ef49592233b69291bbe5a80c527
  • The checksum address is 0x1386B4185A223EF49592233b69291bbe5a80C527

CIP37 address

In order to solve the problem of address confusion with wrong, CIP37 (https://github.com/Conflux-Chain/CIPs/blob/master/CIPs/cip-37.md), the introduction of new base32 check and address, In addition to the checksum, the address can also contain information such as network and type.

Comparison of old and new addresses:

  • Hex40 address:0x1386b4185a223ef49592233b69291bbe5a80c527
  • Provided base32 address: CFX: aak2rra2njvd77ezwjvx04kkds9fzagfe6ku8scz91

New address specifications using custom base32 encoding of the characters, the use of characters are as follows: abcdefghjkmnprstuvwxyz0123456789 (removed o, I, l, q)

In the new format, the network type information is also included, which currently includes three types: CFX, CFxtest, net[n] (Chinese for main network, test network, custom network).

  • cfx:aak2rra2njvd77ezwjvx04kkds9fzagfe6ku8scz91
  • cfxtest:aak2rra2njvd77ezwjvx04kkds9fzagfe6d5r8e957
  • net1921:aak2rra2njvd77ezwjvx04kkds9fzagfe65k87kwdf

In addition, the address can also contain optional types of information. Currently, there are four types of addresses (addresses with types are generally represented in uppercase) :

  • Provided the USER address: CFX: TYPE. USER: AAK2RRA2NJVD77EZWJVX04KKDS9FZAGFE6KU8SCZ91
  • Provided the CONTRACT address: CFX: TYPE. CONTRACT: ACB2RRA2NJVD77EZWJVX04KKDS9FZAGFE640XW9UAE
  • Provided address: built-in contracts CFX: TYPE. BUILTIN: AAEJUAAAAAAAAAAAAAAAAAAAAAAAAAAAAJRWUC9JNB
  • Provided zero address: CFX: TYPE. NULL: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA0SFBNJM2

Hex40 addresses and CIP37 addresses can be converted to each other as byte arrays. However, older versions of hex40 (starting with 0x) require additional network ID information when converting to Base32Check.

Conflux fullnode RPC

Conflux-rust will use the new address format starting with version 1.1.1. The new address format will be used wherever the address is included in the request parameters and return results. The address with the type information is returned.

If you use hex40 in an RPC call, the following error is returned:

{
    "code": -32602."message": "Invalid params: Invalid base32 address: zero or multiple prefixes."
}
Copy the code

If you use the wrong network address (main network RPC service, using the test network address), the following error will be returned:

{
    "code": -32602."message": "Invalid parameters: address"."data": "\"network prefix unexpected: ours cfx, got cfxtest\""
}
Copy the code

Address

Address class originally only a validate the static method, is used only for hex Address format and the types of calibration, in 1.0. This method has been moved to the AddressType validateHexAddress.

Create an instance
import conflux.web3j.types.Address;

// Use the new format address to initialize
Address address = new Address("cfx:aajg4wt2mbmbb44sp6szd783ry0jtad5bea80xdy7p");
// Initialize with hex address and netId
new Address("0x106d49f8505410eb4e671d51f7d96d2c87807b09".1);
Copy the code
Instance methods
address.getAddress(); // get a base32 address
// cfx:aajg4wt2mbmbb44sp6szd783ry0jtad5bea80xdy7p
address.getVerboseAddress(); // get a base32 address with type info
// CFX:TYPE.USER:AAJG4WT2MBMBB44SP6SZD783RY0JTAD5BEA80XDY7P
address.getNetworkId();  // get networkId mainnet is 1029, testnet is 1
/ / 1
address.getType(); // get address type
// user
address.getHexAddress();  // get hex40 address
// 0x106d49f8505410eb4e671d51f7d96d2c87807b09
address.getABIAddress();  // get a org.web3j.abi.datatypes.Address instance
// get a org.web3j.abi.datatypes.Address instance
Copy the code

In version 1.0, the Address class can be used for Address instantiation and anywhere an Address is used, including:

  1. RPC request parameters and return results
  2. Account, AccountManager Related methods
  3. RawTransaction, TransactionBuilder related methods
  4. Call, ContractCall, ERC20, ERC777 related methods

networkId

When converting a hex40 address to a new address, a networkId is required. The current networkId is the same as the chainId value, which is 1029 on the primary network and 1 on the test network

fullnode
  • cfx_getStatusMethod new returnsnetworkIdfield
Interface Cfx

Added methods to get chainId, networkId from instance:

  • getNetworkId()
  • getChainId()
AccountManager

AccountManager instantiation requires the networkId to be passed

Contract interaction

Contract method address parameter

If the address needs to be passed when a contract method is called, there are two ways:

  1. Create the Address instance and callgetABIAddress()Method to get an ABI-encodable address type
import conflux.web3j.types.Address;
Address a = new Address("0x106d49f8505410eb4e671d51f7d96d2c87807b09".1);
a.getABIAddress()
Copy the code
  1. If the address is an Ethereum address, you need to manually create an ABI-encodable address
import org.web3j.abi.datatypes.Address;
new Address("0x206d49f8505410eb4e671d51f7d96d2c87807b09");
Copy the code

Contract returns the Address decode, also need to use the org. Web3j. Abi. Datatypes. Address, and then, according to need configuration networkId baseCheck Address.