- var keyhash = bitcoinjs.bitcoin.crypto.hash160(key.getPublicKeyBuffer());
- var scriptsig = bitcoinjs.bitcoin.script.witnessPubKeyHash.output.encode(keyhash);
- var addressbytes = bitcoinjs.bitcoin.crypto.hash160(scriptsig);
- var scriptpubkey = bitcoinjs.bitcoin.script.scriptHash.output.encode(addressbytes);
- address = bitcoinjs.bitcoin.address.fromOutputScript(scriptpubkey, network)
+ if (isP2wpkh) {
+ var keyhash = bitcoinjs.bitcoin.crypto.hash160(key.getPublicKeyBuffer());
+ var scriptpubkey = bitcoinjs.bitcoin.script.witnessPubKeyHash.output.encode(keyhash);
+ address = bitcoinjs.bitcoin.address.fromOutputScript(scriptpubkey, network)
+ }
+ else if (isP2wpkhInP2sh) {
+ var keyhash = bitcoinjs.bitcoin.crypto.hash160(key.getPublicKeyBuffer());
+ var scriptsig = bitcoinjs.bitcoin.script.witnessPubKeyHash.output.encode(keyhash);
+ var addressbytes = bitcoinjs.bitcoin.crypto.hash160(scriptsig);
+ var scriptpubkey = bitcoinjs.bitcoin.script.scriptHash.output.encode(addressbytes);
+ address = bitcoinjs.bitcoin.address.fromOutputScript(scriptpubkey, network)
+ }