티스토리 뷰
라이브러리 설치
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://www.blockchain.com/explorer/assets/btc/broadcast-transaction
Ref.
- [비트코인 구조] 세그윗(Segwit), Bech32 주소
- bitcoinlib 라이브러리 문서
- HD wallet usage
- Native segwit 주소 생성
728x90
반응형
'비트코인 공부' 카테고리의 다른 글
지갑 여러 개를 관리하고 싶다면? BIP85 (3) | 2023.06.15 |
---|---|
Making bitcoin node reachable (0) | 2023.04.07 |
[umbrel] 1TB NVMe SSD 용량이 부족할 때, 외부 2TB HDD 로 이사가는 방법 (0) | 2023.03.23 |
외장 hdd 에 umbrel 설치하기 (0) | 2023.03.20 |
니모닉을 클라우드에 안전하게 보관하기 (6) | 2023.03.20 |