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 Jan 3, 2025
1 parent 4fceeea commit 546acb7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
38 changes: 37 additions & 1 deletion 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 parent to establish secure communication via MessageChannel API.
// Value: MessageChannel port
TurnkeyInitMessageChannel = "TURNKEY_INIT_MESSAGE_CHANNEL",
Expand Down Expand Up @@ -239,10 +245,22 @@ export class IframeStamper {
switch (event.data?.type) {
case IframeEventType.Stamp:
resolve({
stampHeaderName: stampHeaderName,
stampHeaderName,
stampHeaderValue: event.data["value"],
});
break;
case 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);
break;
}
case IframeEventType.Error:
reject(event.data["value"]);
break;
Expand Down Expand Up @@ -390,4 +408,22 @@ export class IframeStamper {

return this.addMessageHandler();
}

/**
* 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()?"
);
}

this.messageChannel.port1.postMessage({
type: IframeEventType.BatchStamp,
value: payloads,
});

return this.addMessageHandler();
}
}
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 @@ -488,6 +488,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 546acb7

Please sign in to comment.