1
1
import * as assert from 'assert' ;
2
2
import { describe , it } from 'mocha' ;
3
3
4
- import { bip32 , ECPair , networks as NETWORKS , Psbt } from '..' ;
4
+ import {
5
+ bip32 ,
6
+ ECPair ,
7
+ networks as NETWORKS ,
8
+ Psbt ,
9
+ Signer ,
10
+ SignerAsync ,
11
+ } from '..' ;
5
12
6
13
import * as preFixtures from './fixtures/psbt.json' ;
7
14
@@ -22,6 +29,40 @@ const fixtures = initBuffers(preFixtures);
22
29
const upperCaseFirstLetter = ( str : string ) : string =>
23
30
str . replace ( / ^ ./ , s => s . toUpperCase ( ) ) ;
24
31
32
+ const toAsyncSigner = ( signer : Signer ) : SignerAsync => {
33
+ const ret : SignerAsync = {
34
+ publicKey : signer . publicKey ,
35
+ sign : ( hash : Buffer , lowerR : boolean | undefined ) : Promise < Buffer > => {
36
+ return new Promise (
37
+ ( resolve , rejects ) : void => {
38
+ setTimeout ( ( ) => {
39
+ try {
40
+ const r = signer . sign ( hash , lowerR ) ;
41
+ resolve ( r ) ;
42
+ } catch ( e ) {
43
+ rejects ( e ) ;
44
+ }
45
+ } , 10 ) ;
46
+ } ,
47
+ ) ;
48
+ } ,
49
+ } ;
50
+ return ret ;
51
+ } ;
52
+ const failedAsyncSigner = ( publicKey : Buffer ) : SignerAsync => {
53
+ return {
54
+ publicKey,
55
+ sign : ( __ : Buffer ) : Promise < Buffer > => {
56
+ return new Promise (
57
+ ( _ , reject ) : void => {
58
+ setTimeout ( ( ) => {
59
+ reject ( new Error ( 'sign failed' ) ) ;
60
+ } , 10 ) ;
61
+ } ,
62
+ ) ;
63
+ } ,
64
+ } ;
65
+ } ;
25
66
// const b = (hex: string) => Buffer.from(hex, 'hex');
26
67
27
68
describe ( `Psbt` , ( ) => {
@@ -164,25 +205,39 @@ describe(`Psbt`, () => {
164
205
it ( f . description , async ( ) => {
165
206
if ( f . shouldSign ) {
166
207
const psbtThatShouldsign = Psbt . fromBase64 ( f . shouldSign . psbt ) ;
167
- assert . doesNotReject ( async ( ) => {
208
+ await assert . doesNotReject ( async ( ) => {
168
209
await psbtThatShouldsign . signInputAsync (
169
210
f . shouldSign . inputToCheck ,
170
211
ECPair . fromWIF ( f . shouldSign . WIF ) ,
171
212
f . shouldSign . sighashTypes || undefined ,
172
213
) ;
173
214
} ) ;
215
+ await assert . rejects ( async ( ) => {
216
+ await psbtThatShouldsign . signInputAsync (
217
+ f . shouldSign . inputToCheck ,
218
+ failedAsyncSigner ( ECPair . fromWIF ( f . shouldSign . WIF ) . publicKey ) ,
219
+ f . shouldSign . sighashTypes || undefined ,
220
+ ) ;
221
+ } , / s i g n f a i l e d / ) ;
174
222
}
175
223
176
224
if ( f . shouldThrow ) {
177
225
const psbtThatShouldThrow = Psbt . fromBase64 ( f . shouldThrow . psbt ) ;
178
- assert . rejects ( async ( ) => {
226
+ await assert . rejects ( async ( ) => {
179
227
await psbtThatShouldThrow . signInputAsync (
180
228
f . shouldThrow . inputToCheck ,
181
229
ECPair . fromWIF ( f . shouldThrow . WIF ) ,
182
230
( f . shouldThrow as any ) . sighashTypes || undefined ,
183
231
) ;
184
232
} , new RegExp ( f . shouldThrow . errorMessage ) ) ;
185
- assert . rejects ( async ( ) => {
233
+ await assert . rejects ( async ( ) => {
234
+ await psbtThatShouldThrow . signInputAsync (
235
+ f . shouldThrow . inputToCheck ,
236
+ toAsyncSigner ( ECPair . fromWIF ( f . shouldThrow . WIF ) ) ,
237
+ ( f . shouldThrow as any ) . sighashTypes || undefined ,
238
+ ) ;
239
+ } , new RegExp ( f . shouldThrow . errorMessage ) ) ;
240
+ await assert . rejects ( async ( ) => {
186
241
await ( psbtThatShouldThrow . signInputAsync as any ) (
187
242
f . shouldThrow . inputToCheck ,
188
243
) ;
@@ -229,7 +284,7 @@ describe(`Psbt`, () => {
229
284
it ( f . description , async ( ) => {
230
285
if ( f . shouldSign ) {
231
286
const psbtThatShouldsign = Psbt . fromBase64 ( f . shouldSign . psbt ) ;
232
- assert . doesNotReject ( async ( ) => {
287
+ await assert . doesNotReject ( async ( ) => {
233
288
await psbtThatShouldsign . signAllInputsAsync (
234
289
ECPair . fromWIF ( f . shouldSign . WIF ) ,
235
290
f . shouldSign . sighashTypes || undefined ,
@@ -239,13 +294,13 @@ describe(`Psbt`, () => {
239
294
240
295
if ( f . shouldThrow ) {
241
296
const psbtThatShouldThrow = Psbt . fromBase64 ( f . shouldThrow . psbt ) ;
242
- assert . rejects ( async ( ) => {
297
+ await assert . rejects ( async ( ) => {
243
298
await psbtThatShouldThrow . signAllInputsAsync (
244
299
ECPair . fromWIF ( f . shouldThrow . WIF ) ,
245
300
( f . shouldThrow as any ) . sighashTypes || undefined ,
246
301
) ;
247
302
} , new RegExp ( 'No inputs were signed' ) ) ;
248
- assert . rejects ( async ( ) => {
303
+ await assert . rejects ( async ( ) => {
249
304
await ( psbtThatShouldThrow . signAllInputsAsync as any ) ( ) ;
250
305
} , new RegExp ( 'Need Signer to sign input' ) ) ;
251
306
}
@@ -288,7 +343,7 @@ describe(`Psbt`, () => {
288
343
it ( f . description , async ( ) => {
289
344
if ( f . shouldSign ) {
290
345
const psbtThatShouldsign = Psbt . fromBase64 ( f . shouldSign . psbt ) ;
291
- assert . doesNotReject ( async ( ) => {
346
+ await assert . doesNotReject ( async ( ) => {
292
347
await psbtThatShouldsign . signInputHDAsync (
293
348
f . shouldSign . inputToCheck ,
294
349
bip32 . fromBase58 ( f . shouldSign . xprv ) ,
@@ -299,14 +354,14 @@ describe(`Psbt`, () => {
299
354
300
355
if ( f . shouldThrow ) {
301
356
const psbtThatShouldThrow = Psbt . fromBase64 ( f . shouldThrow . psbt ) ;
302
- assert . rejects ( async ( ) => {
357
+ await assert . rejects ( async ( ) => {
303
358
await psbtThatShouldThrow . signInputHDAsync (
304
359
f . shouldThrow . inputToCheck ,
305
360
bip32 . fromBase58 ( f . shouldThrow . xprv ) ,
306
361
( f . shouldThrow as any ) . sighashTypes || undefined ,
307
362
) ;
308
363
} , new RegExp ( f . shouldThrow . errorMessage ) ) ;
309
- assert . rejects ( async ( ) => {
364
+ await assert . rejects ( async ( ) => {
310
365
await ( psbtThatShouldThrow . signInputHDAsync as any ) (
311
366
f . shouldThrow . inputToCheck ,
312
367
) ;
@@ -354,7 +409,7 @@ describe(`Psbt`, () => {
354
409
it ( f . description , async ( ) => {
355
410
if ( f . shouldSign ) {
356
411
const psbtThatShouldsign = Psbt . fromBase64 ( f . shouldSign . psbt ) ;
357
- assert . doesNotReject ( async ( ) => {
412
+ await assert . doesNotReject ( async ( ) => {
358
413
await psbtThatShouldsign . signAllInputsHDAsync (
359
414
bip32 . fromBase58 ( f . shouldSign . xprv ) ,
360
415
( f . shouldSign as any ) . sighashTypes || undefined ,
@@ -364,13 +419,13 @@ describe(`Psbt`, () => {
364
419
365
420
if ( f . shouldThrow ) {
366
421
const psbtThatShouldThrow = Psbt . fromBase64 ( f . shouldThrow . psbt ) ;
367
- assert . rejects ( async ( ) => {
422
+ await assert . rejects ( async ( ) => {
368
423
await psbtThatShouldThrow . signAllInputsHDAsync (
369
424
bip32 . fromBase58 ( f . shouldThrow . xprv ) ,
370
425
( f . shouldThrow as any ) . sighashTypes || undefined ,
371
426
) ;
372
427
} , new RegExp ( 'No inputs were signed' ) ) ;
373
- assert . rejects ( async ( ) => {
428
+ await assert . rejects ( async ( ) => {
374
429
await ( psbtThatShouldThrow . signAllInputsHDAsync as any ) ( ) ;
375
430
} , new RegExp ( 'Need HDSigner to sign input' ) ) ;
376
431
}
0 commit comments