Skip to content

Commit

Permalink
feat: bump sdk to v0.19.0 in neuron-wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Aug 27, 2019
1 parent 6c2c618 commit 6ca8200
Show file tree
Hide file tree
Showing 16 changed files with 225 additions and 115 deletions.
6 changes: 3 additions & 3 deletions packages/neuron-wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
]
},
"dependencies": {
"@nervosnetwork/ckb-sdk-core": "0.18.0",
"@nervosnetwork/ckb-sdk-utils": "0.18.0",
"@nervosnetwork/ckb-sdk-core": "0.19.0",
"@nervosnetwork/ckb-sdk-utils": "0.19.0",
"bn.js": "4.11.8",
"chalk": "2.4.2",
"electron-log": "3.0.7",
Expand All @@ -51,7 +51,7 @@
"uuid": "3.3.2"
},
"devDependencies": {
"@nervosnetwork/ckb-types": "0.18.0",
"@nervosnetwork/ckb-types": "0.19.0",
"@types/electron-devtools-installer": "2.2.0",
"@types/elliptic": "6.4.8",
"@types/sqlite3": "3.1.5",
Expand Down
11 changes: 2 additions & 9 deletions packages/neuron-wallet/src/database/chain/entities/input.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Entity, BaseEntity, Column, ManyToOne, PrimaryGeneratedColumn } from 'typeorm'
import { OutPoint, Input as InputInterface, CellOutPoint } from 'types/cell-types'
import { OutPoint, Input as InputInterface } from 'types/cell-types'
import Transaction from './transaction'

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

