@@ -47,6 +47,12 @@ export enum IframeEventType {
47
47
// Event sent by the iframe to communicate the result of a stamp operation.
48
48
// Value: signed payload
49
49
Stamp = "STAMP" ,
50
+ // Event sent by the parent page to request a batch of signatures
51
+ // Value: payloads to sign
52
+ BatchStampRequest = "BATCH_STAMP_REQUEST" ,
53
+ // Event sent by the iframe to communicate the result of a batch stamp operation.
54
+ // Value: signed payloads
55
+ BatchStamp = "BATCH_STAMP" ,
50
56
// Event sent by the iframe to communicate an error
51
57
// Value: serialized error
52
58
Error = "ERROR" ,
@@ -494,4 +500,53 @@ export class IframeStamper {
494
500
) ;
495
501
} ) ;
496
502
}
503
+
504
+ /**
505
+ * Function to sign a batch of payloads with the underlying iframe
506
+ */
507
+ async batchStamp ( payloads : string [ ] ) : Promise < TStamp [ ] > {
508
+ if ( this . iframePublicKey === null ) {
509
+ throw new Error (
510
+ "null iframe public key. Have you called/awaited .init()?"
511
+ ) ;
512
+ }
513
+
514
+ const iframeOrigin = this . iframeOrigin ;
515
+
516
+ this . iframe . contentWindow ?. postMessage (
517
+ {
518
+ type : IframeEventType . BatchStampRequest ,
519
+ value : payloads ,
520
+ } ,
521
+ "*"
522
+ ) ;
523
+
524
+ return new Promise ( function ( resolve , reject ) {
525
+ window . addEventListener (
526
+ "message" ,
527
+ ( event ) => {
528
+ if ( event . origin !== iframeOrigin ) {
529
+ // There might be other things going on in the window, for example: react dev tools, other extensions, etc.
530
+ // Instead of erroring out we simply return. Not our event!
531
+ return ;
532
+ }
533
+ if ( event . data ?. type === IframeEventType . BatchStamp ) {
534
+ const response = JSON . parse ( event . data [ "value" ] ) ; // array of stamped values
535
+ const stamps = response . map ( ( s : string ) => {
536
+ return {
537
+ stampHeaderName,
538
+ stampHeaderValue : s ,
539
+ } ;
540
+ } ) ;
541
+
542
+ resolve ( stamps ) ;
543
+ }
544
+ if ( event . data ?. type === IframeEventType . Error ) {
545
+ reject ( event . data [ "value" ] ) ;
546
+ }
547
+ } ,
548
+ false
549
+ ) ;
550
+ } ) ;
551
+ }
497
552
}
0 commit comments