Skip to content

Commit

Permalink
Merge pull request #1128 from nervosnetwork/better-chain-info
Browse files Browse the repository at this point in the history
feat: ChainInfo delegates Networks Service to get current chain
  • Loading branch information
ashchan authored Nov 15, 2019
2 parents 00b0068 + 81f1eff commit 7cb8ad3
Show file tree
Hide file tree
Showing 16 changed files with 147 additions and 210 deletions.
17 changes: 9 additions & 8 deletions packages/neuron-wallet/src/controllers/app/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { UpdateController } from 'controllers'
import { showWindow } from './show-window'
import NetworksService from 'services/networks'
import WalletsService from 'services/wallets'
import ChainInfo from 'models/chain-info'
import CommandSubject from 'models/subjects/command'

enum URL {
Expand Down Expand Up @@ -295,10 +294,8 @@ const contextMenuTemplate: {
},
networkList: async (id: string) => {
const networksService = NetworksService.getInstance()
const [network, currentNetworkID] = await Promise.all([
networksService.get(id).catch(() => null),
networksService.getCurrentID().catch(() => null),
])
const network = networksService.get(id)
const currentNetworkID = networksService.getCurrentID()

if (!network) {
showMessageBox({
Expand Down Expand Up @@ -401,7 +398,11 @@ const contextMenuTemplate: {
return []
}

const address = bech32Address(identifier)
const address = bech32Address(identifier, {
prefix: NetworksService.getInstance().isMainnet() ? AddressPrefix.Mainnet : AddressPrefix.Testnet,
type: AddressType.HashIdx,
codeHashOrCodeHashIndex: '0x00',
})
return [
{
label: i18n.t('contextMenu.copy-address'),
Expand All @@ -413,7 +414,7 @@ const contextMenuTemplate: {
},
{
label: i18n.t('contextMenu.view-on-explorer'),
click: () => { shell.openExternal(`${ChainInfo.getInstance().explorerUrl()}/address/${address}`) }
click: () => { shell.openExternal(`${NetworksService.getInstance().explorerUrl()}/address/${address}`) }
},
]
},
Expand All @@ -431,7 +432,7 @@ const contextMenuTemplate: {
},
{
label: i18n.t('contextMenu.view-on-explorer'),
click: () => { shell.openExternal(`${ChainInfo.getInstance().explorerUrl()}/transaction/${hash}`) }
click: () => { shell.openExternal(`${NetworksService.getInstance().explorerUrl()}/transaction/${hash}`) }
},
]
},
Expand Down
4 changes: 2 additions & 2 deletions packages/neuron-wallet/src/controllers/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import fs from 'fs'
import { parseAddress } from '@nervosnetwork/ckb-sdk-utils'
import { dialog, SaveDialogReturnValue, BrowserWindow } from 'electron'
import WalletsService, { Wallet, WalletProperties, FileKeystoreWallet } from 'services/wallets'
import NetworksService from 'services/networks'
import Keystore from 'models/keys/keystore'
import Keychain from 'models/keys/keychain'
import ChainInfo from 'models/chain-info'
import { validateMnemonic, mnemonicToSeedSync } from 'models/keys/mnemonic'
import { AccountExtendedPublicKey, ExtendedPrivateKey } from 'models/keys/key'
import { ResponseCode } from 'utils/const'
Expand Down Expand Up @@ -341,7 +341,7 @@ export default class WalletsController {
feeRate = '1000'
}

const isMainnet = ChainInfo.getInstance().isMainnet()
const isMainnet = NetworksService.getInstance().isMainnet()
params.items.forEach(item => {
if (isMainnet && !item.address.startsWith('ckb')) {
throw new MainnetAddressRequired(item.address)
Expand Down
32 changes: 0 additions & 32 deletions packages/neuron-wallet/src/models/chain-info.ts

This file was deleted.

8 changes: 3 additions & 5 deletions packages/neuron-wallet/src/models/lock-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { OutPoint, Script, ScriptHashType } from 'types/cell-types'
import ConvertTo from 'types/convert-to'
import { SystemScriptSubject } from 'models/subjects/system-script'
import Core from '@nervosnetwork/ckb-sdk-core'
import ChainInfo from './chain-info'

export interface SystemScript {
codeHash: string
Expand Down Expand Up @@ -119,13 +118,12 @@ export default class LockUtils {
return LockUtils.computeScriptHash(lock)
}

static lockScriptToAddress(lock: Script): string {
static lockScriptToAddress(lock: Script, prefix: AddressPrefix = AddressPrefix.Mainnet): string {
const blake160: string = lock.args!
return LockUtils.blake160ToAddress(blake160)
return LockUtils.blake160ToAddress(blake160, prefix)
}

static blake160ToAddress(blake160: string): string {
const prefix = ChainInfo.getInstance().isMainnet() ? AddressPrefix.Mainnet : AddressPrefix.Testnet
static blake160ToAddress(blake160: string, prefix: AddressPrefix = AddressPrefix.Mainnet): string {
return bech32Address(blake160, {
prefix,
type: AddressType.HashIdx,
Expand Down
6 changes: 3 additions & 3 deletions packages/neuron-wallet/src/services/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import LockUtils from 'models/lock-utils'
import AddressDao, { Address as AddressInterface, AddressVersion } from 'database/address/address-dao'
import AddressCreatedSubject from 'models/subjects/address-created-subject'
import NodeService from './node'
import ChainInfo from 'models/chain-info'
import NetworksService from 'services/networks'

const MAX_ADDRESS_COUNT = 30

Expand Down Expand Up @@ -162,7 +162,7 @@ export default class AddressService {
AddressPrefix.Mainnet
).address

const addressToParse = ChainInfo.getInstance().isMainnet() ? mainnetAddress : testnetAddress
const addressToParse = NetworksService.getInstance().isMainnet() ? mainnetAddress : testnetAddress
const blake160: string = LockUtils.addressToBlake160(addressToParse)

const testnetAddressInfo: AddressInterface = {
Expand Down Expand Up @@ -238,6 +238,6 @@ export default class AddressService {
}

private static getAddressVersion = (): AddressVersion => {
return ChainInfo.getInstance().isMainnet() ? AddressVersion.Mainnet : AddressVersion.Testnet
return NetworksService.getInstance().isMainnet() ? AddressVersion.Mainnet : AddressVersion.Testnet
}
}
17 changes: 14 additions & 3 deletions packages/neuron-wallet/src/services/indexer/queue.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Subject, Subscription } from 'rxjs'
import { AddressPrefix } from '@nervosnetwork/ckb-sdk-utils'
import Utils from 'services/sync/utils'
import logger from 'utils/logger'
import GetBlocks from 'services/sync/get-blocks'
import NetworksService from 'services/networks'
import { Transaction, TransactionWithStatus } from 'types/cell-types'
import TypeConvert from 'types/type-convert'
import BlockNumber from 'services/sync/block-number'
Expand Down Expand Up @@ -211,7 +213,10 @@ export default class IndexerQueue {
blockHash: transactionWithStatus.txStatus.blockHash!
}
if (type === TxPointType.CreatedBy && this.latestCreatedBy.includes(txUniqueFlag)) {
const address = LockUtils.lockScriptToAddress(transaction.outputs![parseInt(txPoint.index, 16)].lock)
const address = LockUtils.lockScriptToAddress(
transaction.outputs![parseInt(txPoint.index, 16)].lock,
NetworksService.getInstance().isMainnet ? AddressPrefix.Mainnet : AddressPrefix.Testnet
)
AddressesUsedSubject.getSubject().next({
addresses: [address],
url: this.url,
Expand Down Expand Up @@ -273,13 +278,19 @@ export default class IndexerQueue {

let address: string | undefined
if (type === TxPointType.CreatedBy) {
address = LockUtils.lockScriptToAddress(transaction.outputs![parseInt(txPoint.index, 16)].lock)
address = LockUtils.lockScriptToAddress(
transaction.outputs![parseInt(txPoint.index, 16)].lock,
NetworksService.getInstance().isMainnet ? AddressPrefix.Mainnet : AddressPrefix.Testnet
)
this.latestCreatedBy.push(txUniqueFlag)
} else if (type === TxPointType.ConsumedBy) {
const input = txEntity.inputs[parseInt(txPoint.index, 16)]
const output = await IndexerTransaction.updateInputLockHash(input.outPointTxHash!, input.outPointIndex!)
if (output) {
address = LockUtils.lockScriptToAddress(output.lock)
address = LockUtils.lockScriptToAddress(
output.lock,
NetworksService.getInstance().isMainnet ? AddressPrefix.Mainnet : AddressPrefix.Testnet
)
}
}
if (address) {
Expand Down
Loading

0 comments on commit 7cb8ad3

Please sign in to comment.