简体   繁体   English

bitcoinjs-lib 的无效前缀

[英]invalid prefix with bitcoinjs-lib

I have this code:我有这段代码:

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)

I have this error:我有这个错误:

Error: tb1q8selj8yz0yh79esajd77x3r6ea5q0vf8svjev4 has an invalid prefix

I dont understand what happen.我不明白发生了什么。 The destination address is generated by electrum wallet and seems to be correct.目标地址是由电子钱包生成的,似乎是正确的。 Im using tes.net.我正在使用 tes.net。

Here is the code that checks the address:这是检查地址的代码

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

By default the library uses mai.net.network and the check fails due to tes.net address prefix.默认情况下,库使用 mai.net.network 并且由于 tes.net 地址前缀而导致检查失败。

Try this:试试这个:

const RawTransaction = new bitcoin.Psbt({ network: testnet })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM