Skip to content

Commit bba662f

Browse files
committed
wip: batch iframe stamping
1 parent cefc9eb commit bba662f

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

packages/iframe-stamper/src/index.ts

+55
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ export enum IframeEventType {
4747
// Event sent by the iframe to communicate the result of a stamp operation.
4848
// Value: signed payload
4949
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",
5056
// Event sent by the iframe to communicate an error
5157
// Value: serialized error
5258
Error = "ERROR",
@@ -494,4 +500,53 @@ export class IframeStamper {
494500
);
495501
});
496502
}
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+
}
497552
}

packages/sdk-browser/src/sdk-client.ts

+3
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,9 @@ export class TurnkeyIframeClient extends TurnkeyBrowserClient {
462462
this.iframePublicKey = (config.stamper as IframeStamper).iframePublicKey;
463463
}
464464

465+
// Take in a batch of Turnkey requests and resolve them all
466+
batchRequests = async (_requests: Promise<any>[]): Promise<void> => {}
467+
465468
injectCredentialBundle = async (
466469
credentialBundle: string
467470
): Promise<boolean> => {

0 commit comments

Comments
 (0)