I'm trying to generate a SegWit address using my wallet's public key and it doesn't seem to be working. I've tried two different transactions using the addresses generated and neither of them came through to the receiving wallet.
let btc_node = Bitcoin.HDNode.fromBase58( "[xpub key]" );
for( let i = 0; i < 3; i++ ) {
let btc_node_derivation = btc_node.derive( i );
let redeem_script = Bitcoin.script.witnessPubKeyHash.output.encode( Bitcoin.crypto.hash160( btc_node_derivation.getPublicKeyBuffer() ) );
// Generate P2SH address instead of Bech32 for backwards compatibility
let script_pub_key = Bitcoin.script.scriptHash.output.encode( Bitcoin.crypto.hash160( redeem_script ) );
console.log( "segwit address", Bitcoin.address.fromOutputScript( script_pub_key ) );
}
What wallet are you using?
It's a Ledger Nano S. I was able to export the public key to one of the accounts and am using that to generate the address.
@NeuralChris does Ledger Nano S support Segwit P2SH? And if it does, does it support it using the derivation scheme you are using?
Finally, in my own experience, use the Ledger to do all the derivations, and only use fromBase58 to import them. (no .derive).
Yes I believe it does as when I say I want to receive money on my SegWit wallet, it generates an address that starts with a 3. I was trying to generate unique addresses for each user of my ICO so that I can easily track who has and hasn't paid. Is there another way to do this rather than using the ledger? It seemed to work fine on the Ethereum side.
Derive the change layer first.
Change
.derive( i )
To
.derive(0).derive(i)
Ethereum ledger is not BIP44 compliant and there is no concept of change which is why it worked and bitcoin didn鈥檛.
@dabura667 That did it! By adding the .derive(0) before the custom derivation, it generated the proper SegWit address and I was able to send some BTC to the right account on my ledger! I appreciate it.
@dabura667 that... would explain my derivation issues... ha.
let btc_node = Bitcoin.HDNode.fromBase58( "[xpub key]" ).derive(0);
And not having to derive that 0 every time it loops is a little faster too.
@dcousens I think I have an xkcd... ahh yes... 927
Most helpful comment
Ethereum ledger is not BIP44 compliant and there is no concept of change which is why it worked and bitcoin didn鈥檛.