OutCast3k / coinbin

Javascript Bitcoin Wallet. Supports Multisig, Stealth, HD, SegWit, Bech32, Time Locked Addresses, RBF and more!
https://coinb.in/
MIT License
908 stars 622 forks source link

Generate Testnet SegWit Addresses #217

Open hjoseph96 opened 3 years ago

hjoseph96 commented 3 years ago

I want to support SegWit, but I cannot use them in testnet using the ones generated by coinb.in. In testnet, I generate a bech32 address, but it starts with bc1 (production moede), not tb1.

How can I generate a testnet SegWit address?

ghost commented 3 years ago

You can add this at line 1924 of coinbin.js:

    else if (coinjs.pub == 0x6f){        // BITCOIN TESTNET
        explorer_addr = "https://chain.so/address/BTCTEST/";
        coinjs.bech32.hrp = "tb";
    }

bech32 is not supported very well in coinbin right now. There is a bug generating new bech32 addresses where the redeem script does not generate correctly (missing 0014 at the beginning) so the address generated will be wrong.

Line 193 of coin.js:

/* create a new segwit bech32 encoded address */
coinjs.bech32Address = function(pubkey){
    var program = [0x00,0x14].concat(ripemd160(Crypto.SHA256(Crypto.util.hexToBytes(pubkey), {asBytes: true}), {asBytes: true}));

But fixing the redeem script causes transaction signing to break. Needs some work.

hjoseph96 commented 3 years ago

That sucks, I rely on the transaction signing to broadcast my txs. SegWit has lower Tc_fee's, I wish coinb.in supported it.

zilveer commented 1 year ago

@OutCast3k is it possible for you to fix this issue?

right now the Bitcoin testnet doesn´t support correct bech32 addresses, only bc1 addresses when it should be tp1.