NodeJS 使用bitcoinjs-lib发送比特币

uelo1irk  于 2023-05-17  发布在  Node.js
关注(0)|答案(3)|浏览(193)

我在www.example.com上学习比特币教程https://medium.com/@orweinberger/how-to-create-a-raw-transaction-using-bitcoinjs-lib-1347a502a3a#.wkf9g2lk0
我收到未定义的错误

var key = bitcoin.ECKey.fromWIF("L1Kzcyy88LyckShYdvoLFg1FYpB5ce1JmTYtieHrhkN65GhVoq73");

读取我使用阅读https://github.com/bitcoinjs/bitcoinjs-lib/issues/487

var key = bitcoin.ECPair.fromWIF("L1Kzcyy88LyckShYdvoLFg1FYpB5ce1JmTYtieHrhkN65GhVoq73");

对于行:console.log(key.pub.getAddress().toString());(来自教程)
我收到类似的例外:

TypeError: Cannot read property 'getAddress' of undefined
    at repl:1:20
    at REPLServer.defaultEval (repl.js:262:27)
    at bound (domain.js:287:14)
    at REPLServer.runBound [as eval] (domain.js:300:12)
    at REPLServer.<anonymous> (repl.js:431:12)
    at emitOne (events.js:82:20)
    at REPLServer.emit (events.js:169:7)
    at REPLServer.Interface._onLine (readline.js:211:10)
    at REPLServer.Interface._line (readline.js:550:8)
    at REPLServer.Interface._ttyWrite (readline.js:827:14)

'getAddress'方法也不推荐使用,使用什么代替?
还有其他发送比特币的教程吗?他们似乎很难找到?

2hh7jdfx

2hh7jdfx1#

这个应该能用

var key = bitcoin.ECPair.fromWIF("L1Kzcyy88LyckShYdvoLFg1FYpB5ce1JmTYtieHrhkN65GhVoq73");
var address = key.getAddress().toString()

console.log(address) // 17hFoVScNKVDfDTT6vVhjYwvCu6iDEiXC4
g9icjywg

g9icjywg2#

最好还是使用较新版本的bitcoin.js库

const bitcoin = require('bitcoinjs-lib');
let keyPair = bitcoin.ECPair.makeRandom();
let publicKey = keyPair.publicKey
const { address } = bitcoin.payments.p2pkh({ pubkey: publicKey });
const privateKey = keyPair.toWIF();
console.log(address)
console.log(privateKey)
qmb5sa22

qmb5sa223#

2023年最新解决方案:如你所知,ECPair已经在bitcoinjs-lib中与比特币分离。

const bitcoin = require("bitcoinjs-lib");
const ECPairFactory = require('ecpair');
const ecc = require('tiny-secp256k1');

const ECPair = ECPairFactory.ECPairFactory(ecc);
const testnet = bitcoin.networks.testnet;
var key = 
ECPair.fromWIF("Your testnet wallet private key",testnet);
const { address } = bitcoin.payments.p2pkh({ pubkey: key.publicKey,network: testnet });
console.log('====================================');
console.log(address);
console.log('====================================');

相关问题