Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(kms-web3): use ethers _signTypedData #939

Merged
merged 1 commit into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion __tests__/localAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -121,6 +122,7 @@ const setup = async (options?: IAgentOptions): Promise<boolean> => {
})

const { provider, registry } = await createGanacheProvider()
const ethersProvider = createEthersProvider()

agent = createAgent<
IDIDManager &
Expand All @@ -146,7 +148,7 @@ const setup = async (options?: IAgentOptions): Promise<boolean> => {
kms: {
local: new KeyManagementSystem(new PrivateKeyStore(dbConnection, new SecretBox(secretKey))),
web3: new Web3KeyManagementSystem({
'ganache': provider
'ethers': ethersProvider
})
},
}),
Expand Down
16 changes: 7 additions & 9 deletions __tests__/shared/web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -35,7 +35,7 @@ export default (testContext: {
publicKeyHex: '',
meta: {
account,
provider: 'ganache',
provider: 'ethers',
algorithms: [
'eth_signMessage',
'eth_signTypedData',
Expand All @@ -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',
Expand Down
34 changes: 34 additions & 0 deletions __tests__/utils/ethers-provider.ts
Original file line number Diff line number Diff line change
@@ -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<any> {

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}`)
}
}
}
16 changes: 4 additions & 12 deletions packages/kms-web3/src/web3-key-management-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/did-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down