Skip to content

Commit

Permalink
feat: bump sdk to v0.21.0
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Sep 23, 2019
1 parent c64806f commit 3abf7ca
Show file tree
Hide file tree
Showing 16 changed files with 101 additions and 85 deletions.
2 changes: 1 addition & 1 deletion packages/neuron-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"last 2 chrome versions"
],
"dependencies": {
"@nervosnetwork/ckb-sdk-core": "0.20.0",
"@nervosnetwork/ckb-sdk-core": "0.21.0",
"@uifabric/experiments": "7.16.1",
"@uifabric/styling": "7.6.2",
"canvg": "2.0.0",
Expand Down
6 changes: 4 additions & 2 deletions packages/neuron-ui/src/containers/Main/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const useSyncChainData = ({ chainURL, dispatch }: { chainURL: string; dis
.then(tipBlockNumber => {
dispatch({
type: AppActions.UpdateTipBlockNumber,
payload: tipBlockNumber,
payload: BigInt(tipBlockNumber).toString(),
})
})
.catch((err: Error) => {
Expand All @@ -51,7 +51,9 @@ export const useSyncChainData = ({ chainURL, dispatch }: { chainURL: string; dis
getBlockchainInfo()
.then(info => {
if (info) {
const { chain = '', difficulty = '', epoch = '', alerts = [] } = info
const { chain = '', difficulty: difficultyHex = '', epoch: epochHex = '', alerts = [] } = info
const difficulty = BigInt(difficultyHex).toString()
const epoch = BigInt(epochHex).toString()
if (alerts.length) {
alerts.forEach(a => {
// TODO: display alerts in Notification
Expand Down
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.20.0",
"@nervosnetwork/ckb-sdk-utils": "0.20.0",
"@nervosnetwork/ckb-sdk-core": "0.21.0",
"@nervosnetwork/ckb-sdk-utils": "0.21.0",
"bn.js": "4.11.8",
"chalk": "2.4.2",
"electron-log": "3.0.7",
Expand All @@ -51,7 +51,7 @@
"uuid": "3.3.3"
},
"devDependencies": {
"@nervosnetwork/ckb-types": "0.20.0",
"@nervosnetwork/ckb-types": "0.21.0",
"@types/electron-devtools-installer": "2.2.0",
"@types/elliptic": "6.4.9",
"@types/sqlite3": "3.1.5",
Expand Down
6 changes: 3 additions & 3 deletions packages/neuron-wallet/src/listeners/tx-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ const getTransactionStatus = async (hash: string) => {
}
if (tx.txStatus.status === 'committed') {
return {
tx: tx.transaction,
tx: TypeConvert.toTransaction(tx.transaction),
status: TransactionStatus.Success,
blockHash: tx.txStatus.blockHash,
}
}
return {
tx: tx.transaction,
tx: TypeConvert.toTransaction(tx.transaction),
status: TransactionStatus.Pending,
blockHash: null,
}
Expand Down Expand Up @@ -68,7 +68,7 @@ const trackingStatus = async () => {
const { core }: { core: Core } = nodeService
const getBlockService = new GetBlocks(core.rpc.node.url)
for (const successTx of successTxs) {
const transaction = TypeConvert.toTransaction(successTx.tx)
const transaction = successTx.tx
const { blockHash } = successTx
const blockHeader = await getBlockService.getHeader(blockHash!)
transaction.blockHash = blockHash!
Expand Down
6 changes: 4 additions & 2 deletions packages/neuron-wallet/src/models/keys/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ export enum AddressType {
Change = 1, // Internal chain
}

export const publicKeyToAddress = (publicKey: string, prefix = AddressPrefix.Testnet) =>
pubkeyToAddress(publicKey, {
export const publicKeyToAddress = (publicKey: string, prefix = AddressPrefix.Testnet) => {
const pubkey = publicKey.startsWith('0x') ? publicKey : `0x${publicKey}`
return pubkeyToAddress(pubkey, {
prefix,
type: Type.HashIdx,
codeHashIndex: '0x00',
})
}

export default class Address {
publicKey?: string
Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-wallet/src/models/lock-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export default class LockUtils {
const prefix = env.testnet ? core.utils.AddressPrefix.Testnet : core.utils.AddressPrefix.Mainnet
const result: string = core.utils.parseAddress(address, prefix, 'hex') as string
const hrp: string = `0100`
let blake160: string = result.slice(hrp.length, result.length)
let blake160: string = result.slice(hrp.length + 2, result.length)
if (!blake160.startsWith('0x')) {
blake160 = `0x${blake160}`
}
Expand Down
7 changes: 4 additions & 3 deletions packages/neuron-wallet/src/services/indexer/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import TransactionPersistor from 'services/tx/transaction-persistor'
import IndexerTransaction from 'services/tx/indexer-transaction'

import IndexerRPC from './indexer-rpc'
import HexUtils from 'utils/hex'

export interface LockHashInfo {
lockHash: string
Expand Down Expand Up @@ -137,7 +138,7 @@ export default class IndexerQueue {
const lockHashIndexStates = await this.indexerRPC.getLockHashIndexStates()
const blockNumbers = lockHashIndexStates
.filter(state => lockHashes.includes(state.lockHash))
.map(state => state.blockNumber)
.map(state => HexUtils.toDecimal(state.blockNumber))
const uniqueBlockNumbers = [...new Set(blockNumbers)]
const blockNumbersBigInt = uniqueBlockNumbers.map(num => BigInt(num))
const minBlockNumber = Utils.min(blockNumbersBigInt)
Expand Down Expand Up @@ -192,9 +193,9 @@ export default class IndexerQueue {

let address: string | undefined
if (type === TxPointType.CreatedBy) {
address = LockUtils.lockScriptToAddress(transaction.outputs![+txPoint.index].lock)
address = LockUtils.lockScriptToAddress(transaction.outputs![parseInt(txPoint.index, 16)].lock)
} else if (type === TxPointType.ConsumedBy) {
const input = txEntity.inputs[+txPoint.index]
const input = txEntity.inputs[parseInt(txPoint.index, 16)]
const output = await IndexerTransaction.updateInputLockHash(input.outPointTxHash!, input.outPointIndex!)
if (output) {
address = LockUtils.lockScriptToAddress(output.lock)
Expand Down
3 changes: 2 additions & 1 deletion packages/neuron-wallet/src/services/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ShouldBeTypeOf } from 'exceptions'
import { ConnectionStatusSubject } from 'models/subjects/node'
import { CurrentNetworkIDSubject } from 'models/subjects/networks'
import NetworksService from 'services/networks'
import HexUtils from 'utils/hex'

class NodeService {
private static instance: NodeService
Expand Down Expand Up @@ -96,7 +97,7 @@ class NodeService {
if (!this.delayTime) {
this.delayTime = 0
}
this.tipNumberSubject.next(tipNumber)
this.tipNumberSubject.next(HexUtils.toDecimal(tipNumber))
},
() => {
if (this.delayTime < 10 * this.intervalTime) {
Expand Down
5 changes: 3 additions & 2 deletions packages/neuron-wallet/src/services/sync/get-blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Block, BlockHeader } from 'types/cell-types'
import TypeConvert from 'types/type-convert'
import CheckAndSave from './check-and-save'
import Utils from './utils'
import HexUtils from 'utils/hex'

export default class GetBlocks {
private retryTime: number
Expand Down Expand Up @@ -59,13 +60,13 @@ export default class GetBlocks {
}

public getBlockByNumber = async (num: string): Promise<Block> => {
const block = await this.core.rpc.getBlockByNumber(num)
const block = await this.core.rpc.getBlockByNumber(HexUtils.toHex(num))
return TypeConvert.toBlock(block)
}

public genesisBlockHash = async (): Promise<string> => {
const hash: string = await Utils.retry(3, 100, async () => {
const h: string = await this.core.rpc.getBlockHash('0')
const h: string = await this.core.rpc.getBlockHash('0x0')
return h
})

Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-wallet/src/services/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ export default class WalletService {
blake2b.update(data)
})
const message = blake2b.digest()
const signature = `0x${addrObj.signRecoverable(message)}`
const signature = addrObj.signRecoverable(message)
const newWitness: Witness = {
data: [signature],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { core } = NodeService.getInstance()

export const genesisBlockHash = async () => {
const hash: string = await Utils.retry(3, 100, async () => {
const h: string = await core.rpc.getBlockHash('0')
const h: string = await core.rpc.getBlockHash('0x0')
return h
})

Expand Down
19 changes: 14 additions & 5 deletions packages/neuron-wallet/src/types/convert-to.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Transaction, Input, Cell, Script, TransactionWithoutHash, CellDep, OutPoint } from './cell-types'
import HexUtils from 'utils/hex'

export default class ConvertTo {
public static toSdkTransaction = (tx: Transaction): CKBComponents.Transaction => {
const transaction: CKBComponents.Transaction = {
...tx,
hash: tx.hash,
version: HexUtils.toHex(tx.version),
inputs: tx.inputs!.map(input => ConvertTo.toSdkInput(input)),
outputs: tx.outputs!.map(output => ConvertTo.toSdkOutput(output)),
cellDeps: tx.cellDeps ? tx.cellDeps.map(cellDep => ConvertTo.toSdkCellDep(cellDep)) : [],
Expand All @@ -23,7 +25,7 @@ export default class ConvertTo {

public static toSdkTxWithoutHash = (tx: TransactionWithoutHash): CKBComponents.RawTransaction => {
const transaction = {
...tx,
version: HexUtils.toHex(tx.version),
inputs: tx.inputs!.map(input => ConvertTo.toSdkInput(input)),
outputs: tx.outputs!.map(output => ConvertTo.toSdkOutput(output)),
cellDeps: tx.cellDeps ? tx.cellDeps.map(cellDep => ConvertTo.toSdkCellDep(cellDep)) : [],
Expand All @@ -36,16 +38,16 @@ export default class ConvertTo {

public static toSdkInput = (input: Input): CKBComponents.CellInput => {
return {
since: input.since!,
previousOutput: input.previousOutput!,
since: HexUtils.toHex(input.since!),
previousOutput: ConvertTo.toSdkOutPoint(input.previousOutput!),
}
}

public static toSdkOutput = (output: Cell): CKBComponents.CellOutput => {
const type = output.type ? ConvertTo.toSdkScript(output.type) : undefined

return {
...output,
capacity: HexUtils.toHex(output.capacity),
lock: ConvertTo.toSdkScript(output.lock!),
type,
}
Expand All @@ -58,4 +60,11 @@ export default class ConvertTo {
hashType: script.hashType,
}
}

public static toSdkOutPoint = (outPoint: OutPoint): CKBComponents.OutPoint => {
return {
txHash: outPoint.txHash,
index: HexUtils.toHex(outPoint.index),
}
}
}
26 changes: 15 additions & 11 deletions packages/neuron-wallet/src/types/type-convert.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import HexUtils from 'utils/hex';
import {
Block,
BlockHeader,
Expand All @@ -23,19 +24,18 @@ export default class TypeConvert {

static toBlockHeader(blockHeader: CKBComponents.BlockHeader): BlockHeader {
return {
version: blockHeader.version,
timestamp: blockHeader.timestamp.toString(),
version: HexUtils.toDecimal(blockHeader.version),
timestamp: HexUtils.toDecimal(blockHeader.timestamp),
hash: blockHeader.hash,
parentHash: blockHeader.parentHash,
number: blockHeader.number.toString(),
number: HexUtils.toDecimal(blockHeader.number),
}
}

static toTransaction(transaction: CKBComponents.Transaction, blockHeader?: BlockHeader): Transaction {
const tx: Transaction = {
hash: transaction.hash,
version: transaction.version,
// deps: transaction.deps,
version: HexUtils.toDecimal(transaction.version),
cellDeps: transaction.cellDeps.map(cellDep => TypeConvert.toCellDep(cellDep)),
headerDeps: transaction.headerDeps,
witnesses: transaction.witnesses,
Expand All @@ -44,8 +44,8 @@ export default class TypeConvert {
outputsData: transaction.outputsData,
}
if (blockHeader) {
tx.timestamp = blockHeader.timestamp
tx.blockNumber = blockHeader.number
tx.timestamp = HexUtils.toDecimal(blockHeader.timestamp)
tx.blockNumber = HexUtils.toDecimal(blockHeader.number)
tx.blockHash = blockHeader.hash
}
return tx
Expand All @@ -59,16 +59,20 @@ export default class TypeConvert {
}

static toInput(input: CKBComponents.CellInput): Input {
let previousOutput: OutPoint | null = null
if (input.previousOutput) {
previousOutput = TypeConvert.toOutPoint(input.previousOutput)
}
return {
previousOutput: input.previousOutput,
since: input.since,
previousOutput,
since: HexUtils.toDecimal(input.since),
}
}

static toOutPoint(outPoint: CKBComponents.OutPoint): OutPoint {
return {
txHash: outPoint.txHash,
index: outPoint.index,
index: HexUtils.toDecimal(outPoint.index),
}
}

Expand All @@ -78,7 +82,7 @@ export default class TypeConvert {
type = TypeConvert.toScript(output.type)
}
return {
capacity: output.capacity.toString(),
capacity: HexUtils.toDecimal(output.capacity),
lock: TypeConvert.toScript(output.lock),
type,
}
Expand Down
4 changes: 2 additions & 2 deletions packages/neuron-wallet/src/utils/blake2b.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export default class Blake2b {
}

public update = (message: string): void => {
const msg = hexToBytes(message.replace(/0x/, ''))
this.blake2b.update(msg)
const msg = message.startsWith('0x') ? message : `0x${message}`
this.blake2b.update(hexToBytes(msg))
}

public digest = (): string => {
Expand Down
9 changes: 9 additions & 0 deletions packages/neuron-wallet/src/utils/hex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default class HexUtils {
public static toDecimal(hex: string): string {
return BigInt(hex).toString()
}

public static toHex(num: string): string {
return `0x${BigInt(num).toString(16)}`
}
}

0 comments on commit 3abf7ca

Please sign in to comment.