bitcoinjs / bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
MIT License
5.67k stars 2.1k forks source link

Segwit via P2SH issue #1167

Closed PeterGarner closed 6 years ago

PeterGarner commented 6 years ago

Hi, I have some confusion about implementation, apologies in advance for my misunderstanding.

I would like to create p2wpkh addresses but in doing so on testnet my address is not recognised. I also found the main net address from the library example ('can generate a SegWit address') returns an error: https://blockchain.info/rawaddr/bc1qt97wqg464zrhnx23upykca5annqvwkwujjglky Illegal character l at position 39 (using this one 'can generate a SegWit address')

Using 'can generate a SegWit address (via P2SH)' does create recognisable addresses but I'm not clear why p2sh is used with a single signer. I assumed p2sh was only for multisig and other complex types.

(I have a multisig function that works great using p2sh < p2wsh.)

PeterGarner commented 6 years ago

So I found out this is just a limitation of blockchain (or at least on that API); btc.com can check basic segwits just fine. So the question remains, why the conversion to p2sh for single key segwit addresses? Would I be right it assuming this is precisely to make their use easier with apis such as blockchain and no doubt many other wallets, apis and apps? In which case we should just just make the payments using your P2SH(P2WPKH) process and not worry (too much) about the fact that we are creating p2sh for 'standard' keys, is this correct?

dabura667 commented 6 years ago

Correct.

It's a chicken and egg problem.

dcousens commented 6 years ago

@PeterGarner use the SegWit address, IMHO. The P2SH(P2WPKH) will phase out within 12-18 months I think, but, I may be wrong!

It'd be good to see some statistics...

PeterGarner commented 6 years ago

Thanks both @dcousens I would like to use the bech32s. However, I've started developing on testnet and cannot find a faucet for them.

The keys below were derived from the same node for testnet. If anyone knows where I can faucet and I would be grateful.

tb1qgaxxcnqg4qmlgshfuckq6g09xq78he0pc6ulu6 2Muk3rTaiZQRH4EXWfkdrCejMy9maBTm1A8

I have found an explorer however (after a bit of hunt) which will be useful for my project, https://tchain.btc.com/tb1qgaxxcnqg4qmlgshfuckq6g09xq78he0pc6ulu6

My app 'encloses' a transaction on the blockchain so having this will allow me to use Bech32s 'internally'.

I guess the answer here is to use regtest. Unfortunately I'm up against the clock this time so I just need a solution. Thankfully we have p2sh(p2wpkh) :)

dcousens commented 6 years ago

@PeterGarner I recommend you look at the method we use in our integration tests https://github.com/bitcoinjs/bitcoinjs-lib/blob/fbaad04f2440ab666cc1e7096c9167ec66d84cd4/test/integration/_regtest.js#L49-L72

It routes a 'faucet' payment via a P2PKH first, which we then manually form a new descendant transaction to send on to the actual desired output.

PeterGarner commented 6 years ago

Thanks for this. However, I think I can work with testnet and P2SH(P2WPKH). My app internalises transactions with the (internal) receiving addresses being the (potentially large) multisigs, so I can use pure Bech32s for those as they are under my control. Being on the 'receiving' side, they will not need a faucet.