Skip to content

Commit

Permalink
fix something
Browse files Browse the repository at this point in the history
  • Loading branch information
adonesky1 committed Aug 16, 2022
1 parent cb484d3 commit 1e4f176
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
27 changes: 13 additions & 14 deletions src/sign-typed-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,24 +159,25 @@ function encodeField(
'bytes32',
version === SignTypedDataVersion.V4 && value == null // eslint-disable-line no-eq-null
? '0x0000000000000000000000000000000000000000000000000000000000000000'
: keccak256(encodeData(type, value, types, version)),
: toBuffer(keccak256(encodeData(type, value, types, version))),
];
}

if (value === undefined) {
throw new Error(`missing value for field ${name} of type ${type}`);
}

const textEncoder = new (global as any).TextEncoder();
if (type === 'bytes') {
return ['bytes32', keccak256(value)];
return ['bytes32', toBuffer(keccak256(textEncoder.encode(value)))];
}

if (type === 'string') {
// convert string to buffer - prevents ethUtil from interpreting strings like '0xabcd' as hex
if (typeof value === 'string') {
value = Buffer.from(value, 'utf8');
}
return ['bytes32', keccak256(value)];
return ['bytes32', toBuffer(keccak256(textEncoder.encode(value)))];
}

if (type.lastIndexOf(']') === type.length - 1) {
Expand All @@ -191,10 +192,12 @@ function encodeField(
);
return [
'bytes32',
keccak256(
rawEncode(
typeValuePairs.map(([t]) => t),
typeValuePairs.map(([, v]) => v),
toBuffer(
keccak256(
rawEncode(
typeValuePairs.map(([t]) => t),
typeValuePairs.map(([, v]) => v),
),
),
),
];
Expand Down Expand Up @@ -313,10 +316,8 @@ function hashStruct(
version: SignTypedDataVersion.V3 | SignTypedDataVersion.V4,
): Buffer {
validateVersion(version, [SignTypedDataVersion.V3, SignTypedDataVersion.V4]);
const encodedData = new Uint8Array(
encodeData(primaryType, data, types, version),
);
return toBuffer(keccak256(encodedData));

return toBuffer(keccak256(encodeData(primaryType, data, types, version)));
}

/**
Expand All @@ -331,9 +332,7 @@ function hashType(
types: Record<string, MessageTypeProperty[]>,
): Buffer {
const textEncoder = new (global as any).TextEncoder();
const encodedHashType = textEncoder.encode(
encodeType(primaryType, types),
);
const encodedHashType = textEncoder.encode(encodeType(primaryType, types));
return toBuffer(keccak256(encodedHashType));
}

Expand Down
3 changes: 3 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ export function normalize(input: number | string): string {
}

if (typeof input === 'number') {
if (input < 0) {
return '0x';
}
const buffer = toBuffer(input);
input = bufferToHex(buffer);
}
Expand Down

0 comments on commit 1e4f176

Please sign in to comment.