|
| 1 | +import { |
| 2 | + KeyringRpcV2Method, |
| 3 | + PrivateKeyEncoding, |
| 4 | + type KeyringAccount, |
| 5 | + type KeyringRequest, |
| 6 | +} from '@metamask/keyring-api'; |
| 7 | +import type { Json } from '@metamask/utils'; |
| 8 | + |
| 9 | +import { KeyringClientV2 } from './KeyringClientV2'; |
| 10 | + |
| 11 | +describe('KeyringClient', () => { |
| 12 | + const mockSender = { |
| 13 | + send: jest.fn(), |
| 14 | + }; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + mockSender.send.mockClear(); |
| 18 | + }); |
| 19 | + |
| 20 | + describe('KeyringClientV2', () => { |
| 21 | + const client = new KeyringClientV2(mockSender); |
| 22 | + |
| 23 | + describe('getAccounts', () => { |
| 24 | + it('sends a request to get accounts and return the response', async () => { |
| 25 | + const expectedResponse: KeyringAccount[] = [ |
| 26 | + { |
| 27 | + id: '49116980-0712-4fa5-b045-e4294f1d440e', |
| 28 | + address: '0xE9A74AACd7df8112911ca93260fC5a046f8a64Ae', |
| 29 | + options: {}, |
| 30 | + methods: [], |
| 31 | + scopes: ['eip155:0'], |
| 32 | + type: 'eip155:eoa', |
| 33 | + }, |
| 34 | + ]; |
| 35 | + |
| 36 | + mockSender.send.mockResolvedValue(expectedResponse); |
| 37 | + const accounts = await client.getAccounts(); |
| 38 | + expect(mockSender.send).toHaveBeenCalledWith({ |
| 39 | + jsonrpc: '2.0', |
| 40 | + id: expect.any(String), |
| 41 | + method: `${KeyringRpcV2Method.GetAccounts}`, |
| 42 | + }); |
| 43 | + expect(accounts).toStrictEqual(expectedResponse); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + describe('getAccount', () => { |
| 48 | + it('sends a request to get an account by ID and return the response', async () => { |
| 49 | + const id = '49116980-0712-4fa5-b045-e4294f1d440e'; |
| 50 | + const expectedResponse: KeyringAccount = { |
| 51 | + id: '49116980-0712-4fa5-b045-e4294f1d440e', |
| 52 | + address: '0xE9A74AACd7df8112911ca93260fC5a046f8a64Ae', |
| 53 | + options: {}, |
| 54 | + methods: [], |
| 55 | + scopes: ['eip155:0'], |
| 56 | + type: 'eip155:eoa', |
| 57 | + }; |
| 58 | + |
| 59 | + mockSender.send.mockResolvedValue(expectedResponse); |
| 60 | + const account = await client.getAccount(id); |
| 61 | + expect(mockSender.send).toHaveBeenCalledWith({ |
| 62 | + jsonrpc: '2.0', |
| 63 | + id: expect.any(String), |
| 64 | + method: `${KeyringRpcV2Method.GetAccount}`, |
| 65 | + params: { id }, |
| 66 | + }); |
| 67 | + expect(account).toStrictEqual(expectedResponse); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + describe('createAccounts', () => { |
| 72 | + it('sends a request to create an account and return the response', async () => { |
| 73 | + const expectedResponse: KeyringAccount[] = [ |
| 74 | + { |
| 75 | + id: '49116980-0712-4fa5-b045-e4294f1d440e', |
| 76 | + address: '0xE9A74AACd7df8112911ca93260fC5a046f8a64Ae', |
| 77 | + options: {}, |
| 78 | + methods: [], |
| 79 | + scopes: ['eip155:0'], |
| 80 | + type: 'eip155:eoa', |
| 81 | + }, |
| 82 | + ]; |
| 83 | + |
| 84 | + mockSender.send.mockResolvedValue(expectedResponse); |
| 85 | + const account = await client.createAccounts(); |
| 86 | + expect(mockSender.send).toHaveBeenCalledWith({ |
| 87 | + jsonrpc: '2.0', |
| 88 | + id: expect.any(String), |
| 89 | + method: `${KeyringRpcV2Method.CreateAccounts}`, |
| 90 | + params: { options: {} }, |
| 91 | + }); |
| 92 | + expect(account).toStrictEqual(expectedResponse); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + describe('deleteAccount', () => { |
| 97 | + it('sends a request to delete an account', async () => { |
| 98 | + const id = '49116980-0712-4fa5-b045-e4294f1d440e'; |
| 99 | + |
| 100 | + mockSender.send.mockResolvedValue(null); |
| 101 | + const response = await client.deleteAccount(id); |
| 102 | + expect(mockSender.send).toHaveBeenCalledWith({ |
| 103 | + jsonrpc: '2.0', |
| 104 | + id: expect.any(String), |
| 105 | + method: `${KeyringRpcV2Method.DeleteAccount}`, |
| 106 | + params: { id }, |
| 107 | + }); |
| 108 | + expect(response).toBeUndefined(); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + describe('exportAccount', () => { |
| 113 | + it('sends a request to export an account', async () => { |
| 114 | + const id = '49116980-0712-4fa5-b045-e4294f1d440e'; |
| 115 | + const expectedResponse = { |
| 116 | + type: 'private-key', |
| 117 | + privateKey: '0x000000000', |
| 118 | + encoding: 'hexadecimal', |
| 119 | + }; |
| 120 | + |
| 121 | + mockSender.send.mockResolvedValue(expectedResponse); |
| 122 | + const response = await client.exportAccount(id); |
| 123 | + expect(mockSender.send).toHaveBeenCalledWith({ |
| 124 | + jsonrpc: '2.0', |
| 125 | + id: expect.any(String), |
| 126 | + method: `${KeyringRpcV2Method.ExportAccount}`, |
| 127 | + params: { id }, |
| 128 | + }); |
| 129 | + expect(response).toStrictEqual(expectedResponse); |
| 130 | + }); |
| 131 | + |
| 132 | + it('sends a request to export an account with options', async () => { |
| 133 | + const id = '49116980-0712-4fa5-b045-e4294f1d440e'; |
| 134 | + const expectedResponse = { |
| 135 | + type: 'private-key', |
| 136 | + privateKey: '0x000000000', |
| 137 | + encoding: 'hexadecimal', |
| 138 | + }; |
| 139 | + const options = { |
| 140 | + type: 'private-key' as const, |
| 141 | + encoding: PrivateKeyEncoding.Hexadecimal, |
| 142 | + }; |
| 143 | + |
| 144 | + mockSender.send.mockResolvedValue(expectedResponse); |
| 145 | + const response = await client.exportAccount(id, options); |
| 146 | + expect(mockSender.send).toHaveBeenCalledWith({ |
| 147 | + jsonrpc: '2.0', |
| 148 | + id: expect.any(String), |
| 149 | + method: `${KeyringRpcV2Method.ExportAccount}`, |
| 150 | + params: { |
| 151 | + id, |
| 152 | + options, |
| 153 | + }, |
| 154 | + }); |
| 155 | + expect(response).toStrictEqual(expectedResponse); |
| 156 | + }); |
| 157 | + }); |
| 158 | + |
| 159 | + describe('submitRequest', () => { |
| 160 | + it('sends a request to submit a request', async () => { |
| 161 | + const request: KeyringRequest = { |
| 162 | + id: '71621d8d-62a4-4bf4-97cc-fb8f243679b0', |
| 163 | + scope: 'eip155:1', |
| 164 | + origin: 'test', |
| 165 | + account: '46b5ccd3-4786-427c-89d2-cef626dffe9b', |
| 166 | + request: { |
| 167 | + method: 'personal_sign', |
| 168 | + params: ['0xe9a74aacd7df8112911ca93260fc5a046f8a64ae', '0x0'], |
| 169 | + }, |
| 170 | + }; |
| 171 | + const expectedResponse: Json = { |
| 172 | + result: 'success', |
| 173 | + }; |
| 174 | + |
| 175 | + mockSender.send.mockResolvedValue(expectedResponse); |
| 176 | + const response = await client.submitRequest(request); |
| 177 | + expect(mockSender.send).toHaveBeenCalledWith({ |
| 178 | + jsonrpc: '2.0', |
| 179 | + id: expect.any(String), |
| 180 | + method: `${KeyringRpcV2Method.SubmitRequest}`, |
| 181 | + params: request, |
| 182 | + }); |
| 183 | + expect(response).toStrictEqual(expectedResponse); |
| 184 | + }); |
| 185 | + }); |
| 186 | + }); |
| 187 | +}); |
0 commit comments