Skip to content

Commit

Permalink
chore(linter): fix linter
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed May 31, 2024
1 parent 8f17e0c commit 164a886
Show file tree
Hide file tree
Showing 7 changed files with 10,464 additions and 7,842 deletions.
8 changes: 5 additions & 3 deletions packages/taco-auth/src/eip4361.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { ethers } from 'ethers';
import { generateNonce, SiweMessage } from 'siwe';

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

export type FormattedEIP4361 = string;

export class EIP4361SignatureProvider {
private readonly storage: LocalStorage;
Expand All @@ -14,7 +16,7 @@ export class EIP4361SignatureProvider {
this.storage = new LocalStorage();
}

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

Expand All @@ -30,7 +32,7 @@ export class EIP4361SignatureProvider {
return typedSignature;
}

private async createSiweMessage(): Promise<TypedSignature> {
private async createSiweMessage(): Promise<AuthSignature> {
const address = await this.signer.getAddress();
const domain = 'TACo';
const version = '1';
Expand Down
17 changes: 8 additions & 9 deletions packages/taco-auth/src/eip712.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { ethers } from 'ethers';
import { utils as ethersUtils } from 'ethers/lib/ethers';

import { LocalStorage } from './storage';
import type { TypedSignature } from './types';
import type { AuthSignature } from './types';

interface Eip712 {
interface EIP712 {
types: {
Wallet: { name: string; type: string }[];
};
Expand All @@ -23,7 +23,7 @@ interface Eip712 {
};
}

export interface FormattedEip712 extends Eip712 {
export interface FormattedEIP712 extends EIP712 {
primaryType: 'Wallet';
types: {
EIP712Domain: { name: string; type: string }[];
Expand Down Expand Up @@ -66,7 +66,7 @@ export class EIP712SignatureProvider {
this.storage = new LocalStorage();
}

public async getOrCreateWalletSignature(): Promise<TypedSignature> {
public async getOrCreateWalletSignature(): Promise<AuthSignature> {
const address = await this.signer.getAddress();
const storageKey = `eip712-signature-${address}`;

Expand All @@ -82,16 +82,14 @@ export class EIP712SignatureProvider {
return typedSignature;
}

private async createWalletSignature(): Promise<TypedSignature> {
private async createWalletSignature(): Promise<AuthSignature> {
// Ensure freshness of the signature
const { blockNumber, blockHash, chainId } = await this.getChainData();
const address = await this.signer.getAddress();
const signatureText = `I'm the owner of address ${address} as of block number ${blockNumber}`;
const salt = ethersUtils.hexlify(ethersUtils.randomBytes(32));

const scheme = 'EIP712';

const typedData: Eip712 = {
const typedData: EIP712 = {
types: {
Wallet: [
{ name: 'address', type: 'address' },
Expand All @@ -118,14 +116,15 @@ export class EIP712SignatureProvider {
this.signer as unknown as TypedDataSigner
)._signTypedData(typedData.domain, typedData.types, typedData.message);

const formattedTypedData: FormattedEip712 = {
const formattedTypedData: FormattedEIP712 = {
...typedData,
primaryType: 'Wallet',
types: {
...typedData.types,
EIP712Domain,
},
};
const scheme = 'EIP712';
return { signature, address, scheme, typedData: formattedTypedData };
}

Expand Down
7 changes: 4 additions & 3 deletions packages/taco-auth/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { FormattedEip712 } from './eip712';
import { FormattedEIP4361 } from './eip4361';
import { FormattedEIP712 } from './eip712';

export interface TypedSignature {
export interface AuthSignature {
signature: string;
address: string;
scheme: 'EIP712' | 'EIP4361';
typedData: FormattedEip712 | string;
typedData: FormattedEIP712 | FormattedEIP4361;
}
4 changes: 2 additions & 2 deletions packages/taco-auth/test/taco-auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { describe, expect, it } from 'vitest';
import {
EIP4361SignatureProvider,
EIP712SignatureProvider,
FormattedEip712,
FormattedEIP712,
} from '../src';

describe('taco authorization', () => {
Expand All @@ -32,7 +32,7 @@ describe('taco authorization', () => {
expect(eip712Message.address).toEqual(await signer.getAddress());
expect(eip712Message.scheme).toEqual('EIP712');

const typedData = eip712Message.typedData as FormattedEip712;
const typedData = eip712Message.typedData as FormattedEIP712;
expect(typedData).toBeDefined();
expect(typedData.types.Wallet).toBeDefined();
expect(typedData.domain.name).toEqual('TACo');
Expand Down
4 changes: 2 additions & 2 deletions packages/taco/src/conditions/context/context.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Context, Conditions as WASMConditions } from '@nucypher/nucypher-core';
import { fromJSON, toJSON } from '@nucypher/shared';
import {
AuthSignature,
EIP4361SignatureProvider,
EIP712SignatureProvider,
TypedSignature,
} from '@nucypher/taco-auth';
import { ethers } from 'ethers';

Expand All @@ -20,7 +20,7 @@ import {
} from '../const';

export type CustomContextParam = string | number | boolean;
export type ContextParam = CustomContextParam | TypedSignature;
export type ContextParam = CustomContextParam | AuthSignature;

const ERR_RESERVED_PARAM = (key: string) =>
`Cannot use reserved parameter name ${key} as custom parameter`;
Expand Down
59 changes: 34 additions & 25 deletions packages/taco/test/conditions/context.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { initialize } from '@nucypher/nucypher-core';
import {
AuthSignature,
EIP4361SignatureProvider,
EIP712SignatureProvider,
FormattedEIP712,
} from '@nucypher/taco-auth';
import { fakeProvider, fakeSigner } from '@nucypher/test-utils';
import { ethers } from 'ethers';
Expand All @@ -21,6 +23,7 @@ import {
USER_ADDRESS_PARAM_EIP712,
} from '../../src/conditions/const';
import { CustomContextParam } from '../../src/conditions/context';
import { ContextParam } from '../../src/conditions/context/context';
import {
paramOrContextParamSchema,
ReturnValueTestProps,
Expand Down Expand Up @@ -357,17 +360,20 @@ describe('authentication provider', () => {
expect(conditionExpr.contextRequiresSigner()).toBe(true);
const builtContext = conditionExpr.buildContext(provider, {}, signer);

const resolvedContextRecords = await builtContext.toObj();
const contextVars = await builtContext.toObj();

const typeSignature = resolvedContextRecords[USER_ADDRESS_PARAM_DEFAULT];
expect(typeSignature).toBeDefined();
expect(typeSignature.signature).toBeDefined();
expect(typeSignature.scheme).toEqual('EIP712');
expect(typeSignature.address).toEqual(await signer.getAddress());
expect(typeSignature.typedData.domain.name).toEqual('TACo');
expect(typeSignature.typedData.message.address).toEqual(
await signer.getAddress(),
);
const typedSignature = contextVars[
USER_ADDRESS_PARAM_DEFAULT
] as AuthSignature;
expect(typedSignature).toBeDefined();
expect(typedSignature.signature).toBeDefined();
expect(typedSignature.scheme).toEqual('EIP712');
expect(typedSignature.address).toEqual(await signer.getAddress());

const typedData = typedSignature.typedData as FormattedEIP712;
expect(typedData).toBeDefined();
expect(typedData.domain.name).toEqual('TACo');
expect(typedData.message.address).toEqual(await signer.getAddress());
expect(eip712Spy).toHaveBeenCalledOnce();
});

Expand All @@ -390,13 +396,15 @@ describe('authentication provider', () => {
const builtContext = conditionExpr.buildContext(provider, {}, signer);
const resolvedContextRecords = await builtContext.toObj();

const typeSignature = resolvedContextRecords[USER_ADDRESS_PARAM_EIP712];
expect(typeSignature).toBeDefined();
expect(typeSignature.signature).toBeDefined();
expect(typeSignature.scheme).toEqual('EIP712');
expect(typeSignature.address).toEqual(await signer.getAddress());
expect(typeSignature.typedData.domain.name).toEqual('TACo');
expect(typeSignature.typedData.message.address).toEqual(
const typedSignature = resolvedContextRecords[USER_ADDRESS_PARAM_EIP712] as AuthSignature;
expect(typedSignature).toBeDefined();
expect(typedSignature.signature).toBeDefined();
expect(typedSignature.scheme).toEqual('EIP712');
expect(typedSignature.address).toEqual(await signer.getAddress());

const typedData = typedSignature.typedData as FormattedEIP712;
expect(typedData.domain.name).toEqual('TACo');
expect(typedData.message.address).toEqual(
await signer.getAddress(),
);
expect(eip712Spy).toHaveBeenCalledOnce();
Expand All @@ -421,21 +429,22 @@ describe('authentication provider', () => {
const builtContext = conditionExpr.buildContext(provider, {}, signer);
const resolvedContextRecords = await builtContext.toObj();

const typeSignature = resolvedContextRecords[USER_ADDRESS_PARAM_EIP4361];
expect(typeSignature).toBeDefined();
expect(typeSignature.signature).toBeDefined();
expect(typeSignature.scheme).toEqual('EIP4361');
const typedSignature: ContextParam =
resolvedContextRecords[USER_ADDRESS_PARAM_EIP4361] as AuthSignature;
expect(typedSignature).toBeDefined();
expect(typedSignature.signature).toBeDefined();
expect(typedSignature.scheme).toEqual('EIP4361');

const signerAddress = await signer.getAddress();
expect(typeSignature.address).toEqual(signerAddress);
expect(typedSignature.address).toEqual(signerAddress);

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

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

expect(eip4361Spy).toHaveBeenCalledOnce();
});
Expand Down
Loading

0 comments on commit 164a886

Please sign in to comment.