@@ -6,6 +6,7 @@ import type { Injected, InjectedAccount, InjectedAccountWithMeta, InjectedExtens
6
6
import { u8aEq } from '@polkadot/util' ;
7
7
import { decodeAddress , encodeAddress } from '@polkadot/util-crypto' ;
8
8
9
+ import initCompat from './compat' ;
9
10
import { documentReadyPromise } from './util' ;
10
11
11
12
// expose utility functions
@@ -29,12 +30,13 @@ function throwError (method: string): never {
29
30
30
31
// internal helper to map from Array<InjectedAccount> -> Array<InjectedAccountWithMeta>
31
32
function mapAccounts ( source : string , list : InjectedAccount [ ] , ss58Format ?: number ) : InjectedAccountWithMeta [ ] {
32
- return list . map ( ( { address, genesisHash, name } ) : InjectedAccountWithMeta => {
33
+ return list . map ( ( { address, genesisHash, name, type } ) : InjectedAccountWithMeta => {
33
34
const encodedAddress = address . length === 42 ? address : encodeAddress ( decodeAddress ( address ) , ss58Format ) ;
34
35
35
36
return ( {
36
37
address : encodedAddress ,
37
- meta : { genesisHash, name, source }
38
+ meta : { genesisHash, name, source } ,
39
+ type
38
40
} ) ;
39
41
} ) ;
40
42
}
@@ -49,13 +51,14 @@ export { isWeb3Injected, web3EnablePromise };
49
51
50
52
function getWindowExtensions ( originName : string ) : Promise < [ InjectedExtensionInfo , Injected | void ] [ ] > {
51
53
return Promise . all (
52
- Object . entries ( win . injectedWeb3 ) . map ( ( [ name , { enable, version } ] ) : Promise < [ InjectedExtensionInfo , Injected | void ] > =>
53
- Promise . all ( [
54
- Promise . resolve ( { name, version } ) ,
55
- enable ( originName ) . catch ( ( error : Error ) : void => {
56
- console . error ( `Error initializing ${ name } : ${ error . message } ` ) ;
57
- } )
58
- ] )
54
+ Object . entries ( win . injectedWeb3 ) . map (
55
+ ( [ name , { enable, version } ] ) : Promise < [ InjectedExtensionInfo , Injected | void ] > =>
56
+ Promise . all ( [
57
+ Promise . resolve ( { name, version } ) ,
58
+ enable ( originName ) . catch ( ( error : Error ) : void => {
59
+ console . error ( `Error initializing ${ name } : ${ error . message } ` ) ;
60
+ } )
61
+ ] )
59
62
)
60
63
) ;
61
64
}
@@ -66,42 +69,50 @@ export function web3Enable (originName: string): Promise<InjectedExtension[]> {
66
69
throw new Error ( 'You must pass a name for your app to the web3Enable function' ) ;
67
70
}
68
71
69
- web3EnablePromise = documentReadyPromise ( ( ) : Promise < InjectedExtension [ ] > =>
70
- getWindowExtensions ( originName )
71
- . then ( ( values ) : InjectedExtension [ ] =>
72
- values
73
- . filter ( ( value ) : value is [ InjectedExtensionInfo , Injected ] => ! ! value [ 1 ] )
74
- . map ( ( [ info , ext ] ) : InjectedExtension => {
75
- // if we don't have an accounts subscriber, add a single-shot version
76
- if ( ! ext . accounts . subscribe ) {
77
- ext . accounts . subscribe = ( cb : ( accounts : InjectedAccount [ ] ) => void | Promise < void > ) : Unsubcall => {
78
- ext . accounts . get ( ) . then ( cb ) . catch ( console . error ) ;
79
-
80
- return ( ) : void => {
81
- // no ubsubscribe needed, this is a single-shot
82
- } ;
83
- } ;
84
- }
85
-
86
- return { ...info , ...ext } ;
72
+ web3EnablePromise = documentReadyPromise (
73
+ ( ) : Promise < InjectedExtension [ ] > =>
74
+ initCompat ( ) . then ( ( ) =>
75
+ getWindowExtensions ( originName )
76
+ . then ( ( values ) : InjectedExtension [ ] => {
77
+ return values
78
+ . filter ( ( value ) : value is [ InjectedExtensionInfo , Injected ] => ! ! value [ 1 ] )
79
+ . map (
80
+ ( [ info , ext ] ) : InjectedExtension => {
81
+ // if we don't have an accounts subscriber, add a single-shot version
82
+ if ( ! ext . accounts . subscribe ) {
83
+ ext . accounts . subscribe = ( cb : ( accounts : InjectedAccount [ ] ) => void | Promise < void > ) : Unsubcall => {
84
+ ext . accounts . get ( ) . then ( cb ) . catch ( console . error ) ;
85
+
86
+ return ( ) : void => {
87
+ // no ubsubscribe needed, this is a single-shot
88
+ } ;
89
+ } ;
90
+ }
91
+
92
+ return { ...info , ...ext } ;
93
+ }
94
+ ) ;
95
+ }
96
+ )
97
+ . catch ( ( ) : InjectedExtension [ ] => [ ] )
98
+ . then ( ( values ) : InjectedExtension [ ] => {
99
+ const names = values . map ( ( { name, version } ) : string => `${ name } /${ version } ` ) ;
100
+
101
+ isWeb3Injected = web3IsInjected ( ) ;
102
+ console . log (
103
+ `web3Enable: Enabled ${ values . length } extension${ values . length !== 1 ? 's' : '' } : ${ names . join ( ', ' ) } `
104
+ ) ;
105
+
106
+ return values ;
87
107
} )
88
108
)
89
- . catch ( ( ) : InjectedExtension [ ] => [ ] )
90
- . then ( ( values ) : InjectedExtension [ ] => {
91
- const names = values . map ( ( { name, version } ) : string => `${ name } /${ version } ` ) ;
92
-
93
- isWeb3Injected = web3IsInjected ( ) ;
94
- console . log ( `web3Enable: Enabled ${ values . length } extension${ values . length !== 1 ? 's' : '' } : ${ names . join ( ', ' ) } ` ) ;
95
-
96
- return values ;
97
- } )
98
109
) ;
99
110
100
111
return web3EnablePromise ;
101
112
}
102
113
103
114
// retrieve all the accounts accross all providers
104
- export async function web3Accounts ( { ss58Format } : Web3AccountsOptions = { } ) : Promise < InjectedAccountWithMeta [ ] > {
115
+ export async function web3Accounts ( { accountType , ss58Format } : Web3AccountsOptions = { } ) : Promise < InjectedAccountWithMeta [ ] > {
105
116
if ( ! web3EnablePromise ) {
106
117
return throwError ( 'web3Accounts' ) ;
107
118
}
@@ -110,16 +121,18 @@ export async function web3Accounts ({ ss58Format }: Web3AccountsOptions = {}): P
110
121
const injected = await web3EnablePromise ;
111
122
112
123
const retrieved = await Promise . all (
113
- injected . map ( async ( { accounts, name : source } ) : Promise < InjectedAccountWithMeta [ ] > => {
114
- try {
115
- const list = await accounts . get ( ) ;
116
-
117
- return mapAccounts ( source , list , ss58Format ) ;
118
- } catch ( error ) {
119
- // cannot handle this one
120
- return [ ] ;
124
+ injected . map (
125
+ async ( { accounts, name : source } ) : Promise < InjectedAccountWithMeta [ ] > => {
126
+ try {
127
+ const list = await accounts . get ( ) ;
128
+
129
+ return mapAccounts ( source , list . filter ( ( acc ) => acc . type && accountType ? accountType . includes ( acc . type ) : true ) , ss58Format ) ;
130
+ } catch ( error ) {
131
+ // cannot handle this one
132
+ return [ ] ;
133
+ }
121
134
}
122
- } )
135
+ )
123
136
) ;
124
137
125
138
retrieved . forEach ( ( result ) : void => {
@@ -128,35 +141,43 @@ export async function web3Accounts ({ ss58Format }: Web3AccountsOptions = {}): P
128
141
129
142
const addresses = accounts . map ( ( { address } ) : string => address ) ;
130
143
131
- console . log ( `web3Accounts: Found ${ accounts . length } address${ accounts . length !== 1 ? 'es' : '' } : ${ addresses . join ( ', ' ) } ` ) ;
144
+ console . log (
145
+ `web3Accounts: Found ${ accounts . length } address${ accounts . length !== 1 ? 'es' : '' } : ${ addresses . join ( ', ' ) } `
146
+ ) ;
132
147
133
148
return accounts ;
134
149
}
135
150
136
- export async function web3AccountsSubscribe ( cb : ( accounts : InjectedAccountWithMeta [ ] ) => void | Promise < void > , { ss58Format } : Web3AccountsOptions = { } ) : Promise < Unsubcall > {
151
+ export async function web3AccountsSubscribe (
152
+ cb : ( accounts : InjectedAccountWithMeta [ ] ) => void | Promise < void > ,
153
+ { ss58Format } : Web3AccountsOptions = { }
154
+ ) : Promise < Unsubcall > {
137
155
if ( ! web3EnablePromise ) {
138
156
return throwError ( 'web3AccountsSubscribe' ) ;
139
157
}
140
158
141
159
const accounts : Record < string , InjectedAccount [ ] > = { } ;
142
160
143
- const triggerUpdate = ( ) : void | Promise < void > => cb (
144
- Object
145
- . entries ( accounts )
146
- . reduce ( ( result : InjectedAccountWithMeta [ ] , [ source , list ] ) : InjectedAccountWithMeta [ ] => {
147
- result . push ( ...mapAccounts ( source , list , ss58Format ) ) ;
161
+ const triggerUpdate = ( ) : void | Promise < void > =>
162
+ cb (
163
+ Object . entries ( accounts ) . reduce (
164
+ ( result : InjectedAccountWithMeta [ ] , [ source , list ] ) : InjectedAccountWithMeta [ ] => {
165
+ result . push ( ...mapAccounts ( source , list , ss58Format ) ) ;
148
166
149
- return result ;
150
- } , [ ] )
151
- ) ;
167
+ return result ;
168
+ } ,
169
+ [ ]
170
+ )
171
+ ) ;
152
172
153
- const unsubs = ( await web3EnablePromise ) . map ( ( { accounts : { subscribe } , name : source } ) : Unsubcall =>
154
- subscribe ( ( result ) : void => {
155
- accounts [ source ] = result ;
173
+ const unsubs = ( await web3EnablePromise ) . map (
174
+ ( { accounts : { subscribe } , name : source } ) : Unsubcall =>
175
+ subscribe ( ( result ) : void => {
176
+ accounts [ source ] = result ;
156
177
157
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
158
- triggerUpdate ( ) ;
159
- } )
178
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
179
+ triggerUpdate ( ) ;
180
+ } )
160
181
) ;
161
182
162
183
return ( ) : void => {
0 commit comments