diff --git a/packages/iframe-stamper/src/index.ts b/packages/iframe-stamper/src/index.ts index 666bcde19..cd3ec5494 100644 --- a/packages/iframe-stamper/src/index.ts +++ b/packages/iframe-stamper/src/index.ts @@ -47,6 +47,12 @@ export enum IframeEventType { // Event sent by the iframe to communicate the result of a stamp operation. // Value: signed payload Stamp = "STAMP", + // Event sent by the parent page to request a batch of signatures + // Value: payloads to sign + BatchStampRequest = "BATCH_STAMP_REQUEST", + // Event sent by the iframe to communicate the result of a batch stamp operation. + // Value: signed payloads + BatchStamp = "BATCH_STAMP", // Event sent by the iframe to communicate an error // Value: serialized error Error = "ERROR", @@ -494,4 +500,53 @@ export class IframeStamper { ); }); } + + /** + * Function to sign a batch of payloads with the underlying iframe + */ + async batchStamp(payloads: string[]): Promise { + if (this.iframePublicKey === null) { + throw new Error( + "null iframe public key. Have you called/awaited .init()?" + ); + } + + const iframeOrigin = this.iframeOrigin; + + this.iframe.contentWindow?.postMessage( + { + type: IframeEventType.BatchStampRequest, + value: payloads, + }, + "*" + ); + + return new Promise(function (resolve, reject) { + window.addEventListener( + "message", + (event) => { + if (event.origin !== iframeOrigin) { + // There might be other things going on in the window, for example: react dev tools, other extensions, etc. + // Instead of erroring out we simply return. Not our event! + return; + } + if (event.data?.type === IframeEventType.BatchStamp) { + const response = JSON.parse(event.data["value"]); // array of stamped values + const stamps = response.map((s: string) => { + return { + stampHeaderName, + stampHeaderValue: s, + }; + }); + + resolve(stamps); + } + if (event.data?.type === IframeEventType.Error) { + reject(event.data["value"]); + } + }, + false + ); + }); + } } diff --git a/packages/sdk-browser/src/sdk-client.ts b/packages/sdk-browser/src/sdk-client.ts index 239f30977..d29bab741 100644 --- a/packages/sdk-browser/src/sdk-client.ts +++ b/packages/sdk-browser/src/sdk-client.ts @@ -462,6 +462,9 @@ export class TurnkeyIframeClient extends TurnkeyBrowserClient { this.iframePublicKey = (config.stamper as IframeStamper).iframePublicKey; } + // Take in a batch of Turnkey requests and resolve them all + batchRequests = async (_requests: Promise[]): Promise => {} + injectCredentialBundle = async ( credentialBundle: string ): Promise => {