Skip to content

Commit 2dad9a3

Browse files
committed
Merge branch 'develop' into feature/hardware-wallets
2 parents ca4c365 + f521451 commit 2dad9a3

File tree

10 files changed

+725
-403
lines changed

10 files changed

+725
-403
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.2.0",
3+
"version": "1.2.3",
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",
@@ -47,7 +47,7 @@
4747
"@portis/web3": "^2.0.0-beta.42",
4848
"@toruslabs/torus-embed": "^0.2.11",
4949
"@walletconnect/web3-provider": "^1.0.0-beta.45",
50-
"authereum": "^0.0.4-beta.26",
50+
"authereum": "^0.0.4-beta.88",
5151
"bignumber.js": "^9.0.0",
5252
"bnc-sdk": "1.0.3",
5353
"bowser": "^2.5.2",

src/interfaces.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ export interface Helpers {
9898
getAddress: (provider: any) => Promise<string | any>
9999
getNetwork: (provider: any) => Promise<number | any>
100100
getBalance: (provider: any) => Promise<string | any>
101-
resetWalletState: (options: { disconnected?: boolean }) => void
101+
resetWalletState: (options?: {
102+
disconnected: boolean
103+
walletName: string
104+
}) => void
102105
}
103106

104107
export interface WalletInterface {
@@ -159,16 +162,23 @@ export interface TorusOptions {
159162
enableLogging?: boolean
160163
}
161164

162-
export interface WalletInitOptions
163-
extends CommonWalletOptions,
164-
SdkWalletOptions,
165-
WalletConnectOptions,
166-
TorusOptions,
167-
TrezorOptions,
168-
LedgerOptions {
165+
export interface AuthereumOptions {
166+
disableNotifications?: boolean
167+
}
168+
169+
interface WalletName {
169170
walletName: string
170171
}
171172

173+
export type WalletInitOptions = CommonWalletOptions &
174+
SdkWalletOptions &
175+
WalletConnectOptions &
176+
TorusOptions &
177+
TrezorOptions &
178+
AuthereumOptions &
179+
LedgerOptions &
180+
WalletName
181+
172182
export interface WalletCheckInit {
173183
checkName: string
174184
minimumBalance?: string

src/modules/select/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ function select(
2424
const { walletName, ...initParams } = wallet
2525
const module = getModule(walletName)
2626

27+
if (!module) {
28+
throw new Error(`${walletName} is not a valid walletName.`)
29+
}
30+
2731
return (
2832
module &&
2933
module.then((m: any) => m.default({ ...initParams, networkId }))

src/modules/select/wallets/authereum.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import authereumIcon from '../wallet-icons/icon-authereum.png'
22
import { networkName } from '../../../utilities'
3-
import { WalletModule, CommonWalletOptions } from '../../../interfaces'
3+
import { WalletModule, AuthereumOptions, CommonWalletOptions } from '../../../interfaces'
44

5-
function authereum(options: CommonWalletOptions): WalletModule {
6-
const { networkId, preferred, label, iconSrc, svg } = options
5+
function authereum(options: AuthereumOptions & CommonWalletOptions): WalletModule {
6+
const { networkId, preferred, label, iconSrc, svg, disableNotifications } = options
77

88
return {
99
name: label || 'Authereum',
@@ -13,7 +13,7 @@ function authereum(options: CommonWalletOptions): WalletModule {
1313
const { default: Authereum } = await import('authereum')
1414
const instance = new Authereum({
1515
networkName: networkName(networkId),
16-
disableNotifications: true
16+
disableNotifications: disableNotifications // default: false
1717
})
1818

1919
const provider = instance.getProvider()
@@ -46,7 +46,7 @@ function authereum(options: CommonWalletOptions): WalletModule {
4646
type: 'sdk',
4747
desktop: true,
4848
mobile: true,
49-
url: 'https://accounts.authereum.org/',
49+
url: `https://${networkId !== 1 ? `${networkName(networkId)}.` : ''}authereum.com/`,
5050
preferred
5151
}
5252
}

src/modules/select/wallets/torus.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ function torus(options: TorusOptions & CommonWalletOptions): WalletModule {
8181
type: 'sdk',
8282
desktop: true,
8383
mobile: true,
84+
url: 'https://app.tor.us/',
8485
preferred
8586
}
8687
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function walletConnect(
3030
provider.autoRefreshOnNetworkChange = false
3131

3232
provider.wc.on('disconnect', () => {
33-
resetWalletState({ disconnected: true })
33+
resetWalletState({ disconnected: true, walletName: 'WalletConnect' })
3434
})
3535

3636
return {

src/stores.ts

Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,19 @@ export const walletInterface: WalletInterfaceStore = createWalletInterfaceStore(
7777
)
7878

7979
walletInterface.subscribe((walletInterface: WalletInterface | null) => {
80-
if (walletInterface) {
81-
const currentState = get(state)
82-
// reset state
83-
currentState.balance && balance.reset()
84-
currentState.address && address.reset()
85-
currentState.network && network.reset()
80+
// clear all current intervals if they exist
81+
currentSyncerIntervals.forEach(
82+
(interval: number | undefined) => interval && clearInterval(interval)
83+
)
84+
85+
const currentState = get(state)
8686

87-
// clear all current intervals if they exist
88-
currentSyncerIntervals.forEach(
89-
(interval: number | undefined) => interval && clearInterval(interval)
90-
)
87+
// reset state
88+
currentState.balance && balance.reset()
89+
currentState.address && address.reset()
90+
currentState.network && network.reset()
9191

92+
if (walletInterface) {
9293
// start syncing state and save intervals
9394
currentSyncerIntervals = [
9495
address.setStateSyncer(walletInterface.address),
@@ -98,33 +99,54 @@ walletInterface.subscribe((walletInterface: WalletInterface | null) => {
9899
}
99100
})
100101

101-
export function resetWalletState(options: { disconnected?: boolean } = {}) {
102-
// clear all current intervals if they exist
103-
currentSyncerIntervals.forEach(
104-
(interval: number | undefined) => interval && clearInterval(interval)
105-
)
106-
102+
export function resetWalletState(options?: {
103+
disconnected: boolean
104+
walletName: string
105+
}) {
107106
walletInterface.update((currentInterface: WalletInterface | null) => {
108-
const { disconnected } = options
109-
!disconnected &&
110-
currentInterface &&
111-
currentInterface.disconnect &&
112-
currentInterface.disconnect()
113-
return null
114-
})
107+
// no interface then don't do anything
108+
if (!currentInterface) {
109+
return currentInterface
110+
}
115111

116-
wallet.update(() => ({
117-
name: undefined,
118-
provider: undefined,
119-
connect: undefined,
120-
instance: undefined,
121-
url: undefined,
122-
loading: undefined
123-
}))
112+
// no options object, so do a full reset by disconnecting and setting interface to null
113+
if (!options) {
114+
wallet.update(() => ({
115+
name: undefined,
116+
provider: undefined,
117+
connect: undefined,
118+
instance: undefined,
119+
url: undefined,
120+
loading: undefined
121+
}))
122+
123+
currentInterface.disconnect && currentInterface.disconnect()
124+
125+
return null
126+
}
124127

125-
balance.reset()
126-
address.reset()
127-
network.reset()
128+
const { walletName, disconnected } = options
129+
130+
// if walletName is the same as the current interface name then do a full reset (checking if to do a disconnect)
131+
if (currentInterface.name === walletName) {
132+
wallet.update(() => ({
133+
name: undefined,
134+
provider: undefined,
135+
connect: undefined,
136+
instance: undefined,
137+
url: undefined,
138+
loading: undefined
139+
}))
140+
141+
disconnected &&
142+
currentInterface.disconnect &&
143+
currentInterface.disconnect()
144+
145+
return null
146+
}
147+
148+
return currentInterface
149+
})
128150

129151
app.update(store => ({
130152
...store,
@@ -157,14 +179,13 @@ function createWalletStateSliceStore(options: {
157179
const { subscribe, set } = writable(initialState)
158180

159181
let currentState: string | number | null | undefined
160-
const unsubscribe = subscribe(store => {
182+
subscribe(store => {
161183
currentState = store
162184
})
163185

164186
return {
165187
subscribe,
166188
reset: () => {
167-
unsubscribe()
168189
set(undefined)
169190
},
170191
setStateSyncer: (stateSyncer: StateSyncer) => {

src/utilities.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export function getNetwork(provider: any): Promise<number | any> {
1313
},
1414
(e: any, res: any) => {
1515
e && reject(e)
16-
resolve(Number(res && res.result))
16+
const result = res && res.result
17+
resolve(result && Number(result))
1718
}
1819
)
1920
})
@@ -29,7 +30,8 @@ export function getAddress(provider: any): Promise<string | any> {
2930
},
3031
(e: any, res: any) => {
3132
e && reject(e)
32-
resolve(res && res.result && res.result[0])
33+
const result = res && res.result && res.result[0]
34+
resolve(result)
3335
}
3436
)
3537
})
@@ -52,7 +54,8 @@ export function getBalance(provider: any): Promise<string | any> {
5254
},
5355
(e: any, res: any) => {
5456
e && reject(e)
55-
resolve(res && res.result && new BigNumber(res.result).toString(10))
57+
const result = res && res.result
58+
resolve(result && new BigNumber(result).toString(10))
5659
}
5760
)
5861
})
@@ -70,7 +73,7 @@ export function createModernProviderInterface(provider: any): WalletInterface {
7073
// get the initial value
7174
getAddress(provider).then(func)
7275
provider.on('accountsChanged', (accounts: string[]) =>
73-
func(accounts[0])
76+
func(accounts && accounts[0])
7477
)
7578
}
7679
}
@@ -82,7 +85,9 @@ export function createModernProviderInterface(provider: any): WalletInterface {
8285
onChange: (func: (val: string | number) => void) => {
8386
// get initial value
8487
getNetwork(provider).then(func)
85-
provider.on('networkChanged', func)
88+
provider.on('networkChanged', (netId: string | number) =>
89+
func(netId && Number(netId))
90+
)
8691
}
8792
}
8893
: { get: () => getNetwork(provider) },

src/validation.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ export function validateWalletInit(
548548
enableLogging,
549549
loginMethod,
550550
showTorusButton,
551+
disableNotifications,
551552
...otherParams
552553
} = walletInit
553554

@@ -686,4 +687,11 @@ export function validateWalletInit(
686687
type: 'string',
687688
optional: true
688689
})
690+
691+
validateType({
692+
name: 'walletInit.disableNotifications',
693+
value: disableNotifications,
694+
type: 'boolean',
695+
optional: true
696+
})
689697
}

0 commit comments

Comments
 (0)