Skip to content
Merged
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
17 changes: 10 additions & 7 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,25 +130,28 @@ jobs:

# Repeatable config for defining the workflow below.
tag_regex: &tag_regex /v[0-9]+(\.[0-9]+)*(-[a-zA-Z-]+\.[0-9]+)?/
tag_filter: &tag_filter
tags:
only: *tag_regex
defaults: &defaults
filters:
tags:
only: *tag_regex
context:
- build
yarn_project: &yarn_project
requires:
- yarn-project-base
filters: *tag_filter
<<: *defaults
e2e_test: &e2e_test
requires:
- e2e-join
filters: *tag_filter
<<: *defaults

workflows:
system:
when:
equal: [system, << pipeline.parameters.workflow >>]
jobs:
- yarn-project-base:
filters: *tag_filter
<<: *defaults
- ethereum-js: *yarn_project
- aztec-js: *yarn_project
- end-to-end: *yarn_project
Expand All @@ -157,5 +160,5 @@ workflows:
- ethereum-js
- aztec-js
- end-to-end
filters: *tag_filter
<<: *defaults
- e2e-deploy-contract: *e2e_test
6 changes: 0 additions & 6 deletions yarn-project/ethereum.js/src/contract/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,6 @@ export class Contract<T extends ContractDefinition | void = void> {
return this.executorFactory([new ContractFunctionEntry(abiEntry)]);
}

// public async getTransactionReceipt(txHash: TxHash)

// public async getError(txHash: TxHash) {
// return await decodeErrorFromContractByTxHash(this.contractAbi, txHash, this.eth);
// }

// PRIVATE METHODS

