1
+ import {
2
+ InjectedExtension ,
3
+ InjectedWindow ,
4
+ } from '@polkadot/extension-inject/types'
5
+
1
6
/**
2
7
* Substrate Wallet Type(s)
3
8
*/
@@ -41,7 +46,7 @@ export const polkadotjs: SubstrateWallet = {
41
46
}
42
47
43
48
export const subwallet : SubstrateWallet = {
44
- id : 'subwallet' ,
49
+ id : 'subwallet-js ' ,
45
50
name : 'SubWallet' ,
46
51
platforms : [ SubstrateWalletPlatform . Browser ] ,
47
52
urls : {
@@ -95,9 +100,9 @@ export const nova: SubstrateWallet = {
95
100
* Exporting all wallets separately
96
101
*/
97
102
export const allSubstrateWallets : SubstrateWallet [ ] = [
98
- polkadotjs ,
99
103
subwallet ,
100
104
talisman ,
105
+ polkadotjs ,
101
106
nova ,
102
107
]
103
108
@@ -109,3 +114,42 @@ export const getSubstrateWallet = (id: string): SubstrateWallet | undefined => {
109
114
( wallet ) => wallet . id . toLowerCase ( ) === id . toLowerCase ( ) ,
110
115
)
111
116
}
117
+
118
+ /*
119
+ * Returns `true` if wallet is installed, `false` if not, and
120
+ * `undefined` if the environment is not a client browser.
121
+ */
122
+ export const isWalletInstalled = ( wallet : SubstrateWallet ) => {
123
+ try {
124
+ if ( typeof window === 'undefined' ) return undefined
125
+ const injectedWindow = window as Window & InjectedWindow
126
+ const injectedExtension = injectedWindow ?. injectedWeb3 ?. [ wallet . id ]
127
+ return ! ! injectedExtension
128
+ } catch ( e ) {
129
+ return undefined
130
+ }
131
+ }
132
+
133
+ /**
134
+ * Enables the given wallet (if existent) and returns the injected extension.
135
+ */
136
+ export const enableWallet = async (
137
+ wallet : SubstrateWallet ,
138
+ appName : string ,
139
+ ) => {
140
+ if ( ! isWalletInstalled ( wallet ) ) return undefined
141
+
142
+ try {
143
+ if ( typeof window === 'undefined' ) return undefined
144
+ const injectedWindow = window as Window & InjectedWindow
145
+ const injectedWindowProvider = injectedWindow ?. injectedWeb3 ?. [ wallet . id ]
146
+ const injectedExtension : InjectedExtension = {
147
+ ...( await injectedWindowProvider ?. enable ( appName ) ) ,
148
+ name : wallet . id ,
149
+ version : injectedWindowProvider . version ,
150
+ }
151
+ return injectedExtension
152
+ } catch ( e ) {
153
+ return undefined
154
+ }
155
+ }
0 commit comments