forked from bitcoinjs/bitcoinjs-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay.mjs
105 lines (83 loc) · 4.48 KB
/
play.mjs
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import * as ecc from 'tiny-secp256k1';
import { ECPairFactory } from 'ecpair'
import { Psbt } from './src/psbt.js'
import { p2pkh, p2wpkh, p2tr } from './src/payments/index.js'
import { testnet as network } from './src/networks.js'
console.log(''.padEnd(100, '#'))
const ECPair = ECPairFactory(ecc);
const hex = (s) => Buffer.from(s, 'hex')
const inP2pkhKey = ECPair.fromPrivateKey(hex('82fd530c9eb33570c7e05ca5e80b740bcf1118e8f4c73d44a801fa9dd60f6449'))
const inP2wpkhKey = ECPair.fromPrivateKey(hex('35dfb4dc373860005d6f74d37064e328bc772343c354c95e31b654d0c5e22f58'))
const inP2trKey = ECPair.fromPrivateKey(hex('accaf12e04e11b08fc28f5fe75b47ea663843b698981e31f1cafa2224d6e28c0'))
const outP2trKey = ECPair.fromPrivateKey(hex('900afde76badc8914c9940379c74857d70b4d7da590097285572df6b88ad2975'))
const outP2wpkhKey = ECPair.fromPrivateKey(hex('65ba77c6052f41325d13df8c740b5e33a26d6612e1923bf3afd67ad8081227ee'))
const inP2wpkh = p2wpkh({ pubkey: inP2wpkhKey.publicKey, network })
const inP2tr = p2tr({ internalPubkey: inP2trKey.publicKey.slice(1), network }, { eccLib: ecc })
const outP2tr = p2tr({ internalPubkey: outP2trKey.publicKey.slice(1), network }, { eccLib: ecc })
const outP2wpkh = p2wpkh({ pubkey: outP2wpkhKey.publicKey, network })
const inTweakedP2trKey = Psbt.tweakSigner(inP2trKey, { network })
console.log('### inP2trKey.privateKey ', inP2trKey.privateKey.toString('hex'))
console.log('### inP2trKey.publicKey ', inP2trKey.publicKey.toString('hex'))
console.log('### inP2trKey.toWIF() ', inP2trKey.toWIF())
console.log('### inTweakedP2trKey.privateKey ', inTweakedP2trKey.privateKey.toString('hex'))
console.log('### inTweakedP2trKey.publicKey ', inTweakedP2trKey.publicKey.toString('hex'))
console.log(''.padEnd(100, '#'))
console.log('### inP2tr.script ', inP2tr.output.toString('hex'))
console.log('### inP2tr.address ', inP2tr.address)
console.log('### inP2tr.pubkey ', inP2tr.pubkey.toString('hex'))
console.log(''.padEnd(100, '#'))
console.log('### p2pkh.address ', p2pkh({ pubkey: inP2pkhKey.publicKey, network }).address)
console.log('### p2wpkh.address ', inP2wpkh.address)
console.log('### inP2tr.address ', inP2tr.address)
console.log('### outP2tr.address ', outP2tr.address)
console.log(''.padEnd(100, '#'))
const psbt = new Psbt({ network })
// spend p2pkh
psbt.addInput({
hash: hex('32833f8502f64f85674d2b637ec3ff0032d5585cd305b8d68db4b83ed977f303').reverse(),
index: 0,
}).updateInput(0, { nonWitnessUtxo: hex('0200000000010147d8d83d6dd1dc8c7841f7f42d7239d2318a0a6a9bb5c936a1d54f72dcf859220000000000feffffff02122c1b00000000001976a9149e7ef1767764ff34a0595dbc4c2b70db017ed06688ac9874aeb10000000017a914b0c19f9f547df19b5fbbbfa25c850c6d1e1b550987024730440220301ddecd390bad5f957545a8eb68bf43681d11abf0a15c3a91fa47ff35178e8402206dfa3f611f2bd3da1604c9ff1f758b0cf4881b9f26ed3d5fa44531c36ba463d001210269997f09a81ec9829043a7f407d14e1fbceb799445e96613c852a8be0c1b5132749c2000') })
// spend p2tr
psbt.addInput({
hash: hex('8b9fd7f222dfa16191a15485e241d2e94baa1bef3e6a989cf2d584bda800d066').reverse(),
index: 0,
}).updateInput(1, { witnessUtxo: { script: inP2tr.output, value: 67000 } })
// spend pwpkh
psbt.addInput({
hash: hex('2258ccf6dcb061db928b8f74c6bb74596b43935eb04f03c5d431020ba5e4877e').reverse(),
index: 0,
}).updateInput(2, { witnessUtxo: { script: inP2wpkh.output, value: 10000 } })
psbt.addOutput({
address: outP2tr.address,
value: 1780000
})
psbt.addOutput({
address: outP2wpkh.address,
value: 67000
})
console.log('### psbt 1', psbt.toBase64())
// psbt.signInput(0, inP2pkhKey)
psbt.signInput(1, inTweakedP2trKey)
// psbt.signInput(2, inP2wpkhKey)
console.log('### psbt 2', psbt.toBase64())
const validator = (
pubkey,
msghash,
signature,
) => {
console.log('### verifySchnorr', pubkey.toString('hex'))
// msghash[0]=0
return ECPair.fromPublicKey(pubkey).verifySchnorr(msghash, signature)
};
const isValid = psbt.validateSignaturesOfInput(1, validator)
console.log('### isValid', isValid)
// Serialize tx
psbt.finalizeAllInputs()
const tx = psbt.extractTransaction()
const rawTx = tx.toBuffer()
console.log('### rawTx', rawTx.toString('hex'))
/// 1b9e7e80288a059adb9da6fd7c30e7383a5a924f78d3a0b6b047e07345990f48
/// b2138ee2639c569fe7f2666635399caaa7a435b3763c079f069e194116840892
/// e7a79cc65d17bebfc4cf03d26d6d879b754afe9cdc56ce98d2f1fac29cc7f1e2
/// 8b9fd7f222dfa16191a15485e241d2e94baa1bef3e6a989cf2d584bda800d066
/// 735321667cf43ded23dc935f355c4b1b1e77d9a47d11c77d384af5094e1ad171 -> 3 inputs, 2 outputs