7

who know some javascript library for decoding optimized Micheline representation of an address value?

011cd5f135e80fd8ebb6e43335b24ca6116edeba6900 => KT1BDEn6wobs7tDReKkGheXAhoq278TGaNn5
0
8

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

0
3

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'));
  }
2
  • 1
    Ezy thanks for answer, but i try eztz.utility.b58cencode('011cd5f135e80fd8ebb6e43335b24ca6116edeba6900',eztz.prefix.KT) and this will return me "8RYhTWyrcLNgHVTCLfb3KP9TzGRZzttWK7CUJyYNLSD9xjpwC918e3BfpYBpwPBTy5UCi" not "KT1BDEn6wobs7tDReKkGheXAhoq278TGaNn5" Feb 2 '19 at 22:48
  • 1
    @МихаилМагомедов yea sorry there were extra steps involved prior to calling that function as stephen showed!
    – Ezy
    Feb 3 '19 at 3:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.