Skip to content

Commit 0707d4c

Browse files
authored
Merge pull request #179 from blocknative/develop
Release 1.2.0 - Add `walletReset` function - Ensure WalletConnect disconnects
2 parents 7d5652b + 72b75b0 commit 0707d4c

File tree

12 files changed

+141
-114
lines changed

12 files changed

+141
-114
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bnc-onboard",
3-
"version": "1.1.11",
3+
"version": "1.2.0",
44
"description": "Onboard users to web3 by allowing them to select a wallet, get that wallet ready to transact and have access to synced wallet state.",
55
"keywords": [
66
"ethereum",
@@ -41,7 +41,7 @@
4141
},
4242
"dependencies": {
4343
"@portis/web3": "^2.0.0-beta.42",
44-
"@walletconnect/web3-provider": "^1.0.0-beta.36",
44+
"@walletconnect/web3-provider": "^1.0.0-beta.45",
4545
"authereum": "^0.0.4-beta.26",
4646
"bignumber.js": "^9.0.0",
4747
"bnc-sdk": "1.0.3",

src/components/Wallets.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
overflow-y: scroll;
2323
padding: 0;
2424
scrollbar-width: none;
25-
font-family: 'Helvetica Neue';
25+
font-family: inherit;
2626
font-size: inherit;
2727
line-height: 1.15;
2828
box-sizing: border-box;

src/elements/Button.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
padding: 0.55em 1.4em;
1414
cursor: pointer;
1515
color: #4a90e2;
16-
font-family: "Helvetica Neue";
16+
font-family: inherit;
1717
transition: background 150ms ease-in-out;
1818
line-height: 1.15;
1919
}

