Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle the case when nonce is a base64 string for signMessage #194

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/frontend/src/utils/signMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ const schema = {
},
};

export const validateNonce = (nonce?: string) => nonce && nonce.length === 32;
const isBase64 = (value: string) => /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$/.test(value);

export const validateNonce = (nonce?: string) => {
if (nonce && isBase64(nonce)) {
return Buffer.from(nonce, 'base64').length === 32;
}
return nonce && nonce.length === 32;
};

export const serialize = (value) => borsh.serialize(schema, value);
export const deserialize = (value) => borsh.deserialize(schema, value);
Expand All @@ -24,10 +31,11 @@ export const messageToSign = (data: {
recipient: string;
callbackUrl?: string;
}) => {
const nonce = isBase64(data.nonce) ? Buffer.from(data.nonce, 'base64') : Buffer.from(data.nonce);
const payload = {
tag: 2147484061,
...data,
nonce: Buffer.from(data.nonce),
nonce,
};

return serialize(payload);
Expand Down
Loading