Skip to content

Commit 6ca8200

Browse files
committed
feat: bump sdk to v0.19.0 in neuron-wallet
1 parent 6c2c618 commit 6ca8200

16 files changed

Lines changed: 225 additions & 115 deletions

File tree

packages/neuron-wallet/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
]
3535
},
3636
"dependencies": {
37-
"@nervosnetwork/ckb-sdk-core": "0.18.0",
38-
"@nervosnetwork/ckb-sdk-utils": "0.18.0",
37+
"@nervosnetwork/ckb-sdk-core": "0.19.0",
38+
"@nervosnetwork/ckb-sdk-utils": "0.19.0",
3939
"bn.js": "4.11.8",
4040
"chalk": "2.4.2",
4141
"electron-log": "3.0.7",
@@ -51,7 +51,7 @@
5151
"uuid": "3.3.2"
5252
},
5353
"devDependencies": {
54-
"@nervosnetwork/ckb-types": "0.18.0",
54+
"@nervosnetwork/ckb-types": "0.19.0",
5555
"@types/electron-devtools-installer": "2.2.0",
5656
"@types/elliptic": "6.4.8",
5757
"@types/sqlite3": "3.1.5",

packages/neuron-wallet/src/database/chain/entities/input.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Entity, BaseEntity, Column, ManyToOne, PrimaryGeneratedColumn } from 'typeorm'
2-
import { OutPoint, Input as InputInterface, CellOutPoint } from 'types/cell-types'
2+
import { OutPoint, Input as InputInterface } from 'types/cell-types'
33
import Transaction from './transaction'
44

55
/* eslint @typescript-eslint/no-unused-vars: "warn" */
@@ -42,7 +42,7 @@ export default class Input extends BaseEntity {
4242
})
4343
capacity: string | null = null
4444

