Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.

Commit

Permalink
refactor: clean up naming and todos
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Jun 13, 2024
1 parent ee96bef commit 5eec120
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 43 deletions.
16 changes: 9 additions & 7 deletions packages/taco-auth/src/providers/eip4361.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { ethers } from 'ethers';
import { generateNonce, SiweMessage } from 'siwe';

import { LocalStorage } from '../storage';
import { AuthSignature } from '../types';
import { AuthSignature, EIP4361_AUTH_METHOD } from '../types';

export type FormattedEIP4361 = string;
export type EIP4361TypedData = string;

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,
) {
Expand All @@ -18,7 +19,7 @@ export class EIP4361AuthProvider {

public async getOrCreateAuthSignature(): Promise<AuthSignature> {
const address = await this.signer.getAddress();
const storageKey = `eth-signin-message-${address}`;
const storageKey = `eth-${EIP4361_AUTH_METHOD}-message-${address}`;

// If we have a message in localStorage, return it
const maybeMessage = this.storage.getItem(storageKey);
Expand All @@ -27,12 +28,12 @@ export class EIP4361AuthProvider {
}

// If at this point we didn't return, we need to create a new message
const typedSignature = await this.createSiweMessage();
this.storage.setItem(storageKey, JSON.stringify(typedSignature));
return typedSignature;
const authMessage = await this.createSIWEAuthMessage();
this.storage.setItem(storageKey, JSON.stringify(authMessage));
return authMessage;
}

private async createSiweMessage(): Promise<AuthSignature> {
private async createSIWEAuthMessage(): Promise<AuthSignature> {
const address = await this.signer.getAddress();
const { domain, uri } = this.getParametersOrDefault();
const version = '1';
Expand All @@ -53,6 +54,7 @@ export class EIP4361AuthProvider {
return { signature, address, scheme, typedData: message };
}

// TODO: Create a facility to set these parameters or expose them to the user
private getParametersOrDefault() {
// If we are in a browser environment, we can get the domain and uri from the window object
if (typeof window !== 'undefined') {
Expand Down
13 changes: 7 additions & 6 deletions packages/taco-auth/src/providers/eip712.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface EIP712 {
};
}

export interface FormattedEIP712 extends EIP712 {
export interface EIP712TypedData extends EIP712 {
primaryType: 'Wallet';
types: {
EIP712Domain: { name: string; type: string }[];
Expand Down Expand Up @@ -60,6 +60,7 @@ export class EIP712AuthProvider implements AuthProvider {
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,
) {
Expand All @@ -77,12 +78,12 @@ export class EIP712AuthProvider implements AuthProvider {
}

// If at this point we didn't return, we need to create a new signature
const typedSignature = await this.createWalletSignature();
this.storage.setItem(storageKey, JSON.stringify(typedSignature));
return typedSignature;
const authMessage = await this.createAuthMessage();
this.storage.setItem(storageKey, JSON.stringify(authMessage));
return authMessage;
}

private async createWalletSignature(): Promise<AuthSignature> {
private async createAuthMessage(): Promise<AuthSignature> {
// Ensure freshness of the signature
const { blockNumber, blockHash, chainId } = await this.getChainData();
const address = await this.signer.getAddress();
Expand Down Expand Up @@ -116,7 +117,7 @@ export class EIP712AuthProvider implements AuthProvider {
this.signer as unknown as TypedDataSigner
)._signTypedData(typedData.domain, typedData.types, typedData.message);

const formattedTypedData: FormattedEIP712 = {
const formattedTypedData: EIP712TypedData = {
...typedData,
primaryType: 'Wallet',
types: {
Expand Down
7 changes: 4 additions & 3 deletions packages/taco-auth/src/providers/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EIP4361_AUTH_METHOD, EIP712_AUTH_METHOD } from '../types';

import { EIP4361AuthProvider, FormattedEIP4361 } from './eip4361';
import { EIP712AuthProvider, FormattedEIP712 } from './eip712';
import { EIP4361AuthProvider, EIP4361TypedData } from './eip4361';
import { EIP712AuthProvider, EIP712TypedData } from './eip712';

export interface AuthSignatureProvider {
getOrCreateAuthSignature(): Promise<AuthSignature>;
Expand All @@ -18,7 +18,8 @@ export interface AuthSignature {
signature: string;
address: string;
scheme: 'EIP712' | 'EIP4361';
typedData: FormattedEIP712 | FormattedEIP4361;
typedData: EIP712TypedData | EIP4361TypedData;
}

// Add other providers here
export type AuthProvider = AuthSignatureProvider;
6 changes: 3 additions & 3 deletions packages/taco-auth/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {EIP4361AuthProvider, FormattedEIP4361} from './providers/eip4361';
import {EIP712AuthProvider, FormattedEIP712} from './providers/eip712';
import {EIP4361AuthProvider, EIP4361TypedData} from './providers/eip4361';
import {EIP712AuthProvider, EIP712TypedData} from './providers/eip712';

export interface AuthProvider {
getOrCreateAuthSignature(): Promise<AuthSignature>;
Expand All @@ -16,7 +16,7 @@ export interface AuthSignature {
signature: string;
address: string;
scheme: 'EIP712' | 'EIP4361';
typedData: FormattedEIP712 | FormattedEIP4361;
typedData: EIP712TypedData | EIP4361TypedData;
}

export const EIP712_AUTH_METHOD = 'EIP712';
Expand Down
24 changes: 3 additions & 21 deletions packages/taco-auth/test/taco-auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,21 @@ import { describe, expect, it } from 'vitest';
import {
EIP4361AuthProvider,
EIP712AuthProvider,
FormattedEIP712,
EIP712TypedData,
} from '../src';

describe('taco authorization', () => {
it('creates a new EIP-712 message', async () => {
const provider = fakeProvider(bobSecretKeyBytes);
const signer = fakeSigner(bobSecretKeyBytes);
const eip712Provider = new EIP712AuthProvider(provider, signer);

const eip712Provider = new EIP712AuthProvider(provider, signer);
const eip712Message = await eip712Provider.getOrCreateAuthSignature();

// Expected format:
// {
// "signature": "<signature>",
// "address": "<address>",
// "scheme": "EIP712" | "EIP4361" | ...
// "typeData": ...
// }

expect(eip712Message.signature).toBeDefined();
expect(eip712Message.address).toEqual(await signer.getAddress());
expect(eip712Message.scheme).toEqual('EIP712');

const typedData = eip712Message.typedData as FormattedEIP712;
const typedData = eip712Message.typedData as EIP712TypedData;
expect(typedData).toBeDefined();
expect(typedData.types.Wallet).toBeDefined();
expect(typedData.domain.name).toEqual('TACo');
Expand All @@ -54,15 +45,6 @@ describe('taco authorization', () => {

const eip4361Provider = new EIP4361AuthProvider(provider, signer);
const typedSignature = await eip4361Provider.getOrCreateAuthSignature();

// Expected format:
// {
// "signature": "<signature>",
// "address": "<address>",
// "scheme": "EIP712" | "EIP4361" | ...
// "typedData": ...
// }

expect(typedSignature.signature).toBeDefined();
expect(typedSignature.address).toEqual(await signer.getAddress());
expect(typedSignature.scheme).toEqual('EIP4361');
Expand Down
5 changes: 2 additions & 3 deletions packages/taco/test/conditions/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import {
AuthProviders,
AuthSignature,
EIP4361AuthProvider,
EIP712AuthProvider,
FormattedEIP712,
EIP712AuthProvider, EIP712TypedData,
makeAuthProviders,
} from '@nucypher/taco-auth';
import {
Expand Down Expand Up @@ -378,7 +377,7 @@ describe('authentication provider', () => {
expect(authSignature.scheme).toEqual('EIP712');
expect(authSignature.address).toEqual(await signer.getAddress());

const typedData = authSignature.typedData as FormattedEIP712;
const typedData = authSignature.typedData as EIP712TypedData;
expect(typedData).toBeDefined();
expect(typedData.domain.name).toEqual('TACo');
expect(typedData.message.address).toEqual(await signer.getAddress());
Expand Down

0 comments on commit 5eec120

Please sign in to comment.