From bf8bd55571ddb06c7168f2b01be1017b19d7731a Mon Sep 17 00:00:00 2001 From: MrNerdHair Date: Fri, 11 Feb 2022 19:28:37 -0500 Subject: [PATCH] integration: improve xdefi mocks --- integration/src/wallets/mocks/@xdefi/xdefi.ts | 87 ----------------- integration/src/wallets/xdefi.ts | 93 ++++++++++++++++++- 2 files changed, 90 insertions(+), 90 deletions(-) delete mode 100644 integration/src/wallets/mocks/@xdefi/xdefi.ts diff --git a/integration/src/wallets/mocks/@xdefi/xdefi.ts b/integration/src/wallets/mocks/@xdefi/xdefi.ts deleted file mode 100644 index b5f070c7c..000000000 --- a/integration/src/wallets/mocks/@xdefi/xdefi.ts +++ /dev/null @@ -1,87 +0,0 @@ -import * as core from "@shapeshiftoss/hdwallet-core"; -import * as xdefi from "@shapeshiftoss/hdwallet-xdefi"; - -const mockSignEthTxResponse = { - r: "0x122269dc9cffc02962cdaa5af54913ac3e7293c3dd2a8ba7e38da2bc638f92df", - s: "0x36334d475fc12eb62681fb2cb10f177101d5cf4c3a735c94460d92bfa2389cc8", - v: 1, - serialized: - "0x02f872018084540ae4808516854be509825ac394fc0cc6e85dff3d75e3985e0cb83b090cfd498dd1871550f7dca7000080c001a0122269dc9cffc02962cdaa5af54913ac3e7293c3dd2a8ba7e38da2bc638f92dfa036334d475fc12eb62681fb2cb10f177101d5cf4c3a735c94460d92bfa2389cc8", -}; - -const mockSignEthTxResponse1559 = { - r: "0x63db3dd3bf3e1fe7dde1969c0fc8850e34116d0b501c0483a0e08c0f77b8ce0a", - s: "0x28297d012cccf389f6332415e96ee3fc0bbf8474d05f646e029cd281a031464b", - v: 38, - serialized: - "0xf86b018501dcd650008256229412ec06288edd7ae2cc41a843fe089237fc7354f0872c68af0bb140008026a063db3dd3bf3e1fe7dde1969c0fc8850e34116d0b501c0483a0e08c0f77b8ce0aa028297d012cccf389f6332415e96ee3fc0bbf8474d05f646e029cd281a031464b", -}; -const mockSignEthTxResponse1559Optional = { - r: "0x63db3dd3bf3e1fe7dde1969c0fc8850e34116d0b501c0483a0e08c0f77b8ce0a", - s: "0x28297d012cccf389f6332415e96ee3fc0bbf8474d05f646e029cd281a031464b", - v: 38, - serialized: - "0xf86b018501dcd650008256229412ec06288edd7ae2cc41a843fe089237fc7354f0872c68af0bb140008026a063db3dd3bf3e1fe7dde1969c0fc8850e34116d0b501c0483a0e08c0f77b8ce0aa028297d012cccf389f6332415e96ee3fc0bbf8474d05f646e029cd281a031464b", -}; -const mockSignERC20Tx = { - r: "0x1238fd332545415f09a01470350a5a20abc784dbf875cf58f7460560e66c597f", - s: "0x10efa4dd6fdb381c317db8f815252c2ac0d2a883bd364901dee3dec5b7d3660a", - v: 37, - serialized: - "0xf8a20114149441e5560054824ea6b0732e656e3ad64e20e94e4580b844a9059cbb0000000000000000000000001d8ce9022f6284c3a5c317f8f34620107214e54500000000000000000000000000000000000000000000000000000002540be40025a01238fd332545415f09a01470350a5a20abc784dbf875cf58f7460560e66c597fa010efa4dd6fdb381c317db8f815252c2ac0d2a883bd364901dee3dec5b7d3660a", -}; -const mockSignLongContractData = { - r: "0x5ea245ddd00fdf3958d6223255e37dcb0c61fa62cfa9cfb25e507da16ec8d96a", - s: "0x6c428730776958b80fd2b2201600420bb49059f9b34ee3b960cdcce45d4a1e09", - v: 37, - serialized: - "0xf9063081ab85055ae826008305140e94def1c0ded9bec7f1a1670819833240f027b25eff80b905c8415565b0000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000000000000000c5c360b9c0000000000000000000000000000000000000000000000000000000c58cb06ec00000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000003600000000000000000000000000000000000000000000000000000000000000013000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000002c000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000280000000000000000000000000000000000000000000000000000000000000028000000000000000000000000000000000000000000000000000000000000002600000000000000000000000000000000000000000000000000000000c5c360b9c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000a446f646f0000000000000000000000000000000000000000000000000000000000000000000000000000000c5c360b9c0000000000000000000000000000000000000000000000000000000c58cb06ec00000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000060000000000000000000000000533da777aedce766ceae696bf90f8541a4ba80eb000000000000000000000000c9f93163c99695c6526b799ebca2207fdf7d61ad0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000003000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee0000000000000000000000000000000000000000000000000000000000000000869584cd000000000000000000000000c770eefad204b5180df6a14ee197d99d808ee52d0000000000000000000000000000000000000000000000da413736cc60c8dd4e25a05ea245ddd00fdf3958d6223255e37dcb0c61fa62cfa9cfb25e507da16ec8d96aa06c428730776958b80fd2b2201600420bb49059f9b34ee3b960cdcce45d4a1e09", -}; - -export async function createMockWallet(): Promise { - const wallet = new xdefi.XDeFiHDWallet(); - // mock wallet provider - wallet.ethSignTx = jest - .fn() - .mockReturnValueOnce(mockSignEthTxResponse) - .mockReturnValueOnce(mockSignEthTxResponse1559) - .mockReturnValueOnce(mockSignEthTxResponse1559Optional) - .mockReturnValueOnce(mockSignERC20Tx) - .mockReturnValueOnce(mockSignLongContractData); - - wallet.ethSignMessage = jest.fn().mockReturnValue({ - address: "0x3f2329C9ADFbcCd9A84f52c906E936A42dA18CB8", - signature: - "0x29f7212ecc1c76cea81174af267b67506f754ea8c73f144afa900a0d85b24b21319621aeb062903e856352f38305710190869c3ce5a1425d65ef4fa558d0fc251b", - }); - - wallet.ethVerifyMessage = jest.fn().mockReturnValue({ - address: "0x3f2329C9ADFbcCd9A84f52c906E936A42dA18CB8", - message: "Hello World", - signature: - "0x29f7212ecc1c76cea81174af267b67506f754ea8c73f144afa900a0d85b24b21319621aeb062903e856352f38305710190869c3ce5a1425d65ef4fa558d0fc251b", - }); - wallet.provider = { - request: jest.fn(({ method, params }: any) => { - switch (method) { - case "eth_accounts": - return ["0x3f2329C9ADFbcCd9A84f52c906E936A42dA18CB8"]; - case "personal_sign": - const [message] = params; - - if (message === "48656c6c6f20576f726c64") - return "0x29f7212ecc1c76cea81174af267b67506f754ea8c73f144afa900a0d85b24b21319621aeb062903e856352f38305710190869c3ce5a1425d65ef4fa558d0fc251b"; - - throw new Error("unknown message"); - case "eth_sendTransaction": - const [{ to }] = params; - - return `txHash-${to}`; - default: - throw new Error(`ethereum: Unknown method ${method}`); - } - }), - }; - - return wallet; -} diff --git a/integration/src/wallets/xdefi.ts b/integration/src/wallets/xdefi.ts index a08b2fa80..1e5e0d989 100644 --- a/integration/src/wallets/xdefi.ts +++ b/integration/src/wallets/xdefi.ts @@ -1,14 +1,101 @@ import * as core from "@shapeshiftoss/hdwallet-core"; import * as xdefi from "@shapeshiftoss/hdwallet-xdefi"; -import { createMockWallet } from "./mocks/@xdefi/xdefi"; + +const mockSignEthTxResponse = { + r: "0x122269dc9cffc02962cdaa5af54913ac3e7293c3dd2a8ba7e38da2bc638f92df", + s: "0x36334d475fc12eb62681fb2cb10f177101d5cf4c3a735c94460d92bfa2389cc8", + v: 1, + serialized: + "0x02f872018084540ae4808516854be509825ac394fc0cc6e85dff3d75e3985e0cb83b090cfd498dd1871550f7dca7000080c001a0122269dc9cffc02962cdaa5af54913ac3e7293c3dd2a8ba7e38da2bc638f92dfa036334d475fc12eb62681fb2cb10f177101d5cf4c3a735c94460d92bfa2389cc8", +}; + +const mockSignEthTxResponse1559 = { + r: "0x63db3dd3bf3e1fe7dde1969c0fc8850e34116d0b501c0483a0e08c0f77b8ce0a", + s: "0x28297d012cccf389f6332415e96ee3fc0bbf8474d05f646e029cd281a031464b", + v: 38, + serialized: + "0xf86b018501dcd650008256229412ec06288edd7ae2cc41a843fe089237fc7354f0872c68af0bb140008026a063db3dd3bf3e1fe7dde1969c0fc8850e34116d0b501c0483a0e08c0f77b8ce0aa028297d012cccf389f6332415e96ee3fc0bbf8474d05f646e029cd281a031464b", +}; + +const mockSignEthTxResponse1559Optional = { + r: "0x63db3dd3bf3e1fe7dde1969c0fc8850e34116d0b501c0483a0e08c0f77b8ce0a", + s: "0x28297d012cccf389f6332415e96ee3fc0bbf8474d05f646e029cd281a031464b", + v: 38, + serialized: + "0xf86b018501dcd650008256229412ec06288edd7ae2cc41a843fe089237fc7354f0872c68af0bb140008026a063db3dd3bf3e1fe7dde1969c0fc8850e34116d0b501c0483a0e08c0f77b8ce0aa028297d012cccf389f6332415e96ee3fc0bbf8474d05f646e029cd281a031464b", +}; + +const mockSignERC20Tx = { + r: "0x1238fd332545415f09a01470350a5a20abc784dbf875cf58f7460560e66c597f", + s: "0x10efa4dd6fdb381c317db8f815252c2ac0d2a883bd364901dee3dec5b7d3660a", + v: 37, + serialized: + "0xf8a20114149441e5560054824ea6b0732e656e3ad64e20e94e4580b844a9059cbb0000000000000000000000001d8ce9022f6284c3a5c317f8f34620107214e54500000000000000000000000000000000000000000000000000000002540be40025a01238fd332545415f09a01470350a5a20abc784dbf875cf58f7460560e66c597fa010efa4dd6fdb381c317db8f815252c2ac0d2a883bd364901dee3dec5b7d3660a", +}; + +const mockSignLongContractData = { + r: "0x5ea245ddd00fdf3958d6223255e37dcb0c61fa62cfa9cfb25e507da16ec8d96a", + s: "0x6c428730776958b80fd2b2201600420bb49059f9b34ee3b960cdcce45d4a1e09", + v: 37, + serialized: + "0xf9063081ab85055ae826008305140e94def1c0ded9bec7f1a1670819833240f027b25eff80b905c8415565b0000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000000000000000c5c360b9c0000000000000000000000000000000000000000000000000000000c58cb06ec00000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000003600000000000000000000000000000000000000000000000000000000000000013000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000002c000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000280000000000000000000000000000000000000000000000000000000000000028000000000000000000000000000000000000000000000000000000000000002600000000000000000000000000000000000000000000000000000000c5c360b9c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000a446f646f0000000000000000000000000000000000000000000000000000000000000000000000000000000c5c360b9c0000000000000000000000000000000000000000000000000000000c58cb06ec00000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000060000000000000000000000000533da777aedce766ceae696bf90f8541a4ba80eb000000000000000000000000c9f93163c99695c6526b799ebca2207fdf7d61ad0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000003000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee0000000000000000000000000000000000000000000000000000000000000000869584cd000000000000000000000000c770eefad204b5180df6a14ee197d99d808ee52d0000000000000000000000000000000000000000000000da413736cc60c8dd4e25a05ea245ddd00fdf3958d6223255e37dcb0c61fa62cfa9cfb25e507da16ec8d96aa06c428730776958b80fd2b2201600420bb49059f9b34ee3b960cdcce45d4a1e09", +}; export function name(): string { return "XDeFi"; } export async function createWallet(): Promise { - const wallet = createMockWallet(); - if (!wallet) throw new Error("No XDeFi wallet found"); + // mock xdefi + (globalThis as any).xfi = { + ethereum: { + request: jest.fn(({ method, params }: any) => { + switch (method) { + case "eth_accounts": + case "eth_requestAccounts": + return ["0x3f2329C9ADFbcCd9A84f52c906E936A42dA18CB8"]; + case "personal_sign": + const [message] = params; + + if (message === "48656c6c6f20576f726c64") + return "0x29f7212ecc1c76cea81174af267b67506f754ea8c73f144afa900a0d85b24b21319621aeb062903e856352f38305710190869c3ce5a1425d65ef4fa558d0fc251b"; + + throw new Error("unknown message"); + case "eth_sendTransaction": + const [{ to }] = params; + + return `txHash-${to}`; + default: + throw new Error(`ethereum: Unknown method ${method}`); + } + }) + } + }; + + const adapter = xdefi.XDeFiAdapter.useKeyring(new core.Keyring()) + const wallet = await adapter.pairDevice() + + wallet.ethSignTx = jest + .fn() + .mockReturnValueOnce(mockSignEthTxResponse) + .mockReturnValueOnce(mockSignEthTxResponse1559) + .mockReturnValueOnce(mockSignEthTxResponse1559Optional) + .mockReturnValueOnce(mockSignERC20Tx) + .mockReturnValueOnce(mockSignLongContractData); + + wallet.ethSignMessage = jest.fn().mockReturnValue({ + address: "0x3f2329C9ADFbcCd9A84f52c906E936A42dA18CB8", + signature: + "0x29f7212ecc1c76cea81174af267b67506f754ea8c73f144afa900a0d85b24b21319621aeb062903e856352f38305710190869c3ce5a1425d65ef4fa558d0fc251b", + }); + + wallet.ethVerifyMessage = jest.fn().mockReturnValue({ + address: "0x3f2329C9ADFbcCd9A84f52c906E936A42dA18CB8", + message: "Hello World", + signature: + "0x29f7212ecc1c76cea81174af267b67506f754ea8c73f144afa900a0d85b24b21319621aeb062903e856352f38305710190869c3ce5a1425d65ef4fa558d0fc251b", + }); + return wallet; }