45-
public cellOutPoint(): CellOutPoint | null {
45+
public previousOutput(): OutPoint | null {
4646
if (!this.outPointTxHash || !this.outPointIndex) {
4747
return null
4848
}
@@ -52,13 +52,6 @@ export default class Input extends BaseEntity {
5252
}
5353
}
5454

55-
public previousOutput(): OutPoint {
56-
return {
57-
blockHash: null,
58-
cell: this.cellOutPoint(),
59-
}
60-
}
61-
6255
public toInterface(): InputInterface {
6356
return {
6457
previousOutput: this.previousOutput(),

packages/neuron-wallet/src/database/chain/entities/output.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Entity, BaseEntity, Column, PrimaryColumn, ManyToOne } from 'typeorm'
2-
import { Script, OutPoint, Cell, CellOutPoint } from 'types/cell-types'
2+
import { Script, OutPoint, Cell } from 'types/cell-types'
33
import TransactionEntity from './transaction'
44

55
/* eslint @typescript-eslint/no-unused-vars: "warn" */
@@ -35,20 +35,13 @@ export default class Output extends BaseEntity {
3535
})
3636
status!: string
3737

38-
public cellOutPoint(): CellOutPoint {
38+
public outPoint(): OutPoint {
3939
return {
4040
txHash: this.outPointTxHash,
4141
index: this.outPointIndex,
4242
}
4343
}
4444

45-
public outPoint(): OutPoint {
46-
return {
47-
blockHash: null,
48-
cell: this.cellOutPoint(),
49-
}
50-
}
51-
5245
@ManyToOne(_type => TransactionEntity, transaction => transaction.outputs, { onDelete: 'CASCADE' })
5346
transaction!: TransactionEntity
5447

packages/neuron-wallet/src/database/chain/entities/transaction.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
AfterRemove,
1212
} from 'typeorm'
1313
import { remote } from 'electron'
14-
import { Witness, OutPoint, Transaction as TransactionInterface, TransactionStatus } from 'types/cell-types'
14+
import { Witness, Transaction as TransactionInterface, TransactionStatus, CellDep } from 'types/cell-types'
1515
import TxDbChangedSubject from 'models/subjects/tx-db-changed-subject'
1616
import InputEntity from './input'
1717
import OutputEntity from './output'
@@ -37,7 +37,12 @@ export default class Transaction extends BaseEntity {
3737
@Column({
3838
type: 'simple-json',
3939
})
40-
deps!: OutPoint[]
40+
cellDeps!: CellDep[]
41+
42+
@Column({
43+
type: 'simple-json',
44+
})
45+
headerDeps!: string[]
4146

4247
@Column({
4348
type: 'simple-json',
@@ -101,7 +106,8 @@ export default class Transaction extends BaseEntity {
101106
return {
102107
hash: this.hash,
103108
version: this.version,
104-
deps: this.deps,
109+
cellDeps: this.cellDeps,
110+
headerDeps: this.headerDeps,
105111
inputs,
106112
outputs,
107113
timestamp: this.timestamp,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { MigrationInterface, QueryRunner, TableColumn } from 'typeorm';
2+
3+
export class AlterDepsFromTransaction1566900661931 implements MigrationInterface {
4+
5+
public async up(queryRunner: QueryRunner): Promise<any> {
6+
await queryRunner.addColumn('transaction', new TableColumn({
7+
name: 'cellDeps',
8+
type: 'simple-json',
9+
default: [],
10+
}))
11+
12+
await queryRunner.addColumn('transaction', new TableColumn({
13+
name: 'headerDeps',
14+
type: 'simple-json',
15+
default: [],
16+
}))
17+
18+
await queryRunner.dropColumn('transaction', 'deps')
19+
}
20+
21+
public async down(queryRunner: QueryRunner): Promise<any> {
22+
await queryRunner.addColumn('transaction', new TableColumn({
23+
name: 'deps',
24+
type: 'simple-json',
25+
default: [],
26+
}))
27+
28+
await queryRunner.dropColumn('transaction', 'cellDeps')
29+
await queryRunner.dropColumn('transaction', 'headerDeps')
30+
}
31+
32+
}

packages/neuron-wallet/src/database/chain/ormconfig.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import SyncInfo from './entities/sync-info'
1313
import { InitMigration1561695143591 } from './migrations/1561695143591-InitMigration'
1414
import { AddStatusToTx1562038960990 } from './migrations/1562038960990-AddStatusToTx'
1515
import { AddConfirmed1565693320664 } from './migrations/1565693320664-AddConfirmed'
16+
import { AlterDepsFromTransaction1566900661931 } from './migrations/1566900661931-AlterDepsFromTransaction'
1617

1718
export const CONNECTION_NOT_FOUND_NAME = 'ConnectionNotFoundError'
1819

@@ -32,7 +33,12 @@ const connectOptions = async (genesisBlockHash: string): Promise<SqliteConnectio
3233
type: 'sqlite',
3334
database: dbPath(genesisBlockHash),
3435
entities: [Transaction, Input, Output, SyncInfo],
35-
migrations: [InitMigration1561695143591, AddStatusToTx1562038960990, AddConfirmed1565693320664],
36+
migrations: [
37+
InitMigration1561695143591,
38+
AddStatusToTx1562038960990,
39+
AddConfirmed1565693320664,
40+
AlterDepsFromTransaction1566900661931,
41+
],
3642
logging,
3743
}
3844
}

packages/neuron-wallet/src/models/lock-utils.ts

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import NodeService from 'services/node'
22
import { OutPoint, Script, ScriptHashType } from 'types/cell-types'
33
import env from 'env'
4-
import { SystemScriptSubject } from './subjects/system-script'
4+
import ConvertTo from 'types/convert-to'
5+
import { SystemScriptSubject } from 'models/subjects/system-script'
56

67
const { core } = NodeService.getInstance()
78

89
export interface SystemScript {
910
codeHash: string
1011
outPoint: OutPoint
12+
hashType: ScriptHashType
1113
}
1214

1315
const subscribed = (target: any, propertyName: string) => {
@@ -30,34 +32,27 @@ export default class LockUtils {
3032
return this.systemScriptInfo
3133
}
3234

33-
const systemCell = await core.loadSystemCell()
35+
const systemCell = await core.loadSecp256k1Dep()
3436
let { codeHash } = systemCell
35-
const { outPoint } = systemCell
36-
let { blockHash } = outPoint
37-
let { txHash } = outPoint.cell
38-
const { index } = outPoint.cell
37+
const { outPoint, hashType } = systemCell
38+
let { txHash } = outPoint
39+
const { index } = outPoint
3940

4041
if (!codeHash.startsWith('0x')) {
4142
codeHash = `0x${codeHash}`
4243
}
4344

44-
if (!blockHash.startsWith('0x')) {
45-
blockHash = `0x${blockHash}`
46-
}
47-
4845
if (!txHash.startsWith('0x')) {
4946
txHash = `0x${txHash}`
5047
}
5148

5249
const systemScriptInfo = {
5350
codeHash,
5451
outPoint: {
55-
blockHash,
56-
cell: {
57-
txHash,
58-
index,
59-
},
52+
txHash,
53+
index,
6054
},
55+
hashType: hashType as ScriptHashType,
6156
}
6257

6358
this.systemScriptInfo = systemScriptInfo
@@ -70,23 +65,18 @@ export default class LockUtils {
7065
SystemScriptSubject.next({ codeHash: info.codeHash })
7166
}
7267

73-
// use SDK lockScriptToHash
74-
static lockScriptToHash = (lock: Script) => {
75-
const codeHash: string = lock!.codeHash!
76-
const args: string[] = lock.args!
77-
const { hashType } = lock
78-
// TODO: should support ScriptHashType.Type in the future
79-
const lockHash: string = core.utils.lockScriptToHash({
80-
codeHash,
81-
args,
82-
hashType,
83-
})
84-
85-
if (lockHash.startsWith('0x')) {
86-
return lockHash
68+
static computeScriptHash = async (script: Script): Promise<string> => {
69+
const ckbScript: CKBComponents.Script = ConvertTo.toSdkScript(script)
70+
const hash: string = await (core.rpc as any).computeScriptHash(ckbScript)
71+
if (!hash.startsWith('0x')) {
72+
return `0x${hash}`
8773
}
74+
return hash
75+
}
8876

89-
return `0x${lockHash}`
77+
// use SDK lockScriptToHash
78+
static lockScriptToHash = async (lock: Script) => {
79+
return LockUtils.computeScriptHash(lock)
9080
}
9181

9282
static async addressToLockScript(address: string, hashType: ScriptHashType = ScriptHashType.Data): Promise<Script> {

packages/neuron-wallet/src/services/cells.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,11 @@ export default class CellsService {
3636
}
3737

3838
private static getLiveCellEntity = async (outPoint: OutPoint): Promise<OutputEntity | undefined> => {
39-
const cell = outPoint.cell!
4039
const cellEntity: OutputEntity | undefined = await getConnection()
4140
.getRepository(OutputEntity)
4241
.findOne({
43-
outPointTxHash: cell.txHash,
44-
outPointIndex: cell.index,
42+
outPointTxHash: outPoint.txHash,
43+
outPointIndex: outPoint.index,
4544
status: 'live',
4645
})
4746

packages/neuron-wallet/src/services/sync/check-and-save/output.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@ export default class CheckOutput {
66

77
constructor(output: Cell) {
88
this.output = output
9-
this.calcLockHash()
9+
// this.calcLockHash()
1010
}
1111

12-
public calcLockHash = (): Cell => {
12+
public calcLockHash = async (): Promise<Cell> => {
1313
if (this.output.lockHash) {
1414
return this.output
1515
}
1616

17-
this.output.lockHash = LockUtils.lockScriptToHash(this.output.lock)
17+
this.output.lockHash = await LockUtils.lockScriptToHash(this.output.lock)
1818
return this.output
1919
}
2020

21-
public checkLockHash = (lockHashList: string[]): boolean | undefined => {
21+
public checkLockHash = async (lockHashList: string[]): Promise<boolean | undefined> => {
22+
await this.calcLockHash()
2223
return lockHashList.includes(this.output.lockHash!)
2324
}
2425
}

packages/neuron-wallet/src/services/sync/check-and-save/tx.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ export default class CheckTx {
4040
}
4141

4242
public filterOutputs = (lockHashes: string[]) => {
43-
return this.tx.outputs!.filter(output => {
43+
const cells: Cell[] = this.tx.outputs!.filter(async output => {
4444
const checkOutput = new CheckOutput(output)
45-
return checkOutput.checkLockHash(lockHashes)
45+
const result = await checkOutput.checkLockHash(lockHashes)
46+
return result
4647
})
48+
return cells
4749
}
4850

4951
/* eslint no-await-in-loop: "off" */
@@ -53,14 +55,13 @@ export default class CheckTx {
5355

5456
const addresses: string[] = []
5557
for (const input of inputs) {
56-
const outPoint: OutPoint = input.previousOutput
57-
const { cell } = outPoint
58-
if (cell) {
58+
const outPoint: OutPoint | null = input.previousOutput
59+
if (outPoint) {
5960
const output = await getConnection()
6061
.getRepository(OutputEntity)
6162
.findOne({
62-
outPointTxHash: cell.txHash,
63-
outPointIndex: cell.index,
63+
outPointTxHash: outPoint.txHash,
64+
outPointIndex: outPoint.index,
6465
})
6566
if (output && lockHashes.includes(output.lockHash)) {
6667
addresses.push(LockUtils.lockScriptToAddress(output.lock))

0 commit comments

Comments
 (0)