Skip to content

Commit

Permalink
feat: use nonce instead of incremental promise id
Browse files Browse the repository at this point in the history
- also added a ping in signIn that should resolve almost immediately to avoid auto login stuck
  • Loading branch information
Elabar committed Jan 9, 2025
1 parent 3a83dd0 commit 86655df
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions packages/meteor-wallet-app/src/lib/meteor-wallet-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ enum EMethod {
sign_and_send_transaction = "sign_and_send_transaction",
sign_and_send_transactions = "sign_and_send_transactions",
sign_message = "sign_message",
ping = "ping",
}

type Result =
Expand All @@ -28,9 +29,8 @@ interface IMeteorWalletAppAction {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
};
promiseId: number;
nonce: string;
}
let promiseId = 0;

const postMessage = function (data: string) {
// why this?
Expand Down Expand Up @@ -65,20 +65,42 @@ const postMessage = function (data: string) {
};

const tryPostOrFail = <R extends Result>(
action: IMeteorWalletAppAction
action: Omit<IMeteorWalletAppAction, "nonce">,
timeoutInMs?: number
): Promise<R> => {
postMessage(JSON.stringify(action));
const nonce = Buffer.from(
crypto.getRandomValues(new Uint8Array(10))
).toString("base64");

postMessage(
JSON.stringify({
...action,
nonce,
})
);

return new Promise<R>((resolve, reject) => {
const abortController = new AbortController();
let resolved = false;

if (timeoutInMs) {
setTimeout(() => {
if (!resolved) {
abortController.abort();
reject(new Error(`Timeout of ${timeoutInMs}ms`));
}
}, timeoutInMs);
}

window.addEventListener(
"message",
(ev) => {
if (
typeof ev.data === "object" &&
ev.data?.response &&
ev.data?.promiseId === action.promiseId
ev.data?.nonce === nonce
) {
resolved = true;
if (ev.data.isError) {
reject(new Error(ev.data.response));
} else {
Expand Down Expand Up @@ -110,7 +132,6 @@ const createMeteorWalletAppInjected: WalletBehaviourFactory<
return signedInAccounts;
},
async signAndSendTransaction(params) {
promiseId++;
const receiverId = params.receiverId ?? metadata.contractId;
if (!receiverId) {
throw new Error("No receiver found to send the transaction to");
Expand All @@ -122,28 +143,29 @@ const createMeteorWalletAppInjected: WalletBehaviourFactory<
...params,
receiverId,
},
promiseId,
});
return data;
},
async signAndSendTransactions(params) {
promiseId++;

const data = await tryPostOrFail<Array<FinalExecutionOutcome>>({
method: EMethod.sign_and_send_transactions,
args: {
...params,
},
promiseId,
});
return data;
},
async signIn(params) {
promiseId++;
await tryPostOrFail(
{
method: EMethod.ping,
args: {},
},
1000
);
const data = await tryPostOrFail<Array<Account>>({
method: EMethod.sign_in,
args: params,
promiseId,
});

signedInAccounts = data;
Expand All @@ -152,12 +174,9 @@ const createMeteorWalletAppInjected: WalletBehaviourFactory<
},
async signOut() {
if (signedInAccounts.length > 0) {
promiseId++;

await tryPostOrFail<Array<Account>>({
method: EMethod.sign_out,
args: {},
promiseId,
});

signedInAccounts = [];
Expand All @@ -169,15 +188,12 @@ const createMeteorWalletAppInjected: WalletBehaviourFactory<
);
},
async signMessage(params) {
promiseId++;

const data = await tryPostOrFail<SignedMessage>({
method: EMethod.sign_message,
args: {
...params,
nonce: [...params.nonce],
},
promiseId,
});
return data;
},
Expand Down

0 comments on commit 86655df

Please sign in to comment.