1
1
import type { Messenger } from '@metamask/base-controller' ;
2
- import type { SnapKeyring } from '@metamask/eth-snap-keyring' ;
3
- import type { KeyringMetadata } from '@metamask/keyring-controller' ;
4
- import type {
5
- EthKeyring ,
6
- InternalAccount ,
7
- } from '@metamask/keyring-internal-api' ;
2
+ import type { InternalAccount } from '@metamask/keyring-internal-api' ;
8
3
9
4
import { SolAccountProvider } from './SolAccountProvider' ;
10
5
import {
11
6
getMultichainAccountControllerMessenger ,
12
7
getRootMessenger ,
13
8
MOCK_HD_ACCOUNT_1 ,
14
9
MOCK_SNAP_ACCOUNT_1 ,
15
- MockAccountBuilder ,
16
10
} from '../tests' ;
17
11
import type {
18
12
AllowedActions ,
@@ -21,61 +15,6 @@ import type {
21
15
MultichainAccountControllerEvents ,
22
16
} from '../types' ;
23
17
24
- class MockSolanaKeyring {
25
- readonly type = 'MockSolanaKeyring' ;
26
-
27
- readonly metadata : KeyringMetadata = {
28
- id : 'mock-solana-keyring-id' ,
29
- name : '' ,
30
- } ;
31
-
32
- readonly accounts : InternalAccount [ ] ;
33
-
34
- constructor ( accounts : InternalAccount [ ] ) {
35
- this . accounts = accounts ;
36
- }
37
-
38
- #getIndexFromDerivationPath( derivationPath : string ) : number {
39
- // eslint-disable-next-line prefer-regex-literals
40
- const derivationPathIndexRegex = new RegExp (
41
- "m/44'/501'/(?<index>[0-9]+)'/0" ,
42
- 'u' ,
43
- ) ;
44
-
45
- const matched = derivationPath . match ( derivationPathIndexRegex ) ;
46
- if ( matched ?. groups ?. index === undefined ) {
47
- throw new Error ( 'Unable to extract index' ) ;
48
- }
49
-
50
- const { index } = matched . groups ;
51
- return Number ( index ) ;
52
- }
53
-
54
- createAccount : SnapKeyring [ 'createAccount' ] = jest
55
- . fn ( )
56
- . mockImplementation ( ( _ , options ) => {
57
- if ( options . derivationPath !== undefined ) {
58
- const index = this . #getIndexFromDerivationPath( options . derivationPath ) ;
59
- const found = this . accounts . find (
60
- ( account ) => account . options . index === index ,
61
- ) ;
62
-
63
- if ( found ) {
64
- return found ; // Idempotent.
65
- }
66
- }
67
-
68
- const account = MockAccountBuilder . from ( MOCK_SNAP_ACCOUNT_1 )
69
- . withUuid ( )
70
- . withAddressSuffix ( `${ this . accounts . length } ` )
71
- . withGroupIndex ( this . accounts . length )
72
- . get ( ) ;
73
- this . accounts . push ( account ) ;
74
-
75
- return account ;
76
- } ) ;
77
- }
78
-
79
18
/**
80
19
* Sets up a SolAccountProvider for testing.
81
20
*
@@ -99,56 +38,19 @@ function setup({
99
38
MultichainAccountControllerActions | AllowedActions ,
100
39
MultichainAccountControllerEvents | AllowedEvents
101
40
> ;
102
- keyring : MockSolanaKeyring ;
103
- mocks : {
104
- handleRequest : jest . Mock ;
105
- keyring : {
106
- createAccount : jest . Mock ;
107
- } ;
108
- } ;
109
41
} {
110
- const keyring = new MockSolanaKeyring ( accounts ) ;
111
-
112
42
messenger . registerActionHandler (
113
43
'AccountsController:listMultichainAccounts' ,
114
44
( ) => accounts ,
115
45
) ;
116
46
117
- const mockHandleRequest = jest
118
- . fn ( )
119
- . mockImplementation ( ( address : string ) =>
120
- keyring . accounts . find ( ( account ) => account . address === address ) ,
121
- ) ;
122
- messenger . registerActionHandler (
123
- 'SnapController:handleRequest' ,
124
- mockHandleRequest ,
125
- ) ;
126
-
127
- messenger . registerActionHandler (
128
- 'KeyringController:withKeyring' ,
129
- async ( _ , operation ) =>
130
- operation ( {
131
- // We type-cast here, since `withKeyring` defaults to `EthKeyring` and the
132
- // Snap keyring does really implement this interface (this is expected).
133
- keyring : keyring as unknown as EthKeyring ,
134
- metadata : keyring . metadata ,
135
- } ) ,
136
- ) ;
137
-
138
47
const provider = new SolAccountProvider (
139
48
getMultichainAccountControllerMessenger ( messenger ) ,
140
49
) ;
141
50
142
51
return {
143
52
provider,
144
53
messenger,
145
- keyring,
146
- mocks : {
147
- handleRequest : mockHandleRequest ,
148
- keyring : {
149
- createAccount : keyring . createAccount as jest . Mock ,
150
- } ,
151
- } ,
152
54
} ;
153
55
}
154
56
0 commit comments