1
1
import { type Bip44Account } from '@metamask/account-api' ;
2
2
import type { EntropySourceId , KeyringAccount } from '@metamask/keyring-api' ;
3
+ import { SolScope } from '@metamask/keyring-api' ;
3
4
import {
4
5
KeyringAccountEntropyTypeOption ,
5
6
SolAccountType ,
6
7
} from '@metamask/keyring-api' ;
7
8
import { KeyringTypes } from '@metamask/keyring-controller' ;
8
9
import type { InternalAccount } from '@metamask/keyring-internal-api' ;
10
+ import { KeyringClient } from '@metamask/keyring-snap-client' ;
9
11
import type { SnapId } from '@metamask/snaps-sdk' ;
12
+ import { HandlerType } from '@metamask/snaps-utils' ;
13
+ import type { Json , JsonRpcRequest } from '@metamask/utils' ;
10
14
import type { MultichainAccountServiceMessenger } from 'src/types' ;
11
15
12
16
import { assertAreBip44Accounts } from './BaseBip44AccountProvider' ;
@@ -15,8 +19,30 @@ import { SnapAccountProvider } from './SnapAccountProvider';
15
19
export class SolAccountProvider extends SnapAccountProvider {
16
20
static SOLANA_SNAP_ID = 'npm:@metamask/solana-wallet-snap' as SnapId ;
17
21
22
+ readonly #client: KeyringClient ;
23
+
18
24
constructor ( messenger : MultichainAccountServiceMessenger ) {
19
25
super ( SolAccountProvider . SOLANA_SNAP_ID , messenger ) ;
26
+ this . #client = this . #getKeyringClientFromSnapId(
27
+ SolAccountProvider . SOLANA_SNAP_ID ,
28
+ ) ;
29
+ }
30
+
31
+ #getKeyringClientFromSnapId( snapId : string ) : KeyringClient {
32
+ return new KeyringClient ( {
33
+ send : async ( request : JsonRpcRequest ) => {
34
+ const response = await this . messenger . call (
35
+ 'SnapController:handleRequest' ,
36
+ {
37
+ snapId : snapId as SnapId ,
38
+ origin : 'metamask' ,
39
+ handler : HandlerType . OnKeyringRequest ,
40
+ request,
41
+ } ,
42
+ ) ;
43
+ return response as Promise < Json > ;
44
+ } ,
45
+ } ) ;
20
46
}
21
47
22
48
isAccountCompatible ( account : Bip44Account < InternalAccount > ) : boolean {
@@ -60,10 +86,26 @@ export class SolAccountProvider extends SnapAccountProvider {
60
86
return accounts ;
61
87
}
62
88
63
- async discoverAndCreateAccounts ( _ : {
89
+ async discoverAndCreateAccounts ( {
90
+ entropySource,
91
+ groupIndex,
92
+ } : {
64
93
entropySource : EntropySourceId ;
65
94
groupIndex : number ;
66
95
} ) : Promise < Bip44Account < KeyringAccount > [ ] > {
67
- return [ ] ; // TODO: Implement account discovery.
96
+ const discoveredAccounts = await this . #client. discoverAccounts (
97
+ [ SolScope . Mainnet ] ,
98
+ entropySource ,
99
+ groupIndex ,
100
+ ) ;
101
+
102
+ const createdAccounts = await Promise . all (
103
+ discoveredAccounts . map (
104
+ async ( _account ) =>
105
+ await this . createAccounts ( { entropySource, groupIndex } ) ,
106
+ ) ,
107
+ ) ;
108
+
109
+ return createdAccounts . flat ( ) ;
68
110
}
69
111
}
0 commit comments