Skip to content

Commit

Permalink
fix: signTypedData of imKey (#2192)
Browse files Browse the repository at this point in the history
* fix: signTypedData of imKey

* Update eth-imkey-keyring.ts

* fix: wait ui
  • Loading branch information
heisenberg-2077 authored Apr 12, 2024
1 parent d7ba92b commit 1cb57e1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
15 changes: 9 additions & 6 deletions src/background/controller/provider/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,12 +440,8 @@ class ProviderController extends BaseController {
const chainItem = findChainByEnum(chain);

// wait ui
if (
currentAccount.type === KEYRING_TYPE.SimpleKeyring ||
currentAccount.type === KEYRING_TYPE.HdKeyring
) {
await new Promise((r) => setTimeout(r, 200));
}
await new Promise((r) => setTimeout(r, 100));

const statsData: StatsData = {
signed: false,
signedSuccess: false,
Expand Down Expand Up @@ -779,6 +775,10 @@ class ProviderController extends BaseController {
])
personalSign = async ({ data, approvalRes, session }) => {
if (!data.params) return;

// wait ui
await new Promise((r) => setTimeout(r, 100));

const currentAccount = preferenceService.getCurrentAccount()!;
try {
const [string, from] = data.params;
Expand Down Expand Up @@ -821,6 +821,9 @@ class ProviderController extends BaseController {
}
}

// wait ui
await new Promise((r) => setTimeout(r, 100));

return keyringService.signTypedMessage(
keyring,
{ from, data: _data },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { EVENTS } from '@/constant';
import { is1559Tx } from '@/utils/transaction';
import { bytesToHex } from 'web3-utils';
import { ImKeyBridgeInterface } from './imkey-bridge-interface';
import { signHashHex } from './utils';

const keyringType = 'imKey Hardware';
const MAX_INDEX = 1000;
Expand Down Expand Up @@ -330,10 +331,17 @@ export class EthImKeyKeyring extends EventEmitter {
await this.unlock();
const checksummedAddress = ethUtil.toChecksumAddress(address);
const accountDetail = this.accountDetails[checksummedAddress];
const isV4 = opts.version === 'V4';

if (opts.version !== 'V4' && opts.version !== 'V3') {
throw new Error('ImKey only supports V3 and V4 of typed data');
}

const eip712HashHexWithoutSha3 = signHashHex(data, isV4);

const res = await this.invokeApp('signMessage', [
accountDetail.hdPath,
JSON.stringify(data),
eip712HashHexWithoutSha3,
checksummedAddress,
false,
]);
Expand Down
32 changes: 32 additions & 0 deletions src/background/service/keyring/eth-imkey-keyring/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as sigUtil from 'eth-sig-util';
import * as ethUtil from 'ethereumjs-util';

/**
* Signs a typed message as per EIP-712 and returns its sha3 hash
*
* @param {Object} typedData - Types message data to sign
* @returns {Buffer} - sha3 hash of the resulting signed message
*/
export function signHashHex(typedData, useV4 = true): string {
const sanitizedData = sigUtil.TypedDataUtils.sanitizeData(typedData);
const parts = [Buffer.from('1901', 'hex')];
parts.push(
sigUtil.TypedDataUtils.hashStruct(
'EIP712Domain',
sanitizedData.domain,
sanitizedData.types,
useV4
)
);
if (sanitizedData.primaryType !== 'EIP712Domain') {
parts.push(
sigUtil.TypedDataUtils.hashStruct(
sanitizedData.primaryType as string,
sanitizedData.message,
sanitizedData.types,
useV4
)
);
}
return ethUtil.bufferToHex(Buffer.concat(parts));
}
2 changes: 1 addition & 1 deletion src/background/service/keyring/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class SignHelper {
const result = await fn();
resolve(result);
} catch (e) {
throwError(e.message, this.errorEventName);
throwError(e?.message ?? e, this.errorEventName);
}
};
this.signFn();
Expand Down

0 comments on commit 1cb57e1

Please sign in to comment.