Skip to content

Commit 59e3a20

Browse files
authored
Merge pull request #72 from blocknative/feature/preferred-wallet-default
Add preferred wallet to wallet default and fix interface. Closes #71
2 parents e588ef1 + 4fee6e8 commit 59e3a20

File tree

3 files changed

+51
-22
lines changed

3 files changed

+51
-22
lines changed

src/interfaces.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,14 @@ export interface WalletConnectOptions {
138138
}
139139

140140
export interface SelectDefaultsOptions {
141-
heading: string
142-
description: string
141+
heading?: string
142+
description?: string
143143
networkId: number
144-
fortmaticInit: { apiKey: string }
145-
portisInit: { apiKey: string }
146-
squarelinkInit: { apiKey: string }
147-
walletConnectInit: WalletConnectOptions
144+
fortmaticInit?: { apiKey: string }
145+
portisInit?: { apiKey: string }
146+
squarelinkInit?: { apiKey: string }
147+
walletConnectInit?: WalletConnectOptions
148+
preferredWallets?: string[]
148149
}
149150

150151
export interface WalletSelectFunction {

src/modules/select/index.ts

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,47 +23,67 @@ function defaults(options: SelectDefaultsOptions): WalletSelectModule {
2323
fortmaticInit,
2424
portisInit,
2525
squarelinkInit,
26-
walletConnectInit
26+
walletConnectInit,
27+
preferredWallets
2728
} = options
2829

29-
const desktopModules = [metamask(), dapper(), opera()]
30-
const mobileModules = [coinbase(), trust(), operaTouch()]
30+
let desktopWallets = [metamask(), dapper(), opera()]
31+
let mobileWallets = [coinbase(), trust(), operaTouch()]
3132

3233
if (portisInit) {
33-
desktopModules.push(portis({ ...portisInit, networkId }))
34-
mobileModules.push(portis({ ...portisInit, networkId }))
34+
desktopWallets.push(portis({ ...portisInit, networkId }))
35+
mobileWallets.push(portis({ ...portisInit, networkId }))
3536
}
3637

3738
if (fortmaticInit) {
38-
desktopModules.push(fortmatic({ ...fortmaticInit, networkId }))
39-
mobileModules.push(fortmatic({ ...fortmaticInit, networkId }))
39+
desktopWallets.push(fortmatic({ ...fortmaticInit, networkId }))
40+
mobileWallets.push(fortmatic({ ...fortmaticInit, networkId }))
4041
}
4142

4243
if (walletConnectInit) {
43-
desktopModules.push(
44+
desktopWallets.push(
4445
walletConnect({ infuraKey: walletConnectInit.infuraKey })
4546
)
46-
mobileModules.push(
47+
mobileWallets.push(
4748
walletConnect({ infuraKey: walletConnectInit.infuraKey })
4849
)
4950
}
5051

5152
if (squarelinkInit) {
52-
desktopModules.push(squarelink({ ...squarelinkInit, networkId }))
53-
mobileModules.push(squarelink({ ...squarelinkInit, networkId }))
53+
desktopWallets.push(squarelink({ ...squarelinkInit, networkId }))
54+
mobileWallets.push(squarelink({ ...squarelinkInit, networkId }))
5455
}
5556

56-
desktopModules.push(authereum({ networkId }))
57-
mobileModules.push(authereum({ networkId }))
57+
desktopWallets.push(authereum({ networkId }))
58+
mobileWallets.push(authereum({ networkId }))
59+
60+
//set preferred wallets if provided
61+
if (preferredWallets) {
62+
desktopWallets = desktopWallets.map(wallet => {
63+
if (preferredWallets.includes(wallet.name)) {
64+
wallet.preferred = true
65+
}
66+
67+
return wallet
68+
})
69+
70+
mobileWallets = mobileWallets.map(wallet => {
71+
if (preferredWallets.includes(wallet.name)) {
72+
wallet.preferred = true
73+
}
74+
75+
return wallet
76+
})
77+
}
5878

5979
return {
6080
heading: heading || "Select a Wallet",
6181
description:
6282
description ||
6383
"Please select the wallet that you would like to use with this dapp:",
6484
wallets: {
65-
mobile: mobileModules,
66-
desktop: desktopModules
85+
mobile: mobileWallets,
86+
desktop: desktopWallets
6787
}
6888
}
6989
}

src/validation.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,8 @@ export function validateSelectDefaultsOptions(
347347
fortmaticInit,
348348
portisInit,
349349
squarelinkInit,
350-
walletConnectInit
350+
walletConnectInit,
351+
preferredWallets
351352
} = options
352353

353354
validateType({
@@ -400,4 +401,11 @@ export function validateSelectDefaultsOptions(
400401
type: "object",
401402
optional: true
402403
})
404+
405+
validateType({
406+
name: "preferredWallets",
407+
value: preferredWallets,
408+
type: "array",
409+
optional: true
410+
})
403411
}

0 commit comments

Comments
 (0)