3

我正在尝试创建一个原始交易以发送到区块链。为此,我想在浏览器中执行此操作。

我在哪里可以得到nonWitnessUtxo

我在这里列出的所有信息,都是我在测试中找到的。我在做正确的事吗?

const bitcoin = require('bitcoinjs-lib')
const testnet = bitcoin.networks.testnet
const keyPair = bitcoin.ECPair.makeRandom({ network: testnet })
const publicKey = keyPair.publicKey
const { address } = bitcoin.payments.p2pkh({
  pubkey: publicKey,
  network: testnet
})
const privateKey = keyPair.toWIF()
const psbt = new bitcoin.Psbt({ network: testnet })
const txid = '226a14d30cfd411b14bf20b7ffd211f7f206699690c54d456cc1bef70c2de5a6'
const key = bitcoin.ECPair.fromWIF(privateKey, testnet)
psbt.addInput({
  hash: txid,
  index: 0,
  nonWitnessUtxo: Buffer.from('Where can i get this?', 'hex')
})
psbt.addOutput({
  script: Buffer.from('mmpAPZSvhJs1NGw8UaJXEJ9vRByAxProUL', 'hex')
  value: 10000
})
psbt.signInput(0, key)
psbt.validateSignaturesOfInput(0)
psbt.finalizeAllInputs()
psbt.extractTransaction().toHex()

如果有任何帮助,我将不胜感激!

4

2 回答 2

2

The nonWitnessUtxo is the full rawtransaction that you are referencing with the input txid.

于 2020-07-02T14:05:58.157 回答
0

这个答案适用于那些正在寻找一种在浏览器中创建事务但无法处理的人bitcoinjs-lib

我使用bitcore-lib- https://www.npmjs.com/package/bitcore-lib

const bitcore = require('bitcore-lib')

const firstPrivateKey = new bitcore.PrivateKey()
const secondPrivateKey = new bitcore.PrivateKey() 

const wif = firstPrivateKey.toString()
const toAddress = secondPrivateKey.toAddress().toString()
const satoshiAmount = 10000

const privateKey = bitcore.PrivateKey.fromWIF(wif)
const sourceAddress = privateKey.toAddress(bitcore.Networks.testnet)
const targetAddress = bitcore.Address.fromString(toAddress)
const utxos = [
  {
    address: 'mywRqUpbENhbu5VsYDwiMTJouVK9g2ZEJQ',
    txid: '761693565e82ca176532c52a37fb38cd9f1eb0172a00562b394e60ede0b7df8a',
    vout: 1,
    scriptPubKey: '76a914ca133ceac705b723b91263aa163ea8a45954e49a88ac',
    amount: 0.0001,
    satoshis: 10000,
    height: 1578273,
    confirmations: 338
  }
]
const transaction = new bitcore.Transaction()

transaction.from(utxos)
transaction.to(targetAddress, Number(satoshiAmount))
transaction.change(sourceAddress)
transaction.sign(privateKey)

const serializedTX = tx.serialize()

然后你需要将此serializedTX作为原始交易发送到比特币网络。

PS这个例子不起作用,因为有一个无效的呈现utxos。让您utxos使用API,例如https://bitpay.com/api/addr/${sourceAddress}/utxo然后一切都会正常工作。

于 2019-09-22T18:25:41.493 回答