@@ -14,18 +14,18 @@ bitcoin.initEccLib(ecc);
14
14
const bip32 = BIP32Factory ( ecc ) ;
15
15
16
16
describe ( 'bitcoinjs-lib (silent payments)' , ( ) => {
17
+ // for simplicity the transactions in this test have only one input and one output
17
18
it ( 'can create (and broadcast via 3PBP) a simple silent payment' , async ( ) => {
18
- // for simplicity the transactions in this test have only one input and one output
19
-
20
19
const { senderKeyPair, receiverKeyPair, sharedSecret } = initParticipants ( ) ;
21
20
22
21
// this is what the sender sees/scans (from twitter bio, public forum, truck door)
23
22
const silentPublicKey = toXOnly ( receiverKeyPair . publicKey ) ;
24
23
25
24
const senderUtxo = await fundP2pkhUtxo ( senderKeyPair . publicKey ) ;
26
-
27
25
// amount to pay the silent address
28
26
const payAmount = senderUtxo . value - 1e4 ;
27
+
28
+ // The sender pays to the tweaked slient adddress
29
29
const {
30
30
psbt : payPsbt ,
31
31
address : tweakedSilentAddress ,
@@ -42,10 +42,12 @@ describe('bitcoinjs-lib (silent payments)', () => {
42
42
const payTx = payPsbt . extractTransaction ( ) ;
43
43
await broadcastAndVerifyTx ( payTx , tweakedSilentAddress ! , payAmount ) ;
44
44
45
- // the amount the receiver will spend
46
- const sendAmount = payAmount - 1e4 ;
47
45
// the utxo with the tweaked silent address
48
46
const receiverUtxo = { value : payAmount , script : payTx . outs [ 0 ] . script } ;
47
+ // the amount the receiver will spend
48
+ const sendAmount = payAmount - 1e4 ;
49
+
50
+ // the receiver spends from the tweaked silent address
49
51
const { psbt : spendPsbt , address } = buildSpendFromSilentAddress (
50
52
payTx . getId ( ) ,
51
53
receiverUtxo ,
@@ -66,7 +68,9 @@ describe('bitcoinjs-lib (silent payments)', () => {
66
68
} ) ;
67
69
} ) ;
68
70
69
- async function fundP2pkhUtxo ( senderPubKey : Buffer ) {
71
+ async function fundP2pkhUtxo (
72
+ senderPubKey : Buffer ,
73
+ ) : Promise < { value : number ; script : Buffer ; txId : string } > {
70
74
// the input being spent
71
75
const { output : p2wpkhOutput } = bitcoin . payments . p2wpkh ( {
72
76
pubkey : senderPubKey ,
@@ -85,7 +89,7 @@ async function broadcastAndVerifyTx(
85
89
tx : bitcoin . Transaction ,
86
90
address : string ,
87
91
value : number ,
88
- ) {
92
+ ) : Promise < void > {
89
93
await regtestUtils . broadcast ( tx . toBuffer ( ) . toString ( 'hex' ) ) ;
90
94
await regtestUtils . verify ( {
91
95
txId : tx . getId ( ) ,
@@ -95,7 +99,11 @@ async function broadcastAndVerifyTx(
95
99
} ) ;
96
100
}
97
101
98
- function initParticipants ( ) {
102
+ function initParticipants ( ) : {
103
+ receiverKeyPair : bitcoin . Signer ;
104
+ senderKeyPair : bitcoin . Signer ;
105
+ sharedSecret : Buffer ;
106
+ } {
99
107
const receiverKeyPair = bip32 . fromSeed ( rng ( 64 ) , regtest ) ;
100
108
const senderKeyPair = bip32 . fromSeed ( rng ( 64 ) , regtest ) ;
101
109
@@ -125,7 +133,7 @@ function buildPayToSilentAddress(
125
133
silentPublicKey : Buffer ,
126
134
sendAmount : number ,
127
135
sharedSecret : Buffer ,
128
- ) {
136
+ ) : { psbt : bitcoin . Psbt ; address : string } {
129
137
const psbt = new bitcoin . Psbt ( { network : regtest } ) ;
130
138
psbt . addInput ( {
131
139
hash : prevOutTxId ,
@@ -141,7 +149,7 @@ function buildPayToSilentAddress(
141
149
} ) ;
142
150
psbt . addOutput ( { value : sendAmount , address : address ! } ) ;
143
151
144
- return { psbt, address } ;
152
+ return { psbt, address : address ! } ;
145
153
}
146
154
147
155
function buildSpendFromSilentAddress (
@@ -150,7 +158,7 @@ function buildSpendFromSilentAddress(
150
158
silentPublicKey : Buffer ,
151
159
sendAmount : number ,
152
160
sharedSecret : Buffer ,
153
- ) {
161
+ ) : { psbt : bitcoin . Psbt ; address : string } {
154
162
const psbt = new bitcoin . Psbt ( { network : regtest } ) ;
155
163
psbt . addInput ( {
156
164
hash : prevOutTxId ,
0 commit comments