Skip to content

Commit

Permalink
feat: add siweMessage helper
Browse files Browse the repository at this point in the history
  • Loading branch information
einaralex committed Jan 10, 2025
1 parent f72c80f commit 51909d3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 22 deletions.
31 changes: 10 additions & 21 deletions apps/customer/app/test/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
PaymentStandard,
placeOrderMessage,
rfc3339,
siweMessage,
} from '@monerium/sdk';
import {
MoneriumContext,
Expand Down Expand Up @@ -644,27 +645,15 @@ export default function Test() {
});
};
const authorizeSiwe = () => {
const date = new Date();
const issueDate = rfc3339(new Date(date.toISOString()));

date.setMinutes(date.getMinutes() + 5);
const expiryDate = date.toISOString();

const siwe_message = `localhost:3000 wants you to sign in with your Ethereum account:
0xB64Fed2aFF534D5320BF401d0D5B93Ed7AbCf13E
Allow SDK TEST APP to access my data on Monerium
URI: http://localhost:3000/dashboard
Version: 1
Chain ID: 100
Nonce: ${Math.random().toString(36).substring(2, 16)}
Issued At: ${issueDate}
Expiration Time: ${expiryDate}
Resources:
- https://monerium.com/siwe
- https://example.com/privacy-policy
- https://example.com/terms-of-service`;
const siwe_message = siweMessage({
domain: 'localhost:3000',
address: walletAddress,
appName: 'SDK TEST APP',
redirectUri: 'http://localhost:3000/dashboard',
chainId: chainId,
privacyPolicyUrl: 'https://example.com/privacy-policy',
termsOfServiceUrl: 'https://example.com/terms-of-service',
});

signMessageAsync({ message: siwe_message }).then((signature) => {
siwe({ message: siwe_message, signature });
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export {
getChain,
parseChain,
shortenIban,
siweMessage,
} from './utils';

export { MoneriumClient };
Expand Down
50 changes: 49 additions & 1 deletion packages/sdk/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { Balances, Chain, ChainId, Currency, Environment } from './types';
import { generateCodeChallenge, generateRandomString } from './helpers';
import {
Balances,
Chain,
ChainId,
Currency,
Environment,
EvmChainId,
} from './types';

/**
*
Expand Down Expand Up @@ -136,6 +144,46 @@ export const placeOrderMessage = (
}
return `Send ${curr} ${amount} to ${receiver} at ${rfc3339(new Date())}`;
};
/**
* https://monerium.com/siwe
*/
export const siweMessage = ({
domain,
address,
appName,
redirectUri,
chainId,
issuedAt = new Date().toISOString(),
expiryAt = new Date(Date.now() + 1000 * 60 * 5).toISOString(),
privacyPolicyUrl,
termsOfServiceUrl,
}: {
domain: string;
address: string;
appName: string;
redirectUri: string;
chainId: EvmChainId;
issuedAt: string;
expiryAt: string;
privacyPolicyUrl: string;
termsOfServiceUrl: string;
}) => {
return `${domain} wants you to sign in with your Ethereum account:
${address}
Allow ${appName} to access my data on Monerium
URI: ${redirectUri}
Version: 1
Chain ID: ${chainId}
Nonce: ${generateRandomString().slice(0, 16)}
Issued At: ${issuedAt}
Expiration Time: ${expiryAt}
Resources:
- https://monerium.com/siwe
- ${privacyPolicyUrl}
- ${termsOfServiceUrl}`;
};

/**
* Replacement for URLSearchParams, Metamask snaps do not include node globals.
Expand Down

0 comments on commit 51909d3

Please sign in to comment.