aboutsummaryrefslogtreecommitdiffstats
path: root/packages/utils/src
diff options
context:
space:
mode:
authorJacob Evans <jacob@dekz.net>2018-10-04 15:32:54 +0800
committerJacob Evans <jacob@dekz.net>2018-10-05 10:02:09 +0800
commit3e2fe40a11919f09f1f454c71f02aaa147b46b0c (patch)
tree0ff88e480944cfab0cefd211f5623f2870462351 /packages/utils/src
parent2a82ff48c061eacb3b6f9fb36eeae7f515b6d11d (diff)
downloaddexon-sol-tools-3e2fe40a11919f09f1f454c71f02aaa147b46b0c.tar
dexon-sol-tools-3e2fe40a11919f09f1f454c71f02aaa147b46b0c.tar.gz
dexon-sol-tools-3e2fe40a11919f09f1f454c71f02aaa147b46b0c.tar.bz2
dexon-sol-tools-3e2fe40a11919f09f1f454c71f02aaa147b46b0c.tar.lz
dexon-sol-tools-3e2fe40a11919f09f1f454c71f02aaa147b46b0c.tar.xz
dexon-sol-tools-3e2fe40a11919f09f1f454c71f02aaa147b46b0c.tar.zst
dexon-sol-tools-3e2fe40a11919f09f1f454c71f02aaa147b46b0c.zip
Add eth_signTypedData support to our wallet subproviders
Diffstat (limited to 'packages/utils/src')
-rw-r--r--packages/utils/src/sign_typed_data_utils.ts60
1 files changed, 26 insertions, 34 deletions
diff --git a/packages/utils/src/sign_typed_data_utils.ts b/packages/utils/src/sign_typed_data_utils.ts
index 902d8530c..b72fd099b 100644
--- a/packages/utils/src/sign_typed_data_utils.ts
+++ b/packages/utils/src/sign_typed_data_utils.ts
@@ -1,29 +1,30 @@
import * as ethUtil from 'ethereumjs-util';
import * as ethers from 'ethers';
-export interface EIP712Parameter {
- name: string;
- type: string;
-}
-
-export interface EIP712Types {
- [key: string]: EIP712Parameter[];
-}
-export interface EIP712TypedData {
- types: EIP712Types;
- domain: any;
- message: any;
- primaryType: string;
-}
+import { EIP712TypedData, EIP712Types } from '@0xproject/types';
export const signTypedDataUtils = {
- findDependencies(primaryType: string, types: EIP712Types, found: string[] = []): string[] {
+ /**
+ * Computes the Sign Typed Data hash
+ * @param typedData An object that conforms to the EIP712TypedData interface
+ * @return A Buffer containing the hash of the sign typed data.
+ */
+ signTypedDataHash(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)) {
+ for (const dep of signTypedDataUtils._findDependencies(field.type, types, found)) {
if (!found.includes(dep)) {
found.push(dep);
}
@@ -31,8 +32,8 @@ export const signTypedDataUtils = {
}
return found;
},
- encodeType(primaryType: string, types: EIP712Types): string {
- let deps = signTypedDataUtils.findDependencies(primaryType, types);
+ _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 = '';
@@ -41,9 +42,9 @@ export const signTypedDataUtils = {
}
return result;
},
- encodeData(primaryType: string, data: any, types: EIP712Types): string {
+ _encodeData(primaryType: string, data: any, types: EIP712Types): string {
const encodedTypes = ['bytes32'];
- const encodedValues = [signTypedDataUtils.typeHash(primaryType, types)];
+ const encodedValues = [signTypedDataUtils._typeHash(primaryType, types)];
for (const field of types[primaryType]) {
let value = data[field.name];
if (field.type === 'string' || field.type === 'bytes') {
@@ -52,7 +53,7 @@ export const signTypedDataUtils = {
encodedValues.push(value);
} else if (types[field.type] !== undefined) {
encodedTypes.push('bytes32');
- value = ethUtil.sha3(signTypedDataUtils.encodeData(field.type, value, types));
+ value = ethUtil.sha3(signTypedDataUtils._encodeData(field.type, value, types));
encodedValues.push(value);
} else if (field.type.lastIndexOf(']') === field.type.length - 1) {
throw new Error('Arrays currently unimplemented in encodeData');
@@ -63,19 +64,10 @@ export const signTypedDataUtils = {
}
return ethers.utils.defaultAbiCoder.encode(encodedTypes, encodedValues);
},
- typeHash(primaryType: string, types: EIP712Types): Buffer {
- return ethUtil.sha3(signTypedDataUtils.encodeType(primaryType, types));
+ _typeHash(primaryType: string, types: EIP712Types): Buffer {
+ return ethUtil.sha3(signTypedDataUtils._encodeType(primaryType, types));
},
- structHash(primaryType: string, data: any, types: EIP712Types): Buffer {
- return ethUtil.sha3(signTypedDataUtils.encodeData(primaryType, data, types));
- },
- signTypedDataHash(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),
- ]),
- );
+ _structHash(primaryType: string, data: any, types: EIP712Types): Buffer {
+ return ethUtil.sha3(signTypedDataUtils._encodeData(primaryType, data, types));
},
};