From f601d45b52f7e4b79fc078084eaa14b73546a436 Mon Sep 17 00:00:00 2001 From: zhangyouxin Date: Mon, 19 Jun 2023 12:44:36 +0800 Subject: [PATCH 1/2] refactor: remove "HexUtils" and repalce with "@ckb-lumos.codec" --- packages/neuron-ui/package.json | 7 ++- packages/neuron-wallet/package.json | 4 +- .../sync/light-connector.ts | 48 +++++++++---------- .../src/database/address/meta.ts | 3 +- .../src/models/asset-account-info.ts | 42 +++++++++------- .../neuron-wallet/src/models/chain/input.ts | 4 +- .../src/models/chain/out-point.ts | 4 +- .../neuron-wallet/src/models/chain/output.ts | 7 +-- .../neuron-wallet/src/models/chain/script.ts | 14 ++++-- .../src/models/chain/transaction.ts | 4 +- .../src/models/transaction-size.ts | 8 ++-- .../src/services/hardware/ledger.ts | 27 +++++++++-- packages/neuron-wallet/src/services/node.ts | 4 +- .../src/services/transaction-sender.ts | 8 ++-- .../src/services/tx/transaction-generator.ts | 3 +- packages/neuron-wallet/src/utils/hex.ts | 34 ------------- .../light-connector.test.ts | 6 +-- .../tests/models/chain/block-header.test.ts | 9 ++-- .../tests/models/transaction-size.test.ts | 4 +- .../tests/services/cells.test.ts | 5 +- .../services/tx/transaction-generator.test.ts | 5 +- .../services/tx/transaction-sender.test.ts | 10 ++-- yarn.lock | 2 +- 23 files changed, 136 insertions(+), 126 deletions(-) delete mode 100644 packages/neuron-wallet/src/utils/hex.ts diff --git a/packages/neuron-ui/package.json b/packages/neuron-ui/package.json index 2be123408c..e598ccbd21 100644 --- a/packages/neuron-ui/package.json +++ b/packages/neuron-ui/package.json @@ -28,9 +28,14 @@ "registry": "https://registry.npmjs.org/" }, "lint-staged": { - "src/**/*.{ts,tsx}": [ + "src/**/*.{js,cjs,mjs,jsx,ts,tsx}": [ + "prettier --write", "eslint --fix", "git add" + ], + "src/**/*.{css,scss}": [ + "prettier --write", + "git add" ] }, "jest": { diff --git a/packages/neuron-wallet/package.json b/packages/neuron-wallet/package.json index fdb039728f..9fa3cf0924 100644 --- a/packages/neuron-wallet/package.json +++ b/packages/neuron-wallet/package.json @@ -30,13 +30,15 @@ "rebuild:nativemodules": "electron-builder install-app-deps" }, "lint-staged": { - "src/**/*.ts": [ + "src/**/*.{js,cjs,mjs,jsx,ts,tsx}": [ + "prettier --write", "eslint --fix", "git add" ] }, "dependencies": { "@ckb-lumos/base": "0.18.0-rc2", + "@ckb-lumos/bi": "0.19.0", "@ckb-lumos/codec": "0.19.0", "@ckb-lumos/rpc": "0.18.0-rc2", "@iarna/toml": "2.2.5", diff --git a/packages/neuron-wallet/src/block-sync-renderer/sync/light-connector.ts b/packages/neuron-wallet/src/block-sync-renderer/sync/light-connector.ts index e50f5a99fb..15be587741 100644 --- a/packages/neuron-wallet/src/block-sync-renderer/sync/light-connector.ts +++ b/packages/neuron-wallet/src/block-sync-renderer/sync/light-connector.ts @@ -1,3 +1,4 @@ +import { BI } from '@ckb-lumos/bi' import { Subject } from 'rxjs' import { queue, QueueObject } from 'async' import { HexString, QueryOptions } from '@ckb-lumos/base' @@ -10,7 +11,6 @@ import SyncProgressService from '../../services/sync-progress' import { BlockTips, LumosCellQuery, Connector, AppendScript } from './connector' import { scriptToHash } from '@nervosnetwork/ckb-sdk-utils' import { FetchTransactionReturnType, LightRPC, LightScriptFilter } from '../../utils/ckb-rpc' -import HexUtils from '../../utils/hex' import Multisig from '../../services/multisig' import { SyncAddressType } from '../../database/chain/entities/sync-progress' import WalletService from '../../services/wallets' @@ -29,7 +29,7 @@ const unpackGroup = molecule.vector( molecule.struct( { tx_hash: number.Uint256BE, - index: number.Uint32LE + index: number.Uint32LE, }, ['tx_hash', 'index'] ) @@ -42,10 +42,8 @@ export default class LightConnector extends Connector { private syncQueue: QueueObject = queue(this.syncNextWithScript.bind(this), 1) private indexerQueryQueue: QueueObject | undefined private pollingIndexer: boolean = false - private syncInQueue: Map< - CKBComponents.Hash, - { blockStartNumber: number; blockEndNumber: number; cursor?: string } - > = new Map() + private syncInQueue: Map = + new Map() public readonly blockTipsSubject: Subject = new Subject() public readonly transactionsSubject = new Subject<{ txHashes: CKBComponents.Hash[]; params: CKBComponents.Hash }>() @@ -70,11 +68,9 @@ export default class LightConnector extends Connector { assetAccountInfo.getNftInfo().cellDep, assetAccountInfo.getNftIssuerInfo().cellDep, assetAccountInfo.getLegacyAnyoneCanPayInfo().cellDep, - assetAccountInfo.getChequeInfo().cellDep + assetAccountInfo.getChequeInfo().cellDep, ] - const fetchTxHashes = fetchCellDeps - .map(v => v.outPoint.txHash) - .map<[string, string]>(v => ['fetchTransaction', v]) + const fetchTxHashes = fetchCellDeps.map(v => v.outPoint.txHash).map<[string, string]>(v => ['fetchTransaction', v]) const txs = await this.lightRpc .createBatchRequest(fetchTxHashes) .exec() @@ -96,7 +92,7 @@ export default class LightConnector extends Connector { private async fetchDepCell() { const depGroupOutputsData: string[] = await this.getDepTxs() const depGroupTxHashes = [ - ...new Set(depGroupOutputsData.map(v => unpackGroup.unpack(v).map(v => v.tx_hash.toHexString())).flat()) + ...new Set(depGroupOutputsData.map(v => unpackGroup.unpack(v).map(v => v.tx_hash.toHexString())).flat()), ] if (depGroupTxHashes.length) { await this.lightRpc @@ -120,11 +116,11 @@ export default class LightConnector extends Connector { script: { codeHash: v.codeHash, hashType: v.hashType, - args: v.args + args: v.args, }, - blockRange: [HexUtils.toHex(v.blockStartNumber), HexUtils.toHex(v.blockEndNumber)], + blockRange: [BI.from(v.blockStartNumber).toHexString(), BI.from(v.blockEndNumber).toHexString()], scriptType: v.scriptType, - cursor: v.cursor + cursor: v.cursor, }) } }) @@ -139,9 +135,9 @@ export default class LightConnector extends Connector { ) { this.syncQueue.push({ script: syncScript.script, - blockRange: [HexUtils.toHex(syncStatus.blockEndNumber), syncScript.blockNumber], + blockRange: [BI.from(syncStatus.blockEndNumber).toHexString(), syncScript.blockNumber], scriptType: syncScript.scriptType, - cursor: undefined + cursor: undefined, }) } }) @@ -152,7 +148,7 @@ export default class LightConnector extends Connector { const header = await this.lightRpc.getTipHeader() this.blockTipsSubject.next({ cacheTipNumber: minSyncBlockNumber, - indexerTipNumber: +header.number + indexerTipNumber: +header.number, }) } @@ -172,12 +168,12 @@ export default class LightConnector extends Connector { const lockScripts = [ addressMeta.generateDefaultLockScript(), addressMeta.generateACPLockScript(), - addressMeta.generateLegacyACPLockScript() + addressMeta.generateLegacyACPLockScript(), ] return lockScripts.map(v => ({ script: v.toSDK(), scriptType: 'lock' as CKBRPC.ScriptType, - walletId: addressMeta.walletId + walletId: addressMeta.walletId, })) }) .flat() @@ -194,14 +190,14 @@ export default class LightConnector extends Connector { blockNumber: existSyncscripts[scriptToHash(v.script)]?.blockNumber ?? walletStartBlockMap[v.walletId] ?? - `0x${(walletMinBlockNumber?.[v.walletId] ?? 0).toString(16)}` + `0x${(walletMinBlockNumber?.[v.walletId] ?? 0).toString(16)}`, })), ...appendScripts.map(v => ({ ...v, blockNumber: existSyncscripts[scriptToHash(v.script)]?.blockNumber ?? - `0x${(otherTypeSyncProgress[scriptToHash(v.script)] ?? 0).toString(16)}` - })) + `0x${(otherTypeSyncProgress[scriptToHash(v.script)] ?? 0).toString(16)}`, + })), ] await this.lightRpc.setScripts(setScriptsParams) const walletIds = [...new Set(this.addressMetas.map(v => v.walletId))] @@ -232,7 +228,7 @@ export default class LightConnector extends Connector { await SyncProgressService.updateSyncStatus(syncProgress.hash, { blockStartNumber: parseInt(blockRange[1]), blockEndNumber: parseInt(blockRange[1]), - cursor: undefined + cursor: undefined, }) return } @@ -240,7 +236,7 @@ export default class LightConnector extends Connector { this.syncInQueue.set(syncProgress.hash, { blockStartNumber: result.lastCursor === '0x' ? parseInt(blockRange[1]) : parseInt(blockRange[0]), blockEndNumber: parseInt(blockRange[1]), - cursor: result.lastCursor === '0x' ? undefined : result.lastCursor + cursor: result.lastCursor === '0x' ? undefined : result.lastCursor, }) } @@ -255,14 +251,14 @@ export default class LightConnector extends Connector { queries.lock = { code_hash: lock.codeHash, hash_type: lock.hashType, - args: lock.args + args: lock.args, } } if (type) { queries.type = { code_hash: type.codeHash, hash_type: type.hashType, - args: type.args + args: type.args, } } queries.data = data || 'any' diff --git a/packages/neuron-wallet/src/database/address/meta.ts b/packages/neuron-wallet/src/database/address/meta.ts index 56f2c05a1b..3fd8f99abc 100644 --- a/packages/neuron-wallet/src/database/address/meta.ts +++ b/packages/neuron-wallet/src/database/address/meta.ts @@ -1,3 +1,4 @@ +import { bytes } from '@ckb-lumos/codec' import { Address, AddressVersion } from '../../models/address' import { AddressType } from '../../models/keys/address' import Script from '../../models/chain/script' @@ -116,6 +117,6 @@ export default class AddressMeta implements Address { public generateChequeLockScriptWithReceiverLockHash(): Script { const defaultLockScript = this.generateDefaultLockScript() const assetAccountInfo = new AssetAccountInfo() - return assetAccountInfo.generateChequeScript(defaultLockScript.computeHash(), '0'.repeat(40)) + return assetAccountInfo.generateChequeScript(defaultLockScript.computeHash(), bytes.hexify(Buffer.alloc(20))) } } diff --git a/packages/neuron-wallet/src/models/asset-account-info.ts b/packages/neuron-wallet/src/models/asset-account-info.ts index dac6d3722a..af97883dad 100644 --- a/packages/neuron-wallet/src/models/asset-account-info.ts +++ b/packages/neuron-wallet/src/models/asset-account-info.ts @@ -1,11 +1,12 @@ +import { bytes, molecule } from '@ckb-lumos/codec' import CellDep, { DepType } from './chain/cell-dep' import Script, { ScriptHashType } from './chain/script' import OutPoint from './chain/out-point' import NetworksService from '../services/networks' import Transaction from './chain/transaction' -import HexUtils from '../utils/hex' import SystemScriptInfo from './system-script-info' import { Address } from './address' +import { createFixedHexBytesCodec } from '@ckb-lumos/codec/lib/blockchain' export interface ScriptCellInfo { cellDep: CellDep @@ -244,9 +245,7 @@ export default class AssetAccountInfo { } public generateChequeScript(receiverLockHash: string, senderLockHash: string): Script { - const receiverLockHash20 = HexUtils.removePrefix(receiverLockHash).slice(0, 40) - const senderLockHash20 = HexUtils.removePrefix(senderLockHash).slice(0, 40) - const args = `0x${receiverLockHash20}${senderLockHash20}` + const args = bytes.hexify(bytes.concat(receiverLockHash.slice(0, 42), senderLockHash.slice(0, 42))) const info = this.chequeInfo return new Script(info.codeHash, args, info.hashType) } @@ -286,20 +285,31 @@ export default class AssetAccountInfo { } public static findSignPathForCheque(addressInfos: Address[], chequeLockArgs: string) { - const receiverLockHash = HexUtils.removePrefix(chequeLockArgs).slice(0, 40) - const senderLockHash = HexUtils.removePrefix(chequeLockArgs).slice(40) + const Bytes20 = createFixedHexBytesCodec(20) + const ChequeLockArgsCodec = molecule.struct( + { + receiverLockHash: Bytes20, + senderLockHash: Bytes20, + }, + ['receiverLockHash', 'senderLockHash'] + ) - for (const lockHash20 of [receiverLockHash, senderLockHash]) { - const foundAddressInfo = addressInfos.find(info => { - const defaultLockScript = SystemScriptInfo.generateSecpScript(info.blake160) - const addressLockHash20 = HexUtils.removePrefix(defaultLockScript.computeHash()).slice(0, 40) - return lockHash20 === addressLockHash20 - }) - if (foundAddressInfo) { - return foundAddressInfo - } + const { receiverLockHash, senderLockHash } = ChequeLockArgsCodec.unpack(chequeLockArgs) + + const foundReceiver = addressInfos.find(info => { + const target = bytes.bytify(SystemScriptInfo.generateSecpScript(info.blake160).computeHash()).slice(0, 20) + return bytes.equal(target, receiverLockHash) + }) + + if (foundReceiver) { + return foundReceiver } - return null + const foundSender = addressInfos.find(info => { + const target = bytes.bytify(SystemScriptInfo.generateSecpScript(info.blake160).computeHash()).slice(0, 20) + return bytes.equal(target, senderLockHash) + }) + + return foundSender || null } } diff --git a/packages/neuron-wallet/src/models/chain/input.ts b/packages/neuron-wallet/src/models/chain/input.ts index ec3dacece7..9c00015ede 100644 --- a/packages/neuron-wallet/src/models/chain/input.ts +++ b/packages/neuron-wallet/src/models/chain/input.ts @@ -1,6 +1,6 @@ import OutPoint from './out-point' import Script from './script' -import HexUtils from '../../utils/hex' +import { BI } from '@ckb-lumos/bi' import TypeChecker from '../../utils/type-checker' export default class Input { @@ -112,7 +112,7 @@ export default class Input { public toSDK(): CKBComponents.CellInput { return { - since: HexUtils.toHex(this.since!), + since: BI.from(this.since || 0).toHexString(), previousOutput: this.previousOutput?.toSDK() || null, } } diff --git a/packages/neuron-wallet/src/models/chain/out-point.ts b/packages/neuron-wallet/src/models/chain/out-point.ts index 1f889e7f16..4b7f050791 100644 --- a/packages/neuron-wallet/src/models/chain/out-point.ts +++ b/packages/neuron-wallet/src/models/chain/out-point.ts @@ -1,4 +1,4 @@ -import HexUtils from '../../utils/hex' +import { BI } from '@ckb-lumos/bi' import TypeChecker from '../../utils/type-checker' export default class OutPoint { @@ -21,7 +21,7 @@ export default class OutPoint { public toSDK(): CKBComponents.OutPoint { return { txHash: this.txHash, - index: HexUtils.toHex(this.index), + index: BI.from(this.index).toHexString(), } } diff --git a/packages/neuron-wallet/src/models/chain/output.ts b/packages/neuron-wallet/src/models/chain/output.ts index 071a3f24e3..5a8e2f568f 100644 --- a/packages/neuron-wallet/src/models/chain/output.ts +++ b/packages/neuron-wallet/src/models/chain/output.ts @@ -1,6 +1,7 @@ import Script from './script' import OutPoint from './out-point' -import HexUtils from '../../utils/hex' +import { bytes as byteUtils } from '@ckb-lumos/codec' +import { BI } from '@ckb-lumos/bi' import TypeChecker from '../../utils/type-checker' // sent: pending transaction's output @@ -203,7 +204,7 @@ export default class Output { } public calculateBytesize(): number { - let bytesize = 8 + HexUtils.byteLength(this.data) + this.lock.calculateBytesize() + let bytesize = 8 + byteUtils.bytify(this.data).byteLength + this.lock.calculateBytesize() if (this.type) { bytesize += this.type.calculateBytesize() } @@ -212,7 +213,7 @@ export default class Output { public toSDK(): CKBComponents.CellOutput { return { - capacity: HexUtils.toHex(this.capacity), + capacity: BI.from(this.capacity).toHexString(), lock: this.lock.toSDK(), type: this.type ? this.type.toSDK() : this.type, } diff --git a/packages/neuron-wallet/src/models/chain/script.ts b/packages/neuron-wallet/src/models/chain/script.ts index 09a341e039..609b8802c0 100644 --- a/packages/neuron-wallet/src/models/chain/script.ts +++ b/packages/neuron-wallet/src/models/chain/script.ts @@ -1,5 +1,5 @@ import { scriptToHash } from '@nervosnetwork/ckb-sdk-utils' -import HexUtils from '../../utils/hex' +import { bytes as byteUtils } from '@ckb-lumos/codec' import TypeChecker from '../../utils/type-checker' export enum ScriptHashType { @@ -34,12 +34,18 @@ export default class Script { } public computeHash(): string { - const hash: string = scriptToHash(this.toSDK()) - return HexUtils.addPrefix(hash) + return scriptToHash(this.toSDK()) } + /** + * @deprecated please move to `calculateOccupiedByteSize` + */ public calculateBytesize(): number { - return 1 + HexUtils.byteLength(this.codeHash) + HexUtils.byteLength(this.args) + return this.calculateOccupiedByteSize() + } + + public calculateOccupiedByteSize(): number { + return 1 + byteUtils.concat(this.args, this.codeHash).byteLength } public toSDK(): CKBComponents.Script { diff --git a/packages/neuron-wallet/src/models/chain/transaction.ts b/packages/neuron-wallet/src/models/chain/transaction.ts index d128270a8f..b78f59d5ae 100644 --- a/packages/neuron-wallet/src/models/chain/transaction.ts +++ b/packages/neuron-wallet/src/models/chain/transaction.ts @@ -2,7 +2,7 @@ import CellDep from './cell-dep' import Input from './input' import Output from './output' import WitnessArgs from './witness-args' -import HexUtils from '../../utils/hex' +import { BI } from '@ckb-lumos/bi' import { serializeWitnessArgs, rawTransactionToHash } from '@nervosnetwork/ckb-sdk-utils' import BlockHeader from './block-header' import TypeCheckerUtils from '../../utils/type-checker' @@ -273,7 +273,7 @@ export default class Transaction { public toSDKRawTransaction(): CKBComponents.RawTransaction { return { - version: HexUtils.toHex(this.version), + version: BI.from(this.version).toHexString(), inputs: this.inputs.map(i => i.toSDK()), outputs: this.outputs.map(o => o.toSDK()), cellDeps: this.cellDeps.map(cd => cd.toSDK()), diff --git a/packages/neuron-wallet/src/models/transaction-size.ts b/packages/neuron-wallet/src/models/transaction-size.ts index 4bdca0deb3..61832d221c 100644 --- a/packages/neuron-wallet/src/models/transaction-size.ts +++ b/packages/neuron-wallet/src/models/transaction-size.ts @@ -1,5 +1,4 @@ import { serializeOutput, serializeWitnessArgs } from '@nervosnetwork/ckb-sdk-utils/lib/serialization/transaction' -import HexUtils from '../utils/hex' import { serializeFixVec } from '@nervosnetwork/ckb-sdk-utils/lib/serialization' import Output from './chain/output' import WitnessArgs from './chain/witness-args' @@ -7,6 +6,7 @@ import Transaction from './chain/transaction' import Multisig from './multisig' import Script, { ScriptHashType } from './chain/script' import BufferUtils from '../utils/buffer' +import { bytes as byteUtils } from '@ckb-lumos/codec' export default class TransactionSize { public static SERIALIZED_OFFSET_BYTESIZE = 4 @@ -29,7 +29,7 @@ export default class TransactionSize { public static output(output: Output): number { const bytes = serializeOutput(output.toSDK()) - return HexUtils.byteLength(bytes) + TransactionSize.SERIALIZED_OFFSET_BYTESIZE + return byteUtils.bytify(bytes).byteLength + TransactionSize.SERIALIZED_OFFSET_BYTESIZE } public static ckbAnyoneCanPayOutput(): number { @@ -56,7 +56,7 @@ export default class TransactionSize { public static outputData(data: string): number { const bytes = serializeFixVec(data) - return HexUtils.byteLength(bytes) + TransactionSize.SERIALIZED_OFFSET_BYTESIZE + return byteUtils.bytify(bytes).byteLength + TransactionSize.SERIALIZED_OFFSET_BYTESIZE } // TODO: and here @@ -68,7 +68,7 @@ export default class TransactionSize { public static witness(witness: WitnessArgs | string): number { const wit: string = typeof witness === 'string' ? witness : serializeWitnessArgs(witness.toSDK()) const bytes = serializeFixVec(wit) - return HexUtils.byteLength(bytes) + TransactionSize.SERIALIZED_OFFSET_BYTESIZE + return byteUtils.bytify(bytes).byteLength + TransactionSize.SERIALIZED_OFFSET_BYTESIZE } public static secpLockWitness(): number { diff --git a/packages/neuron-wallet/src/services/hardware/ledger.ts b/packages/neuron-wallet/src/services/hardware/ledger.ts index a1b8de2793..6366cd1494 100644 --- a/packages/neuron-wallet/src/services/hardware/ledger.ts +++ b/packages/neuron-wallet/src/services/hardware/ledger.ts @@ -9,7 +9,6 @@ import { takeUntil, filter, scan } from 'rxjs/operators' import Transaction from '../../models/chain/transaction' import NodeService from '../../services/node' import Address, { AddressType } from '../../models/keys/address' -import HexUtils from '../../utils/hex' import logger from '../../utils/logger' import NetworksService from '../../services/networks' import { generateRPC } from '../../utils/ckb-rpc' @@ -49,7 +48,13 @@ export default class Ledger extends Hardware { } } - public async signTransaction (_: string, tx: Transaction, witnesses: string[], path: string, context?: RPC.RawTransaction[]) { + public async signTransaction( + _: string, + tx: Transaction, + witnesses: string[], + path: string, + context?: RPC.RawTransaction[] + ) { const rpc = generateRPC(NodeService.getInstance().nodeUrl) const rawTx = rpc.paramsFormatter.toRawTransaction(tx.toSDKRawTransaction()) @@ -70,13 +75,13 @@ export default class Ledger extends Hardware { } async signMessage(path: string, messageHex: string) { - const message = HexUtils.removePrefix(messageHex) + const message = this.removePrefix(messageHex) const signed = await this.ledgerCKB!.signMessage( path === Address.pathForReceiving(0) ? this.defaultPath : path, message, false ) - return HexUtils.addPrefix(signed) + return this.addPrefix(signed) } async getAppVersion(): Promise { @@ -143,4 +148,18 @@ export default class Ledger extends Hardware { .catch(() => [] as DeviceInfo[]) ) } + + private removePrefix(hex: string): string { + if (hex.startsWith('0x')) { + return hex.slice(2) + } + return hex + } + + private addPrefix(hex: string): string { + if (hex.startsWith('0x')) { + return hex + } + return `0x${hex}` + } } diff --git a/packages/neuron-wallet/src/services/node.ts b/packages/neuron-wallet/src/services/node.ts index b8e38df6d3..c998d5331b 100644 --- a/packages/neuron-wallet/src/services/node.ts +++ b/packages/neuron-wallet/src/services/node.ts @@ -1,5 +1,6 @@ import fs from 'fs' import path from 'path' +import { BI } from '@ckb-lumos/bi' import { app as electronApp, dialog, shell } from 'electron' import { t } from 'i18next' import { interval, BehaviorSubject, merge } from 'rxjs' @@ -17,7 +18,6 @@ import logger from '../utils/logger' import redistCheck from '../utils/redist-check' import { rpcRequest } from '../utils/rpc-request' import { generateRPC } from '../utils/ckb-rpc' -import HexUtils from '..//utils/hex' import startMonitor from './monitor' import { CKBLightRunner } from './light-runner' @@ -119,7 +119,7 @@ class NodeService { if (!this.delayTime) { this.delayTime = 0 } - const tip: string = HexUtils.toDecimal(tipNumber) + const tip: string = BI.from(tipNumber).toString() this._tipBlockNumber = tip this.tipNumberSubject.next(tip) }, diff --git a/packages/neuron-wallet/src/services/transaction-sender.ts b/packages/neuron-wallet/src/services/transaction-sender.ts index 916d24fd07..d7114602d0 100644 --- a/packages/neuron-wallet/src/services/transaction-sender.ts +++ b/packages/neuron-wallet/src/services/transaction-sender.ts @@ -21,7 +21,7 @@ import Script from '../models/chain/script' import Multisig from '../models/multisig' import Blake2b from '../models/blake2b' import logger from '../utils/logger' -import HexUtils from '../utils/hex' +import { bytes as byteUtils } from '@ckb-lumos/codec' import SystemScriptInfo from '../models/system-script-info' import AddressParser from '../models/address-parser' import HardwareWalletService from './hardware' @@ -32,7 +32,7 @@ import { NoMatchAddressForSign, SignTransactionFailed, CellIsNotYetLive, - TransactionIsNotCommittedYet + TransactionIsNotCommittedYet, } from '../exceptions' import AssetAccountInfo from '../models/asset-account-info' import MultisigConfigModel from '../models/multisig-config' @@ -400,7 +400,7 @@ export default class TransactionSender { lock: `0x` + serializedMultiSign.slice(2) + '0'.repeat(130 * m), }) const serializedEmptyWitness = serializeWitnessArgs(emptyWitness.toSDK()) - const serialziedEmptyWitnessSize = HexUtils.byteLength(serializedEmptyWitness) + const serialziedEmptyWitnessSize = byteUtils.bytify(serializedEmptyWitness).byteLength const blake2b = new Blake2b() blake2b.update(txHash) blake2b.update(toUint64Le(`0x${serialziedEmptyWitnessSize.toString(16)}`)) @@ -408,7 +408,7 @@ export default class TransactionSender { restWitnesses.forEach(w => { const wit: string = typeof w === 'string' ? w : serializeWitnessArgs(w.toSDK()) - const byteLength = HexUtils.byteLength(wit) + const byteLength = byteUtils.bytify(wit).byteLength blake2b.update(toUint64Le(`0x${byteLength.toString(16)}`)) blake2b.update(wit) }) diff --git a/packages/neuron-wallet/src/services/tx/transaction-generator.ts b/packages/neuron-wallet/src/services/tx/transaction-generator.ts index 94fc6c4cc8..990f24eeff 100644 --- a/packages/neuron-wallet/src/services/tx/transaction-generator.ts +++ b/packages/neuron-wallet/src/services/tx/transaction-generator.ts @@ -1,3 +1,4 @@ +import { bytes } from '@ckb-lumos/codec' import CellsService from '../../services/cells' import { CapacityTooSmall, @@ -1026,7 +1027,7 @@ export class TransactionGenerator { const chequeCellTmp = Output.fromObject({ capacity: BigInt(162 * 10 ** 8).toString(), - lock: assetAccountInfo.generateChequeScript('0'.repeat(40), '0'.repeat(40)), + lock: assetAccountInfo.generateChequeScript(bytes.hexify(Buffer.alloc(20)), bytes.hexify(Buffer.alloc(20))), type: assetAccountInfo.generateSudtScript(assetAccount.tokenID), }) diff --git a/packages/neuron-wallet/src/utils/hex.ts b/packages/neuron-wallet/src/utils/hex.ts deleted file mode 100644 index 9f3cdf3d63..0000000000 --- a/packages/neuron-wallet/src/utils/hex.ts +++ /dev/null @@ -1,34 +0,0 @@ -export default class HexUtils { - public static toDecimal(hex: string): string { - return BigInt(hex).toString() - } - - public static toHex(num: string | number, padStartCount: number = 0): string { - if (typeof num === 'string' && num.startsWith('0x')) { - return num - } - return `0x${(typeof num === 'string' ? BigInt(num) : num).toString(16).padStart(padStartCount, '0')}` - } - - public static removePrefix(hex: string): string { - if (hex.startsWith('0x')) { - return hex.slice(2) - } - return hex - } - - public static addPrefix(hex: string): string { - if (hex.startsWith('0x')) { - return hex - } - return `0x${hex}` - } - - public static byteLength(hex: string): number { - return Buffer.byteLength(HexUtils.removePrefix(hex), 'hex') - } - - public static fromBuffer(buffer: Buffer): string { - return '0x' + buffer.toString('hex') - } -} diff --git a/packages/neuron-wallet/tests/block-sync-renderer/light-connector.test.ts b/packages/neuron-wallet/tests/block-sync-renderer/light-connector.test.ts index 779a7f366e..6706d24ea3 100644 --- a/packages/neuron-wallet/tests/block-sync-renderer/light-connector.test.ts +++ b/packages/neuron-wallet/tests/block-sync-renderer/light-connector.test.ts @@ -1,7 +1,7 @@ import { scriptToHash } from '@nervosnetwork/ckb-sdk-utils' import LightConnector from '../../src/block-sync-renderer/sync/light-connector' import SyncProgress from '../../src/database/chain/entities/sync-progress' -import HexUtils from '../../src/utils/hex' +import { BI } from '@ckb-lumos/bi' import AddressMeta from '../../src/database/address/meta' const getSyncStatusMock = jest.fn() @@ -154,7 +154,7 @@ describe('test light connector', () => { hashType: syncProgress.hashType, args: syncProgress.args }, - blockRange: [HexUtils.toHex(syncProgress.blockStartNumber), HexUtils.toHex(syncProgress.blockEndNumber)], + blockRange: [BI.from(syncProgress.blockStartNumber).toHexString(), BI.from(syncProgress.blockEndNumber).toHexString()], scriptType: syncProgress.scriptType, cursor: syncProgress.cursor }) @@ -209,7 +209,7 @@ describe('test light connector', () => { hashType: syncProgress.hashType, args: syncProgress.args }, - blockRange: [HexUtils.toHex(syncProgress.blockEndNumber), HexUtils.toHex('0xaa')], + blockRange: [BI.from(syncProgress.blockEndNumber).toHexString(), BI.from('0xaa').toHexString()], scriptType: syncProgress.scriptType, cursor: syncProgress.cursor }) diff --git a/packages/neuron-wallet/tests/models/chain/block-header.test.ts b/packages/neuron-wallet/tests/models/chain/block-header.test.ts index a6b1680124..c959958bbc 100644 --- a/packages/neuron-wallet/tests/models/chain/block-header.test.ts +++ b/packages/neuron-wallet/tests/models/chain/block-header.test.ts @@ -1,5 +1,4 @@ import BlockHeader from '../../../src/models/chain/block-header' -import HexUtils from '../../../src/utils/hex' describe('BlockHeader', () => { const sdkHeader: CKBComponents.BlockHeader = { @@ -19,11 +18,11 @@ describe('BlockHeader', () => { it('fromSDK', () => { const result: BlockHeader = BlockHeader.fromSDK(sdkHeader) - expect(result.version).toEqual(HexUtils.toDecimal(sdkHeader.version)) - expect(result.timestamp).toEqual(HexUtils.toDecimal(sdkHeader.timestamp)) + expect(result.version).toEqual('0') // 0x0 + expect(result.timestamp).toEqual('1557311767') // 0x5cd2b117 expect(result.hash).toEqual(sdkHeader.hash) expect(result.parentHash).toEqual(sdkHeader.parentHash) - expect(result.number).toEqual(HexUtils.toDecimal(sdkHeader.number)) - expect(result.epoch).toEqual(HexUtils.toDecimal(sdkHeader.epoch)) + expect(result.number).toEqual('1024') // 0x400 + expect(result.epoch).toEqual('1979121332649985') // 0x7080018000001 }) }) diff --git a/packages/neuron-wallet/tests/models/transaction-size.test.ts b/packages/neuron-wallet/tests/models/transaction-size.test.ts index de105ce411..26371673b8 100644 --- a/packages/neuron-wallet/tests/models/transaction-size.test.ts +++ b/packages/neuron-wallet/tests/models/transaction-size.test.ts @@ -1,5 +1,5 @@ import TransactionSize from '../../src/models/transaction-size' -import HexUtils from '../../src/utils/hex' +import { bytes as byteUtils } from '@ckb-lumos/codec' import Script, { ScriptHashType } from '../../src/models/chain/script' import WitnessArgs from '../../src/models/chain/witness-args' import Transaction from '../../src/models/chain/transaction' @@ -25,7 +25,7 @@ describe('TransactionSize', () => { it('witnessArgs', () => { const result = TransactionSize.witness(witnessArgs) - expect(result).toEqual(HexUtils.byteLength('0x10000000100000001000000010000000') + 4 + 4) + expect(result).toEqual(byteUtils.bytify('0x10000000100000001000000010000000').byteLength + 4 + 4) }) it('witnessArgs only lock', () => { diff --git a/packages/neuron-wallet/tests/services/cells.test.ts b/packages/neuron-wallet/tests/services/cells.test.ts index 09ba116676..fddefddae4 100644 --- a/packages/neuron-wallet/tests/services/cells.test.ts +++ b/packages/neuron-wallet/tests/services/cells.test.ts @@ -1,5 +1,6 @@ import { getConnection } from 'typeorm' import { scriptToAddress } from '@nervosnetwork/ckb-sdk-utils' +import { bytes } from '@ckb-lumos/codec' import { initConnection } from '../../src/database/chain/ormconfig' import OutputEntity from '../../src/database/chain/entities/output' import { OutputStatus } from '../../src/models/chain/output' @@ -1144,8 +1145,8 @@ describe('CellsService', () => { lockScript: multiSignLockScript, } - const receiverChequeLock = assetAccountInfo.generateChequeScript(bobDefaultLock.computeHash(), '0'.repeat(40)) - const senderChequeLock = assetAccountInfo.generateChequeScript('0'.repeat(40), bobDefaultLock.computeHash()) + const receiverChequeLock = assetAccountInfo.generateChequeScript(bobDefaultLock.computeHash(), bytes.hexify(Buffer.alloc(20))) + const senderChequeLock = assetAccountInfo.generateChequeScript(bytes.hexify(Buffer.alloc(20)), bobDefaultLock.computeHash()) const acpLock = assetAccountInfo.generateAnyoneCanPayScript('0x') const sudtType = new Script(assetAccountInfo.getSudtCodeHash(), '0x', ScriptHashType.Type) diff --git a/packages/neuron-wallet/tests/services/tx/transaction-generator.test.ts b/packages/neuron-wallet/tests/services/tx/transaction-generator.test.ts index 84daf602bc..63686dd0ce 100644 --- a/packages/neuron-wallet/tests/services/tx/transaction-generator.test.ts +++ b/packages/neuron-wallet/tests/services/tx/transaction-generator.test.ts @@ -1,5 +1,6 @@ import { when } from 'jest-when' import { getConnection } from 'typeorm' +import { bytes } from '@ckb-lumos/codec' import { initConnection } from '../../../src/database/chain/ormconfig' import OutputEntity from '../../../src/database/chain/entities/output' import InputEntity from '../../../src/database/chain/entities/input' @@ -2203,7 +2204,7 @@ describe('TransactionGenerator', () => { it('creates cheque output', () => { const chequeOutput = tx.outputs[0] expect(chequeOutput.lock.computeHash()).toEqual(expectedChequeOutput.lockHash) - expect(chequeOutput.lock.args.length).toEqual(82) + expect(bytes.bytify(chequeOutput.lock.args).byteLength).toEqual(40) }) it('sender lock hash equals to one of default lock inputs', () => { const defaultLockInput = tx.inputs.find(input => { @@ -2367,7 +2368,7 @@ describe('TransactionGenerator', () => { senderDefaultLockInputEntity = createInput(senderDefaultLock, undefined, transaction.hash) - const chequeLock = assetAccountInfo.generateChequeScript('0x' + '0'.repeat(40), senderDefaultLock.computeHash()) + const chequeLock = assetAccountInfo.generateChequeScript(bytes.hexify(Buffer.alloc(20)), senderDefaultLock.computeHash()) chequeOutputEntity = createOutput( chequeLock, typeScript, diff --git a/packages/neuron-wallet/tests/services/tx/transaction-sender.test.ts b/packages/neuron-wallet/tests/services/tx/transaction-sender.test.ts index 853d4e6aa9..af52af9724 100644 --- a/packages/neuron-wallet/tests/services/tx/transaction-sender.test.ts +++ b/packages/neuron-wallet/tests/services/tx/transaction-sender.test.ts @@ -1,3 +1,5 @@ +import { bytes } from "@ckb-lumos/codec"; + const stubbedRPCServiceConstructor = jest.fn() const stubbedWalletsServiceConstructor = jest.fn() const stubbedGetLiveCell = jest.fn() @@ -452,7 +454,7 @@ describe('TransactionSender Test', () => { }) describe('when matched receiver lock hash', () => { beforeEach(() => { - const chequeLock = assetAccountInfo.generateChequeScript(receiverDefaultLock.computeHash(), '0'.repeat(40)) + const chequeLock = assetAccountInfo.generateChequeScript(receiverDefaultLock.computeHash(), bytes.hexify(Buffer.alloc(20))) tx.inputs[0].lock = chequeLock }) it('success', async () => { @@ -464,7 +466,7 @@ describe('TransactionSender Test', () => { }) describe('when not matched receiver lock hash', () => { beforeEach(() => { - const chequeLock = assetAccountInfo.generateChequeScript('0'.repeat(40), '0'.repeat(40)) + const chequeLock = assetAccountInfo.generateChequeScript(bytes.hexify(Buffer.alloc(20)), bytes.hexify(Buffer.alloc(20))) tx.inputs[0].lock = chequeLock }) it('throws', async () => { @@ -502,7 +504,7 @@ describe('TransactionSender Test', () => { }) describe('when matched sender lock hash', () => { beforeEach(() => { - const chequeLock = assetAccountInfo.generateChequeScript('0'.repeat(40), senderDefaultLock.computeHash()) + const chequeLock = assetAccountInfo.generateChequeScript(bytes.hexify(Buffer.alloc(20)), senderDefaultLock.computeHash()) tx.inputs[0].lock = chequeLock }) it('success', async () => { @@ -513,7 +515,7 @@ describe('TransactionSender Test', () => { }) describe('when not matched sender lock hash', () => { beforeEach(() => { - const chequeLock = assetAccountInfo.generateChequeScript('0'.repeat(40), '0'.repeat(40)) + const chequeLock = assetAccountInfo.generateChequeScript(bytes.hexify(Buffer.alloc(20)), bytes.hexify(Buffer.alloc(20))) tx.inputs[0].lock = chequeLock }) it('throws', async () => { diff --git a/yarn.lock b/yarn.lock index 7a57ceb7d3..add1441606 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1286,7 +1286,7 @@ dependencies: jsbi "^4.1.0" -"@ckb-lumos/bi@^0.19.0": +"@ckb-lumos/bi@0.19.0", "@ckb-lumos/bi@^0.19.0": version "0.19.0" resolved "https://registry.yarnpkg.com/@ckb-lumos/bi/-/bi-0.19.0.tgz#ccd7e9e32e58eec7effc78cd171eb79f0f858d4a" integrity sha512-+EFkUOqCtIwilAfrd680wEeMTXME9Wjjyl436+0x32jlbfo4sa+iNqwPaTjUQ2/SDwsuqK3eB+DntYxxFxoEtw== From 944a8945ea3af4b7b789626a41e9fe47e92e0f80 Mon Sep 17 00:00:00 2001 From: zhangyouxin Date: Fri, 14 Jul 2023 16:09:27 +0800 Subject: [PATCH 2/2] fix ci --- .../NervosDAODetail/CellsCard/index.tsx | 10 +- .../src/components/NervosDAODetail/index.tsx | 10 +- .../src/services/tx/transaction-generator.ts | 8 +- .../light-connector.test.ts | 216 ++++++++++++------ .../tests/services/cells.test.ts | 10 +- .../services/tx/transaction-generator.test.ts | 37 +-- .../services/tx/transaction-sender.test.ts | 30 ++- 7 files changed, 216 insertions(+), 105 deletions(-) diff --git a/packages/neuron-ui/src/components/NervosDAODetail/CellsCard/index.tsx b/packages/neuron-ui/src/components/NervosDAODetail/CellsCard/index.tsx index 2a786ddecc..b9b5e1f709 100644 --- a/packages/neuron-ui/src/components/NervosDAODetail/CellsCard/index.tsx +++ b/packages/neuron-ui/src/components/NervosDAODetail/CellsCard/index.tsx @@ -11,10 +11,12 @@ import LockInfoDialog from 'components/LockInfoDialog' import { useState as useGlobalState } from 'states' import styles from './cellsCard.module.scss' -const TabsVariantWithCellsCard: FC> = ({ tabs, selectedTab, onTabChange }) => { +const TabsVariantWithCellsCard: FC< + VariantProps<{ + title: string + cells: (State.DetailedInput | State.DetailedOutput)[] + }> +> = ({ tabs, selectedTab, onTabChange }) => { const [t] = useTranslation() const { chain: { networkID }, diff --git a/packages/neuron-ui/src/components/NervosDAODetail/index.tsx b/packages/neuron-ui/src/components/NervosDAODetail/index.tsx index e8f0b421e4..712274d0ac 100644 --- a/packages/neuron-ui/src/components/NervosDAODetail/index.tsx +++ b/packages/neuron-ui/src/components/NervosDAODetail/index.tsx @@ -24,10 +24,12 @@ import styles from './nervosDAODetail.module.scss' import hooks from './hooks' import CellsCard from './CellsCard' -const TabsVariantWithTxTypes: FC> = ({ tabs, selectedTab, onTabChange }) => { +const TabsVariantWithTxTypes: FC< + VariantProps<{ + title: string + hash: string + }> +> = ({ tabs, selectedTab, onTabChange }) => { const [t] = useTranslation() const [transaction, setTransaction] = useState(transactionState) const [error, setError] = useState() diff --git a/packages/neuron-wallet/src/services/tx/transaction-generator.ts b/packages/neuron-wallet/src/services/tx/transaction-generator.ts index 1da3803dec..99c0dfc867 100644 --- a/packages/neuron-wallet/src/services/tx/transaction-generator.ts +++ b/packages/neuron-wallet/src/services/tx/transaction-generator.ts @@ -714,7 +714,7 @@ export class TransactionGenerator { public static async generateDestroyAssetAccountTx( walletId: string, - asssetAccountInputs: Input[], + assetAccountInputs: Input[], changeBlake160: string, isCKBAccount: boolean ) { @@ -722,7 +722,7 @@ export class TransactionGenerator { const assetAccountInfo = new AssetAccountInfo() const cellDeps = [secpCellDep, assetAccountInfo.anyoneCanPayCellDep] - if (asssetAccountInputs.some(v => v.type && v.data !== '0x' && BigInt(v.data || 0) !== BigInt(0))) { + if (assetAccountInputs.some(v => v.type && v.data !== '0x' && BigInt(v.data || 0) !== BigInt(0))) { throw new SudtAcpHaveDataError() } if (!isCKBAccount) { @@ -738,13 +738,13 @@ export class TransactionGenerator { version: '0', headerDeps: [], cellDeps, - inputs: asssetAccountInputs, + inputs: assetAccountInputs, outputs: [output], outputsData: [output.data], witnesses: [], }) - let allCapacities = asssetAccountInputs.reduce((a, b) => { + let allCapacities = assetAccountInputs.reduce((a, b) => { return a + BigInt(b.capacity as string) }, BigInt(0)) tx.fee = TransactionFee.fee(TransactionSize.tx(tx), BigInt(1e4)).toString() diff --git a/packages/neuron-wallet/tests/block-sync-renderer/light-connector.test.ts b/packages/neuron-wallet/tests/block-sync-renderer/light-connector.test.ts index 6706d24ea3..34f574438c 100644 --- a/packages/neuron-wallet/tests/block-sync-renderer/light-connector.test.ts +++ b/packages/neuron-wallet/tests/block-sync-renderer/light-connector.test.ts @@ -56,13 +56,15 @@ jest.mock('../../src/services/sync-progress', () => { static updateSyncStatus: any = (hash: string, update: any) => updateSyncStatusMock(hash, update) static updateSyncProgressFlag: any = (walletIds: string[]) => updateSyncProgressFlagMock(walletIds) static getWalletMinBlockNumber: any = () => getWalletMinBlockNumberMock() - static removeByHashesAndAddressType: any = (type: number, scripts: CKBComponents.Script[]) => removeByHashesAndAddressType(type, scripts) + static removeByHashesAndAddressType: any = (type: number, scripts: CKBComponents.Script[]) => + removeByHashesAndAddressType(type, scripts) + static getOtherTypeSyncProgress: any = () => getOtherTypeSyncProgressMock() } }) jest.mock('../../src/utils/ckb-rpc', () => ({ - LightRPC: function() { + LightRPC: function () { return { setScripts: setScriptsMock, getScripts: getScriptsMock, @@ -70,11 +72,11 @@ jest.mock('../../src/utils/ckb-rpc', () => ({ getTransactions: getTransactionsMock, createBatchRequest: () => ({ exec: createBatchRequestMock }), } - } + }, })) jest.mock('../../src/services/multisig', () => ({ - getMultisigConfigForLight: () => getMultisigConfigForLightMock() + getMultisigConfigForLight: () => getMultisigConfigForLightMock(), })) jest.mock('../../src/services/wallets', () => ({ @@ -83,19 +85,19 @@ jest.mock('../../src/services/wallets', () => ({ getCurrent: walletGetCurrentMock, getAll: walletGetAllMock, } - } + }, })) jest.mock('timers/promises', () => ({ scheduler: { wait: (delay: number) => schedulerWaitMock(delay), - } + }, })) const script: CKBComponents.Script = { args: '0x403f0d4e833b2a8d372772a63facaa310dfeef92', codeHash: '0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8', - hashType: 'type' + hashType: 'type', } const scriptHash = scriptToHash(script) const address = 'ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsq2q8ux5aqem92xnwfmj5cl6e233phlwlysqhjx5w' @@ -135,7 +137,7 @@ describe('test light connector', () => { const syncProgress = SyncProgress.fromObject({ script, scriptType: 'lock', - walletId: 'walletId1' + walletId: 'walletId1', }) syncProgress.blockStartNumber = 0 syncProgress.blockEndNumber = 1 @@ -152,11 +154,14 @@ describe('test light connector', () => { script: { codeHash: syncProgress.codeHash, hashType: syncProgress.hashType, - args: syncProgress.args + args: syncProgress.args, }, - blockRange: [BI.from(syncProgress.blockStartNumber).toHexString(), BI.from(syncProgress.blockEndNumber).toHexString()], + blockRange: [ + BI.from(syncProgress.blockStartNumber).toHexString(), + BI.from(syncProgress.blockEndNumber).toHexString(), + ], scriptType: syncProgress.scriptType, - cursor: syncProgress.cursor + cursor: syncProgress.cursor, }) }) it('some script sync cursor is not empty but is in sync queue', async () => { @@ -164,7 +169,7 @@ describe('test light connector', () => { const syncProgress = SyncProgress.fromObject({ script, scriptType: 'lock', - walletId: 'walletId1' + walletId: 'walletId1', }) syncProgress.blockStartNumber = 0 syncProgress.blockEndNumber = 1 @@ -183,15 +188,17 @@ describe('test light connector', () => { expect(connector.syncQueue.length()).toBe(0) }) it('some script sync to new block', async () => { - getScriptsMock.mockResolvedValue([{ - script, - scriptType: 'lock', - blockNumber: '0xaa' - }]) + getScriptsMock.mockResolvedValue([ + { + script, + scriptType: 'lock', + blockNumber: '0xaa', + }, + ]) const syncProgress = SyncProgress.fromObject({ script, scriptType: 'lock', - walletId: 'walletId1' + walletId: 'walletId1', }) syncProgress.blockStartNumber = 0 syncProgress.blockEndNumber = 1 @@ -207,39 +214,41 @@ describe('test light connector', () => { script: { codeHash: syncProgress.codeHash, hashType: syncProgress.hashType, - args: syncProgress.args + args: syncProgress.args, }, blockRange: [BI.from(syncProgress.blockEndNumber).toHexString(), BI.from('0xaa').toHexString()], scriptType: syncProgress.scriptType, - cursor: syncProgress.cursor + cursor: syncProgress.cursor, }) }), - it('some script sync to new block but is in sync queue', async () => { - getScriptsMock.mockResolvedValue([{ - script, - scriptType: 'lock', - blockNumber: '0xaa' - }]) - const syncProgress = SyncProgress.fromObject({ - script, - scriptType: 'lock', - walletId: 'walletId1' + it('some script sync to new block but is in sync queue', async () => { + getScriptsMock.mockResolvedValue([ + { + script, + scriptType: 'lock', + blockNumber: '0xaa', + }, + ]) + const syncProgress = SyncProgress.fromObject({ + script, + scriptType: 'lock', + walletId: 'walletId1', + }) + syncProgress.blockStartNumber = 0 + syncProgress.blockEndNumber = 1 + getAllSyncStatusToMapMock.mockResolvedValue(new Map([[scriptHash, syncProgress]])) + const connector = new LightConnector([], '') + // @ts-ignore: private-method + connector.syncInQueue.set(scriptHash, {}) + // @ts-ignore: private-method + connector.subscribeSync = jest.fn() + // @ts-ignore: private-method + connector.syncQueue.pause() + // @ts-ignore: private-method + await connector.synchronize() + // @ts-ignore: private-method + expect(connector.syncQueue.length()).toBe(0) }) - syncProgress.blockStartNumber = 0 - syncProgress.blockEndNumber = 1 - getAllSyncStatusToMapMock.mockResolvedValue(new Map([[scriptHash, syncProgress]])) - const connector = new LightConnector([], '') - // @ts-ignore: private-method - connector.syncInQueue.set(scriptHash, {}) - // @ts-ignore: private-method - connector.subscribeSync = jest.fn() - // @ts-ignore: private-method - connector.syncQueue.pause() - // @ts-ignore: private-method - await connector.synchronize() - // @ts-ignore: private-method - expect(connector.syncQueue.length()).toBe(0) - }) }) describe('test subscribeSync', () => { @@ -254,7 +263,7 @@ describe('test light connector', () => { // @ts-ignore: private-method expect(connector.blockTipsSubject.next).toBeCalledWith({ cacheTipNumber: 100, - indexerTipNumber: 170 + indexerTipNumber: 170, }) }) }) @@ -273,19 +282,41 @@ describe('test light connector', () => { await connect.initSyncProgress([{ walletId: 'walletId', script, addressType: 1, scriptType: 'lock' }]) expect(getScriptsMock).toBeCalledTimes(1) expect(setScriptsMock).toBeCalledWith([ - { script, scriptType: 'lock', walletId: 'walletId', blockNumber: '0x0', addressType: 1, }, + { script, scriptType: 'lock', walletId: 'walletId', blockNumber: '0x0', addressType: 1 }, ]) }) it('there is not exist sync scripts with light client', async () => { getScriptsMock.mockResolvedValue([{ script, blockNumber: '0xaa' }]) - const addressMeta = AddressMeta.fromObject({ walletId: 'walletId', address, path: '', addressIndex: 10, addressType: 0, blake160: script.args }) + const addressMeta = AddressMeta.fromObject({ + walletId: 'walletId', + address, + path: '', + addressIndex: 10, + addressType: 0, + blake160: script.args, + }) const connect = new LightConnector([addressMeta], '') //@ts-ignore await connect.initSyncProgress() expect(setScriptsMock).toBeCalledWith([ - { script: addressMeta.generateDefaultLockScript().toSDK(), scriptType: 'lock', walletId: 'walletId', blockNumber: '0xaa' }, - { script: addressMeta.generateACPLockScript().toSDK(), scriptType: 'lock', walletId: 'walletId', blockNumber: '0x0' }, - { script: addressMeta.generateLegacyACPLockScript().toSDK(), scriptType: 'lock', walletId: 'walletId', blockNumber: '0x0' }, + { + script: addressMeta.generateDefaultLockScript().toSDK(), + scriptType: 'lock', + walletId: 'walletId', + blockNumber: '0xaa', + }, + { + script: addressMeta.generateACPLockScript().toSDK(), + scriptType: 'lock', + walletId: 'walletId', + blockNumber: '0x0', + }, + { + script: addressMeta.generateLegacyACPLockScript().toSDK(), + scriptType: 'lock', + walletId: 'walletId', + blockNumber: '0x0', + }, ]) expect(resetSyncProgressMock).toBeCalledWith([ { script: addressMeta.generateDefaultLockScript().toSDK(), scriptType: 'lock', walletId: 'walletId' }, @@ -296,29 +327,73 @@ describe('test light connector', () => { }) it('set new script with the synced min block number', async () => { getScriptsMock.mockResolvedValue([]) - const addressMeta = AddressMeta.fromObject({ walletId: 'walletId', address, path: '', addressIndex: 10, addressType: 0, blake160: script.args }) - getWalletMinBlockNumberMock.mockResolvedValue({ 'walletId': 170}) + const addressMeta = AddressMeta.fromObject({ + walletId: 'walletId', + address, + path: '', + addressIndex: 10, + addressType: 0, + blake160: script.args, + }) + getWalletMinBlockNumberMock.mockResolvedValue({ walletId: 170 }) const connect = new LightConnector([addressMeta], '') //@ts-ignore await connect.initSyncProgress() expect(setScriptsMock).toBeCalledWith([ - { script: addressMeta.generateDefaultLockScript().toSDK(), scriptType: 'lock', walletId: 'walletId', blockNumber: '0xaa' }, - { script: addressMeta.generateACPLockScript().toSDK(), scriptType: 'lock', walletId: 'walletId', blockNumber: '0xaa' }, - { script: addressMeta.generateLegacyACPLockScript().toSDK(), scriptType: 'lock', walletId: 'walletId', blockNumber: '0xaa' }, + { + script: addressMeta.generateDefaultLockScript().toSDK(), + scriptType: 'lock', + walletId: 'walletId', + blockNumber: '0xaa', + }, + { + script: addressMeta.generateACPLockScript().toSDK(), + scriptType: 'lock', + walletId: 'walletId', + blockNumber: '0xaa', + }, + { + script: addressMeta.generateLegacyACPLockScript().toSDK(), + scriptType: 'lock', + walletId: 'walletId', + blockNumber: '0xaa', + }, ]) }) it('set new script with start block number in wallet', async () => { getScriptsMock.mockResolvedValue([]) - const addressMeta = AddressMeta.fromObject({ walletId: 'walletId', address, path: '', addressIndex: 10, addressType: 0, blake160: script.args }) + const addressMeta = AddressMeta.fromObject({ + walletId: 'walletId', + address, + path: '', + addressIndex: 10, + addressType: 0, + blake160: script.args, + }) getWalletMinBlockNumberMock.mockResolvedValue({}) walletGetAllMock.mockReturnValue([{ id: 'walletId', startBlockNumberInLight: '0xaa' }]) const connect = new LightConnector([addressMeta], '') //@ts-ignore await connect.initSyncProgress() expect(setScriptsMock).toBeCalledWith([ - { script: addressMeta.generateDefaultLockScript().toSDK(), scriptType: 'lock', walletId: 'walletId', blockNumber: '0xaa' }, - { script: addressMeta.generateACPLockScript().toSDK(), scriptType: 'lock', walletId: 'walletId', blockNumber: '0xaa' }, - { script: addressMeta.generateLegacyACPLockScript().toSDK(), scriptType: 'lock', walletId: 'walletId', blockNumber: '0xaa' }, + { + script: addressMeta.generateDefaultLockScript().toSDK(), + scriptType: 'lock', + walletId: 'walletId', + blockNumber: '0xaa', + }, + { + script: addressMeta.generateACPLockScript().toSDK(), + scriptType: 'lock', + walletId: 'walletId', + blockNumber: '0xaa', + }, + { + script: addressMeta.generateLegacyACPLockScript().toSDK(), + scriptType: 'lock', + walletId: 'walletId', + blockNumber: '0xaa', + }, ]) }) }) @@ -362,8 +437,17 @@ describe('test light connector', () => { const connect = new LightConnector([], '') //@ts-ignore await connect.syncNextWithScript({ script, scriptType: 'lock', blockRange: ['0xaa', '0xbb'] }) - expect(getTransactionsMock).toBeCalledWith({ script, blockRange: ['0xaa', '0xbb'], scriptType: 'lock' }, 'asc', '0x64', undefined) - expect(updateSyncStatusMock).toBeCalledWith(scriptHash, { blockStartNumber: 187, blockEndNumber: 187, cursor: undefined }) + expect(getTransactionsMock).toBeCalledWith( + { script, blockRange: ['0xaa', '0xbb'], scriptType: 'lock' }, + 'asc', + '0x64', + undefined + ) + expect(updateSyncStatusMock).toBeCalledWith(scriptHash, { + blockStartNumber: 187, + blockEndNumber: 187, + cursor: undefined, + }) }) it('there are some txs in blockRange but no more', async () => { const syncProgress = SyncProgress.fromObject({ script, scriptType: 'lock', walletId: 'walletId' }) @@ -421,7 +505,9 @@ describe('test light connector', () => { const connect = new LightConnector([], '') //@ts-ignore connect.initSync = mockFn - mockFn.mockImplementation(() => { throw new Error('error') }) + mockFn.mockImplementation(() => { + throw new Error('error') + }) expect(connect.connect()).rejects.toThrowError(new Error('error')) }) }) @@ -438,7 +524,7 @@ describe('test light connector', () => { }) describe('test notifyCurrentBlockNumberProcessed', () => { - it ('hash is not in syncInQueue', async () => { + it('hash is not in syncInQueue', async () => { const connect = new LightConnector([], '') const mockFn = jest.fn() //@ts-ignore @@ -447,7 +533,7 @@ describe('test light connector', () => { expect(updateSyncStatusMock).toBeCalledTimes(0) expect(mockFn).toBeCalledTimes(1) }) - it ('hash is in syncInQueue', async () => { + it('hash is in syncInQueue', async () => { const connect = new LightConnector([], '') //@ts-ignore connect.subscribeSync = jest.fn() diff --git a/packages/neuron-wallet/tests/services/cells.test.ts b/packages/neuron-wallet/tests/services/cells.test.ts index c0bfd83e37..a617a0eba5 100644 --- a/packages/neuron-wallet/tests/services/cells.test.ts +++ b/packages/neuron-wallet/tests/services/cells.test.ts @@ -1145,8 +1145,14 @@ describe('CellsService', () => { lockScript: multiSignLockScript, } - const receiverChequeLock = assetAccountInfo.generateChequeScript(bobDefaultLock.computeHash(), bytes.hexify(Buffer.alloc(20))) - const senderChequeLock = assetAccountInfo.generateChequeScript(bytes.hexify(Buffer.alloc(20)), bobDefaultLock.computeHash()) + const receiverChequeLock = assetAccountInfo.generateChequeScript( + bobDefaultLock.computeHash(), + bytes.hexify(Buffer.alloc(20)) + ) + const senderChequeLock = assetAccountInfo.generateChequeScript( + bytes.hexify(Buffer.alloc(20)), + bobDefaultLock.computeHash() + ) const acpLock = assetAccountInfo.generateAnyoneCanPayScript('0x') const sudtType = new Script(assetAccountInfo.getSudtCodeHash(), '0x', ScriptHashType.Type) diff --git a/packages/neuron-wallet/tests/services/tx/transaction-generator.test.ts b/packages/neuron-wallet/tests/services/tx/transaction-generator.test.ts index 11184887b9..0e99769455 100644 --- a/packages/neuron-wallet/tests/services/tx/transaction-generator.test.ts +++ b/packages/neuron-wallet/tests/services/tx/transaction-generator.test.ts @@ -2369,7 +2369,10 @@ describe('TransactionGenerator', () => { senderDefaultLockInputEntity = createInput(senderDefaultLock, undefined, transaction.hash) - const chequeLock = assetAccountInfo.generateChequeScript(bytes.hexify(Buffer.alloc(20)), senderDefaultLock.computeHash()) + const chequeLock = assetAccountInfo.generateChequeScript( + bytes.hexify(Buffer.alloc(20)), + senderDefaultLock.computeHash() + ) chequeOutputEntity = createOutput( chequeLock, typeScript, @@ -2811,12 +2814,12 @@ describe('TransactionGenerator', () => { describe('CKB account', () => { it('capacity not enough for fee', async () => { const input = createInput(alice.lockScript, undefined, '0x' + '0'.repeat(64)) - const asssetAccountInput = input.toModel() - asssetAccountInput.capacity = toShannon('61') + const assetAccountInput = input.toModel() + assetAccountInput.capacity = toShannon('61') await expect( TransactionGenerator.generateDestroyAssetAccountTx( 'walletId', - [asssetAccountInput], + [assetAccountInput], bob.publicKeyInBlake160, true ) @@ -2824,13 +2827,13 @@ describe('TransactionGenerator', () => { }) it('account capacity not enough for fee need other address', async () => { const input = createInput(alice.lockScript, undefined, '0x' + '0'.repeat(64)) - const asssetAccountInput = input.toModel() - asssetAccountInput.capacity = toShannon('61') + const assetAccountInput = input.toModel() + assetAccountInput.capacity = toShannon('61') const cell: OutputEntity = generateCell(toShannon('62'), OutputStatus.Live, false, null, alice) await getConnection().manager.save(cell) const res = await TransactionGenerator.generateDestroyAssetAccountTx( alice.walletId, - [asssetAccountInput], + [assetAccountInput], bob.publicKeyInBlake160, true ) @@ -2841,11 +2844,11 @@ describe('TransactionGenerator', () => { }) it('account capacity enough for fee', async () => { const input = createInput(alice.lockScript, undefined, '0x' + '0'.repeat(64)) - const asssetAccountInput = input.toModel() - asssetAccountInput.capacity = toShannon('62') + const assetAccountInput = input.toModel() + assetAccountInput.capacity = toShannon('62') const res = await TransactionGenerator.generateDestroyAssetAccountTx( alice.walletId, - [asssetAccountInput], + [assetAccountInput], bob.publicKeyInBlake160, true ) @@ -2861,13 +2864,13 @@ describe('TransactionGenerator', () => { ScriptHashType.Type ) const input = createInput(alice.lockScript, typeScript, '0x' + '0'.repeat(64)) - const asssetAccountInput = input.toModel() - asssetAccountInput.capacity = toShannon('142') - asssetAccountInput.data = BufferUtils.writeBigUInt128LE(BigInt('10')) + const assetAccountInput = input.toModel() + assetAccountInput.capacity = toShannon('142') + assetAccountInput.data = BufferUtils.writeBigUInt128LE(BigInt('10')) await expect( TransactionGenerator.generateDestroyAssetAccountTx( alice.walletId, - [asssetAccountInput], + [assetAccountInput], bob.publicKeyInBlake160, false ) @@ -2880,11 +2883,11 @@ describe('TransactionGenerator', () => { ScriptHashType.Type ) const input = createInput(alice.lockScript, typeScript, '0x' + '0'.repeat(64)) - const asssetAccountInput = input.toModel() - asssetAccountInput.capacity = toShannon('142') + const assetAccountInput = input.toModel() + assetAccountInput.capacity = toShannon('142') const res = await TransactionGenerator.generateDestroyAssetAccountTx( alice.walletId, - [asssetAccountInput], + [assetAccountInput], bob.publicKeyInBlake160, false ) diff --git a/packages/neuron-wallet/tests/services/tx/transaction-sender.test.ts b/packages/neuron-wallet/tests/services/tx/transaction-sender.test.ts index eaa0deb119..e750b13ce4 100644 --- a/packages/neuron-wallet/tests/services/tx/transaction-sender.test.ts +++ b/packages/neuron-wallet/tests/services/tx/transaction-sender.test.ts @@ -1,4 +1,4 @@ -import { bytes } from "@ckb-lumos/codec"; +import { bytes } from '@ckb-lumos/codec' const stubbedRPCServiceConstructor = jest.fn() const stubbedWalletsServiceConstructor = jest.fn() @@ -133,7 +133,7 @@ jest.doMock('services/hardware', () => ({ })) jest.doMock('@nervosnetwork/ckb-sdk-core', () => { - return function() { + return function () { return { calculateDaoMaximumWithdraw: stubbedCalculateDaoMaximumWithdraw, } @@ -143,13 +143,13 @@ jest.doMock('@nervosnetwork/ckb-sdk-core', () => { jest.doMock('utils/ckb-rpc.ts', () => ({ generateRPC() { return { - sendTransaction: stubbedSendTransaction + sendTransaction: stubbedSendTransaction, } - } + }, })) jest.doMock('services/cells', () => ({ - getLiveCell: stubbedGetLiveCell + getLiveCell: stubbedGetLiveCell, })) import Transaction from '../../../src/models/chain/transaction' @@ -454,7 +454,10 @@ describe('TransactionSender Test', () => { }) describe('when matched receiver lock hash', () => { beforeEach(() => { - const chequeLock = assetAccountInfo.generateChequeScript(receiverDefaultLock.computeHash(), bytes.hexify(Buffer.alloc(20))) + const chequeLock = assetAccountInfo.generateChequeScript( + receiverDefaultLock.computeHash(), + bytes.hexify(Buffer.alloc(20)) + ) tx.inputs[0].lock = chequeLock }) it('success', async () => { @@ -466,7 +469,10 @@ describe('TransactionSender Test', () => { }) describe('when not matched receiver lock hash', () => { beforeEach(() => { - const chequeLock = assetAccountInfo.generateChequeScript(bytes.hexify(Buffer.alloc(20)), bytes.hexify(Buffer.alloc(20))) + const chequeLock = assetAccountInfo.generateChequeScript( + bytes.hexify(Buffer.alloc(20)), + bytes.hexify(Buffer.alloc(20)) + ) tx.inputs[0].lock = chequeLock }) it('throws', async () => { @@ -504,7 +510,10 @@ describe('TransactionSender Test', () => { }) describe('when matched sender lock hash', () => { beforeEach(() => { - const chequeLock = assetAccountInfo.generateChequeScript(bytes.hexify(Buffer.alloc(20)), senderDefaultLock.computeHash()) + const chequeLock = assetAccountInfo.generateChequeScript( + bytes.hexify(Buffer.alloc(20)), + senderDefaultLock.computeHash() + ) tx.inputs[0].lock = chequeLock }) it('success', async () => { @@ -515,7 +524,10 @@ describe('TransactionSender Test', () => { }) describe('when not matched sender lock hash', () => { beforeEach(() => { - const chequeLock = assetAccountInfo.generateChequeScript(bytes.hexify(Buffer.alloc(20)), bytes.hexify(Buffer.alloc(20))) + const chequeLock = assetAccountInfo.generateChequeScript( + bytes.hexify(Buffer.alloc(20)), + bytes.hexify(Buffer.alloc(20)) + ) tx.inputs[0].lock = chequeLock }) it('throws', async () => {