-
Notifications
You must be signed in to change notification settings - Fork 112
/
main.ts
154 lines (141 loc) · 4.36 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import discovery, { type WalletProvider } from "./discovery"
import { LocalStorageWrapper } from "./localStorageStore"
import type { GetStarknetOptions, GetStarknetResult } from "./types"
import { pipe, ssrSafeWindow } from "./utils"
import { filterBy, filterByAuthorized } from "./wallet/filter"
import {
isFullWallet,
isVirtualWallet,
isWalletObject,
} from "./wallet/isWalletObject"
import { scanObjectForWallets } from "./wallet/scan"
import { sortBy } from "./wallet/sort"
import {
initiateVirtualWallets,
resolveVirtualWallet,
} from "./wallet/virtualWallets"
import { Permission, type StarknetWindowObject } from "@starknet-io/types-js"
export type {
StarknetWindowObject,
AddDeclareTransactionParameters,
AddDeclareTransactionResult,
AddInvokeTransactionParameters,
AddInvokeTransactionResult,
AddStarknetChainParameters,
RequestAccountsParameters,
SwitchStarknetChainParameters,
AccountDeploymentData,
WatchAssetParameters,
TypedData,
RequestFn,
RpcMessage,
IsParamsOptional,
RpcTypeToMessageMap,
RequestFnCall,
AccountChangeEventHandler,
NetworkChangeEventHandler,
WalletEventHandlers,
WalletEvents,
} from "@starknet-io/types-js"
export { scanObjectForWallets } from "./wallet/scan"
export { isWalletObject } from "./wallet/isWalletObject"
export type {
BrowserStoreVersion,
DisconnectOptions,
GetStarknetOptions,
GetStarknetResult,
GetWalletOptions,
OperatingSystemStoreVersion,
WalletProvider,
} from "./types"
export { ssrSafeWindow } from "./utils"
const defaultOptions: GetStarknetOptions = {
windowObject: ssrSafeWindow ?? {},
isWalletObject,
storageFactoryImplementation: (name: string) => new LocalStorageWrapper(name),
}
export function getStarknet(
options: Partial<GetStarknetOptions> = {},
): GetStarknetResult {
const { storageFactoryImplementation, windowObject, isWalletObject } = {
...defaultOptions,
...options,
}
const lastConnectedStore = storageFactoryImplementation("gsw-last")
initiateVirtualWallets(windowObject)
return {
getAvailableWallets: async (options = {}) => {
const availableWallets = scanObjectForWallets(
windowObject,
isWalletObject,
)
return pipe<StarknetWindowObject[]>(
(_) => filterBy(_, options),
(_) => sortBy(_, options.sort),
)(availableWallets)
},
getAuthorizedWallets: async (options = {}) => {
const availableWallets = scanObjectForWallets(
windowObject,
isWalletObject,
)
return pipe<StarknetWindowObject[]>(
(_) => filterByAuthorized(_),
(_) => filterBy(_, options),
(_) => sortBy(_, options.sort),
)(availableWallets)
},
getDiscoveryWallets: async (options = {}) => {
return pipe<WalletProvider[]>(
(_) => filterBy(_, options),
(_) => sortBy(_, options.sort),
)(discovery)
},
getLastConnectedWallet: async () => {
const lastConnectedWalletId = lastConnectedStore.get()
const allWallets = scanObjectForWallets(windowObject, isWalletObject)
const lastConnectedWallet = allWallets.find(
(w) => w.id === lastConnectedWalletId,
)
const [firstAuthorizedWallet] = await filterByAuthorized(
lastConnectedWallet ? [lastConnectedWallet] : [],
)
if (!firstAuthorizedWallet) {
lastConnectedStore.delete()
return null
}
return firstAuthorizedWallet
},
enable: async (inputWallet, options) => {
let wallet: StarknetWindowObject
if (isVirtualWallet(inputWallet)) {
wallet = await resolveVirtualWallet(windowObject, inputWallet)
} else if (isFullWallet(inputWallet)) {
wallet = inputWallet
} else {
throw new Error("Invalid wallet object")
}
await wallet.request({
type: "wallet_requestAccounts",
params: {
silent_mode: options?.silent_mode,
},
})
// check for permissions
const permissions: Permission[] = await wallet.request({
type: "wallet_getPermissions",
})
if (!permissions?.includes(Permission.ACCOUNTS)) {
throw new Error("Failed to connect to wallet")
}
lastConnectedStore.set(wallet.id)
return wallet
},
disconnect: async ({ clearLastWallet } = {}) => {
if (clearLastWallet) {
lastConnectedStore.delete()
}
},
}
}
export default getStarknet()