Skip to content

Commit

Permalink
feat(auth): expose siwe message creator to user-facing api
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Jun 24, 2024
1 parent 8f795b5 commit f85bc26
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
14 changes: 9 additions & 5 deletions packages/taco-auth/src/eip4361.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,17 @@ export class EIP4361SignatureProvider {

private async createSiweMessage(): Promise<AuthSignature> {
const address = await this.signer.getAddress();
const chainId = (await this.provider.getNetwork()).chainId;
const { domain, uri } = this.getParametersOrDefault();
const message = EIP4361SignatureProvider.prepareSiweMessage(address, chainId, domain, uri);
const signature = await this.signer.signMessage(message);
const scheme = 'EIP4361';
return { signature, address, scheme, typedData: message };
}

public static prepareSiweMessage(address: string, chainId: number, domain: string, uri: string) {
const version = '1';
const nonce = generateNonce();
const chainId = (await this.provider.getNetwork()).chainId;
const siweMessage = new SiweMessage({
domain,
address,
Expand All @@ -47,10 +54,7 @@ export class EIP4361SignatureProvider {
nonce,
chainId,
});
const scheme = 'EIP4361';
const message = siweMessage.prepareMessage();
const signature = await this.signer.signMessage(message);
return { signature, address, scheme, typedData: message };
return siweMessage.prepareMessage();
}

private getParametersOrDefault() {
Expand Down
52 changes: 50 additions & 2 deletions packages/taco/test/conditions/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
EIP712SignatureProvider,
FormattedEIP712,
} from '@nucypher/taco-auth';
import { fakeProvider, fakeSigner } from '@nucypher/test-utils';
import { aliceSecretKeyBytes, fakeProvider, fakeSigner } from '@nucypher/test-utils';
import { ethers } from 'ethers';
import { beforeAll, describe, expect, it, vi } from 'vitest';

Expand Down Expand Up @@ -294,12 +294,14 @@ describe('authentication provider', () => {
expect(authSignature.address).toEqual(signerAddress);

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

const chainId = (await provider.getNetwork()).chainId;
expect(authSignature.typedData).toContain(`Chain ID: ${chainId}`);
expect(authSignature.typedData).toContain(`Version`);
expect(authSignature.typedData).toContain(`Nonce`);
}

beforeAll(async () => {
Expand Down Expand Up @@ -485,6 +487,52 @@ describe('authentication provider', () => {
expect(authSignature).toBeDefined();
await testEIP4361AuthSignature(authSignature);
});

it('creates a siwe message to pass it as a context variable', async () => {
const provider = fakeProvider(aliceSecretKeyBytes);
const signer = fakeSigner(aliceSecretKeyBytes);

const address = await signer.getAddress();
const chainId = (await provider.getNetwork()).chainId;
const domain = 'localhost';
const uri = 'http://localhost:3000';
const message = EIP4361SignatureProvider.prepareSiweMessage(
address,
chainId,
domain,
uri
);
const signature = await signer.signMessage(message);
const authSignature: AuthSignature = { signature, address, scheme: 'EIP4361', typedData: message };
await testEIP4361AuthSignature(authSignature);

const conditionObj = {
...testContractConditionObj,
returnValueTest: {
...testReturnValueTest,
value: USER_ADDRESS_PARAM_EXTERNAL_EIP4361,
},
};
const condition = new ContractCondition(conditionObj);
const conditionExpr = new ConditionExpression(condition);
expect(conditionExpr.contextRequiresSigner()).toBe(true);


const eip4361Spy = vi.spyOn(
EIP4361SignatureProvider.prototype,
'getOrCreateWalletSignature',
);
const customParams: Record<string, CustomContextParam> = {
[USER_ADDRESS_PARAM_EXTERNAL_EIP4361]: authSignature,
};
const builtContext = conditionExpr.buildContext(
provider,
customParams,
signer,
);
await builtContext.toObj();
expect(eip4361Spy).not.toHaveBeenCalledOnce();
});
});

describe('param or context param schema', () => {
Expand Down

0 comments on commit f85bc26

Please sign in to comment.