Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authentication conditions #530

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/taco/src/conditions/auth/eip712.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { z } from 'zod';

import { Condition } from '../condition';
import {
EthAddressOrUserAddressSchema,
OmitConditionType,
} from '../shared';

export const EIP712AuthConditionType = 'auth/eip712';

export const EIP712AuthConditionSchema = z.object({
conditionType: z.literal(EIP712AuthConditionType).default(EIP712AuthConditionType),
parameters: EthAddressOrUserAddressSchema,
});

export type EIP712AuthConditionProps = z.infer<typeof EIP712AuthConditionSchema>;

export class EIP712AuthCondition extends Condition {
constructor(value: OmitConditionType<EIP712AuthConditionProps>) {
super(EIP712AuthConditionSchema, {
conditionType: EIP712AuthConditionType,
...value,
});
}
}
1 change: 1 addition & 0 deletions packages/taco/src/conditions/auth/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as eip712 from './eip712';
3 changes: 2 additions & 1 deletion packages/taco/src/conditions/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as auth from './auth';
import * as base from './base';
import * as predefined from './predefined';

Expand All @@ -6,4 +7,4 @@ export * as condition from './condition';
export * as conditionExpr from './condition-expr';
export { ConditionFactory } from './condition-factory';
export * as context from './context';
export { base, predefined };
export { auth, base, predefined };
67 changes: 67 additions & 0 deletions packages/taco/test/conditions/auth/eip712.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {TEST_USER_ADDRESS} from "@nucypher/test-utils";
import {describe, expect, it} from 'vitest';

import {
EIP712AuthCondition,
EIP712AuthConditionSchema,
EIP712AuthConditionType
} from "../../../src/conditions/auth/eip712";

describe('validation', () => {
const testEIP712AuthConditionObj = {
conditionType: 'auth/eip712',
parameters: TEST_USER_ADDRESS,
};

it('accepts on a valid schema', () => {
const result = EIP712AuthCondition.validate(
EIP712AuthConditionSchema,
testEIP712AuthConditionObj,
);

expect(result.error).toBeUndefined();
expect(result.data).toEqual(testEIP712AuthConditionObj);
});

it('rejects an invalid schema', () => {
const badCondObj = {
...testEIP712AuthConditionObj,
// Intentionally replacing `parameters` with invalid parameters
parameters: undefined
} as unknown as typeof testEIP712AuthConditionObj;

const result = EIP712AuthCondition.validate(EIP712AuthConditionSchema, badCondObj);

expect(result.error).toBeDefined();
expect(result.data).toBeUndefined();
expect(result.error?.format()).toMatchObject({
parameters: {
_errors: [
"Required", "Required",
],
},
});
});

it('infers condition type from constructor', () => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const {conditionType, ...withoutType} = testEIP712AuthConditionObj;
const condition = new EIP712AuthCondition(testEIP712AuthConditionObj);
expect(condition.value.conditionType).toEqual(EIP712AuthConditionType);
});

describe('parameters', () => {
it('accepts a single address', () => {
const result = EIP712AuthCondition.validate(EIP712AuthConditionSchema, {
...testEIP712AuthConditionObj,
parameters: TEST_USER_ADDRESS,
});

expect(result.error).toBeUndefined();
expect(result.data).toEqual({
...testEIP712AuthConditionObj,
parameters: TEST_USER_ADDRESS,
});
});
});
});
4 changes: 3 additions & 1 deletion packages/test-utils/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import axios from 'axios';
import { ethers, providers, Wallet } from 'ethers';
import { expect, SpyInstance, vi } from 'vitest';

import { TEST_USER_ADDRESS } from './variables';

export const bytesEqual = (first: Uint8Array, second: Uint8Array): boolean =>
first.length === second.length &&
first.every((value, index) => value === second[index]);
Expand Down Expand Up @@ -74,7 +76,7 @@ export const fakeSigner = (
_signTypedData: () => Promise.resolve('fake-typed-signature'),
signMessage: () => Promise.resolve('fake-signature'),
getAddress: () =>
Promise.resolve('0x0000000000000000000000000000000000000000'),
Promise.resolve(TEST_USER_ADDRESS),
} as unknown as ethers.providers.JsonRpcSigner;
};

Expand Down
1 change: 1 addition & 0 deletions packages/test-utils/src/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const bobSecretKeyBytes = new Uint8Array([
229, 2, 106, 176, 205, 33, 168, 23, 213, 233, 200, 238, 11, 193, 153,
]);

export const TEST_USER_ADDRESS = '0x0000000000000000000000000000000000000000';
export const TEST_CONTRACT_ADDR = '0x0000000000000000000000000000000000000001';
export const TEST_CONTRACT_ADDR_2 =
'0x0000000000000000000000000000000000000002';
Expand Down
Loading