-
Notifications
You must be signed in to change notification settings - Fork 1
/
toBech32.js
25 lines (15 loc) · 880 Bytes
/
toBech32.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
let bitcoin = require('bitcoinjs-lib')
function toBech32Add(compressedPubKeyAddress) {
//TEST:
//var compressedPubKeyAddress = '032a707c71d262046dbd1cfa2254b1c70ae179e1311ec37225e07fba343d741a44';
//
if (!Buffer.isBuffer(compressedPubKeyAddress)) compressedPubKeyAddress = Buffer.from(compressedPubKeyAddress, 'hex')
var bech32 = bitcoin.networks.bech32;
var keyPairs = [compressedPubKeyAddress].map(function (q) { return bitcoin.ECPair.fromPublicKeyBuffer(Buffer.from(q, 'hex'), bech32) });
var keyPair = keyPairs[0];
var pubKey = keyPair.getPublicKeyBuffer()
var scriptPubKey = bitcoin.script.witnessPubKeyHash.output.encode(bitcoin.crypto.hash160(pubKey))
return bitcoin.address.fromOutputScript(scriptPubKey)
}
module.exports = toBech32Add;
//toBech32Add('032a707c71d262046dbd1cfa2254b1c70ae179e1311ec37225e07fba343d741a44')