aboutsummaryrefslogtreecommitdiffstats
path: root/packages/utils/src
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2018-10-11 20:23:45 +0800
committerFabio Berger <me@fabioberger.com>2018-10-11 20:23:45 +0800
commit1cfcc82ea9869e14c1a1b78e1376c89fdbeb91f4 (patch)
tree48ef8716485c1b961b2494071f5a64f60ff42f29 /packages/utils/src
parent6c9f7839c3948e60f2987f474bb2ad6457588fa3 (diff)
parent8941b6cee5b56fab6d3b89ac8a899f21d5c86350 (diff)
downloaddexon-sol-tools-1cfcc82ea9869e14c1a1b78e1376c89fdbeb91f4.tar
dexon-sol-tools-1cfcc82ea9869e14c1a1b78e1376c89fdbeb91f4.tar.gz
dexon-sol-tools-1cfcc82ea9869e14c1a1b78e1376c89fdbeb91f4.tar.bz2
dexon-sol-tools-1cfcc82ea9869e14c1a1b78e1376c89fdbeb91f4.tar.lz
dexon-sol-tools-1cfcc82ea9869e14c1a1b78e1376c89fdbeb91f4.tar.xz
dexon-sol-tools-1cfcc82ea9869e14c1a1b78e1376c89fdbeb91f4.tar.zst
dexon-sol-tools-1cfcc82ea9869e14c1a1b78e1376c89fdbeb91f4.zip
Merge branch 'development' into dev-section-redesign
* development: (62 commits) Fix linter error Upgrade ethereum-types Lint and update deps Be more explicit with falsiness Add type to cssRuleIfExists Fix issue where we throw if non-numeric characters are used in input Upgrade to more recent types, fix yarn.lock [fix]: [testnet-faucet] Exit 1 on build fail Explains tools we want them to use Add note about button Add dev-tools-pages bundles to gitignore Improve README Fix button and center Increase max bundle size for instant Add stuff Initial project scaffolding Change tslint config to remove conflicts with prettier fix: [testnet-faucet] Signing of orders Update the CHANGELOG Add comments for expiryBuffer ...
Diffstat (limited to 'packages/utils/src')
-rw-r--r--packages/utils/src/abi_decoder.ts6
-rw-r--r--packages/utils/src/index.ts1
-rw-r--r--packages/utils/src/sign_typed_data_utils.ts82
3 files changed, 86 insertions, 3 deletions
diff --git a/packages/utils/src/abi_decoder.ts b/packages/utils/src/abi_decoder.ts
index ea8c91d10..ac3e54efb 100644
--- a/packages/utils/src/abi_decoder.ts
+++ b/packages/utils/src/abi_decoder.ts
@@ -9,7 +9,7 @@ import {
RawLog,
SolidityTypes,
} from 'ethereum-types';
-import * as ethers from 'ethers';
+import { ethers } from 'ethers';
import * as _ from 'lodash';
import { addressUtils } from './address_utils';
@@ -41,7 +41,7 @@ export class AbiDecoder {
return log;
}
const event = this._methodIds[methodId][numIndexedArgs];
- const ethersInterface = new ethers.Interface([event]);
+ const ethersInterface = new ethers.utils.Interface([event]);
const decodedParams: DecodedLogArgs = {};
let topicsIndex = 1;
@@ -96,7 +96,7 @@ export class AbiDecoder {
if (_.isUndefined(abiArray)) {
return;
}
- const ethersInterface = new ethers.Interface(abiArray);
+ const ethersInterface = new ethers.utils.Interface(abiArray);
_.map(abiArray, (abi: AbiDefinition) => {
if (abi.type === AbiType.Event) {
const topic = ethersInterface.events[abi.name].topic;
diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts
index 9d01e5bc5..0723e5788 100644
--- a/packages/utils/src/index.ts
+++ b/packages/utils/src/index.ts
@@ -9,3 +9,4 @@ export { abiUtils } from './abi_utils';
export { NULL_BYTES } from './constants';
export { errorUtils } from './error_utils';
export { fetchAsync } from './fetch_async';
+export { signTypedDataUtils } from './sign_typed_data_utils';
diff --git a/packages/utils/src/sign_typed_data_utils.ts b/packages/utils/src/sign_typed_data_utils.ts
new file mode 100644
index 000000000..cd5bcb42f
--- /dev/null
+++ b/packages/utils/src/sign_typed_data_utils.ts
@@ -0,0 +1,82 @@
+import * as ethUtil from 'ethereumjs-util';
+import * as ethers from 'ethers';
+import * as _ from 'lodash';
+
+import { EIP712Object, EIP712ObjectValue, EIP712TypedData, EIP712Types } from '@0xproject/types';
+
+export const signTypedDataUtils = {
+ /**
+ * Generates the EIP712 Typed Data hash for signing
+ * @param typedData An object that conforms to the EIP712TypedData interface
+ * @return A Buffer containing the hash of the typed data.
+ */
+ generateTypedDataHash(typedData: EIP712TypedData): Buffer {
+ return ethUtil.sha3(
+ Buffer.concat([
+ Buffer.from('1901', 'hex'),
+ signTypedDataUtils._structHash('EIP712Domain', typedData.domain, typedData.types),
+ signTypedDataUtils._structHash(typedData.primaryType, typedData.message, typedData.types),
+ ]),
+ );
+ },
+ _findDependencies(primaryType: string, types: EIP712Types, found: string[] = []): string[] {
+ if (found.includes(primaryType) || types[primaryType] === undefined) {
+ return found;
+ }
+ found.push(primaryType);
+ for (const field of types[primaryType]) {
+ for (const dep of signTypedDataUtils._findDependencies(field.type, types, found)) {
+ if (!found.includes(dep)) {
+ found.push(dep);
+ }
+ }
+ }
+ return found;
+ },
+ _encodeType(primaryType: string, types: EIP712Types): string {
+ let deps = signTypedDataUtils._findDependencies(primaryType, types);
+ deps = deps.filter(d => d !== primaryType);
+ deps = [primaryType].concat(deps.sort());
+ let result = '';
+ for (const dep of deps) {
+ result += `${dep}(${types[dep].map(({ name, type }) => `${type} ${name}`).join(',')})`;
+ }
+ return result;
+ },
+ _encodeData(primaryType: string, data: EIP712Object, types: EIP712Types): string {
+ const encodedTypes = ['bytes32'];
+ const encodedValues: Array<Buffer | EIP712ObjectValue> = [signTypedDataUtils._typeHash(primaryType, types)];
+ for (const field of types[primaryType]) {
+ const value = data[field.name];
+ if (field.type === 'string' || field.type === 'bytes') {
+ const hashValue = ethUtil.sha3(value as string);
+ encodedTypes.push('bytes32');
+ encodedValues.push(hashValue);
+ } else if (types[field.type] !== undefined) {
+ encodedTypes.push('bytes32');
+ const hashValue = ethUtil.sha3(
+ // tslint:disable-next-line:no-unnecessary-type-assertion
+ signTypedDataUtils._encodeData(field.type, value as EIP712Object, types),
+ );
+ encodedValues.push(hashValue);
+ } else if (field.type.lastIndexOf(']') === field.type.length - 1) {
+ throw new Error('Arrays currently unimplemented in encodeData');
+ } else {
+ encodedTypes.push(field.type);
+ const normalizedValue = signTypedDataUtils._normalizeValue(field.type, value);
+ encodedValues.push(normalizedValue);
+ }
+ }
+ return ethers.utils.defaultAbiCoder.encode(encodedTypes, encodedValues);
+ },
+ _normalizeValue(type: string, value: any): EIP712ObjectValue {
+ const normalizedValue = type === 'uint256' && _.isObject(value) && value.isBigNumber ? value.toString() : value;
+ return normalizedValue;
+ },
+ _typeHash(primaryType: string, types: EIP712Types): Buffer {
+ return ethUtil.sha3(signTypedDataUtils._encodeType(primaryType, types));
+ },
+ _structHash(primaryType: string, data: EIP712Object, types: EIP712Types): Buffer {
+ return ethUtil.sha3(signTypedDataUtils._encodeData(primaryType, data, types));
+ },
+};