handshake-org/hs-airdrop

How to convert JSON "address" to bech32?

cjb opened this issue · 3 comments

cjb commented

It'd be nice to be able to prove (to myself) that the JSON generated by hs-airdrop is really going to send the reward to the address I passed to hs-airdrop, and not some other address. But the formats aren't the same -- the one on the command line starts with hs and is 42 chars long and the one in the payload's address field is 40 chars long and doesn't match it. Is there a conversion function? Or is that field not where the target address goes..?

Great question. Shortest answer: Handshake airdrop addresses are segwit p2pkh addresses in bech32 format. That means they contain a version and a 20-byte hash. The JSON output splits the two values up and displays them both in hex.

Here's an example using the first faucet address in https://github.com/handshake-org/hs-tree-data/blob/master/proof.json:

$ bin/hs-airdrop hs1q86q9ska4j263p3cmgycqfrxgnjhrvs227uzpz2

...
  "key": {
    "type": "ADDRESS",
    "version": 0,
    "address": "3e80585bb592b510c71b4130048cc89cae36414a",
    "value": 15000500000000,
    "sponsor": true
  },
...

And here's the proof using hsd as a library and calling the Address module:

$ node

> const {Address} = require('hsd')

> new Address({version: 0, hash: Buffer.from('3e80585bb592b510c71b4130048cc89cae36414a', 'hex')})
<Address: version=0 str=hs1q86q9ska4j263p3cmgycqfrxgnjhrvs227uzpz2>

@cjb does @pinheadmz's response alleviate your issue?

cjb commented

Yes! That worked. Thanks for the great answer.