Skip to content

Commit

Permalink
consolidate
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewkmin committed Dec 5, 2023
1 parent b3fc41d commit 1b49093
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 49 deletions.
3 changes: 1 addition & 2 deletions examples/email-auth/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ type AuthResponse = {
type InjectCredentialsFormData = {
walletName: string;
authBundle: string;
// authenticatorName: string;
};
type AuthFormData = {
email: string;
Expand Down Expand Up @@ -87,7 +86,7 @@ export default function AuthPage() {
}

try {
await iframeStamper.injectAuthBundle(data.authBundle);
await iframeStamper.injectCredentialBundle(data.authBundle);
} catch (e) {
const msg = `error while injecting bundle: ${e}`;
console.error(msg);
Expand Down
2 changes: 1 addition & 1 deletion examples/email-recovery/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export default function RecoveryPage() {
}

try {
await iframeStamper.injectRecoveryBundle(data.recoveryBundle);
await iframeStamper.injectCredentialBundle(data.recoveryBundle);
} catch (e) {
const msg = `error while injecting bundle: ${e}`;
console.error(msg);
Expand Down
4 changes: 2 additions & 2 deletions packages/iframe-stamper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const iframeStamper = new IframeStamper({
const publicKey = await iframeStamper.init();

// Injects a new credential in the iframe
const injected = await iframeStamper.injectRecoveryBundle(recoveryBundle);
const injected = await iframeStamper.injectCredentialBundle(recoveryBundle);

// New HTTP client able to sign with the credentials inside of the iframe
const httpClient = new TurnkeyClient(
Expand Down Expand Up @@ -75,7 +75,7 @@ const iframeStamper = new IframeStamper({
const publicKey = await iframeStamper.init();

// Injects a new credential in the iframe
const injected = await iframeStamper.injectAuthBundle(authBundle);
const injected = await iframeStamper.injectCredentialBundle(authBundle);

// New HTTP client able to sign with the credentials inside of the iframe
const httpClient = new TurnkeyClient(
Expand Down
49 changes: 5 additions & 44 deletions packages/iframe-stamper/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,15 @@ export enum IframeEventType {
// Event sent by the iframe to its parent to indicate readiness.
// Value: the iframe public key
PublicKeyReady = "PUBLIC_KEY_READY",
// Event sent by the parent to inject a recovery bundle into the iframe.
// Event sent by the parent to inject a credential bundle (for recovery or auth) into the iframe.
// Value: the bundle to inject
InjectRecoveryBundle = "INJECT_RECOVERY_BUNDLE",
InjectCredentialBundle = "INJECT_CREDENTIAL_BUNDLE",
// Event sent by the parent to inject a private key export bundle into the iframe.
// Value: the bundle to inject
InjectKeyExportBundle = "INJECT_KEY_EXPORT_BUNDLE",
// Event sent by the parent to inject a wallet export bundle into the iframe.
// Value: the bundle to inject
InjectWalletExportBundle = "INJECT_WALLET_EXPORT_BUNDLE",
// Event sent by the parent to inject an auth bundle into the iframe.
// Value: the bundle to inject
InjectAuthBundle = "INJECT_AUTH_BUNDLE",
// Event sent by the iframe to its parent when `InjectBundle` is successful
// Value: true (boolean)
BundleInjected = "BUNDLE_INJECTED",
Expand Down Expand Up @@ -132,13 +129,13 @@ export class IframeStamper {
* Function to inject a new credential into the iframe
* The bundle should be encrypted to the iframe's initial public key
* Encryption should be performed with HPKE (RFC 9180).
* This is used during recovery flows.
* This is used during recovery and auth flows.
*/
async injectRecoveryBundle(bundle: string): Promise<boolean> {
async injectCredentialBundle(bundle: string): Promise<boolean> {
return new Promise((resolve, reject) => {
this.iframe.contentWindow?.postMessage(
{
type: IframeEventType.InjectRecoveryBundle,
type: IframeEventType.InjectCredentialBundle,
value: bundle,
},
"*"
Expand Down Expand Up @@ -236,42 +233,6 @@ export class IframeStamper {
});
}

/**
* Function to inject a new credential into the iframe
* The bundle should be encrypted to the iframe's initial public key
* Encryption should be performed with HPKE (RFC 9180).
* This is used during auth flows.
*/
async injectAuthBundle(bundle: string): Promise<boolean> {
return new Promise((resolve, reject) => {
this.iframe.contentWindow?.postMessage(
{
type: IframeEventType.InjectAuthBundle,
value: bundle,
},
"*"
);

window.addEventListener(
"message",
(event) => {
if (event.origin !== this.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.BundleInjected) {
resolve(event.data["value"]);
}
if (event.data?.type === IframeEventType.Error) {
reject(event.data["value"]);
}
},
false
);
});
}

/**
* Function to sign a payload with the underlying iframe
*/
Expand Down

0 comments on commit 1b49093

Please sign in to comment.