0

我有这个代码:

var bitcoin = require('bitcoinjs-lib')

var testnet=bitcoin.networks.testnet;
var bitcoin = require('bitcoinjs-lib')
var privateKey = 'cV1hT8dqY6T3UnFjKk883N3nr895JeBVFjMMqi6VbPgDZyfVfzAF'
var keyPair = bitcoin.ECPair.fromWIF(privateKey, testnet);
const RawTransaction = new bitcoin.Psbt(testnet)
RawTransaction.addInput({hash:'9412f5e46f63a65ae140d36cbe6fe137e7f6b047759a8487a73abd5f25727bff', index:0})
RawTransaction.addOutput({address:'tb1q8selj8yz0yh79esajd77x3r6ea5q0vf8svjev4', value:0.00001})


RawTransaction.sign(0, keyPair)
    
var Transaction = RawTransaction.build().toHex();
console.log(Transaction)

我有这个错误:

Error: tb1q8selj8yz0yh79esajd77x3r6ea5q0vf8svjev4 has an invalid prefix

我不明白发生了什么。目标地址是由电子钱包生成的,似乎是正确的。我正在使用测试网。

4

1 回答 1

0

这是检查地址的代码:

if (decodeBech32.prefix !== network.bech32)
    throw new Error(address + ' has an invalid prefix');

默认情况下,该库使用主网网络,由于 testnet 地址前缀,检查失败。

尝试这个:

const RawTransaction = new bitcoin.Psbt({ network: testnet })
于 2020-08-04T10:00:42.980 回答