Baseine地址中的地址的Base58编码/解码
2 个回答
- 投票数
-
- 2019-02-02
优化的地址为22个字节,其格式如下:
- 第一个字节是一个标记,对于隐式(tz)来说是00,对于原始(KT)来说是01
如果第一个字节为00,并且我们正在使用隐式(tz)地址,则:
- 第二个字节描述曲线,因此也描述了前缀.这是00(ed25519/tz1),01(secp256k1/tz2)或02(p256/tz3)
- 剩余的20个字节是地址
如果第一个字节为01,并且我们正在使用原始(KT)地址,则:
- 接下来的20个字节是地址
- 最后一个字节是00缓冲区(将长度填充到22个字节)
要将优化的表单转换为地址,可以使用eztz并执行以下操作:
function getAddressFromOptimized(hex){ var address, prefix; if (hex.substring(0,2) == "00") { if (hex.substring(2,4) == "00") prefix = eztz.prefix.tz1; if (hex.substring(2,4) == "01") prefix = eztz.prefix.tz2; if (hex.substring(2,4) == "02") prefix = eztz.prefix.tz3; address = hex.substring(4,44); } else if (hex.substring(0,2) == "01"){ prefix = eztz.prefix.KT; address = hex.substring(2,42); } return eztz.utility.b58cencode(eztz.utility.hex2buf(address), prefix); } console.log(getAddressFromOptimized("011cd5f135e80fd8ebb6e43335b24ca6116edeba6900"))
此处记录了格式: http://tezos.gitlab.io/mainnet/api/p2p.html#contract-id-22-bytes-8-bit-tag
Optimized addresses are 22 bytes, which follows the following format:
- The first byte is a tag, either 00 for implicit (tz) or 01 for originated (KT)
If the first byte is 00 and we are working with an implicit (tz) address, then:
- The second byte describes the curve and therefore the prefix. This is either 00 (ed25519/tz1), 01 (secp256k1/tz2) or 02 (p256/tz3)
- The remaining 20 bytes is the address
If the first byte is 01 and we are working with an originated (KT) address, then:
- The next 20 bytes are the address
- The last byte is a 00 buffer (to pad the length to 22 bytes)
To convert the optimized form to an address, you can use eztz and do something like this:
function getAddressFromOptimized(hex){ var address, prefix; if (hex.substring(0,2) == "00") { if (hex.substring(2,4) == "00") prefix = eztz.prefix.tz1; if (hex.substring(2,4) == "01") prefix = eztz.prefix.tz2; if (hex.substring(2,4) == "02") prefix = eztz.prefix.tz3; address = hex.substring(4,44); } else if (hex.substring(0,2) == "01"){ prefix = eztz.prefix.KT; address = hex.substring(2,42); } return eztz.utility.b58cencode(eztz.utility.hex2buf(address), prefix); } console.log(getAddressFromOptimized("011cd5f135e80fd8ebb6e43335b24ca6116edeba6900"))
The format is documented here: http://tezos.gitlab.io/mainnet/api/p2p.html#contract-id-22-bytes-8-bit-tag
-
- 2019-02-02
我相信您应该能够使用Stephen Andrews eztz 库来访问各种工具js环境
b58cencode: function (payload, prefix) { const n = new Uint8Array(prefix.length + payload.length); n.set(prefix); n.set(payload, prefix.length); return library.bs58check.encode(new Buffer(n, 'hex')); }
I believe you should be able to make use of Stephen Andrews eztz library to access various tools from a js environment
I would speculate the function is this one where you use the KT prefix
b58cencode: function (payload, prefix) { const n = new Uint8Array(prefix.length + payload.length); n.set(prefix); n.set(payload, prefix.length); return library.bs58check.encode(new Buffer(n, 'hex')); }
-
谢谢你的回答,但我尝试 eztz.utility.b58cencode('011cd5f135e80fd8ebb6e43335b24ca6116edeba6900',eztz.prefix.KT),这将返回我" 8RYhTWyrcLNgHVTCLfb3KP9TzGRZzttWK7CUJyYNLSD9xwwCB1TYB1KYPYB1KYPYB1KYPYB1KYPYBYB1KYPYB1BKeKYPYB1BKeKYPYB1BKeKTyBKtBKeB9B0B0CeB0B0CeB2B0B0Ce9B0Ce9B0B0Ce9B0Ce9B0C0B0B0Ce9BEzy thanks for answer, but i try eztz.utility.b58cencode('011cd5f135e80fd8ebb6e43335b24ca6116edeba6900',eztz.prefix.KT) and this will return me "8RYhTWyrcLNgHVTCLfb3KP9TzGRZzttWK7CUJyYNLSD9xjpwC918e3BfpYBpwPBTy5UCi" not "KT1BDEn6wobs7tDReKkGheXAhoq278TGaNn5"
- 1
- 2019-02-02
- Михаил Магомедов
-
@МихаилМагомедов是的,很抱歉,在调用stephen所示的函数之前,还涉及其他步骤!@МихаилМагомедов yea sorry there were extra steps involved prior to calling that function as stephen showed!
- 1
- 2019-02-03
- Ezy
谁知道一些用于解码地址值的优化Micheline表示形式的JavaScript库?