Skip to content

Commit

Permalink
feat(auth): set defaults for eip4361 providers
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Jun 26, 2024
1 parent 275f00e commit f77f973
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
10 changes: 7 additions & 3 deletions packages/taco-auth/src/helper.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import {ethers} from "ethers";

import { EIP4361AuthProvider } from './providers/eip4361';
import { EIP4361AuthProvider, EIP4361AuthProviderParams } from './providers/eip4361';
import { EIP712AuthProvider } from './providers/eip712';
import { AuthProviders, EIP4361_AUTH_METHOD, EIP712_AUTH_METHOD } from './types';

export const makeAuthProviders = (provider: ethers.providers.Provider, signer?: ethers.Signer): AuthProviders => {
export const makeAuthProviders = (
provider: ethers.providers.Provider,
signer?: ethers.Signer,
siweDefaultParams?: EIP4361AuthProviderParams
): AuthProviders => {
return {
[EIP712_AUTH_METHOD]: signer ? new EIP712AuthProvider(provider, signer) : undefined,
[EIP4361_AUTH_METHOD]: signer ? new EIP4361AuthProvider(provider, signer) : undefined
[EIP4361_AUTH_METHOD]: signer ? new EIP4361AuthProvider(provider, signer, siweDefaultParams) : undefined
} as AuthProviders;
};
25 changes: 19 additions & 6 deletions packages/taco-auth/src/providers/eip4361.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@ import { AuthSignature, EIP4361_AUTH_METHOD } from '../types';

export type EIP4361TypedData = string;

export type EIP4361AuthProviderParams = {
domain: string;
uri: string;
}

const ERR_MISSING_SIWE_PARAMETERS = 'Missing default SIWE parameters';

export class EIP4361AuthProvider {
private readonly storage: LocalStorage;

constructor(
// TODO: We only need the provider to fetch the chainId, consider removing it
private readonly provider: ethers.providers.Provider,
private readonly signer: ethers.Signer,
private readonly providerParams?: EIP4361AuthProviderParams,
) {
this.storage = new LocalStorage();
}
Expand Down Expand Up @@ -55,7 +63,10 @@ export class EIP4361AuthProvider {
}

// TODO: Create a facility to set these parameters or expose them to the user
private getParametersOrDefault() {
private getParametersOrDefault(): {
domain: string;
uri: string;
} {
// 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;
Expand All @@ -64,10 +75,12 @@ export class EIP4361AuthProvider {
uri: maybeOrigin,
};
}
// TODO: Add a facility to manage this case
return {
domain: 'localhost',
uri: 'http://localhost:3000',
};
if (this.providerParams) {
return {
domain: this.providerParams.domain,
uri: this.providerParams.uri,
}
}
throw new Error(ERR_MISSING_SIWE_PARAMETERS);
}
}
4 changes: 2 additions & 2 deletions packages/taco-auth/test/taco-auth.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
bobSecretKeyBytes,
fakeProvider,
fakeSigner,
fakeSigner, TEST_SIWE_PARAMS,
} from '@nucypher/test-utils';
import { SiweMessage } from 'siwe';
import { describe, expect, it } from 'vitest';
Expand Down Expand Up @@ -43,7 +43,7 @@ describe('taco authorization', () => {
const provider = fakeProvider(bobSecretKeyBytes);
const signer = fakeSigner(bobSecretKeyBytes);

const eip4361Provider = new EIP4361AuthProvider(provider, signer);
const eip4361Provider = new EIP4361AuthProvider(provider, signer, TEST_SIWE_PARAMS);
const typedSignature = await eip4361Provider.getOrCreateAuthSignature();
expect(typedSignature.signature).toBeDefined();
expect(typedSignature.address).toEqual(await signer.getAddress());
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 @@ -11,7 +11,7 @@ import {
USER_ADDRESS_PARAM_EIP4361,
USER_ADDRESS_PARAM_EIP712
} from "@nucypher/taco-auth";
import {fakeAuthProviders, fakeProvider, fakeSigner} from '@nucypher/test-utils';
import { fakeAuthProviders, fakeProvider, fakeSigner, TEST_SIWE_PARAMS } from '@nucypher/test-utils';
import { ethers } from 'ethers';
import { beforeAll, describe, expect, it, vi } from 'vitest';

Expand Down Expand Up @@ -314,7 +314,7 @@ describe('No authentication provider', () => {
await initialize();
provider = fakeProvider();
signer = fakeSigner();
authProviders = makeAuthProviders(provider, signer);
authProviders = makeAuthProviders(provider, signer, TEST_SIWE_PARAMS);
});

it('throws an error if there is no auth provider', () => {
Expand Down
5 changes: 5 additions & 0 deletions packages/test-utils/src/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ export const TEST_CONTRACT_ADDR = '0x0000000000000000000000000000000000000001';
export const TEST_CONTRACT_ADDR_2 =
'0x0000000000000000000000000000000000000002';
export const TEST_CHAIN_ID = ChainId.SEPOLIA;

export const TEST_SIWE_PARAMS = {
domain: 'localhost',
uri: 'http://localhost:3000',
};

0 comments on commit f77f973

Please sign in to comment.