Skip to content

Commit

Permalink
fix siwe tests
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Jun 5, 2024
1 parent d2a8baf commit 7129745
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
23 changes: 18 additions & 5 deletions packages/taco-auth/src/eip4361.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,38 @@ export class EIP4361SignatureProvider {

private async createSiweMessage(): Promise<AuthSignature> {
const address = await this.signer.getAddress();
const domain = (window?.location?.origin || '').split('//')[1].split('.')[0];
const { domain, uri } = this.getParametersOrDefault();
const version = '1';
const nonce = generateNonce();
const uri = window?.location?.origin || '';
const chainId = (await this.provider.getNetwork()).chainId;
const siweMessage = new SiweMessage({
domain,
address,
statement: `TACo wants you to verify ownership of Ethereum account: ${address}`,
statement: `${domain} wants you to sign in with your Ethereum account: ${address}`,
uri,
version,
nonce,
chainId,
});

const scheme = 'EIP4361';
const message = siweMessage.prepareMessage();
const signature = await this.signer.signMessage(message);

return { signature, address, scheme, typedData: message };
}

private getParametersOrDefault() {
// If we are in a browser environment, we can get the domain and uri from the window object
if (typeof window !== 'undefined') {
const maybeOrigin = window?.location?.origin;
return {
domain: maybeOrigin.split('//')[1].split('.')[0],
uri: maybeOrigin,
};
}
// TODO: Add a facility to manage this case
return {
domain: 'localhost',
uri: 'http://localhost:3000',
};
}
}
5 changes: 2 additions & 3 deletions packages/taco-auth/test/taco-auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,12 @@ describe('taco authorization', () => {
expect(typedSignature.address).toEqual(await signer.getAddress());
expect(typedSignature.scheme).toEqual('EIP4361');

console.log(typedSignature.typedData);
const typedDataSiweMessage = new SiweMessage(`${typedSignature.typedData}`);
expect(typedDataSiweMessage).toBeDefined();
expect(typedDataSiweMessage.domain).toEqual('TACo');
expect(typedDataSiweMessage.domain).toEqual('localhost');
expect(typedDataSiweMessage.version).toEqual('1');
expect(typedDataSiweMessage.nonce).toBeDefined(); // random
expect(typedDataSiweMessage.uri).toEqual('https://TACo');
expect(typedDataSiweMessage.uri).toEqual('http://localhost:3000');
expect(typedDataSiweMessage.chainId).toEqual(
(await provider.getNetwork()).chainId,
);
Expand Down
4 changes: 2 additions & 2 deletions packages/taco/test/conditions/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,9 +440,9 @@ describe('authentication provider', () => {
expect(typedSignature.address).toEqual(signerAddress);

expect(typedSignature.typedData).toContain(
`TACo wants you to sign in with your Ethereum account:\n${signerAddress}`,
`localhost wants you to sign in with your Ethereum account:\n${signerAddress}`,
);
expect(typedSignature.typedData).toContain('URI: https://TACo');
expect(typedSignature.typedData).toContain('URI: http://localhost:3000');

const chainId = (await provider.getNetwork()).chainId;
expect(typedSignature.typedData).toContain(`Chain ID: ${chainId}`);
Expand Down

0 comments on commit 7129745

Please sign in to comment.