Skip to content

Commit 0673343

Browse files
committed
feat: ChainInfo delegates Networks Service to get current chain
1 parent a078e76 commit 0673343

10 files changed

Lines changed: 69 additions & 74 deletions

File tree

packages/neuron-wallet/src/controllers/app/menu.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { UpdateController } from 'controllers'
66
import { showWindow } from './show-window'
77
import NetworksService from 'services/networks'
88
import WalletsService from 'services/wallets'
9-
import ChainInfo from 'models/chain-info'
9+
import ChainInfo from 'services/chain-info'
1010
import CommandSubject from 'models/subjects/command'
1111

1212
enum URL {
@@ -295,10 +295,8 @@ const contextMenuTemplate: {
295295
},
296296
networkList: async (id: string) => {
297297
const networksService = NetworksService.getInstance()
298-
const [network, currentNetworkID] = await Promise.all([
299-
networksService.get(id).catch(() => null),
300-
networksService.getCurrentID().catch(() => null),
301-
])
298+
const network = networksService.get(id)
299+
const currentNetworkID = networksService.getCurrentID()
302300

303301
if (!network) {
304302
showMessageBox({
@@ -401,7 +399,11 @@ const contextMenuTemplate: {
401399
return []
402400
}
403401

404-
const address = bech32Address(identifier)
402+
const address = bech32Address(identifier, {
403+
prefix: ChainInfo.getInstance().isMainnet() ? AddressPrefix.Mainnet : AddressPrefix.Testnet,
404+
type: AddressType.HashIdx,
405+
codeHashOrCodeHashIndex: '0x00',
406+
})
405407
return [
406408
{
407409
label: i18n.t('contextMenu.copy-address'),

packages/neuron-wallet/src/controllers/wallets.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { dialog, SaveDialogReturnValue, BrowserWindow } from 'electron'
44
import WalletsService, { Wallet, WalletProperties, FileKeystoreWallet } from 'services/wallets'
55
import Keystore from 'models/keys/keystore'
66
import Keychain from 'models/keys/keychain'
7-
import ChainInfo from 'models/chain-info'
7+
import ChainInfo from 'services/chain-info'
88
import { validateMnemonic, mnemonicToSeedSync } from 'models/keys/mnemonic'
99
import { AccountExtendedPublicKey, ExtendedPrivateKey } from 'models/keys/key'
1010
import { ResponseCode } from 'utils/const'

packages/neuron-wallet/src/models/lock-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { OutPoint, Script, ScriptHashType } from 'types/cell-types'
1010
import ConvertTo from 'types/convert-to'
1111
import { SystemScriptSubject } from 'models/subjects/system-script'
1212
import Core from '@nervosnetwork/ckb-sdk-core'
13-
import ChainInfo from './chain-info'
13+
import ChainInfo from '../services/chain-info'
1414

1515
export interface SystemScript {
1616
codeHash: string

packages/neuron-wallet/src/services/addresses.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import LockUtils from 'models/lock-utils'
55
import AddressDao, { Address as AddressInterface, AddressVersion } from 'database/address/address-dao'
66
import AddressCreatedSubject from 'models/subjects/address-created-subject'
77
import NodeService from './node'
8-
import ChainInfo from 'models/chain-info'
8+
import ChainInfo from 'services/chain-info'
99

1010
const MAX_ADDRESS_COUNT = 30
1111

packages/neuron-wallet/src/models/chain-info.ts renamed to packages/neuron-wallet/src/services/chain-info.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import NetworksService from './networks'
2+
13
export default class ChainInfo {
24
private static instance: ChainInfo
35

@@ -9,18 +11,15 @@ export default class ChainInfo {
911
return ChainInfo.instance
1012
}
1113

12-
private chain: string = ''
13-
14-
public setChain = (chain: string) => {
15-
this.chain = chain
14+
public setChain = (_chain: string) => {
1615
}
1716

1817
public getChain = (): string => {
19-
return this.chain
18+
return NetworksService.getInstance().getCurrent().chain
2019
}
2120

2221
public isMainnet = (): boolean => {
23-
return this.chain === 'ckb'
22+
return this.getChain() === 'ckb'
2423
}
2524

2625
public explorerUrl = (): string => {

packages/neuron-wallet/src/services/networks.ts

Lines changed: 49 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -40,61 +40,52 @@ export default class NetworksService extends Store {
4040
constructor() {
4141
super('networks', 'index.json', JSON.stringify(presetNetworks))
4242

43-
this.getAll().then(currentNetworkList => {
44-
if (currentNetworkList) {
45-
NetworkListSubject.next({
46-
currentNetworkList,
47-
})
48-
Promise.all(currentNetworkList.map(n => {
49-
if (n.type == NetworkType.Default) {
50-
return n
51-
} else {
52-
const core = new Core(n.remote)
53-
return Promise.all([
54-
core.rpc.getBlockchainInfo(),
55-
core.rpc.getBlockHash('0x0')
56-
]).then(([info, genesisHash]) => ({
57-
...n,
58-
chain: info.chain,
59-
genesisHash
60-
}))
61-
}
62-
})).then(networkList => {
63-
this.updateAll(networkList)
64-
}).catch((err: Error) => {
65-
logger.error(err)
66-
})
43+
const currentNetworkList = this.getAll()
44+
NetworkListSubject.next({ currentNetworkList })
45+
46+
Promise.all(currentNetworkList.map(n => {
47+
if (n.type == NetworkType.Default) {
48+
return n
49+
} else {
50+
const core = new Core(n.remote)
51+
return Promise.all([
52+
core.rpc.getBlockchainInfo(),
53+
core.rpc.getBlockHash('0x0')
54+
]).then(([info, genesisHash]) => ({
55+
...n,
56+
chain: info.chain,
57+
genesisHash
58+
}))
6759
}
60+
})).then(networkList => {
61+
this.updateAll(networkList)
62+
}).catch((err: Error) => {
63+
logger.error(err)
6864
})
6965

70-
this.getCurrentID().then(currentNetworkID => {
71-
if (currentNetworkID) {
72-
CurrentNetworkIDSubject.next({ currentNetworkID })
73-
this.get(currentNetworkID).then(network => {
74-
if (network) {
75-
networkSwitchSubject.next(network)
76-
}
77-
})
78-
}
79-
})
66+
const currentNetwork = this.getCurrent()
67+
if (currentNetwork) {
68+
CurrentNetworkIDSubject.next({ currentNetworkID: currentNetwork.id })
69+
networkSwitchSubject.next(currentNetwork)
70+
}
8071

8172
this.on(NetworksKey.List, async (_, currentNetworkList: NetworkWithID[] = []) => {
8273
NetworkListSubject.next({ currentNetworkList })
8374

84-
const currentID = await this.getCurrentID()
75+
const currentID = this.getCurrentID()
8576
if (currentNetworkList.find(network => network.id === currentID)) {
8677
return
8778
}
8879

89-
const defaultNetwork = await this.defaultOne()
80+
const defaultNetwork = this.defaultOne()
9081
if (!defaultNetwork) {
9182
throw new LackOfDefaultNetwork()
9283
}
9384
this.activate(defaultNetwork.id)
9485
})
9586

9687
this.on(NetworksKey.Current, async (_, currentNetworkID: NetworkID) => {
97-
const currentNetwork = await this.get(currentNetworkID)
88+
const currentNetwork = this.get(currentNetworkID)
9889
if (!currentNetwork) {
9990
throw new NetworkNotFound(currentNetworkID)
10091
}
@@ -103,19 +94,22 @@ export default class NetworksService extends Store {
10394
})
10495
}
10596

106-
public getAll = async () => {
107-
const list = await this.read<NetworkWithID[]>(NetworksKey.List)
97+
public getAll = () => {
98+
const list = this.readSync<NetworkWithID[]>(NetworksKey.List)
10899
return list || presetNetworks.networks
109100
}
110101

111-
@Validate
112-
public async get(@Required id: NetworkID) {
113-
const list = await this.getAll()
102+
public getCurrent(): NetworkWithID {
103+
const currentID = this.getCurrentID()
104+
return this.get(currentID!)! // Should always have at least one network
105+
}
106+
107+
public get(@Required id: NetworkID) {
108+
const list = this.getAll()
114109
return list.find(item => item.id === id) || null
115110
}
116111

117-
@Validate
118-
public async updateAll(@Required networks: NetworkWithID[]) {
112+
public updateAll(@Required networks: NetworkWithID[]) {
119113
if (!Array.isArray(networks)) {
120114
throw new InvalidFormat('Networks')
121115
}
@@ -124,7 +118,7 @@ export default class NetworksService extends Store {
124118

125119
@Validate
126120
public async create(@Required name: NetworkName, @Required remote: NetworkRemote, type: NetworkType = NetworkType.Normal) {
127-
const list = await this.getAll()
121+
const list = this.getAll()
128122
if (list.some(item => item.name === name)) {
129123
throw new UsedName('Network')
130124
}
@@ -148,13 +142,13 @@ export default class NetworksService extends Store {
148142
chain,
149143
}
150144

151-
await this.updateAll([...list, newOne])
145+
this.updateAll([...list, newOne])
152146
return newOne
153147
}
154148

155149
@Validate
156150
public async update(@Required id: NetworkID, @Required options: Partial<Network>) {
157-
const list = await this.getAll()
151+
const list = this.getAll()
158152
const network = list.find(item => item.id === id)
159153
if (!network) {
160154
throw new NetworkNotFound(id)
@@ -177,30 +171,30 @@ export default class NetworksService extends Store {
177171
}
178172

179173
this.updateAll(list)
180-
const currentID = await this.getCurrentID()
174+
const currentID = this.getCurrentID()
181175
if (currentID === id) {
182176
await this.activate(id)
183177
}
184178
}
185179

186180
@Validate
187181
public async delete(@Required id: NetworkID) {
188-
const networkToDelete = await this.get(id)
182+
const networkToDelete = this.get(id)
189183
if (!networkToDelete) {
190184
throw new NetworkNotFound(id)
191185
}
192186
if (networkToDelete.type === NetworkType.Default) {
193187
throw new DefaultNetworkUnremovable()
194188
}
195189

196-
const prevNetworkList = await this.getAll()
190+
const prevNetworkList = this.getAll()
197191
const currentNetworkList = prevNetworkList.filter(item => item.id !== id)
198192
this.updateAll(currentNetworkList)
199193
}
200194

201195
@Validate
202196
public async activate(@Required id: NetworkID) {
203-
const network = await this.get(id)
197+
const network = this.get(id)
204198
if (!network) {
205199
throw new NetworkNotFound(id)
206200
}
@@ -227,12 +221,12 @@ export default class NetworksService extends Store {
227221
}
228222
}
229223

230-
public getCurrentID = async () => {
231-
return (await this.read<string>(NetworksKey.Current)) || null
224+
public getCurrentID = () => {
225+
return this.readSync<string>(NetworksKey.Current) || null
232226
}
233227

234-
public defaultOne = async () => {
235-
const list = await this.getAll()
228+
public defaultOne = () => {
229+
const list = this.getAll()
236230
return list.find(item => item.type === NetworkType.Default) || null
237231
}
238232
}

packages/neuron-wallet/src/services/wallets.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import FileService from './file'
2121
import { TransactionsService, TransactionPersistor, TransactionGenerator } from './tx'
2222
import AddressService from './addresses'
2323
import { deindexLockHashes } from './indexer/deindex'
24-
import ChainInfo from 'models/chain-info'
24+
import ChainInfo from 'services/chain-info'
2525
import AddressesService from 'services/addresses'
2626
import { Cell, DepType } from 'types/cell-types'
2727
import TypeConvert from 'types/type-convert'

packages/neuron-wallet/src/startup/sync-block-task/indexer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import IndexerQueue, { LockHashInfo } from 'services/indexer/queue'
55
import { Address } from 'database/address/address-dao'
66

77
import initConnection from 'database/chain/ormconfig'
8-
import ChainInfo from 'models/chain-info'
8+
import ChainInfo from 'services/chain-info'
99

1010
const { nodeService, addressCreatedSubject, walletCreatedSubject } = remote.require('./startup/sync-block-task/params')
1111

packages/neuron-wallet/src/startup/sync-block-task/init-database.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { updateMetaInfo, getMetaInfo } from 'database/chain/meta-info'
44
import LockUtils from 'models/lock-utils'
55
import logger from 'utils/logger'
66
import genesisBlockHash, { getChain } from './genesis'
7-
import ChainInfo from 'models/chain-info'
7+
import ChainInfo from 'services/chain-info'
88
import DaoUtils from '../../models/dao-utils'
99
import { NetworkWithID, EMPTY_GENESIS_HASH } from 'types/network'
1010

packages/neuron-wallet/src/startup/sync-block-task/sync.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import BlockListener from 'services/sync/block-listener'
55
import { Address } from 'database/address/address-dao'
66

77
import initConnection from 'database/chain/ormconfig'
8-
import ChainInfo from 'models/chain-info'
8+
import ChainInfo from 'services/chain-info'
99

1010
const { nodeService, addressCreatedSubject, walletCreatedSubject } = remote.require('./startup/sync-block-task/params')
1111

0 commit comments

Comments
 (0)