The original English How does Conflux Golang SDK adapt CIP37 (https://github.com/Conflux-Chain/go-conflux-sdk)
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. A Conflux address starting with 0x1 can be used as an Ethereum address, but not necessarily the other way around.
Currently, Conflux has the following three addresses:
- General address:
0x1
386b4185a223ef49592233b69291bbe5a80c527 - Contract Address:
0x8
269f0add11b4915d78791470d091d25cff73ee5 - Built-in contract:
0x0
888000000000000000000000000000000000002
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: 0x1386
b
4185a
223ef
49592233b69291bbe5a80c
527 - The checksum address is 0x1386
B
4185A
223EF
49592233b69291bbe5a80C
527
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:
0x1
386b4185a223ef49592233b69291bbe5a80c527 - Provided base32 address: CFX: aak2rra2njvd77ezwjvx04kkds9fzagfe6ku8scz91
New address specification adopts base32 encoding the address of custom characters, currently used characters are as follows: abcdefghjkmnprstuvwxyz0123456789
In the new format, network type information is also included, which currently includes three types: CFX, CFxtest, net[n]
- 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) :
- user: CFX:TYPE.USER:AAK2RRA2NJVD77EZWJVX04KKDS9FZAGFE6KU8SCZ91
- contract: CFX:TYPE.CONTRACT:ACB2RRA2NJVD77EZWJVX04KKDS9FZAGFE640XW9UAE
- builtin: CFX:TYPE.BUILTIN:AAEJUAAAAAAAAAAAAAAAAAAAAAAAAAAAAJRWUC9JNB
- null: CFX:TYPE.NULL:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA0SFBNJM2
Hex40 addresses and CIP37 addresses can be converted to each other as byte arrays.
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
go-conflux-sdk
Address
Create the Address
There are four ways to create an address instance, each of which provides a Mustxxx method for quick creation that can cause panic if an error occurs. It is also possible to determine whether an address is valid by using the return value error of the following methods as nil
- Created from the base32 string
func NewFromBase32(base32Str string) (cfxAddress Address, err error)
func MustNewFromBase32(base32Str string) (address Address)
Copy the code
- Created from the hex40 string
func NewFromHex(hexAddressStr string, networkID ...uint32) (val Address, err error)
func MustNewFromHex(hexAddressStr string, networkID ...uint32) (val Address)
Copy the code
- Created by common.address
func NewFromCommon(commonAddress common.Address, networkID ...uint32) (val Address, err error)
func MustNewFromCommon(commonAddress common.Address, networkID ...uint32) (address Address)
Copy the code
- Created from the Byte array
func NewFromBytes(hexAddress []byte, networkID ...uint32) (val Address, err error)
func MustNewFromBytes(hexAddress []byte, networkID ...uint32) (address Address)
Copy the code
Note: The networkID parameter is optional. If not, it will be set automatically when RPC requests are sent in the client.
Address instance method
// ToHex returns hex address string and networkID
func (a *Address) ToHex(a) (hexAddressStr string, networkID uint32)
// ToCommon returns common.Address and networkID
func (a *Address) ToCommon(a) (address common.Address, networkID uint32, err error)
// GetBase32Address returns base32 string of address which doesn't include address type
func (a *Address) GetBase32Address(a) string
// GetVerboseBase32Address returns base32 string of address with address type
func (a *Address) GetVerboseBase32Address(a) string
// MustGetHexAddress returns hex format address and panic if error
func (a *Address) GetHexAddress(a) string
// MustGetCommonAddress returns common address and panic if error
func (a *Address) MustGetCommonAddress(a) common.Address
// MustGetNetworkID returns networkID and panic if error
func (a *Address) GetNetworkID(a) uint32
// GetNetworkType returns network type
func (a *Address) GetNetworkType(a) NetworkType
// GetAddressType returuns address type
func (a *Address) GetAddressType(a) AddressType
// GetBody returns body
func (a *Address) GetBody(a) Body
// GetChecksum returns checksum
func (a *Address) GetChecksum(a) Checksum
// CompleteByClient will set networkID by client.GetNetworkID() if a.networkID not be 0
func (a *Address) CompleteByClient(client NetworkIDGetter) error
// CompleteByNetworkID will set networkID if current networkID isn't 0
func (a *Address) CompleteByNetworkID(networkID uint32) error
// IsValid return true if address is valid
func (a *Address) IsValid(a) bool
// MarshalText implements the encoding.TextMarshaler interface.
func (a Address) MarshalText(a) ([]byte, error)
// UnmarshalJSON implements the json.Unmarshaler interface.
func (a *Address) UnmarshalJSON(data []byte) error
Copy the code
NetworkID
The Client class added the GetNetworkID method to get NetowrkID
func (client *Client) GetNetworkID() (uint32, error)
Copy the code
AccountManager
AccountManager instantiation requires the networkId to be passed
func NewAccountManager(keydir string, networkID uint32) *AccountManager
Copy the code
contract
Since the hex40 Address format is used in solidity, the common.address Address format is still used when Contract objects interact with contracts.
Event Decode
The common.address type is still used for event resolution
Internal contract
Address arguments in the built-in contracts AdminControl, Sponsor, and Staking are in the Base32Check format, and the SDK will help with the translation.