private executorFactory(functions: ContractFunctionEntry[]): TxFactory {
Expand Down
16 changes: 6 additions & 10 deletions yarn-project/ethereum.js/src/eth_typed_data/eip-712.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ describe('getDependencies', () => {
expect(getDependencies(arrayTypedData, 'Person')).toStrictEqual(['Person']);
expect(getDependencies(arrayTypedData, 'Mail')).toStrictEqual(['Mail', 'Person']);

expect(getDependencies(customTypedData, 'FooBarDomain', { domain: 'FooBarDomain' })).toStrictEqual([
'FooBarDomain',
]);
expect(getDependencies(customTypedData, 'FooBarDomain')).toStrictEqual(['FooBarDomain']);
});

it.skip('throws for invalid JSON data', () => {
Expand Down Expand Up @@ -65,7 +63,7 @@ describe('encodeType', () => {
'TransactionApproval(address owner,Transaction transaction)Transaction(address to,uint256 amount,bytes data,uint256 nonce)',
);

expect(encodeType(customTypedData, 'FooBarDomain', { domain: 'FooBarDomain' })).toBe(
expect(encodeType(customTypedData, 'FooBarDomain')).toBe(
'FooBarDomain(string name,string version,uint256 chainId,address verifyingContract)',
);
});
Expand Down Expand Up @@ -113,7 +111,7 @@ describe('getTypeHash', () => {
'b9d8c78acf9b987311de6c7b45bb6a9c8e1bf361fa7fd3467a2163f994c79500',
);

expect(bytesToHex(getTypeHash(customTypedData, 'FooBarDomain', { domain: 'FooBarDomain' }))).toBe(
expect(bytesToHex(getTypeHash(customTypedData, 'FooBarDomain'))).toBe(
'85b412c5db9e26aa4f6bf794e72b1557f463a0978ceef9acaff7f6ff1eb24e57',
);
});
Expand Down Expand Up @@ -149,9 +147,7 @@ describe('encodeData', () => {
'c81112a69b6596b8bc0678e67d97fbf9bed619811fc781419323ec02d1c7290dafd2599280d009dcb3e261f4bccebec901d67c3f54b56d49bf8327359fc69cd7392bb8ab5338a9075ce8fec1b431e334007d4de1e5e83201ca35762e24428e24b7c4150525d88db452c5f08f93f4593daa458ab6280b012532183aed3a8e4a01',
);

expect(
bytesToHex(encodeData(customTypedData, 'FooBarDomain', customTypedData.domain, { domain: 'FooBarDomain' })),
).toBe(
expect(bytesToHex(encodeData(customTypedData, 'FooBarDomain', customTypedData.domain))).toBe(
'85b412c5db9e26aa4f6bf794e72b1557f463a0978ceef9acaff7f6ff1eb24e57c70ef06638535b4881fafcac8287e210e3769ff1a8e91f1b95d6246e61e4d3c6c89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc60000000000000000000000000000000000000000000000000000000000000001000000000000000000000000cccccccccccccccccccccccccccccccccccccccc',
);
});
Expand Down Expand Up @@ -228,7 +224,7 @@ describe('getMessage', () => {
expect(bytesToHex(getMessage(arrayTypedData))).toBe(
'1901f2cee375fa42b42143804025fc449deafd50cc031ca257e0b194a650a912090f6757567025d2ba15d5ebb228ea677055b8b601007e60e9463f6ed7c68f918189',
);
expect(bytesToHex(getMessage(customTypedData, false, { domain: 'FooBarDomain' }))).toBe(
expect(bytesToHex(getMessage(customTypedData, false, 'FooBarDomain'))).toBe(
'19016ff4505ed33bedaadf3491aa039d9ccb91a3114eeab940e69fdecb809fb268826757567025d2ba15d5ebb228ea677055b8b601007e60e9463f6ed7c68f918189',
);
});
Expand All @@ -243,7 +239,7 @@ describe('getMessage', () => {
expect(bytesToHex(getMessage(arrayTypedData, true))).toBe(
'c6f6c8028eadb17bc5c9e2ea2f738e92e49cfa627d19896c250fd2eac653e4e0',
);
expect(bytesToHex(getMessage(customTypedData, true, { domain: 'FooBarDomain' }))).toBe(
expect(bytesToHex(getMessage(customTypedData, true, 'FooBarDomain'))).toBe(
'e028c0622beef9bde70e78a98c1d09a95ffe0cd9cfa5ff6a99f7db7c9245e103',
);
});
Expand Down
57 changes: 18 additions & 39 deletions yarn-project/ethereum.js/src/eth_typed_data/eip-712.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,11 @@ import { abiCoder } from '../contract/index.js';

const EIP_191_PREFIX = Buffer.from('1901', 'hex');

type Options = any;

/**
* Get the dependencies of a struct type. If a struct has the same dependency multiple times, it's only included once
* in the resulting array.
*/
export const getDependencies = (
typedData: TypedData,
type: string,
options?: Options,
dependencies: string[] = [],
): string[] => {
export const getDependencies = (typedData: TypedData, type: string, dependencies: string[] = []): string[] => {
const TYPE_REGEX = /^\w+/;
const match = type.match(TYPE_REGEX)!;
const actualType = match[0];
Expand All @@ -32,9 +25,7 @@ export const getDependencies = (
...typedData.types[actualType].reduce<string[]>(
(previous, type) => [
...previous,
...getDependencies(typedData, type.type, options, previous).filter(
dependency => !previous.includes(dependency),
),
...getDependencies(typedData, type.type, previous).filter(dependency => !previous.includes(dependency)),
],
[],
),
Expand All @@ -49,8 +40,8 @@ export const getDependencies = (
* @param {Options} [options]
* @return {string}
*/
export const encodeType = (typedData: TypedData, type: string, options?: Options): string => {
const [primary, ...dependencies] = getDependencies(typedData, type, options);
export const encodeType = (typedData: TypedData, type: string): string => {
const [primary, ...dependencies] = getDependencies(typedData, type);
const types = [primary, ...dependencies.sort()];

return types
Expand All @@ -63,20 +54,15 @@ export const encodeType = (typedData: TypedData, type: string, options?: Options
/**
* Get a type string as hash.
*/
export const getTypeHash = (typedData: TypedData, type: string, options?: Options) => {
return keccak256(Buffer.from(encodeType(typedData, type, options)));
export const getTypeHash = (typedData: TypedData, type: string) => {
return keccak256(Buffer.from(encodeType(typedData, type)));
};

/**
* Encodes a single value to an ABI serialisable string, number or Buffer. Returns the data as tuple, which consists of
* an array of ABI compatible types, and an array of corresponding values.
*/
const encodeValue = (
typedData: TypedData,
type: string,
data: unknown,
options?: Options,
): [string, Buffer | string] => {
const encodeValue = (typedData: TypedData, type: string, data: unknown): [string, Buffer | string] => {
// Checks for array types
const ARRAY_REGEX = /^(.*)\[([0-9]*?)]$/;
const match = type.match(ARRAY_REGEX);
Expand All @@ -92,15 +78,15 @@ const encodeValue = (
throw new Error(`Cannot encode data: expected length of ${length}, but got ${data.length}`);
}

const encodedData = data.map(item => encodeValue(typedData, arrayType, item, options));
const encodedData = data.map(item => encodeValue(typedData, arrayType, item));
const types = encodedData.map(item => item[0]);
const values = encodedData.map(item => item[1]);

return ['bytes32', keccak256(abiCoder.encodeParameters(types, values))];
}

if (typedData.types[type]) {
return ['bytes32', getStructHash(typedData, type, data as Record<string, unknown>, options)];
return ['bytes32', getStructHash(typedData, type, data as Record<string, unknown>)];
}

// Strings and arbitrary byte arrays are hashed to bytes32
Expand All @@ -119,27 +105,22 @@ const encodeValue = (
* Encode the data to an ABI encoded Buffer. The data should be a key -> value object with all the required values. All
* dependant types are automatically encoded.
*/
export const encodeData = (
typedData: TypedData,
type: string,
data: Record<string, unknown>,
options?: Options,
): Buffer => {
export const encodeData = (typedData: TypedData, type: string, data: Record<string, unknown>): Buffer => {
const [types, values] = typedData.types[type].reduce<[string[], unknown[]]>(
([types, values], field) => {
if (data[field.name] === undefined || data[field.name] === null) {
throw new Error(`Cannot encode data: missing data for '${field.name}'`);
}

const value = data[field.name];
const [type, encodedValue] = encodeValue(typedData, field.type, value, options);
const [type, encodedValue] = encodeValue(typedData, field.type, value);

return [
[...types, type],
[...values, encodedValue],
];
},
[['bytes32'], [getTypeHash(typedData, type, options)]],
[['bytes32'], [getTypeHash(typedData, type)]],
);

return abiCoder.encodeParameters(types, values);
Expand All @@ -149,20 +130,19 @@ export const encodeData = (
* Get encoded data as a hash. The data should be a key -> value object with all the required values. All dependant
* types are automatically encoded.
*/
export const getStructHash = (typedData: TypedData, type: string, data: Record<string, unknown>, options?: Options) => {
return keccak256(encodeData(typedData, type, data, options));
export const getStructHash = (typedData: TypedData, type: string, data: Record<string, unknown>) => {
return keccak256(encodeData(typedData, type, data));
};

/**
* Get the EIP-191 encoded message to sign, from the typedData object. If `hash` is enabled, the message will be hashed
* with Keccak256.
*/
export const getMessage = (typedData: TypedData, hash?: boolean, options?: Options) => {
const { domain } = options || { domain: 'EIP712Domain', verifyDomain: true };
export const getMessage = (typedData: TypedData, hash?: boolean, domain = 'EIP712Domain') => {
const message = Buffer.concat([
EIP_191_PREFIX,
getStructHash(typedData, domain, typedData.domain as Record<string, unknown>, options),
getStructHash(typedData, typedData.primaryType, typedData.message, options),
getStructHash(typedData, domain, typedData.domain as Record<string, unknown>),
getStructHash(typedData, typedData.primaryType, typedData.message),
]);

if (hash) {
Expand All @@ -179,7 +159,6 @@ export const asArray = (
typedData: TypedData,
type: string = typedData.primaryType,
data: Record<string, unknown> = typedData.message,
options?: Options,
): unknown[] => {
if (!typedData.types[type]) {
throw new Error('Cannot get data as array: type does not exist');
Expand All @@ -191,7 +170,7 @@ export const asArray = (
throw new Error(`Cannot get data as array: missing data for '${name}'`);
}

return [...array, asArray(typedData, type, data[name] as Record<string, unknown>, options)];
return [...array, asArray(typedData, type, data[name] as Record<string, unknown>)];
}

const value = data[name];
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/ethereum.js/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"rootDir": "src",
"tsBuildInfoFile": ".tsbuildinfo"
},
"include": ["src"]
"include": ["src", "src/eth_typed_data/fixtures/*.json"]
}