티스토리 뷰

라이브러리 설치

npm i -g bitcoinjs-lib ecpair tiny-secp256k1
npm link bitcoinjs-lib ecpair tiny-secp256k1

 

 

거래생성 코드

const bitcoin = require('bitcoinjs-lib')
const ecc = require('tiny-secp256k1')
const { ECPairFactory } = require('ecpair')

const ECPair = ECPairFactory(ecc)

const validator = (
  pubkey,
  msghash,
  signature
) => ECPair.fromPublicKey(pubkey).verify(msghash, signature)

const alice = ECPair.fromWIF(
  '~~~ wif string (only-compressed) ~~~'
)

const psbt = new bitcoin.Psbt()
psbt.setVersion(2) // These are defaults. This line is not needed.
psbt.setLocktime(0) // These are defaults. This line is not needed.
psbt.addInput({
  // if hash is string, txid, if hash is Buffer, is reversed compared to txid
  hash: '~~ transaction id (hash value) ~~',
  index: 0,
  sequence: 0xffffffff, // These are defaults. This line is not needed.

  // // If this input was segwit, instead of nonWitnessUtxo, you would add
  // // a witnessUtxo as follows. The scriptPubkey and the value only are needed.
  witnessUtxo: {
    script: Buffer.from(
      '~~~scriptPubKey string~~~',
      'hex'
    ),
    value: 1268
  }
})
psbt.addOutput({
  address: 'output-address',
  value: 1158
})
psbt.signInput(0, alice)
psbt.validateSignaturesOfInput(0, validator)
psbt.finalizeAllInputs()

console.log(psbt.extractTransaction().toHex())

 

 

from) https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts

 

 

 

거래전송

https://mempool.space/tx/push

 

https://www.blockchain.com/explorer/assets/btc/broadcast-transaction

 

 

Ref.

 

 


https://youtu.be/qKC5bmVbemw

 

728x90
반응형
댓글
250x250
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함