Codificación / decodificación Base58 de direcciones en Micheline
2 respuestas
- votos
-
- 2019-02-02
Las direcciones optimizadastienen 22bytes,que siguenel siguienteformato:
- Elprimerbytees unaetiqueta,ya sea 00paraimplícito (tz) o 01para originado (KT)
Sielprimerbytees 00 yestamostrabajando con una direcciónimplícita (tz),entonces:
- El segundobyte describe la curva ypor lotantoelprefijo. Estoes 00 (ed25519/tz1),01 (secp256k1/tz2) o 02 (p256/tz3)
- Los 20bytes restantes son la dirección
Sielprimerbytees 01 yestamostrabajando con una dirección de origen (KT),entonces:
- Los siguientes 20bytes son la dirección
- El últimobytees unbúfer 00 (para completar la longitud a 22bytes)
Para convertirelformulario optimizadoen una dirección,puede usareztz y hacer algo comoesto:
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"))
Elformatoestá documentado aquí: 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
Creo que deberíapoder utilizar labiblioteca eztz de Stephen Andrewspara acceder a varias herramientas desdeunentornojs
Me atrevería aespecular que lafunciónesesta one donde usael prefijo KT
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')); }
-
Ezygraciaspor la respuesta,pero lointento eztz.utility.b58cencode('011cd5f135e80fd8ebb6e43335b24ca6116edeba6900',eztz.prefix.KT)yestome va avolver"8RYhTWyrcLNgHVTCLfb3KP9TzGRZzttWK7CUJyYNLSD9xjpwC918e3BfpYBpwPBTy5UCi"no"KT1BDEn6wobs7tDReKkGheXAhoq278TGaNn5"Ezy 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
- Михаил Магомедов
-
@ МихаилМагомедов sí,lo siento,hubopasos adicionalesinvolucrados antes de llamar aesafunción como lomostró Stephen.@МихаилМагомедов yea sorry there were extra steps involved prior to calling that function as stephen showed!
- 1
- 2019-02-03
- Ezy
¿Quién conoce algunabiblioteca de JavaScriptpara decodificar la representación Micheline optimizada de un valor de dirección?