From 51518cfe4bae258d211ae7b0391ec1949f9d900f Mon Sep 17 00:00:00 2001 From: Shinya Date: Fri, 25 Aug 2023 10:19:24 +0800 Subject: [PATCH] refactor: use lumos to calculate script hash (#2802) --- .../sync/light-connector.ts | 2 +- .../neuron-wallet/src/controllers/sudt.ts | 2 +- .../chain/entities/multisig-output.ts | 2 +- .../database/chain/entities/sync-progress.ts | 2 +- ...652945662504-UpdateOutputChequeLockHash.ts | 4 +- .../neuron-wallet/src/models/chain/script.ts | 13 +++++- .../src/models/multisig-config.ts | 2 +- .../src/models/system-script-info.ts | 5 ++- .../neuron-wallet/src/services/multisig.ts | 2 +- .../src/services/sync-progress.ts | 2 +- packages/neuron-wallet/src/utils/multisig.ts | 2 +- .../light-connector.test.ts | 2 +- .../tests/block-sync-renderer/queue.test.ts | 2 +- .../tests/controllers/sudt.test.ts | 5 ++- .../tests/services/cells.test.ts | 6 +-- .../tests/services/multisig.test.ts | 42 +++++++++---------- .../services/tx/transaction-generator.test.ts | 2 +- .../services/tx/transaction-service.test.ts | 6 +-- 18 files changed, 57 insertions(+), 46 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..a83305a59a 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' @@ -34,7 +34,16 @@ export default class Script { } public computeHash(): string { - return scriptToHash(this.toSDK()) + const script = this.toSDK() + if (!script || !script.codeHash || !script.hashType) { + throw new Error('Invalid script') + } + // empty string is not allowed for args + const formattedScript = { + ...script, + args: script.args || '0x', + } + return scriptToHash(formattedScript) } /** 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/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 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..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 @@ -1,4 +1,4 @@ -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' 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 b2b4d297d6..83d2580dec 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, 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 e78c55540d..c78edf09df 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..67c459c820 100644 --- a/packages/neuron-wallet/tests/services/multisig.test.ts +++ b/packages/neuron-wallet/tests/services/multisig.test.ts @@ -8,7 +8,7 @@ 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 @@ -195,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), @@ -212,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-generator.test.ts b/packages/neuron-wallet/tests/services/tx/transaction-generator.test.ts index a8f84e38ef..b797b6daed 100644 --- a/packages/neuron-wallet/tests/services/tx/transaction-generator.test.ts +++ b/packages/neuron-wallet/tests/services/tx/transaction-generator.test.ts @@ -1197,7 +1197,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 402c308961..f198f60abe 100644 --- a/packages/neuron-wallet/tests/services/tx/transaction-service.test.ts +++ b/packages/neuron-wallet/tests/services/tx/transaction-service.test.ts @@ -612,7 +612,7 @@ describe('Test TransactionService', () => { lock: { codeHash: `0x${'0'.repeat(64)}`, hashType: 'data', - args: '0x0', + args: '0x00', }, }, ], @@ -639,12 +639,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', }, }, ],