From f1fabd5d9d7accf60db7c8fd0f21a2bb7ae18233 Mon Sep 17 00:00:00 2001 From: Simonas Karuzas Date: Tue, 14 Jun 2022 14:13:51 +0300 Subject: [PATCH] fix(kms-web3): use ethers _signTypedData --- __tests__/localAgent.test.ts | 4 ++- __tests__/shared/web3.ts | 16 ++++----- __tests__/utils/ethers-provider.ts | 34 +++++++++++++++++++ .../src/web3-key-management-system.ts | 16 +++------ packages/utils/src/did-utils.ts | 2 +- 5 files changed, 49 insertions(+), 23 deletions(-) create mode 100644 __tests__/utils/ethers-provider.ts diff --git a/__tests__/localAgent.test.ts b/__tests__/localAgent.test.ts index 853534dd4..1f38b08e8 100644 --- a/__tests__/localAgent.test.ts +++ b/__tests__/localAgent.test.ts @@ -57,6 +57,7 @@ import { FakeDidProvider, FakeDidResolver } from '../packages/test-utils/src' import { Connection, createConnection } from 'typeorm' import { createGanacheProvider } from './utils/ganache-provider' +import { createEthersProvider } from './utils/ethers-provider' import { Resolver } from 'did-resolver' import { getResolver as ethrDidResolver } from 'ethr-did-resolver' import { getResolver as webDidResolver } from 'web-did-resolver' @@ -121,6 +122,7 @@ const setup = async (options?: IAgentOptions): Promise => { }) const { provider, registry } = await createGanacheProvider() + const ethersProvider = createEthersProvider() agent = createAgent< IDIDManager & @@ -146,7 +148,7 @@ const setup = async (options?: IAgentOptions): Promise => { kms: { local: new KeyManagementSystem(new PrivateKeyStore(dbConnection, new SecretBox(secretKey))), web3: new Web3KeyManagementSystem({ - 'ganache': provider + 'ethers': ethersProvider }) }, }), diff --git a/__tests__/shared/web3.ts b/__tests__/shared/web3.ts index 88aa10dce..9eb2a2ff4 100644 --- a/__tests__/shared/web3.ts +++ b/__tests__/shared/web3.ts @@ -19,13 +19,13 @@ export default (testContext: { }) afterAll(testContext.tearDown) - it('should import ganache did', async () => { - const account = `0x7e5f4552091a69125d5dfcb7b8c2659029395bdf` - const did = `did:ethr:ganache:${account}` - const controllerKeyId = `ganache-${account}` + it('should import ethers did', async () => { + const account = `0x71CB05EE1b1F506fF321Da3dac38f25c0c9ce6E1` + const did = `did:ethr:${account}` + const controllerKeyId = `ethers-${account}` identifier = await agent.didManagerImport({ did, - provider: 'did:ethr:ganache', + provider: 'did:ethr', controllerKeyId, keys: [{ kid: controllerKeyId, @@ -35,7 +35,7 @@ export default (testContext: { publicKeyHex: '', meta: { account, - provider: 'ganache', + provider: 'ethers', algorithms: [ 'eth_signMessage', 'eth_signTypedData', @@ -45,9 +45,7 @@ export default (testContext: { }) }) - // getting error: The method personal_sign does not exist/is not available - // https://github.com/trufflesuite/ganache/issues/995 - it.skip('should sign a message', async () => { + it('should sign a message', async () => { if (identifier.controllerKeyId) { const signature = await agent.keyManagerSign({ data: 'Hello world', diff --git a/__tests__/utils/ethers-provider.ts b/__tests__/utils/ethers-provider.ts new file mode 100644 index 000000000..c8c266f47 --- /dev/null +++ b/__tests__/utils/ethers-provider.ts @@ -0,0 +1,34 @@ +import { Web3Provider, ExternalProvider } from '@ethersproject/providers' +import { Wallet } from '@ethersproject/wallet' + +export function createEthersProvider(): Web3Provider { + const privateKeyHex = '0x1da6847600b0ee25e9ad9a52abbd786dd2502fa4005dd5af9310b7cc7a3b25db' + const wallet = new Wallet(privateKeyHex) + const mockProvider = new MockWeb3Provider(wallet) + const provider = new Web3Provider(mockProvider) + return provider +} + + +class MockWeb3Provider implements ExternalProvider { + constructor(private wallet: Wallet){ + + } + async request(request: { method: string; params?: any[] }): Promise { + + switch(request.method) { + case 'personal_sign': + //@ts-ignore + return this.wallet.signMessage(request.params[1]) + break + case 'eth_signTypedData_v4': + //@ts-ignore + const {domain, types, message} = JSON.parse(request.params[1]) + delete(types.EIP712Domain) + return this.wallet._signTypedData(domain, types, message) + break + default: + throw Error(`not_available: method ${request.method}`) + } + } +} \ No newline at end of file diff --git a/packages/kms-web3/src/web3-key-management-system.ts b/packages/kms-web3/src/web3-key-management-system.ts index 1524f4219..a84a9def2 100644 --- a/packages/kms-web3/src/web3-key-management-system.ts +++ b/packages/kms-web3/src/web3-key-management-system.ts @@ -110,19 +110,11 @@ export class Web3KeyManagementSystem extends AbstractKeyManagementSystem { `invalid_arguments: Cannot sign typed data. 'domain', 'types', and 'message' must be provided`, ) } - const { signer, account } = this.getAccountAndSignerByKeyRef(keyRef) + delete(msgTypes.EIP712Domain) + + const { signer } = this.getAccountAndSignerByKeyRef(keyRef) + const signature = await signer._signTypedData(msgDomain, msgTypes, msg) - const signature = await signer.provider.send('eth_signTypedData_v4', [ - account, - { - domain: msgDomain, - types: msgTypes, - primaryType: msgPrimaryType, - message: msg - } - ]) - // ._signTypedData(msgDomain, msgTypes, msg) - return signature } diff --git a/packages/utils/src/did-utils.ts b/packages/utils/src/did-utils.ts index 8f4212864..68b160cb0 100644 --- a/packages/utils/src/did-utils.ts +++ b/packages/utils/src/did-utils.ts @@ -89,7 +89,7 @@ function compareBlockchainAccountId( } let vmEthAddr = getEthereumAddress(verificationMethod) if (localKey.meta?.account) { - return vmEthAddr === localKey.meta?.account + return vmEthAddr === localKey.meta?.account.toLowerCase() } const computedAddr = computeAddress('0x' + localKey.publicKeyHex).toLowerCase() return computedAddr === vmEthAddr