diff --git a/packages/neuron-wallet/src/database/address/dao.ts b/packages/neuron-wallet/src/database/address/dao.ts index 0d2a72b363..3b6c295b2c 100644 --- a/packages/neuron-wallet/src/database/address/dao.ts +++ b/packages/neuron-wallet/src/database/address/dao.ts @@ -7,6 +7,7 @@ import { TransactionStatus } from 'types/cell-types' import { OutputStatus } from 'services/tx/params' import AddressEntity, { AddressVersion } from './entities/address' import { getConnection } from './ormconfig' +import NodeService from 'services/node' export interface Address { walletId: string @@ -54,7 +55,10 @@ export default class AddressDao { // so the final balance is (liveBalance + sentBalance - pendingBalance) // balance is the balance of the cells those who don't hold data or type script // totalBalance means balance of all cells, including those who hold data and type script - public static updateTxCountAndBalance = async (address: string): Promise => { + public static updateTxCountAndBalance = async ( + address: string, + url: string = NodeService.getInstance().core.rpc.node.url + ): Promise => { const addressEntities = await getConnection() .getRepository(AddressEntity) .find({ @@ -64,12 +68,12 @@ export default class AddressDao { const txCount: number = await TransactionsService.getCountByAddressAndStatus(address, [ TransactionStatus.Pending, TransactionStatus.Success, - ]) + ], url) const entities = await Promise.all( addressEntities.map(async entity => { const addressEntity = entity addressEntity.txCount = txCount - const lockHashes: string[] = await LockUtils.addressToAllLockHashes(addressEntity.address) + const lockHashes: string[] = await LockUtils.addressToAllLockHashes(addressEntity.address, url) addressEntity.liveBalance = await CellsService.getBalance(lockHashes, OutputStatus.Live, true) addressEntity.sentBalance = await CellsService.getBalance(lockHashes, OutputStatus.Sent, true) addressEntity.pendingBalance = await CellsService.getBalance(lockHashes, OutputStatus.Pending, true) diff --git a/packages/neuron-wallet/src/listeners/address.ts b/packages/neuron-wallet/src/listeners/address.ts index 1a1e91424e..6b3994240e 100644 --- a/packages/neuron-wallet/src/listeners/address.ts +++ b/packages/neuron-wallet/src/listeners/address.ts @@ -1,7 +1,7 @@ import { remote } from 'electron' import { ReplaySubject } from 'rxjs' import { bufferTime } from 'rxjs/operators' -import AddressesUsedSubject from 'models/subjects/addresses-used-subject' +import AddressesUsedSubject, { AddressesWithURL } from 'models/subjects/addresses-used-subject' import AddressService from 'services/addresses' import WalletService from 'services/wallets' import { AccountExtendedPublicKey } from 'models/keys/key' @@ -12,17 +12,21 @@ const addressesUsedSubject = isRenderer : AddressesUsedSubject.getSubject() // pipe not working directly -const bridge = new ReplaySubject(1000) -addressesUsedSubject.subscribe((addresses: string[]) => { - bridge.next(addresses) +const bridge = new ReplaySubject(1000) +addressesUsedSubject.subscribe((params: AddressesWithURL) => { + bridge.next(params) }) // update txCount when addresses used export const register = () => { - bridge.pipe(bufferTime(1000)).subscribe(async (addressesList: string[][]) => { - const addresses = addressesList.reduce((acc, val) => acc.concat(val), []) + bridge.pipe(bufferTime(1000)).subscribe(async (addressesList: AddressesWithURL[]) => { + if (addressesList.length === 0) { + return + } + const addresses = addressesList.map(list => list.addresses).reduce((acc, val) => acc.concat(val), []) + const url: string = addressesList[addressesList.length - 1].url const uniqueAddresses = [...new Set(addresses)] - const addrs = await AddressService.updateTxCountAndBalances(uniqueAddresses) + const addrs = await AddressService.updateTxCountAndBalances(uniqueAddresses, url) const walletIds: string[] = addrs.map(addr => addr.walletId).filter((value, idx, a) => a.indexOf(value) === idx) await Promise.all( walletIds.map(async id => { diff --git a/packages/neuron-wallet/src/listeners/tx-status.ts b/packages/neuron-wallet/src/listeners/tx-status.ts index aaf1cb4e28..3f9be3f9e8 100644 --- a/packages/neuron-wallet/src/listeners/tx-status.ts +++ b/packages/neuron-wallet/src/listeners/tx-status.ts @@ -61,7 +61,11 @@ const trackingStatus = async () => { if (failedTxs.length) { const blake160s = await FailedTransaction.updateFailedTxs(failedTxs.map(tx => tx.hash)) const usedAddresses = blake160s.map(blake160 => LockUtils.blake160ToAddress(blake160)) - AddressesUsedSubject.getSubject().next(usedAddresses) + const { core } = nodeService + AddressesUsedSubject.getSubject().next({ + addresses: usedAddresses, + url: core.rpc.node.url, + }) } if (successTxs.length > 0) { diff --git a/packages/neuron-wallet/src/models/lock-utils.ts b/packages/neuron-wallet/src/models/lock-utils.ts index 6ac0094768..13165a5c02 100644 --- a/packages/neuron-wallet/src/models/lock-utils.ts +++ b/packages/neuron-wallet/src/models/lock-utils.ts @@ -1,11 +1,16 @@ -import { scriptToHash } from '@nervosnetwork/ckb-sdk-utils' +import { + scriptToHash, + AddressPrefix, + bech32Address, + AddressType, + parseAddress +} from '@nervosnetwork/ckb-sdk-utils' import NodeService from 'services/node' import { OutPoint, Script, ScriptHashType } from 'types/cell-types' import env from 'env' import ConvertTo from 'types/convert-to' import { SystemScriptSubject } from 'models/subjects/system-script' - -const { core } = NodeService.getInstance() +import Core from '@nervosnetwork/ckb-sdk-core' export interface SystemScript { codeHash: string @@ -28,11 +33,15 @@ export default class LockUtils { @subscribed static systemScriptInfo: SystemScript | undefined - static async systemScript(): Promise { - if (this.systemScriptInfo) { + static previousURL: string | undefined + + static async systemScript(nodeURL: string = NodeService.getInstance().core.rpc.node.url): Promise { + if (this.systemScriptInfo && nodeURL === LockUtils.previousURL) { return this.systemScriptInfo } + const core = new Core(nodeURL) + const systemCell = await core.loadSecp256k1Dep() let { codeHash } = systemCell const { outPoint, hashType } = systemCell @@ -57,6 +66,7 @@ export default class LockUtils { } this.systemScriptInfo = systemScriptInfo + LockUtils.previousURL = nodeURL return systemScriptInfo } @@ -80,8 +90,12 @@ export default class LockUtils { return LockUtils.computeScriptHash(lock) } - static async addressToLockScript(address: string, hashType: ScriptHashType = ScriptHashType.Type): Promise