From d6b46e8be85fcaae887151b9ea466817859183c3 Mon Sep 17 00:00:00 2001 From: Jesse Clark Date: Fri, 26 Nov 2021 12:39:53 +0200 Subject: [PATCH 1/5] Convert incoming WC values from hex or strings to numbers needed for RIF Wallet. --- .../walletAdapters/WalletConnectAdapter.ts | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/lib/walletAdapters/WalletConnectAdapter.ts b/src/lib/walletAdapters/WalletConnectAdapter.ts index 065037591..65fb709eb 100644 --- a/src/lib/walletAdapters/WalletConnectAdapter.ts +++ b/src/lib/walletAdapters/WalletConnectAdapter.ts @@ -1,5 +1,5 @@ import { TransactionResponse } from '@ethersproject/providers' -import { Signer, constants, utils } from 'ethers' +import { Signer, constants, utils, BigNumber } from 'ethers' import { RIFWallet } from '../core' export class WalletConnectAdapter { private resolvers: IResolver[] @@ -41,13 +41,23 @@ class SendTransactionResolver implements IResolver { async resolve(params: any[]) { const payload = params.reduce((prev, curr) => ({ ...prev, ...curr }), {}) - if (payload.data === '') { - // TODO: assign undefined once the RIFWallet changes are applied - payload.data = constants.HashZero + if (payload.gas) { + delete payload.gas + } + + const toNumber = (value?: any) => + value ? BigNumber.from(payload.gasLimit).toNumber() : undefined + + const formattedPayload = { + ...payload, + data: payload.data || constants.HashZero, + gasLimit: toNumber(payload.gasLimit), + gasPrice: toNumber(payload.gasPrice), + value: toNumber(payload.value), } return this.signer - .sendTransaction(payload) + .sendTransaction(formattedPayload) .then((tx: TransactionResponse) => tx.hash) } } From 14387900ec1d389354e399c184ec307bb82d42a4 Mon Sep 17 00:00:00 2001 From: Jesse Clark Date: Mon, 29 Nov 2021 18:46:50 +0200 Subject: [PATCH 2/5] use correct variable. --- src/lib/walletAdapters/WalletConnectAdapter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/walletAdapters/WalletConnectAdapter.ts b/src/lib/walletAdapters/WalletConnectAdapter.ts index 65fb709eb..96c70c350 100644 --- a/src/lib/walletAdapters/WalletConnectAdapter.ts +++ b/src/lib/walletAdapters/WalletConnectAdapter.ts @@ -46,7 +46,7 @@ class SendTransactionResolver implements IResolver { } const toNumber = (value?: any) => - value ? BigNumber.from(payload.gasLimit).toNumber() : undefined + value ? BigNumber.from(value).toNumber() : undefined const formattedPayload = { ...payload, From b0916869ee14d6df6fee255a62d5777512f3c188 Mon Sep 17 00:00:00 2001 From: Jesse Clark Date: Wed, 1 Dec 2021 13:33:58 +0200 Subject: [PATCH 3/5] Requested changes. --- .../walletAdapters/WalletConnectAdapter.ts | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/lib/walletAdapters/WalletConnectAdapter.ts b/src/lib/walletAdapters/WalletConnectAdapter.ts index 96c70c350..36b1f93f5 100644 --- a/src/lib/walletAdapters/WalletConnectAdapter.ts +++ b/src/lib/walletAdapters/WalletConnectAdapter.ts @@ -1,4 +1,5 @@ import { TransactionResponse } from '@ethersproject/providers' +import { TransactionRequest } from '@ethersproject/abstract-provider' import { Signer, constants, utils, BigNumber } from 'ethers' import { RIFWallet } from '../core' export class WalletConnectAdapter { @@ -41,19 +42,15 @@ class SendTransactionResolver implements IResolver { async resolve(params: any[]) { const payload = params.reduce((prev, curr) => ({ ...prev, ...curr }), {}) - if (payload.gas) { - delete payload.gas - } - - const toNumber = (value?: any) => - value ? BigNumber.from(value).toNumber() : undefined - - const formattedPayload = { - ...payload, + const formattedPayload: TransactionRequest = { + to: payload.to, + from: payload.from, + nonce: payload.nonce, data: payload.data || constants.HashZero, - gasLimit: toNumber(payload.gasLimit), - gasPrice: toNumber(payload.gasPrice), - value: toNumber(payload.value), + value: BigNumber.from(payload.value || 0), + chainId: payload.chainId, + gasLimit: BigNumber.from(payload.gas || 0), // WC's gas to gasLimit + gasPrice: BigNumber.from(payload.gasPrice || 0), } return this.signer From c2fa7eb79fd92cc803e0d62905bc6647ec36ca3f Mon Sep 17 00:00:00 2001 From: Jesse Clark Date: Wed, 1 Dec 2021 13:50:53 +0200 Subject: [PATCH 4/5] Add sample values so it can send. --- src/lib/walletAdapters/WalletConnectAdapter.test.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lib/walletAdapters/WalletConnectAdapter.test.ts b/src/lib/walletAdapters/WalletConnectAdapter.test.ts index c4e37119a..d4c970a95 100644 --- a/src/lib/walletAdapters/WalletConnectAdapter.test.ts +++ b/src/lib/walletAdapters/WalletConnectAdapter.test.ts @@ -23,6 +23,8 @@ describe('Wallet Connect Adapter', () => { from, to, value: '0x3e8', + gas: '25000', + gasPrice: '60000000', }, ] From d8fd4bd6dce3258ac272e45ab7457174d7ee640f Mon Sep 17 00:00:00 2001 From: Jesse Clark Date: Wed, 1 Dec 2021 17:28:06 +0200 Subject: [PATCH 5/5] Rewrite tests to always expect signer and walletAdapters - Spyon the sendTransaction method to get the parameters that are being sent to the provider - Change default values to undefined not zero. --- .../WalletConnectAdapter.test.ts | 82 +++++++++++++++---- .../walletAdapters/WalletConnectAdapter.ts | 8 +- 2 files changed, 70 insertions(+), 20 deletions(-) diff --git a/src/lib/walletAdapters/WalletConnectAdapter.test.ts b/src/lib/walletAdapters/WalletConnectAdapter.test.ts index d4c970a95..bd9c917f2 100644 --- a/src/lib/walletAdapters/WalletConnectAdapter.test.ts +++ b/src/lib/walletAdapters/WalletConnectAdapter.test.ts @@ -1,49 +1,99 @@ -import { Signer } from 'ethers' +import { BigNumber, Signer } from 'ethers' import { getSigner } from '../../../testLib/utils' import { WalletConnectAdapter } from './WalletConnectAdapter' -describe('Wallet Connect Adapter', () => { - let adapter: WalletConnectAdapter | null = null - let signer: Signer | null = null - +describe('Wallet Connect Adapter', function (this: { + adapter: WalletConnectAdapter + signer: Signer +}) { beforeEach(async () => { - signer = getSigner(9) - - adapter = new WalletConnectAdapter(signer) + this.signer = getSigner(9) + this.adapter = new WalletConnectAdapter(this.signer) }) test('send tx', async () => { - const from = await signer?.getAddress() + const from = await this.signer.getAddress() const to = await getSigner(0).getAddress() + const chainId = await this.signer.getChainId() const method = 'eth_sendTransaction' const params = [ { - data: '', + data: '0x123456789a', from, to, value: '0x3e8', gas: '25000', gasPrice: '60000000', + chainId, }, ] - const result = await adapter?.handleCall(method, params) + const mockIt = jest.fn() - expect(result).toBeDefined() - expect(result).toContain('0x') + jest.spyOn(this.signer, 'sendTransaction').mockImplementation((tx: any) => { + mockIt(tx) + return Promise.resolve(tx) + }) + + await this.adapter.handleCall(method, params) + + expect(mockIt).toBeCalledWith({ + data: params[0].data, + from, + to, + chainId, + nonce: undefined, + gasPrice: BigNumber.from(params[0].gasPrice), + gasLimit: BigNumber.from(params[0].gas), + value: BigNumber.from('0x3e8'), + }) + }) + + test('default parameters are set', async () => { + const method = 'eth_sendTransaction' + + const from = await this.signer.getAddress() + const to = await getSigner(0).getAddress() + + const params = [ + { + to, + from, + }, + ] + + const mockIt = jest.fn() + + jest.spyOn(this.signer, 'sendTransaction').mockImplementation((tx: any) => { + mockIt(tx) + return Promise.resolve(tx) + }) + + await this.adapter.handleCall(method, params) + + expect(mockIt).toBeCalledWith({ + to, + from, + data: '0x', + chainId: undefined, + gasLimit: undefined, + gasPrice: undefined, + nonce: undefined, + value: BigNumber.from(0), + }) }) // method not supported by ganache-cli // eslint-disable-next-line jest/no-disabled-tests test.skip('personal sign', async () => { - const from = await signer?.getAddress() + const from = await this.signer.getAddress() const message = '0x68656c6c6f20776f726c6421' const method = 'personal_sign' const params = [message, from] - const result = await adapter?.handleCall(method, params) + const result = await this.adapter.handleCall(method, params) expect(result).toBeDefined() expect(result).toContain('0x') @@ -58,7 +108,7 @@ describe('Wallet Connect Adapter', () => { '{"domain":{"chainId":31,"name":"Ether Mail","version":"1"},"message":{"contents":"{ from: { name: \'Cow\', wallet: \'0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826\' }, to: { name: \'Bob\', wallet: \'0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB\' }, contents: \'Hello, Bob!\' }","from":"Diego","to":"Cesar"},"primaryType":"Mail","types":{"EIP712Domain":[{"name":"name","type":"string"},{"name":"version","type":"string"},{"name":"chainId","type":"uint256"}],"Mail":[{"name":"from","type":"string"},{"name":"to","type":"string"},{"name":"contents","type":"string"}]}}', ] - const result = await adapter?.handleCall(method, params) + const result = await this.adapter.handleCall(method, params) expect(result).toBeDefined() expect(result).toContain('0x') diff --git a/src/lib/walletAdapters/WalletConnectAdapter.ts b/src/lib/walletAdapters/WalletConnectAdapter.ts index 36b1f93f5..797168ae3 100644 --- a/src/lib/walletAdapters/WalletConnectAdapter.ts +++ b/src/lib/walletAdapters/WalletConnectAdapter.ts @@ -1,6 +1,6 @@ import { TransactionResponse } from '@ethersproject/providers' import { TransactionRequest } from '@ethersproject/abstract-provider' -import { Signer, constants, utils, BigNumber } from 'ethers' +import { Signer, utils, BigNumber } from 'ethers' import { RIFWallet } from '../core' export class WalletConnectAdapter { private resolvers: IResolver[] @@ -46,11 +46,11 @@ class SendTransactionResolver implements IResolver { to: payload.to, from: payload.from, nonce: payload.nonce, - data: payload.data || constants.HashZero, + data: payload.data || '0x', value: BigNumber.from(payload.value || 0), chainId: payload.chainId, - gasLimit: BigNumber.from(payload.gas || 0), // WC's gas to gasLimit - gasPrice: BigNumber.from(payload.gasPrice || 0), + gasLimit: payload.gas ? BigNumber.from(payload.gas) : undefined, // WC's gas to gasLimit + gasPrice: payload.gasPrice ? BigNumber.from(payload.gasPrice) : undefined, } return this.signer