src/elements/IconButton.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
cursor: pointer;
2626
color: inherit;
2727
line-height: 1.15;
28-
font-family: 'Helvetica Neue';
28+
font-family: inherit;
2929
}
3030
3131
button:hover {

src/interfaces.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export interface Helpers {
9494
getAddress: (provider: any) => Promise<string | any>
9595
getNetwork: (provider: any) => Promise<number | any>
9696
getBalance: (provider: any) => Promise<string | any>
97+
resetWalletState: (options: { disconnected?: boolean }) => void
9798
}
9899

99100
export interface WalletInterface {
@@ -108,7 +109,7 @@ export interface WalletInterface {
108109

109110
export interface StateSyncer {
110111
get?: () => Promise<string | number | null>
111-
onChange?: (updater: (val: number | string) => void) => void
112+
onChange?: (updater: (val: number | string | undefined) => void) => void
112113
}
113114

114115
export interface Wallet {
@@ -176,6 +177,7 @@ export interface ConfigOptions {
176177
export interface API {
177178
walletSelect: WalletSelectFunction
178179
walletCheck: WalletCheck
180+
walletReset: () => void
179181
config: Config
180182
getState: GetState
181183
}
@@ -189,7 +191,7 @@ export interface WritableStore {
189191
export interface WalletInterfaceStore {
190192
subscribe: (subscriber: (store: any) => void) => () => void
191193
update: (
192-
updater: (walletInterface: WalletInterface | null) => WalletInterface
194+
updater: (walletInterface: WalletInterface | null) => WalletInterface | null
193195
) => void
194196
set: (walletInterface: WalletInterface) => void | never
195197
}

src/modules/select/content.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,33 @@ export const extensionInstallMessage = (helpers: {
66

77
if (currentWallet) {
88
return `
9-
<p style="font-size: 0.889rem; font-family: Helvetica Neue; margin: 0.889rem 0;">
9+
<p style="font-size: 0.889rem; font-family: inherit; margin: 0.889rem 0;">
1010
We have detected that you already have
1111
<b>${currentWallet}</b>
1212
installed. If you would prefer to use
1313
<b>${selectedWallet}</b>
1414
instead, then click below to install.
1515
</p>
16-
<p style="font-size: 0.889rem; font-family: Helvetica Neue; margin: 0.889rem 0;">
16+
<p style="font-size: 0.889rem; font-family: inherit; margin: 0.889rem 0;">
1717
<b>Tip:</b>
1818
If you already have ${selectedWallet} installed, check your
1919
browser extension settings to make sure that you have it enabled
2020
and that you have disabled any other browser extension wallets.
2121
<span
2222
class="bn-onboard-clickable"
23-
style="color: #4a90e2; font-size: 0.889rem; font-family: Helvetica Neue;"
23+
style="color: #4a90e2; font-size: 0.889rem; font-family: inherit;"
2424
onclick="window.location.reload();">
2525
Then refresh the page.
2626
</span>
2727
</p>
2828
`
2929
} else {
3030
return `
31-
<p style="font-size: 0.889rem; font-family: Helvetica Neue; margin: 0.889rem 0;">
31+
<p style="font-size: 0.889rem; font-family: inherit; margin: 0.889rem 0;">
3232
You'll need to install <b>${selectedWallet}</b> to continue. Once you have it installed, go ahead and
3333
<span
3434
class="bn-clickable"
35-
style="color: #4a90e2; font-size: 0.889rem; font-family: Helvetica Neue;"
35+
style="color: #4a90e2; font-size: 0.889rem; font-family: inherit;"
3636
onclick={window.location.reload();}>
3737
refresh the page.
3838
</span>

src/modules/select/wallets/wallet-connect.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { WalletConnectOptions, WalletModule } from '../../../interfaces'
1+
import {
2+
WalletConnectOptions,
3+
WalletModule,
4+
Helpers
5+
} from '../../../interfaces'
26

37
import walletConnectIcon from '../wallet-icons/icon-wallet-connect'
48

@@ -9,7 +13,9 @@ function walletConnect(options: WalletConnectOptions): WalletModule {
913
name: label || 'WalletConnect',
1014
svg: svg || walletConnectIcon,
1115
iconSrc,
12-
wallet: async () => {
16+
wallet: async (helpers: Helpers) => {
17+
const { resetWalletState } = helpers
18+
1319
const { default: WalletConnectProvider } = await import(
1420
'@walletconnect/web3-provider'
1521
)
@@ -20,6 +26,10 @@ function walletConnect(options: WalletConnectOptions): WalletModule {
2026

2127
provider.autoRefreshOnNetworkChange = false
2228

29+
provider.wc.on('disconnect', () => {
30+
resetWalletState({ disconnected: true })
31+
})
32+
2333
return {
2434
provider,
2535
interface: {
@@ -40,7 +50,7 @@ function walletConnect(options: WalletConnectOptions): WalletModule {
4050
onChange: func => {
4151
provider
4252
.send('eth_accounts')
43-
.then((accounts: string[]) => func(accounts[0]))
53+
.then((accounts: string[]) => accounts[0] && func(accounts[0]))
4454
provider.on('accountsChanged', (accounts: string[]) =>
4555
func(accounts[0])
4656
)
@@ -65,6 +75,10 @@ function walletConnect(options: WalletConnectOptions): WalletModule {
6575
'latest'
6676
])
6777
})
78+
},
79+
disconnect: () => {
80+
provider.wc.killSession()
81+
provider.stop()
6882
}
6983
}
7084
}

src/onboard.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import {
1111
balance,
1212
wallet,
1313
state,
14-
walletInterface
14+
walletInterface,
15+
resetWalletState
1516
} from './stores'
1617

1718
import { getDeviceInfo } from './utilities'
@@ -84,7 +85,9 @@ function init(initialization: Initialization): API {
8485

8586
if (subscriptions.network) {
8687
network.subscribe((networkId: number | null) => {
87-
networkId && subscriptions.network(networkId)
88+
if (networkId !== null) {
89+
subscriptions.network(networkId)
90+
}
8891
})
8992
}
9093

@@ -98,7 +101,7 @@ function init(initialization: Initialization): API {
98101

99102
if (subscriptions.wallet) {
100103
wallet.subscribe((wallet: Wallet) => {
101-
wallet.provider && subscriptions.wallet(wallet)
104+
wallet.provider !== null && subscriptions.wallet(wallet)
102105
})
103106
}
104107
}
@@ -144,6 +147,10 @@ function init(initialization: Initialization): API {
144147
})
145148
}
146149

150+
function walletReset(): void {
151+
resetWalletState()
152+
}
153+
147154
function config(options: ConfigOptions): void {
148155
validateConfig(options)
149156
app.update((store: AppState) => ({ ...store, ...options }))
@@ -153,7 +160,7 @@ function init(initialization: Initialization): API {
153160
return get(state)
154161
}
155162

156-
return { walletSelect, walletCheck, config, getState }
163+
return { walletSelect, walletCheck, walletReset, config, getState }
157164
}
158165

159166
export default init

src/stores.ts

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,11 @@ export const walletInterface: WalletInterfaceStore = createWalletInterfaceStore(
7676

7777
walletInterface.subscribe((walletInterface: WalletInterface | null) => {
7878
if (walletInterface) {
79+
const currentState = get(state)
7980
// reset state
80-
balance.reset()
81-
address.reset()
82-
network.reset()
81+
currentState.balance && balance.reset()
82+
currentState.address && address.reset()
83+
currentState.network && network.reset()
8384

8485
// clear all current intervals if they exist
8586
currentSyncerIntervals.forEach(
@@ -95,6 +96,42 @@ walletInterface.subscribe((walletInterface: WalletInterface | null) => {
9596
}
9697
})
9798

99+
export function resetWalletState(options: { disconnected?: boolean } = {}) {
100+
// clear all current intervals if they exist
101+
currentSyncerIntervals.forEach(
102+
(interval: number | undefined) => interval && clearInterval(interval)
103+
)
104+
105+
walletInterface.update((currentInterface: WalletInterface | null) => {
106+
const { disconnected } = options
107+
!disconnected &&
108+
currentInterface &&
109+
currentInterface.disconnect &&
110+
currentInterface.disconnect()
111+
return null
112+
})
113+
114+
wallet.update(() => ({
115+
name: undefined,
116+
provider: undefined,
117+
connect: undefined,
118+
instance: undefined,
119+
url: undefined,
120+
loading: undefined
121+
}))
122+
123+
balance.reset()
124+
address.reset()
125+
network.reset()
126+
127+
app.update(store => ({
128+
...store,
129+
walletSelectInProgress: false,
130+
walletSelectCompleted: false,
131+
autoSelect: false
132+
}))
133+
}
134+
98135
function createWalletInterfaceStore(
99136
initialState: null | WalletInterface
100137
): WalletInterfaceStore {
@@ -164,7 +201,7 @@ function createWalletStateSliceStore(options: {
164201
function createBalanceStore(initialState: string | null): BalanceStore {
165202
let stateSyncer: StateSyncer
166203
let emitter: any
167-
let emitterAddress: String
204+
let emitterAddress: String | undefined
168205
let cancel: () => void = () => {}
169206

170207
const { subscribe } = derived(
@@ -209,8 +246,11 @@ function createBalanceStore(initialState: string | null): BalanceStore {
209246
} else if (emitterAddress && !$address) {
210247
// no address, so set balance to undefined
211248
set && set(undefined)
249+
emitterAddress = undefined
212250
}
213251
}
252+
253+
set(initialState)
214254
}
215255
)
216256

src/views/WalletCheck.svelte

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import BigNumber from 'bignumber.js'
66
77
import { getBlocknative } from '../services'
8-
import { app, state, balanceSyncStatus } from '../stores'
8+
import { app, state, balanceSyncStatus, walletInterface } from '../stores'
99
import { validateModal, validateWalletCheckModule } from '../validation'
1010
import { isPromise } from '../utilities'
1111
@@ -35,9 +35,18 @@
3535
let actionResolved: boolean | undefined = undefined
3636
let loading: boolean = false
3737
let loadingModal: boolean = false
38+
let stopChecking: boolean = false
39+
40+
const unsubscribe = walletInterface.subscribe(currentInterface => {
41+
if (currentInterface === null) {
42+
stopChecking = true
43+
handleExit()
44+
unsubscribe()
45+
}
46+
})
3847
3948
// recheck modules if below conditions
40-
$: if (!activeModal && !checkingModule) {
49+
$: if (!activeModal && !checkingModule && !stopChecking) {
4150
renderModule()
4251
}
4352
@@ -93,7 +102,7 @@
93102
94103
// poll to automatically to check if condition has been met
95104
pollingInterval = setInterval(async () => {
96-
if (currentModule) {
105+
if (currentModule && !stopChecking) {
97106
const invalid = await invalidState(currentModule, get(state))
98107
if (!invalid && actionResolved !== false) {
99108
resetState()
@@ -216,7 +225,7 @@
216225
/* .bn-onboard-prepare-description */
217226
p {
218227
font-size: 0.889em;
219-
font-family: 'Helvetica Neue';
228+
font-family: inherit;
220229
margin: 1em 0;
221230
}
222231

src/views/WalletSelect.svelte

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { get } from 'svelte/store'
44
import { fade } from 'svelte/transition'
55
6-
import { app, walletInterface, wallet } from '../stores'
6+
import { app, walletInterface, wallet, resetWalletState } from '../stores'
77
88
import Modal from '../components/Modal.svelte'
99
import ModalHeader from '../components/ModalHeader.svelte'
@@ -116,7 +116,8 @@
116116
BigNumber,
117117
getNetwork,
118118
getAddress,
119-
getBalance
119+
getBalance,
120+
resetWalletState
120121
})
121122
122123
loadingWallet = undefined
@@ -174,7 +175,7 @@
174175
p {
175176
font-size: 0.889em;
176177
margin: 1.6em 0 0 0;
177-
font-family: 'Helvetica Neue';
178+
font-family: inherit;
178179
}
179180
180181
/* .bn-onboard-select-info-container */

0 commit comments

Comments
 (0)