keep-network / tbtc-dapp

Deposit BTC and redeem TBTC
http://dapp.test.tbtc.network/
MIT License
33 stars 31 forks source link

Accepting more address types in redemption #174

Open Shadowfiend opened 4 years ago

Shadowfiend commented 4 years ago

EDIT to add: current contracts don't support this. When we bump to the latest tbtc contracts, they do support arbitrary redeemer scripts. We'll need to harmonize what we currently do with that anyway (these scripts require length prefixes, which the current code doesn't ship to the contract) and see what the best way is to support it with bcoin. The rest of the issue remains for now, as it will probably still inform what we end up doing.


Right now, we only accept bech32 p2wpkh addresses in the redemption flow. We should be able to do more than that, with a little bit of massaging of our bcoin invocations.

Here's what we do right now:

https://github.com/keep-network/tbtc-dapp/blob/5b0de38078dcad023046d897c87c4fc175dbc3f4/client/src/redemption.js#L54-L55

For a p2pkh address, we can do script.getPubkeyhash(). For a p2sh/p2wsh address, it looks like things might be slightly more complicated… From some mucking around in node-land:

> const bcoin = require('bcoin/lib/bcoin-browser')
undefined
> const script = bcoin.Script.fromAddress("2MxLAgs87oBXvkAL6CwYPzBfZkhx4fGagEK")
undefined
> script
Script {
  raw:
   <Buffer a9 14 37 c7 f4 29 35 b0 3d c9 65 a9 91 60 5e 8c 6d 2b 4d 98 c2 a8 87>,
  code:
   [ Opcode { value: 169, data: null },
     Opcode {
       value: 20,
       data:
        <Buffer 37 c7 f4 29 35 b0 3d c9 65 a9 91 60 5e 8c 6d 2b 4d 98 c2 a8> },
     Opcode { value: 135, data: null } ] }

99% sure we just want script.code[1].data, based on the fact that this looks like the structure that we look for in p2wpkh:

> const bloop = bcoin.Script.fromAddress("tb1qjy5pyuaatyurt4wh7mq56kvdz6vlld28zu2np2")
> bloop.code
[ Opcode { value: 0, data: null },
  Opcode {
    value: 20,
    data:
     <Buffer 91 28 12 73 bd 59 38 35 d5 d7 f6 c1 4d 59 8d 16 99 ff b5 47> } ]
> bloop.getWitnessPubkeyhash()
<Buffer 91 28 12 73 bd 59 38 35 d5 d7 f6 c1 4d 59 8d 16 99 ff b5 47>

However, in p2wpkh that's behind an OP_HASH160 opcode and in p2sh it seems to be behind no opcode at all sooo… Yeah, not sure. Probably easiest to just try the thing, tbh 🤷‍♂

liamzebedee commented 4 years ago

Low-key excited for this - I've got a hacked-together local Bitcoin regtest and ElectrumX setup here, and it's currently only blocked by the dApp redemption script requiring a specific network prefix in the address.

Shadowfiend commented 4 years ago

Update here: all we need to do is Script.fromAddress, which will either work or not (returning null or not). keep-network/tbtc.js#8 will implement this for tbtc.js, then we'll adjust the validation here briefly.