Skip to content

Commit

Permalink
wip: batch iframe stamping
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewkmin committed Dec 8, 2024
1 parent cefc9eb commit bba662f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
55 changes: 55 additions & 0 deletions packages/iframe-stamper/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -494,4 +500,53 @@ export class IframeStamper {
);
});
}

/**
* Function to sign a batch of payloads with the underlying iframe
*/
async batchStamp(payloads: string[]): Promise<TStamp[]> {
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
);
});
}
}
3 changes: 3 additions & 0 deletions packages/sdk-browser/src/sdk-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>[]): Promise<void> => {}

injectCredentialBundle = async (
credentialBundle: string
): Promise<boolean> => {
Expand Down

0 comments on commit bba662f

Please sign in to comment.