public cellOutPoint(): CellOutPoint | null {
public previousOutput(): OutPoint | null {
if (!this.outPointTxHash || !this.outPointIndex) {
return null
}
Expand All @@ -52,13 +52,6 @@ export default class Input extends BaseEntity {
}
}

public previousOutput(): OutPoint {
return {
blockHash: null,
cell: this.cellOutPoint(),
}
}

public toInterface(): InputInterface {
return {
previousOutput: this.previousOutput(),
Expand Down
11 changes: 2 additions & 9 deletions packages/neuron-wallet/src/database/chain/entities/output.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Entity, BaseEntity, Column, PrimaryColumn, ManyToOne } from 'typeorm'
import { Script, OutPoint, Cell, CellOutPoint } from 'types/cell-types'
import { Script, OutPoint, Cell } from 'types/cell-types'
import TransactionEntity from './transaction'

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

public cellOutPoint(): CellOutPoint {
public outPoint(): OutPoint {
return {
txHash: this.outPointTxHash,
index: this.outPointIndex,
}
}

public outPoint(): OutPoint {
return {
blockHash: null,
cell: this.cellOutPoint(),
}
}

@ManyToOne(_type => TransactionEntity, transaction => transaction.outputs, { onDelete: 'CASCADE' })
transaction!: TransactionEntity

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
AfterRemove,
} from 'typeorm'
import { remote } from 'electron'
import { Witness, OutPoint, Transaction as TransactionInterface, TransactionStatus } from 'types/cell-types'
import { Witness, Transaction as TransactionInterface, TransactionStatus, CellDep } from 'types/cell-types'
import TxDbChangedSubject from 'models/subjects/tx-db-changed-subject'
import InputEntity from './input'
import OutputEntity from './output'
Expand All @@ -37,7 +37,12 @@ export default class Transaction extends BaseEntity {
@Column({
type: 'simple-json',
})
deps!: OutPoint[]
cellDeps!: CellDep[]

@Column({
type: 'simple-json',
})
headerDeps!: string[]

@Column({
type: 'simple-json',
Expand Down Expand Up @@ -101,7 +106,8 @@ export default class Transaction extends BaseEntity {
return {
hash: this.hash,
version: this.version,
deps: this.deps,
cellDeps: this.cellDeps,
headerDeps: this.headerDeps,
inputs,
outputs,
timestamp: this.timestamp,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { MigrationInterface, QueryRunner, TableColumn } from 'typeorm';

export class AlterDepsFromTransaction1566900661931 implements MigrationInterface {

public async up(queryRunner: QueryRunner): Promise<any> {
await queryRunner.addColumn('transaction', new TableColumn({
name: 'cellDeps',
type: 'simple-json',
default: [],
}))

await queryRunner.addColumn('transaction', new TableColumn({
name: 'headerDeps',
type: 'simple-json',
default: [],
}))

await queryRunner.dropColumn('transaction', 'deps')
}

public async down(queryRunner: QueryRunner): Promise<any> {
await queryRunner.addColumn('transaction', new TableColumn({
name: 'deps',
type: 'simple-json',
default: [],
}))

await queryRunner.dropColumn('transaction', 'cellDeps')
await queryRunner.dropColumn('transaction', 'headerDeps')
}

}
8 changes: 7 additions & 1 deletion packages/neuron-wallet/src/database/chain/ormconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import SyncInfo from './entities/sync-info'
import { InitMigration1561695143591 } from './migrations/1561695143591-InitMigration'
import { AddStatusToTx1562038960990 } from './migrations/1562038960990-AddStatusToTx'
import { AddConfirmed1565693320664 } from './migrations/1565693320664-AddConfirmed'
import { AlterDepsFromTransaction1566900661931 } from './migrations/1566900661931-AlterDepsFromTransaction'

export const CONNECTION_NOT_FOUND_NAME = 'ConnectionNotFoundError'

Expand All @@ -32,7 +33,12 @@ const connectOptions = async (genesisBlockHash: string): Promise<SqliteConnectio
type: 'sqlite',
database: dbPath(genesisBlockHash),
entities: [Transaction, Input, Output, SyncInfo],
migrations: [InitMigration1561695143591, AddStatusToTx1562038960990, AddConfirmed1565693320664],
migrations: [
InitMigration1561695143591,
AddStatusToTx1562038960990,
AddConfirmed1565693320664,
AlterDepsFromTransaction1566900661931,
],
logging,
}
}
Expand Down
50 changes: 20 additions & 30 deletions packages/neuron-wallet/src/models/lock-utils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import NodeService from 'services/node'
import { OutPoint, Script, ScriptHashType } from 'types/cell-types'
import env from 'env'
import { SystemScriptSubject } from './subjects/system-script'
import ConvertTo from 'types/convert-to'
import { SystemScriptSubject } from 'models/subjects/system-script'

const { core } = NodeService.getInstance()

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

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

const systemCell = await core.loadSystemCell()
const systemCell = await core.loadSecp256k1Dep()
let { codeHash } = systemCell
const { outPoint } = systemCell
let { blockHash } = outPoint
let { txHash } = outPoint.cell
const { index } = outPoint.cell
const { outPoint, hashType } = systemCell
let { txHash } = outPoint
const { index } = outPoint

if (!codeHash.startsWith('0x')) {
codeHash = `0x${codeHash}`
}

if (!blockHash.startsWith('0x')) {
blockHash = `0x${blockHash}`
}

if (!txHash.startsWith('0x')) {
txHash = `0x${txHash}`
}

const systemScriptInfo = {
codeHash,
outPoint: {
blockHash,
cell: {
txHash,
index,
},
txHash,
index,
},
hashType: hashType as ScriptHashType,
}

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

// use SDK lockScriptToHash
static lockScriptToHash = (lock: Script) => {
const codeHash: string = lock!.codeHash!
const args: string[] = lock.args!
const { hashType } = lock
// TODO: should support ScriptHashType.Type in the future
const lockHash: string = core.utils.lockScriptToHash({
codeHash,
args,
hashType,
})

if (lockHash.startsWith('0x')) {
return lockHash
static computeScriptHash = async (script: Script): Promise<string> => {
const ckbScript: CKBComponents.Script = ConvertTo.toSdkScript(script)
const hash: string = await (core.rpc as any).computeScriptHash(ckbScript)
if (!hash.startsWith('0x')) {
return `0x${hash}`
}
return hash
}

return `0x${lockHash}`
// use SDK lockScriptToHash
static lockScriptToHash = async (lock: Script) => {
return LockUtils.computeScriptHash(lock)
}

static async addressToLockScript(address: string, hashType: ScriptHashType = ScriptHashType.Data): Promise<Script> {
Expand Down
5 changes: 2 additions & 3 deletions packages/neuron-wallet/src/services/cells.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@ export default class CellsService {
}

private static getLiveCellEntity = async (outPoint: OutPoint): Promise<OutputEntity | undefined> => {
const cell = outPoint.cell!
const cellEntity: OutputEntity | undefined = await getConnection()
.getRepository(OutputEntity)
.findOne({
outPointTxHash: cell.txHash,
outPointIndex: cell.index,
outPointTxHash: outPoint.txHash,
outPointIndex: outPoint.index,
status: 'live',
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ export default class CheckOutput {

constructor(output: Cell) {
this.output = output
this.calcLockHash()
// this.calcLockHash()
}

public calcLockHash = (): Cell => {
public calcLockHash = async (): Promise<Cell> => {
if (this.output.lockHash) {
return this.output
}

this.output.lockHash = LockUtils.lockScriptToHash(this.output.lock)
this.output.lockHash = await LockUtils.lockScriptToHash(this.output.lock)
return this.output
}

public checkLockHash = (lockHashList: string[]): boolean | undefined => {
public checkLockHash = async (lockHashList: string[]): Promise<boolean | undefined> => {
await this.calcLockHash()
return lockHashList.includes(this.output.lockHash!)
}
}
15 changes: 8 additions & 7 deletions packages/neuron-wallet/src/services/sync/check-and-save/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ export default class CheckTx {
}

public filterOutputs = (lockHashes: string[]) => {
return this.tx.outputs!.filter(output => {
const cells: Cell[] = this.tx.outputs!.filter(async output => {
const checkOutput = new CheckOutput(output)
return checkOutput.checkLockHash(lockHashes)
const result = await checkOutput.checkLockHash(lockHashes)
return result
})
return cells
}

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

const addresses: string[] = []
for (const input of inputs) {
const outPoint: OutPoint = input.previousOutput
const { cell } = outPoint
if (cell) {
const outPoint: OutPoint | null = input.previousOutput
if (outPoint) {
const output = await getConnection()
.getRepository(OutputEntity)
.findOne({
outPointTxHash: cell.txHash,
outPointIndex: cell.index,
outPointTxHash: outPoint.txHash,
outPointIndex: outPoint.index,
})
if (output && lockHashes.includes(output.lockHash)) {
addresses.push(LockUtils.lockScriptToAddress(output.lock))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TransactionWithoutHash, Cell, ScriptHashType } from 'types/cell-types'
import { TransactionWithoutHash, Cell, ScriptHashType, DepType } from 'types/cell-types'
import CellsService, { MIN_CELL_CAPACITY } from 'services/cells'
import LockUtils from 'models/lock-utils'
import { CapacityTooSmall } from 'exceptions'
Expand Down Expand Up @@ -63,7 +63,12 @@ export class TransactionGenerator {

return {
version: '0',
deps: [outPoint],
cellDeps: [
{
outPoint,
depType: DepType.DepGroup,
},
],
inputs,
outputs,
witnesses: [],
Expand Down
Loading

0 comments on commit 6ca8200

Please sign in to comment.