Description
This problem has been bothering me for many days, I want to generate a P2WPKH address from an existing private key, but I always get an error. Thank you for helping me.
root@instance-2:/bitcoinjs-lib# npm install bitcoinjs-lib/bitcoinjs-lib# npm install ecpair bip32
root@instance-2:
root@instance-2:~/bitcoinjs-lib# npm install tiny-secp256k1
root@instance-2:~/bitcoinjs-lib# node
Welcome to Node.js v22.12.0.
Type ".help" for more information.
const bitcoin = require('bitcoinjs-lib');
undefined
const ECPairFactory = require('ecpair');
undefined
const ecc = require('tiny-secp256k1');
undefined
const ECPair = ECPairFactory.ECPairFactory(ecc);
undefined
const keyPair = ECPair.fromWIF('KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn');
undefined
const { address } = bitcoin.payments.p2pkh({pubkey:keyPair.publicKey});
Uncaught Error: Expected property "pubkey" of type ?isPoint, got Uint8Array
at captureStackTrace (/root/bitcoinjs-lib/node_modules/typeforce/errors.js:20:11)
at tfSubError (/root/bitcoinjs-lib/node_modules/typeforce/errors.js:99:3)
at _object (/root/bitcoinjs-lib/node_modules/typeforce/index.js:117:15)
at typeforce (/root/bitcoinjs-lib/node_modules/typeforce/index.js:233:9)
at typeforce (/root/bitcoinjs-lib/node_modules/typeforce/index.js:239:10)
at Object.p2pkh (/root/bitcoinjs-lib/node_modules/bitcoinjs-lib/src/payments/p2pkh.js:25:25) {
__label: undefined,
__property: 'pubkey',
__type: [Function: _maybe] { toJSON: [Function (anonymous)] },
__value: Uint8Array(33) [
2, 121, 190, 102, 126, 249, 220, 187,
172, 85, 160, 98, 149, 206, 135, 11,
7, 2, 155, 252, 219, 45, 206, 40,
217, 89, 242, 129, 91, 22, 248, 23,
152
],
__valueTypeName: 'Uint8Array'
}