地址(Addresses)

请在此介绍地址、格式和校验和。

还可查看此处: constants.AddressZero

地址格式

地址(Address)

一个地址(Address)是一个20字节(40 nibbles)的DataHexString,混合大小写(mixed case)。

如果大小写混合,它是一个Checksum Address,它在给定地址中使用特定的大写和小写字母模式, 以减少输入地址或剪切和粘贴问题带来的错误风险。

所有返回地址的函数将返回一个校验和地址。

ICAP Address

The ICAP Address Format was an early attempt to introduce a checksum into Ethereum addresses using the popular banking industry's IBAN format with the country code specified as XE.

Due to the way IBAN encodes address, only addresses that fit into 30 base-36 characters are actually compatible, so the format was adapted to support 31 base-36 characters which is large enough for a full Ethereum address, however the preferred method was to select a private key whose address has a 0 as the first byte, which allows the address to be formatted as a fully compatibly standard IBAN address with 30 base-36 characters.

In general this format is no longer widely supported anymore, however any function that accepts an address can receive an ICAP address, and it will be converted internally.

要将地址转换为ICAP格式,请参见getIcapAddress

转换和验证

ethers.utils.getAddress( address ) string< 地址(Address) >

返回一个校验和地址

如果address是一个无效的40-nibble HexString,或者它包含大小写混合且校验和无效,则抛出INVALID_ARGUMENT错误。

address的值可以是任何所支持的地址格式。

// 注入校验和(通过大写字母) getAddress("0x8ba1f109551bd432803012645ac136ddd64dba72"); // '0x8ba1f109551bD432803012645Ac136ddd64DBA72' // 转换并注入校验和 getAddress("XE65GB6LDNXYOFTX0NSV3FUWKOWIXAMJK36"); // '0x8ba1f109551bD432803012645Ac136ddd64DBA72' // 如果提供了一个校验和地址,但字母是错误的情况,则抛出错误 // ------------B 应为小写字母 getAddress("0x8Ba1f109551bD432803012645Ac136ddd64DBA72") // [Error: bad address checksum] { // argument: 'address', // code: 'INVALID_ARGUMENT', // reason: 'bad address checksum', // value: '0x8Ba1f109551bD432803012645Ac136ddd64DBA72' // } // 如果 ICAP/IBAN 校验和失败抛出错误 getIcapAddress("XE65GB6LDNXYOFTX0NSV3FUWKOWIXAMJK37"); // Error: getIcapAddress is not defined // 如果地址不合法抛出错误 getIcapAddress("I like turtles!"); // Error: getIcapAddress is not defined
ethers.utils.getIcapAddress( address ) string< IcapAddress >

返回一个ICAP address地址。 与getAddress具有相同的限制条件。

getIcapAddress("0x8ba1f109551bd432803012645ac136ddd64dba72"); // 'XE65GB6LDNXYOFTX0NSV3FUWKOWIXAMJK36' getIcapAddress("XE65GB6LDNXYOFTX0NSV3FUWKOWIXAMJK36"); // 'XE65GB6LDNXYOFTX0NSV3FUWKOWIXAMJK36'
ethers.utils.isAddress( address ) boolean

如果地址有效(任何支持的格式)则返回true。

isAddress("0x8ba1f109551bd432803012645ac136ddd64dba72"); // true isAddress("XE65GB6LDNXYOFTX0NSV3FUWKOWIXAMJK36"); // true isAddress("I like turtles."); // false

Derivation

ethers.utils.computeAddress( publicOrPrivateKey ) string< 地址(Address) >

返回publicOrPrivateKey的地址。公钥可以压缩或不压缩,私钥将自动转换为派生的公钥。

// 私钥 computeAddress("0xb976778317b23a1385ec2d483eda6904d9319135b89f1d8eee9f6d2593e2665d"); // '0x0Ac1dF02185025F65202660F8167210A80dD5086' // 公钥 (压缩过后的) computeAddress("0x0376698beebe8ee5c74d8cc50ab84ac301ee8f10af6f28d0ffd6adf4d6d3b9b762"); // '0x0Ac1dF02185025F65202660F8167210A80dD5086' // 公钥 (未被压缩的) computeAddress("0x0476698beebe8ee5c74d8cc50ab84ac301ee8f10af6f28d0ffd6adf4d6d3b9b762d46ca56d3dad2ce13213a6f42278dabbb53259f2d92681ea6a0b98197a719be3"); // '0x0Ac1dF02185025F65202660F8167210A80dD5086'
ethers.utils.recoverAddress( digest , signature ) string< 地址(Address) >

使用ECDSA Public Key Recovery来确定摘要(digest)生成签名的公钥地址,

const digest = "0x7c5ea36004851c764c44143b1dcb59679b11c9a68e5f41497f6cf3d480715331"; // 使用一个拓展的签名 recoverAddress(digest, { r: "0x528459e4aec8934dc2ee94c4f3265cf6ce00d47cf42bb106afda3642c72e25eb", s: "0x42544137118256121502784e5a6425e6183ca964421ecd577db6c66ba9bccdcf", v: 27 }); // '0x0Ac1dF02185025F65202660F8167210A80dD5086' // Using a flat Signature const signature = "0x528459e4aec8934dc2ee94c4f3265cf6ce00d47cf42bb106afda3642c72e25eb42544137118256121502784e5a6425e6183ca964421ecd577db6c66ba9bccdcf1b"; recoverAddress(digest, signature); // '0x0Ac1dF02185025F65202660F8167210A80dD5086'

合约地址

ethers.utils.getContractAddress( transaction ) string< 地址(Address) >

如果一个交易用于部署合约,则返回部署合约后的合约地址。

const from = "0x8ba1f109551bD432803012645Ac136ddd64DBA72"; const nonce = 5; getContractAddress({ from, nonce }); // '0x082B6aC9e47d7D83ea3FaBbD1eC7DAba9D687b36'
ethers.utils.getCreate2Address( from , salt , initCodeHash ) string< 地址(Address) >

返回给定CREATE2调用后的合约地址。

const from = "0x8ba1f109551bD432803012645Ac136ddd64DBA72"; const salt = "0x7c5ea36004851c764c44143b1dcb59679b11c9a68e5f41497f6cf3d480715331"; const initCode = "0x6394198df16000526103ff60206004601c335afa6040516060f3"; const initCodeHash = keccak256(initCode); getCreate2Address(from, salt, initCodeHash); // '0x533ae9d683B10C02EbDb05471642F85230071FC3'