@@ -14,18 +14,18 @@ bitcoin.initEccLib(ecc);
1414const bip32 = BIP32Factory ( ecc ) ;
1515
1616describe ( 'bitcoinjs-lib (silent payments)' , ( ) => {
17+ // for simplicity the transactions in this test have only one input and one output
1718 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-
2019 const { senderKeyPair, receiverKeyPair, sharedSecret } = initParticipants ( ) ;
2120
2221 // this is what the sender sees/scans (from twitter bio, public forum, truck door)
2322 const silentPublicKey = toXOnly ( receiverKeyPair . publicKey ) ;
2423
2524 const senderUtxo = await fundP2pkhUtxo ( senderKeyPair . publicKey ) ;
26-
2725 // amount to pay the silent address
2826 const payAmount = senderUtxo . value - 1e4 ;
27+
28+ // The sender pays to the tweaked slient adddress
2929 const {
3030 psbt : payPsbt ,
3131 address : tweakedSilentAddress ,
@@ -42,10 +42,12 @@ describe('bitcoinjs-lib (silent payments)', () => {
4242 const payTx = payPsbt . extractTransaction ( ) ;
4343 await broadcastAndVerifyTx ( payTx , tweakedSilentAddress ! , payAmount ) ;
4444
45- // the amount the receiver will spend
46- const sendAmount = payAmount - 1e4 ;
4745 // the utxo with the tweaked silent address
4846 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
4951 const { psbt : spendPsbt , address } = buildSpendFromSilentAddress (
5052 payTx . getId ( ) ,
5153 receiverUtxo ,
@@ -66,7 +68,9 @@ describe('bitcoinjs-lib (silent payments)', () => {
6668 } ) ;
6769} ) ;
6870
69- async function fundP2pkhUtxo ( senderPubKey : Buffer ) {
71+ async function fundP2pkhUtxo (
72+ senderPubKey : Buffer ,
73+ ) : Promise < { value : number ; script : Buffer ; txId : string } > {
7074 // the input being spent
7175 const { output : p2wpkhOutput } = bitcoin . payments . p2wpkh ( {
7276 pubkey : senderPubKey ,
@@ -85,7 +89,7 @@ async function broadcastAndVerifyTx(
8589 tx : bitcoin . Transaction ,
8690 address : string ,
8791 value : number ,
88- ) {
92+ ) : Promise < void > {
8993 await regtestUtils . broadcast ( tx . toBuffer ( ) . toString ( 'hex' ) ) ;
9094 await regtestUtils . verify ( {
9195 txId : tx . getId ( ) ,
@@ -95,7 +99,11 @@ async function broadcastAndVerifyTx(
9599 } ) ;
96100}
97101
98- function initParticipants ( ) {
102+ function initParticipants ( ) : {
103+ receiverKeyPair : bitcoin . Signer ;
104+ senderKeyPair : bitcoin . Signer ;
105+ sharedSecret : Buffer ;
106+ } {
99107 const receiverKeyPair = bip32 . fromSeed ( rng ( 64 ) , regtest ) ;
100108 const senderKeyPair = bip32 . fromSeed ( rng ( 64 ) , regtest ) ;
101109
@@ -125,7 +133,7 @@ function buildPayToSilentAddress(
125133 silentPublicKey : Buffer ,
126134 sendAmount : number ,
127135 sharedSecret : Buffer ,
128- ) {
136+ ) : { psbt : bitcoin . Psbt ; address : string } {
129137 const psbt = new bitcoin . Psbt ( { network : regtest } ) ;
130138 psbt . addInput ( {
131139 hash : prevOutTxId ,
@@ -141,7 +149,7 @@ function buildPayToSilentAddress(
141149 } ) ;
142150 psbt . addOutput ( { value : sendAmount , address : address ! } ) ;
143151
144- return { psbt, address } ;
152+ return { psbt, address : address ! } ;
145153}
146154
147155function buildSpendFromSilentAddress (
@@ -150,7 +158,7 @@ function buildSpendFromSilentAddress(
150158 silentPublicKey : Buffer ,
151159 sendAmount : number ,
152160 sharedSecret : Buffer ,
153- ) {
161+ ) : { psbt : bitcoin . Psbt ; address : string } {
154162 const psbt = new bitcoin . Psbt ( { network : regtest } ) ;
155163 psbt . addInput ( {
156164 hash : prevOutTxId ,
0 commit comments