Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: remove models/script and repalce with lumos #2762

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import RpcService from '../../services/rpc-service'
import TransactionWithStatus from '../../models/chain/transaction-with-status'
import SyncInfoEntity from '../../database/chain/entities/sync-info'
import { TransactionCollector, CellCollector, Indexer as CkbIndexer } from '@ckb-lumos/ckb-indexer'
import { utils } from '@ckb-lumos/base'

export default class IndexerCacheService {
private addressMetas: AddressMeta[]
Expand Down Expand Up @@ -104,7 +105,7 @@ export default class IndexerCacheService {
mappingsByTxHash.set(txHash, [
{
address: addressMeta.address,
lockHash: lockScript.computeHash(),
lockHash: utils.computeScriptHash(lockScript),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can keep using the class model in Neuron to keep the code pattern

},
])
}
Expand Down Expand Up @@ -138,7 +139,7 @@ export default class IndexerCacheService {
mappingsByTxHash.set(txHash, [
{
address: addressMeta.address,
lockHash: lockScript.computeHash(),
lockHash: utils.computeScriptHash(lockScript),
},
])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export default class LightConnector extends Connector<CKBComponents.Hash> {
addressMeta.generateLegacyACPLockScript(),
]
return lockScripts.map(v => ({
script: v.toSDK(),
script: v,
scriptType: 'lock' as CKBRPC.ScriptType,
walletId: addressMeta.walletId,
}))
Expand Down
8 changes: 5 additions & 3 deletions packages/neuron-wallet/src/block-sync-renderer/sync/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { AppendScript, BlockTips, Connector } from './connector'
import LightConnector from './light-connector'
import { generateRPC } from '../../utils/ckb-rpc'
import { BUNDLED_LIGHT_CKB_URL } from '../../utils/const'
import { utils } from '@ckb-lumos/base'

export default class Queue {
#lockHashes: string[]
Expand Down Expand Up @@ -50,13 +51,13 @@ export default class Queue {
.map(blake160 => [
blake160,
Multisig.hash([blake160]),
SystemScriptInfo.generateSecpScript(blake160).computeHash().slice(0, 42),
utils.computeScriptHash(SystemScriptInfo.generateSecpScript(blake160)).slice(0, 42),
])
.flat()
)
this.#multiSignBlake160s = blake160s.map(blake160 => Multisig.hash([blake160]))
this.#anyoneCanPayLockHashes = blake160s.map(b =>
this.#assetAccountInfo.generateAnyoneCanPayScript(b).computeHash()
utils.computeScriptHash(this.#assetAccountInfo.generateAnyoneCanPayScript(b))
)
}

Expand Down Expand Up @@ -217,7 +218,8 @@ export default class Queue {
input.setInputIndex(inputIndex.toString())

if (
previousOutput.type?.computeHash() === SystemScriptInfo.DAO_SCRIPT_HASH &&
previousOutput.type &&
utils.computeScriptHash(previousOutput.type) === SystemScriptInfo.getInstance().getDaoScriptHash() &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we should refactor the SystemScriptInfo.DAO_SCRIPT_HASH here?

previousTx.outputsData![+input.previousOutput!.index] === '0x0000000000000000'
) {
const output = tx.outputs![inputIndex]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default class TxAddressFinder {
}
if (this.lockHashes.has(output.lockHash!)) {
if (output.type) {
if (output.typeHash === SystemScriptInfo.DAO_SCRIPT_HASH) {
if (output.typeHash === SystemScriptInfo.getInstance().getDaoScriptHash()) {
this.tx.outputs![index].setDaoData(this.tx.outputsData![index])
}
}
Expand Down
17 changes: 10 additions & 7 deletions packages/neuron-wallet/src/controllers/sudt.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { scriptToHash } from '@nervosnetwork/ckb-sdk-utils'
import LiveCellService from '../services/live-cell-service'
import AssetAccountInfo from '../models/asset-account-info'
import Script, { ScriptHashType } from '../models/chain/script'
import parseSUDTTokenInfo from '../utils/parse_sudt_token_info'
import { ResponseCode } from '../utils/const'
import { SudtTokenInfo } from '../models/chain/transaction'
import AssetAccountService from '../services/asset-account-service'
import { Script, utils } from '@ckb-lumos/base'

export default class SUDTController {
public async getSUDTTokenInfo(params: { tokenID: string }): Promise<Controller.Response<SudtTokenInfo>> {
Expand All @@ -18,11 +17,11 @@ export default class SUDTController {
}
}

const typeScript = Script.fromObject({
const typeScript: Script = {
codeHash: new AssetAccountInfo().sudtInfoCodeHash,
args: params.tokenID,
hashType: ScriptHashType.Type,
})
hashType: 'type',
}
const liveCell = await LiveCellService.getInstance().getOneByLockScriptAndTypeScript(null, typeScript)
if (!liveCell) {
return {
Expand All @@ -38,10 +37,14 @@ export default class SUDTController {

public getSUDTTypeScriptHash(params: { tokenID: string }): Controller.Response<string> {
const assetAccount = new AssetAccountInfo()
const script = new Script(assetAccount.infos.sudt.codeHash, params.tokenID, assetAccount.infos.sudt.hashType)
const script: Script = {
codeHash: assetAccount.sudtInfoCodeHash,
args: params.tokenID,
hashType: assetAccount.infos.sudt.hashType,
}
return {
status: ResponseCode.Success,
result: scriptToHash(script.toSDK()),
result: utils.computeScriptHash(script),
}
}
}
4 changes: 2 additions & 2 deletions packages/neuron-wallet/src/database/address/meta.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Address, AddressVersion } from '../../models/address'
import { AddressType } from '../../models/keys/address'
import Script from '../../models/chain/script'
import SystemScriptInfo from '../../models/system-script-info'
import AssetAccountInfo from '../../models/asset-account-info'
import HdPublicKeyInfoModel from '../../models/keys/hd-public-key-info'
import Multisig from '../../models/multisig'
import { Script, utils } from '@ckb-lumos/base'

export default class AddressMeta implements Address {
walletId: string
Expand Down Expand Up @@ -116,6 +116,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(utils.computeScriptHash(defaultLockScript), '0'.repeat(40))
}
}
18 changes: 13 additions & 5 deletions packages/neuron-wallet/src/database/chain/entities/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Entity, BaseEntity, Column, ManyToOne, PrimaryGeneratedColumn } from 't
import Transaction from './transaction'
import OutPoint from '../../../models/chain/out-point'
import InputModel from '../../../models/chain/input'
import Script, { ScriptHashType } from '../../../models/chain/script'
import { HashType, Script } from '@ckb-lumos/base'

// cellbase input may have same OutPoint
@Entity()
Expand Down Expand Up @@ -51,7 +51,7 @@ export default class Input extends BaseEntity {
type: 'varchar',
nullable: true,
})
lockHashType: ScriptHashType | null = null
lockHashType: HashType | null = null

@Column({
type: 'varchar',
Expand All @@ -69,7 +69,7 @@ export default class Input extends BaseEntity {
type: 'varchar',
nullable: true,
})
typeHashType: ScriptHashType | null = null
typeHashType: HashType | null = null

@Column({
type: 'varchar',
Expand Down Expand Up @@ -119,14 +119,22 @@ export default class Input extends BaseEntity {

public lockScript(): Script | undefined {
if (this.lockCodeHash && this.lockArgs && this.lockHashType) {
return new Script(this.lockCodeHash, this.lockArgs, this.lockHashType)
return {
codeHash: this.lockCodeHash,
args: this.lockArgs,
hashType: this.lockHashType,
}
}
return undefined
}

public typeScript(): Script | undefined {
if (this.typeCodeHash && this.typeArgs && this.typeHashType) {
return new Script(this.typeCodeHash, this.typeArgs, this.typeHashType)
return {
codeHash: this.typeCodeHash,
args: this.typeArgs,
hashType: this.typeHashType,
}
}
return undefined
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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 { OutputStatus } from '../../../models/chain/output'
import { HashType, Script } from '@ckb-lumos/base'

@Entity()
export default class MultisigOutput extends BaseEntity {
Expand Down Expand Up @@ -39,7 +39,7 @@ export default class MultisigOutput extends BaseEntity {
@Column({
type: 'varchar',
})
lockHashType!: ScriptHashType
lockHashType!: HashType

@Column({
type: 'varchar',
Expand All @@ -56,7 +56,11 @@ export default class MultisigOutput extends BaseEntity {
}

public lockScript(): Script {
return new Script(this.lockCodeHash, this.lockArgs, this.lockHashType)
return {
codeHash: this.lockCodeHash,
args: this.lockArgs,
hashType: this.lockHashType as HashType,
}
}

public static fromIndexer(params: {
Expand All @@ -70,7 +74,7 @@ export default class MultisigOutput extends BaseEntity {
entity.capacity = BigInt(params.output.capacity).toString()
entity.lockArgs = params.output.lock.args
entity.lockCodeHash = params.output.lock.code_hash
entity.lockHashType = params.output.lock.hash_type as ScriptHashType
entity.lockHashType = params.output.lock.hash_type as HashType
entity.lockHash = scriptToHash({
args: entity.lockArgs,
codeHash: entity.lockCodeHash,
Expand Down
18 changes: 13 additions & 5 deletions packages/neuron-wallet/src/database/chain/entities/output.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Entity, BaseEntity, Column, PrimaryColumn, ManyToOne } from 'typeorm'
import TransactionEntity from './transaction'
import Script, { ScriptHashType } from '../../../models/chain/script'
import OutPoint from '../../../models/chain/out-point'
import OutputModel, { OutputStatus } from '../../../models/chain/output'
import { HashType, Script } from '@ckb-lumos/base'

@Entity()
export default class Output extends BaseEntity {
Expand Down Expand Up @@ -34,7 +34,7 @@ export default class Output extends BaseEntity {
@Column({
type: 'varchar',
})
lockHashType!: ScriptHashType
lockHashType!: HashType

@Column({
type: 'varchar',
Expand Down Expand Up @@ -62,7 +62,7 @@ export default class Output extends BaseEntity {
type: 'varchar',
nullable: true,
})
typeHashType: ScriptHashType | null = null
typeHashType: HashType | null = null

@Column({
type: 'varchar',
Expand Down Expand Up @@ -118,12 +118,20 @@ export default class Output extends BaseEntity {
}

public lockScript(): Script {
return new Script(this.lockCodeHash, this.lockArgs, this.lockHashType)
return {
codeHash: this.lockCodeHash,
args: this.lockArgs,
hashType: this.lockHashType as HashType,
}
}

public typeScript(): Script | undefined {
if (this.typeCodeHash && this.typeArgs && this.typeHashType) {
return new Script(this.typeCodeHash, this.typeArgs, this.typeHashType)
return {
codeHash: this.typeCodeHash,
args: this.typeArgs,
hashType: this.typeHashType as HashType,
}
}
return undefined
}
Expand Down
4 changes: 2 additions & 2 deletions packages/neuron-wallet/src/database/chain/meta-info.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import FileService from '../../services/file'
import OutPoint from '../../models/chain/out-point'
import { ScriptHashType } from '../../models/chain/script'
import { HashType } from '@ckb-lumos/base'

const moduleName = 'cells'
const fileName = 'meta-info.json'

export interface SystemScript {
codeHash: string
outPoint: OutPoint
hashType: ScriptHashType
hashType: HashType
}

export interface MetaInfo {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { In, MigrationInterface, QueryRunner } from "typeorm";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe I should not edit this migration file, but it does not build if I don't change it, how should it be?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe changing the type will not have a side effect.

import { scriptToHash } from '@nervosnetwork/ckb-sdk-utils'
import { ScriptHashType } from "../../../models/chain/script";
import Output from "../entities/output";
import { HashType } from "@ckb-lumos/base";

export class UpdateOutputChequeLockHash1652945662504 implements MigrationInterface {
name = 'UpdateOutputChequeLockHash1652945662504'
Expand All @@ -11,12 +11,12 @@ export class UpdateOutputChequeLockHash1652945662504 implements MigrationInterfa
scriptToHash({
args: '0x' + '0'.repeat(80),
codeHash: process.env.TESTNET_CHEQUE_SCRIPT_CODEHASH!,
hashType: process.env.TESTNET_CHEQUE_SCRIPT_HASHTYPE! as ScriptHashType
hashType: process.env.TESTNET_CHEQUE_SCRIPT_HASHTYPE! as HashType
}),
scriptToHash({
args: '0x' + '0'.repeat(80),
codeHash: process.env.MAINNET_CHEQUE_SCRIPT_CODEHASH!,
hashType: process.env.MAINNET_CHEQUE_SCRIPT_HASHTYPE! as ScriptHashType
hashType: process.env.MAINNET_CHEQUE_SCRIPT_HASHTYPE! as HashType
})
]
const chequeOutput = await queryRunner.connection
Expand Down
10 changes: 7 additions & 3 deletions packages/neuron-wallet/src/models/address-parser.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import Script, { ScriptHashType } from './chain/script'
import { addressToScript } from '@nervosnetwork/ckb-sdk-utils'
import SystemScriptInfo from './system-script-info'
import { Script, utils } from '@ckb-lumos/base'

export default class AddressParser {
public static parse(address: string): Script {
try {
const script = addressToScript(address)
return new Script(script.codeHash, script.args, script.hashType as ScriptHashType)
return {
codeHash: script.codeHash,
args: script.args,
hashType: script.hashType,
}
} catch {
throw new Error('Address format error')
}
Expand All @@ -17,7 +21,7 @@ export default class AddressParser {
}

public static batchToLockHash(addresses: string[]): string[] {
return this.batchParse(addresses).map(lock => lock.computeHash())
return this.batchParse(addresses).map(lock => utils.computeScriptHash(lock))
}

public static toBlake160(address: string) {
Expand Down
Loading