From 243a93c2eb16f2b7329ad7e69381aca03ba7d679 Mon Sep 17 00:00:00 2001 From: zhangyouxin Date: Thu, 10 Aug 2023 14:00:57 +0800 Subject: [PATCH 1/6] refactor: use lumos to calculate script hash --- .../src/block-sync-renderer/sync/light-connector.ts | 2 +- packages/neuron-wallet/src/controllers/sudt.ts | 2 +- .../src/database/chain/entities/multisig-output.ts | 2 +- .../src/database/chain/entities/sync-progress.ts | 2 +- .../migrations/1652945662504-UpdateOutputChequeLockHash.ts | 4 +++- packages/neuron-wallet/src/models/chain/script.ts | 2 +- packages/neuron-wallet/src/models/multisig-config.ts | 2 +- packages/neuron-wallet/src/services/multisig.ts | 2 +- packages/neuron-wallet/src/services/sync-progress.ts | 2 +- packages/neuron-wallet/src/utils/multisig.ts | 2 +- .../tests/block-sync-renderer/light-connector.test.ts | 3 ++- packages/neuron-wallet/tests/controllers/sudt.test.ts | 5 +++-- packages/neuron-wallet/tests/services/cells.test.ts | 6 +++--- packages/neuron-wallet/tests/services/multisig.test.ts | 3 ++- .../tests/services/tx/transaction-generator.test.ts | 2 +- .../tests/services/tx/transaction-service.test.ts | 6 +++--- 16 files changed, 26 insertions(+), 21 deletions(-) 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 7978388fb9..313c3b9414 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 @@ -9,7 +9,7 @@ import AddressMeta from '../../database/address/meta' import { scheduler } from 'timers/promises' import SyncProgressService from '../../services/sync-progress' import { BlockTips, LumosCellQuery, Connector, AppendScript } from './connector' -import { scriptToHash } from '@nervosnetwork/ckb-sdk-utils' +import { computeScriptHash as scriptToHash } from '@ckb-lumos/base/lib/utils' import { FetchTransactionReturnType, LightRPC, LightScriptFilter } from '../../utils/ckb-rpc' import Multisig from '../../services/multisig' import { SyncAddressType } from '../../database/chain/entities/sync-progress' diff --git a/packages/neuron-wallet/src/controllers/sudt.ts b/packages/neuron-wallet/src/controllers/sudt.ts index 2a227a48e8..508385cb11 100644 --- a/packages/neuron-wallet/src/controllers/sudt.ts +++ b/packages/neuron-wallet/src/controllers/sudt.ts @@ -1,4 +1,4 @@ -import { scriptToHash } from '@nervosnetwork/ckb-sdk-utils' +import { computeScriptHash as scriptToHash } from '@ckb-lumos/base/lib/utils' import LiveCellService from '../services/live-cell-service' import AssetAccountInfo from '../models/asset-account-info' import Script, { ScriptHashType } from '../models/chain/script' diff --git a/packages/neuron-wallet/src/database/chain/entities/multisig-output.ts b/packages/neuron-wallet/src/database/chain/entities/multisig-output.ts index deeaf9f9ba..628d93f827 100644 --- a/packages/neuron-wallet/src/database/chain/entities/multisig-output.ts +++ b/packages/neuron-wallet/src/database/chain/entities/multisig-output.ts @@ -1,7 +1,7 @@ import { Entity, BaseEntity, Column, PrimaryColumn } from 'typeorm' import Script, { ScriptHashType } from '../../../models/chain/script' import OutPoint from '../../../models/chain/out-point' -import { scriptToHash } from '@nervosnetwork/ckb-sdk-utils' +import { computeScriptHash as scriptToHash } from '@ckb-lumos/base/lib/utils' import { OutputStatus } from '../../../models/chain/output' @Entity() diff --git a/packages/neuron-wallet/src/database/chain/entities/sync-progress.ts b/packages/neuron-wallet/src/database/chain/entities/sync-progress.ts index 33ec2f8118..65d786a503 100644 --- a/packages/neuron-wallet/src/database/chain/entities/sync-progress.ts +++ b/packages/neuron-wallet/src/database/chain/entities/sync-progress.ts @@ -1,5 +1,5 @@ import { HexString } from '@ckb-lumos/base' -import { scriptToHash } from '@nervosnetwork/ckb-sdk-utils' +import { computeScriptHash as scriptToHash } from '@ckb-lumos/base/lib/utils' import { Entity, PrimaryColumn, Column } from 'typeorm' export enum SyncAddressType { diff --git a/packages/neuron-wallet/src/database/chain/migrations/1652945662504-UpdateOutputChequeLockHash.ts b/packages/neuron-wallet/src/database/chain/migrations/1652945662504-UpdateOutputChequeLockHash.ts index 9d4d306ad8..01d8d3a719 100644 --- a/packages/neuron-wallet/src/database/chain/migrations/1652945662504-UpdateOutputChequeLockHash.ts +++ b/packages/neuron-wallet/src/database/chain/migrations/1652945662504-UpdateOutputChequeLockHash.ts @@ -1,8 +1,10 @@ import { In, MigrationInterface, QueryRunner } from "typeorm"; -import { scriptToHash } from '@nervosnetwork/ckb-sdk-utils' +import { computeScriptHash as scriptToHash } from '@ckb-lumos/base/lib/utils' import { ScriptHashType } from "../../../models/chain/script"; import Output from "../entities/output"; + + export class UpdateOutputChequeLockHash1652945662504 implements MigrationInterface { name = 'UpdateOutputChequeLockHash1652945662504' diff --git a/packages/neuron-wallet/src/models/chain/script.ts b/packages/neuron-wallet/src/models/chain/script.ts index 609b8802c0..02f3f5211f 100644 --- a/packages/neuron-wallet/src/models/chain/script.ts +++ b/packages/neuron-wallet/src/models/chain/script.ts @@ -1,4 +1,4 @@ -import { scriptToHash } from '@nervosnetwork/ckb-sdk-utils' +import { computeScriptHash as scriptToHash } from '@ckb-lumos/base/lib/utils' import { bytes as byteUtils } from '@ckb-lumos/codec' import TypeChecker from '../../utils/type-checker' diff --git a/packages/neuron-wallet/src/models/multisig-config.ts b/packages/neuron-wallet/src/models/multisig-config.ts index 69cbfd9fa2..5c2258b7e5 100644 --- a/packages/neuron-wallet/src/models/multisig-config.ts +++ b/packages/neuron-wallet/src/models/multisig-config.ts @@ -1,4 +1,4 @@ -import { scriptToHash } from '@nervosnetwork/ckb-sdk-utils' +import { computeScriptHash as scriptToHash } from '@ckb-lumos/base/lib/utils' import Multisig from './multisig' export default class MultisigConfigModel { diff --git a/packages/neuron-wallet/src/services/multisig.ts b/packages/neuron-wallet/src/services/multisig.ts index 64e08a260e..40397f617a 100644 --- a/packages/neuron-wallet/src/services/multisig.ts +++ b/packages/neuron-wallet/src/services/multisig.ts @@ -3,7 +3,7 @@ import MultisigConfig from '../database/chain/entities/multisig-config' import MultisigOutput from '../database/chain/entities/multisig-output' import { MultisigConfigNotExistError, MultisigConfigExistError } from '../exceptions/multisig' import { rpcBatchRequest } from '../utils/rpc-request' -import { scriptToHash } from '@nervosnetwork/ckb-sdk-utils' +import { computeScriptHash as scriptToHash } from '@ckb-lumos/base/lib/utils' import MultisigOutputChangedSubject from '../models/subjects/multisig-output-db-changed-subject' import Transaction from '../models/chain/transaction' import { OutputStatus } from '../models/chain/output' diff --git a/packages/neuron-wallet/src/services/sync-progress.ts b/packages/neuron-wallet/src/services/sync-progress.ts index ae93cb54dc..806004f75f 100644 --- a/packages/neuron-wallet/src/services/sync-progress.ts +++ b/packages/neuron-wallet/src/services/sync-progress.ts @@ -1,5 +1,5 @@ import { Equal, getConnection, In, Not } from 'typeorm' -import { scriptToHash } from '@nervosnetwork/ckb-sdk-utils' +import { computeScriptHash as scriptToHash } from '@ckb-lumos/base/lib/utils' import { HexString } from '@ckb-lumos/base' import SyncProgress, { SyncAddressType } from '../database/chain/entities/sync-progress' import WalletService from './wallets' diff --git a/packages/neuron-wallet/src/utils/multisig.ts b/packages/neuron-wallet/src/utils/multisig.ts index a31c710219..4ef4e7c058 100644 --- a/packages/neuron-wallet/src/utils/multisig.ts +++ b/packages/neuron-wallet/src/utils/multisig.ts @@ -1,4 +1,4 @@ -import { scriptToHash } from '@nervosnetwork/ckb-sdk-utils' +import { computeScriptHash as scriptToHash } from '@ckb-lumos/base/lib/utils' import Multisig from '../models/multisig' import MultisigConfigModel from '../models/multisig-config' import { Signatures, SignStatus } from '../models/offline-sign' 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 413af2a71c..55eb4a3b54 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,9 +1,10 @@ -import { scriptToHash } from '@nervosnetwork/ckb-sdk-utils' +import { computeScriptHash as scriptToHash } from '@ckb-lumos/base/lib/utils' import LightConnector from '../../src/block-sync-renderer/sync/light-connector' import SyncProgress from '../../src/database/chain/entities/sync-progress' import { BI } from '@ckb-lumos/bi' import AddressMeta from '../../src/database/address/meta' + const getSyncStatusMock = jest.fn() const getCurrentWalletMinBlockNumberMock = jest.fn() const getAllSyncStatusToMapMock = jest.fn() diff --git a/packages/neuron-wallet/tests/controllers/sudt.test.ts b/packages/neuron-wallet/tests/controllers/sudt.test.ts index 44ab0f3347..f079c52175 100644 --- a/packages/neuron-wallet/tests/controllers/sudt.test.ts +++ b/packages/neuron-wallet/tests/controllers/sudt.test.ts @@ -63,8 +63,9 @@ describe('SUDTController', () => { 'txHash', '0', '10000', - Script.fromObject({ codeHash: '', args: '', hashType: ScriptHashType.Type }), - Script.fromObject({ codeHash: '', args: '', hashType: ScriptHashType.Type }), + // codeHash and args are required for Script + Script.fromObject({ codeHash: `0x${'00'.repeat(32)}`, args: '0x', hashType: ScriptHashType.Type }), + Script.fromObject({ codeHash: `0x${'00'.repeat(32)}`, args: '0x', hashType: ScriptHashType.Type }), '0x080a456972632d320a455432' ) beforeEach(async () => { diff --git a/packages/neuron-wallet/tests/services/cells.test.ts b/packages/neuron-wallet/tests/services/cells.test.ts index a617a0eba5..40b0e6c27d 100644 --- a/packages/neuron-wallet/tests/services/cells.test.ts +++ b/packages/neuron-wallet/tests/services/cells.test.ts @@ -1091,9 +1091,9 @@ describe('CellsService', () => { }) describe('#usedByAnyoneCanPayBlake160s', () => { - const fakeArgs1 = '0x1' - const fakeArgs2 = '0x2' - const fakeArgs3 = '0x3' + const fakeArgs1 = '0x01' + const fakeArgs2 = '0x02' + const fakeArgs3 = '0x03' const codeHash = randomHex() const lockScript1 = new Script(codeHash, fakeArgs1, ScriptHashType.Type) const lockScript2 = new Script(codeHash, fakeArgs2, ScriptHashType.Type) diff --git a/packages/neuron-wallet/tests/services/multisig.test.ts b/packages/neuron-wallet/tests/services/multisig.test.ts index 5972057641..6cd9a204b8 100644 --- a/packages/neuron-wallet/tests/services/multisig.test.ts +++ b/packages/neuron-wallet/tests/services/multisig.test.ts @@ -8,7 +8,8 @@ import { OutputStatus } from '../../src/models/chain/output' import { keyInfos } from '../setupAndTeardown/public-key-info.fixture' import Multisig from '../../src/models/multisig' import SystemScriptInfo from '../../src/models/system-script-info' -import { scriptToHash } from '@nervosnetwork/ckb-sdk-utils' +import { computeScriptHash as scriptToHash } from '@ckb-lumos/base/lib/utils' + const [alice, bob, charlie] = keyInfos 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 0e99769455..da0a9fe68f 100644 --- a/packages/neuron-wallet/tests/services/tx/transaction-generator.test.ts +++ b/packages/neuron-wallet/tests/services/tx/transaction-generator.test.ts @@ -1196,7 +1196,7 @@ describe('TransactionGenerator', () => { const targetOutput: Output = Output.fromObject({ capacity: toShannon('61'), lock: aliceAnyoneCanPayLockScript, - type: assetAccountInfo.generateSudtScript('0xuuid'), + type: assetAccountInfo.generateSudtScript('0x1234'), data: '0x', }) diff --git a/packages/neuron-wallet/tests/services/tx/transaction-service.test.ts b/packages/neuron-wallet/tests/services/tx/transaction-service.test.ts index 6915efe39e..bd6e1c338c 100644 --- a/packages/neuron-wallet/tests/services/tx/transaction-service.test.ts +++ b/packages/neuron-wallet/tests/services/tx/transaction-service.test.ts @@ -606,7 +606,7 @@ describe('Test TransactionService', () => { lock: { codeHash: `0x${'0'.repeat(64)}`, hashType: 'data', - args: '0x0' + args: '0x00' }, } ] @@ -633,12 +633,12 @@ describe('Test TransactionService', () => { lock: { codeHash: `0x${'0'.repeat(64)}`, hashType: 'data', - args: '0x0' + args: '0x00' }, type: { codeHash: `0x${'1'.repeat(64)}`, hashType: 'data', - args: '0x1' + args: '0x01' } } ] From 876bb60a1db9653781cf0891569df323a5a1a357 Mon Sep 17 00:00:00 2001 From: zhangyouxin Date: Fri, 11 Aug 2023 13:08:22 +0800 Subject: [PATCH 2/6] chore: prettier --- .../light-connector.test.ts | 1 - .../tests/services/multisig.test.ts | 41 ++++++------- .../services/tx/transaction-service.test.ts | 60 ++++++++++--------- 3 files changed, 52 insertions(+), 50 deletions(-) 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 55eb4a3b54..ef27cccea5 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 @@ -4,7 +4,6 @@ import SyncProgress from '../../src/database/chain/entities/sync-progress' import { BI } from '@ckb-lumos/bi' import AddressMeta from '../../src/database/address/meta' - const getSyncStatusMock = jest.fn() const getCurrentWalletMinBlockNumberMock = jest.fn() const getAllSyncStatusToMapMock = jest.fn() diff --git a/packages/neuron-wallet/tests/services/multisig.test.ts b/packages/neuron-wallet/tests/services/multisig.test.ts index 6cd9a204b8..67c459c820 100644 --- a/packages/neuron-wallet/tests/services/multisig.test.ts +++ b/packages/neuron-wallet/tests/services/multisig.test.ts @@ -10,7 +10,6 @@ import Multisig from '../../src/models/multisig' import SystemScriptInfo from '../../src/models/system-script-info' import { computeScriptHash as scriptToHash } from '@ckb-lumos/base/lib/utils' - const [alice, bob, charlie] = keyInfos const rpcBatchRequestMock = jest.fn() @@ -196,13 +195,11 @@ describe('multisig service', () => { describe('removeDulpicateConfig', () => { it('exist duplicate config', () => { - const multisigConfigModel = new MultisigConfigModel( - 'walletId', - 1, - 2, - 3, - [alice.publicKeyInBlake160, bob.publicKeyInBlake160, charlie.publicKeyInBlake160], - ) + const multisigConfigModel = new MultisigConfigModel('walletId', 1, 2, 3, [ + alice.publicKeyInBlake160, + bob.publicKeyInBlake160, + charlie.publicKeyInBlake160, + ]) const multisigConfigs = [ MultisigConfig.fromModel(multisigConfigModel), MultisigConfig.fromModel(multisigConfigModel), @@ -213,20 +210,20 @@ describe('multisig service', () => { }) it('non-exist duplicate config', () => { const multisigConfigs = [ - MultisigConfig.fromModel(new MultisigConfigModel( - 'walletId', - 1, - 2, - 3, - [alice.publicKeyInBlake160, bob.publicKeyInBlake160, charlie.publicKeyInBlake160], - )), - MultisigConfig.fromModel(new MultisigConfigModel( - 'walletId', - 2, - 2, - 3, - [alice.publicKeyInBlake160, bob.publicKeyInBlake160, charlie.publicKeyInBlake160], - )), + MultisigConfig.fromModel( + new MultisigConfigModel('walletId', 1, 2, 3, [ + alice.publicKeyInBlake160, + bob.publicKeyInBlake160, + charlie.publicKeyInBlake160, + ]) + ), + MultisigConfig.fromModel( + new MultisigConfigModel('walletId', 2, 2, 3, [ + alice.publicKeyInBlake160, + bob.publicKeyInBlake160, + charlie.publicKeyInBlake160, + ]) + ), ] //@ts-ignore private-method const res = MultisigService.removeDulpicateConfig(multisigConfigs) diff --git a/packages/neuron-wallet/tests/services/tx/transaction-service.test.ts b/packages/neuron-wallet/tests/services/tx/transaction-service.test.ts index bd6e1c338c..3142a8a58d 100644 --- a/packages/neuron-wallet/tests/services/tx/transaction-service.test.ts +++ b/packages/neuron-wallet/tests/services/tx/transaction-service.test.ts @@ -1,4 +1,4 @@ - import os from 'os' +import os from 'os' import fs from 'fs' import path from 'path' import TransactionService, { SearchType } from '../../../src/services/tx/transaction-service' @@ -25,9 +25,9 @@ jest.mock('../../../src/models/asset-account-info', () => { const getTransactionMock = jest.fn() jest.mock('../../../src/services/rpc-service', () => { - return function() { + return function () { return { - getTransaction: getTransactionMock + getTransaction: getTransactionMock, } } }) @@ -35,15 +35,15 @@ jest.mock('../../../src/services/rpc-service', () => { const ckbRpcExecMock = jest.fn() jest.mock('@nervosnetwork/ckb-sdk-core', () => { - return function() { + return function () { return { rpc: { createBatchRequest() { return { - exec: ckbRpcExecMock + exec: ckbRpcExecMock, } - } - } + }, + }, } } }) @@ -286,7 +286,9 @@ describe('Test TransactionService', () => { it('Get input and outputs from rpc', async () => { getTransactionMock.mockResolvedValue({ transaction: Transaction.fromObject(transactions[0]) }) - ckbRpcExecMock.mockResolvedValue([{ transaction: { outputs: [{ capacity: '0x100', lock: transactions[0].inputs[0].lock } ]} }]) + ckbRpcExecMock.mockResolvedValue([ + { transaction: { outputs: [{ capacity: '0x100', lock: transactions[0].inputs[0].lock }] } }, + ]) const actual = await TransactionService.get(stubProvider.hash) expect(actual).not.toBeUndefined() expect(actual?.inputs.length).toBe(transactions[0].inputs.length) @@ -472,7 +474,11 @@ describe('Test TransactionService', () => { const args = `0x${'0'.repeat(42)}` const script = SystemScriptInfo.generateSecpScript(args) tx.inputs[0].setLock(script) - await TransactionPersistor.convertTransactionAndSave(tx, TxSaveType.Fetch, new Set([tx.outputs[0].lock.args, tx.outputs[1].lock.args])) + await TransactionPersistor.convertTransactionAndSave( + tx, + TxSaveType.Fetch, + new Set([tx.outputs[0].lock.args, tx.outputs[1].lock.args]) + ) const actual = await TransactionService.getAllByAddresses(stubProvider, scriptToAddress(script)) expect(actual.totalCount).toBe(1) }) @@ -574,8 +580,8 @@ describe('Test TransactionService', () => { it('inputs without txHash', async () => { const inputs = [ Input.fromObject({ - previousOutput: null - }) + previousOutput: null, + }), ] //@ts-ignore private-method const actual = await TransactionService.fillInputFields(inputs) @@ -584,8 +590,8 @@ describe('Test TransactionService', () => { it('can not get output', async () => { const inputs = [ Input.fromObject({ - previousOutput: new OutPoint(`0x${'0'.repeat(64)}`, '0x0') - }) + previousOutput: new OutPoint(`0x${'0'.repeat(64)}`, '0x0'), + }), ] ckbRpcExecMock.mockResolvedValueOnce([]) //@ts-ignore private-method @@ -595,8 +601,8 @@ describe('Test TransactionService', () => { it('success fill input fields without type', async () => { const inputs = [ Input.fromObject({ - previousOutput: new OutPoint(`0x${'0'.repeat(64)}`, '0x0') - }) + previousOutput: new OutPoint(`0x${'0'.repeat(64)}`, '0x0'), + }), ] const transactionWithStatus = { transaction: { @@ -606,11 +612,11 @@ describe('Test TransactionService', () => { lock: { codeHash: `0x${'0'.repeat(64)}`, hashType: 'data', - args: '0x00' + args: '0x00', }, - } - ] - } + }, + ], + }, } ckbRpcExecMock.mockResolvedValueOnce([transactionWithStatus]) //@ts-ignore private-method @@ -622,8 +628,8 @@ describe('Test TransactionService', () => { it('success fill input fields with type', async () => { const inputs = [ Input.fromObject({ - previousOutput: new OutPoint(`0x${'0'.repeat(64)}`, '0x0') - }) + previousOutput: new OutPoint(`0x${'0'.repeat(64)}`, '0x0'), + }), ] const transactionWithStatus = { transaction: { @@ -633,16 +639,16 @@ describe('Test TransactionService', () => { lock: { codeHash: `0x${'0'.repeat(64)}`, hashType: 'data', - args: '0x00' + args: '0x00', }, type: { codeHash: `0x${'1'.repeat(64)}`, hashType: 'data', - args: '0x01' - } - } - ] - } + args: '0x01', + }, + }, + ], + }, } ckbRpcExecMock.mockResolvedValueOnce([transactionWithStatus]) //@ts-ignore private-method From 82b0ed1fad42f09075962c9abbd2bc0d19a4fce1 Mon Sep 17 00:00:00 2001 From: zhangyouxin Date: Fri, 11 Aug 2023 13:41:46 +0800 Subject: [PATCH 3/6] fix: unit tests --- .../neuron-wallet/src/models/chain/script.ts | 12 ++++- .../tests/block-sync-renderer/queue.test.ts | 53 ++++++++++++------- 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/packages/neuron-wallet/src/models/chain/script.ts b/packages/neuron-wallet/src/models/chain/script.ts index 02f3f5211f..6b908bd2a0 100644 --- a/packages/neuron-wallet/src/models/chain/script.ts +++ b/packages/neuron-wallet/src/models/chain/script.ts @@ -34,7 +34,17 @@ export default class Script { } public computeHash(): string { - return scriptToHash(this.toSDK()) + const script = this.toSDK() + if (!script) { + return '' + } + // empty string is not allowed here + const formatedScript = { + args: script.args || '0x', + codeHash: script.codeHash || '0x', + hashType: script.hashType || '0x', + } + return scriptToHash(formatedScript) } /** diff --git a/packages/neuron-wallet/tests/block-sync-renderer/queue.test.ts b/packages/neuron-wallet/tests/block-sync-renderer/queue.test.ts index c510304219..7163715695 100644 --- a/packages/neuron-wallet/tests/block-sync-renderer/queue.test.ts +++ b/packages/neuron-wallet/tests/block-sync-renderer/queue.test.ts @@ -101,7 +101,7 @@ describe('queue', () => { const fakeWalletId = 'w1' const addressInfo: Address = { address, - blake160: '0xfakeblake160', + blake160: `0x${'0'.repeat(40)}`, walletId: fakeWalletId, path: '', addressType: AddressType.Receiving, @@ -123,7 +123,7 @@ describe('queue', () => { jest.useFakeTimers('legacy') stubbedBlockTipsSubject = new Subject() - stubbedTransactionsSubject = new Subject<{ txHashes: CKBComponents.Hash[], params: unknown }>() + stubbedTransactionsSubject = new Subject<{ txHashes: CKBComponents.Hash[]; params: unknown }>() const stubbedIndexerConnector = jest.fn().mockImplementation((...args) => { stubbedIndexerConnectorConstructor(...args) return { @@ -175,11 +175,11 @@ describe('queue', () => { return { createBatchRequest() { return { - exec: stubbedRPCCreateBatchRequestExecFn + exec: stubbedRPCCreateBatchRequestExecFn, } - } + }, } - } + }, } }) const Queue = require('../../src/block-sync-renderer/sync/queue').default @@ -223,10 +223,17 @@ describe('queue', () => { beforeEach(() => { stubbedAddressesFn.mockResolvedValue([true, addresses.map(addressMeta => addressMeta.address), []]) stubbedGetTransactionFn.mockResolvedValue(fakeTxWithStatus1) - stubbedRPCCreateBatchRequestExecFn - .mockResolvedValueOnce(fakeTxs) - .mockResolvedValueOnce(fakeTxs.map(v => ({ ...fakeBlockHeader, timestamp: v.transaction.timestamp, number: v.transaction.blockNumber }))) - stubbedTransactionsSubject.next({ txHashes: fakeTxs.map(v => v.transaction.hash), params: fakeTxs[0].transaction.blockNumber }) + stubbedRPCCreateBatchRequestExecFn.mockResolvedValueOnce(fakeTxs).mockResolvedValueOnce( + fakeTxs.map(v => ({ + ...fakeBlockHeader, + timestamp: v.transaction.timestamp, + number: v.transaction.blockNumber, + })) + ) + stubbedTransactionsSubject.next({ + txHashes: fakeTxs.map(v => v.transaction.hash), + params: fakeTxs[0].transaction.blockNumber, + }) }) describe('when saving transactions is succeeded', () => { beforeEach(flushPromises) @@ -250,11 +257,14 @@ describe('queue', () => { tx.blockHash = fakeTxWithStatus2.txStatus.blockHash! tx.blockNumber = BigInt(fakeTxWithStatus2.transaction.blockNumber!).toString() tx.timestamp = BigInt(fakeTxWithStatus2.transaction.timestamp!).toString() - expect(stubbedSaveFetchFn).toHaveBeenCalledWith(tx, new Set([ - addressInfo.blake160, - Multisig.hash([addressInfo.blake160]), - SystemScriptInfo.generateSecpScript(addressInfo.blake160).computeHash().slice(0, 42), - ])) + expect(stubbedSaveFetchFn).toHaveBeenCalledWith( + tx, + new Set([ + addressInfo.blake160, + Multisig.hash([addressInfo.blake160]), + SystemScriptInfo.generateSecpScript(addressInfo.blake160).computeHash().slice(0, 42), + ]) + ) } }) it('checks and generate new addresses', () => { @@ -276,10 +286,17 @@ describe('queue', () => { const err = new Error() beforeEach(async () => { stubbedSaveFetchFn.mockRejectedValueOnce(err) - stubbedRPCCreateBatchRequestExecFn - .mockResolvedValueOnce(fakeTxs) - .mockResolvedValueOnce(fakeTxs.map(v => ({ ...fakeBlockHeader, timestamp: v.transaction.timestamp, number: v.transaction.blockNumber }))) - stubbedTransactionsSubject.next({ txHashes: fakeTxs.map(v => v.transaction.hash), params: fakeTxs[0].transaction.blockNumber }) + stubbedRPCCreateBatchRequestExecFn.mockResolvedValueOnce(fakeTxs).mockResolvedValueOnce( + fakeTxs.map(v => ({ + ...fakeBlockHeader, + timestamp: v.transaction.timestamp, + number: v.transaction.blockNumber, + })) + ) + stubbedTransactionsSubject.next({ + txHashes: fakeTxs.map(v => v.transaction.hash), + params: fakeTxs[0].transaction.blockNumber, + }) await flushPromises() }) it('handles the exception', async () => { From 7f74f20c0b1396e25a988796f38eea41eb9f773d Mon Sep 17 00:00:00 2001 From: zhangyouxin Date: Fri, 11 Aug 2023 13:42:56 +0800 Subject: [PATCH 4/6] chore: typo --- packages/neuron-wallet/src/models/chain/script.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/neuron-wallet/src/models/chain/script.ts b/packages/neuron-wallet/src/models/chain/script.ts index 6b908bd2a0..c0c7d14012 100644 --- a/packages/neuron-wallet/src/models/chain/script.ts +++ b/packages/neuron-wallet/src/models/chain/script.ts @@ -39,12 +39,12 @@ export default class Script { return '' } // empty string is not allowed here - const formatedScript = { + const formattedScript = { args: script.args || '0x', codeHash: script.codeHash || '0x', hashType: script.hashType || '0x', } - return scriptToHash(formatedScript) + return scriptToHash(formattedScript) } /** From 1995df0c4d28b669fa4d7fa12a496bb8f59dfd98 Mon Sep 17 00:00:00 2001 From: zhangyouxin Date: Fri, 11 Aug 2023 14:06:37 +0800 Subject: [PATCH 5/6] fix: unit tests --- packages/neuron-wallet/src/models/chain/script.ts | 8 ++++---- packages/neuron-wallet/tests/controllers/multisig.test.ts | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/neuron-wallet/src/models/chain/script.ts b/packages/neuron-wallet/src/models/chain/script.ts index c0c7d14012..f8ae60cdac 100644 --- a/packages/neuron-wallet/src/models/chain/script.ts +++ b/packages/neuron-wallet/src/models/chain/script.ts @@ -35,14 +35,14 @@ export default class Script { public computeHash(): string { const script = this.toSDK() - if (!script) { + // TODO: should throw error, skip calculate hash if parameter not enough (only in test environment) + if (!script || !script.codeHash || !script.hashType) { return '' } - // empty string is not allowed here + // empty string is not allowed for args const formattedScript = { + ...script, args: script.args || '0x', - codeHash: script.codeHash || '0x', - hashType: script.hashType || '0x', } return scriptToHash(formattedScript) } diff --git a/packages/neuron-wallet/tests/controllers/multisig.test.ts b/packages/neuron-wallet/tests/controllers/multisig.test.ts index be977b1c88..bc2691f19d 100644 --- a/packages/neuron-wallet/tests/controllers/multisig.test.ts +++ b/packages/neuron-wallet/tests/controllers/multisig.test.ts @@ -23,9 +23,9 @@ jest.mock('services/wallets', () => ({ return { getCurrent() { return jest.fn() - } + }, } - } + }, })) jest.mock('../../src/services/multisig') From 563ef4cb4037d56721d6b1dc2954e427d40518d8 Mon Sep 17 00:00:00 2001 From: zhangyouxin Date: Tue, 15 Aug 2023 18:51:10 +0800 Subject: [PATCH 6/6] chore: throw error when script is invalid --- packages/neuron-wallet/src/models/chain/script.ts | 3 +-- packages/neuron-wallet/src/models/system-script-info.ts | 5 +++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/neuron-wallet/src/models/chain/script.ts b/packages/neuron-wallet/src/models/chain/script.ts index f8ae60cdac..a83305a59a 100644 --- a/packages/neuron-wallet/src/models/chain/script.ts +++ b/packages/neuron-wallet/src/models/chain/script.ts @@ -35,9 +35,8 @@ export default class Script { public computeHash(): string { const script = this.toSDK() - // TODO: should throw error, skip calculate hash if parameter not enough (only in test environment) if (!script || !script.codeHash || !script.hashType) { - return '' + throw new Error('Invalid script') } // empty string is not allowed for args const formattedScript = { diff --git a/packages/neuron-wallet/src/models/system-script-info.ts b/packages/neuron-wallet/src/models/system-script-info.ts index b1a282f02a..a2ef2251af 100644 --- a/packages/neuron-wallet/src/models/system-script-info.ts +++ b/packages/neuron-wallet/src/models/system-script-info.ts @@ -3,6 +3,7 @@ import CellDep, { DepType } from './chain/cell-dep' import NetworksService from '../services/networks' import RpcService from '../services/rpc-service' import Script, { ScriptHashType } from './chain/script' +import { systemScripts } from '@nervosnetwork/ckb-sdk-utils' export default class SystemScriptInfo { static SECP_CODE_HASH = process.env.SECP256K1_CODE_HASH! @@ -14,9 +15,9 @@ export default class SystemScriptInfo { static MULTI_SIGN_HASH_TYPE = ScriptHashType.Type static DAO_SCRIPT_HASH = new Script( - SystemScriptInfo.DAO_CODE_HASH, + systemScripts.NERVOS_DAO.codeHash, '0x', - SystemScriptInfo.DAO_HASH_TYPE + systemScripts.NERVOS_DAO.hashType as ScriptHashType ).computeHash() private static instance: SystemScriptInfo