Skip to content

Commit f3cc18a

Browse files
zhangyouxinhomura
andauthored
refactor: remove ckb-sdk-core and replace with lumos (nervosnetwork#2843)
Co-authored-by: Yonghui Lin <homura.dev@gmail.com>
1 parent 03a08f0 commit f3cc18a

File tree

18 files changed

+938
-129
lines changed

18 files changed

+938
-129
lines changed

_typos.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
[default.extend-words]
22
thur = "thur"
3+
numer = "numer"
34

45
# defined in database schema
56
lastest = "lastest"
67

78
[files]
89
extend-exclude = ["CHANGELOG.md", "**/migrations/*.ts"]
9-
10-
11-

packages/neuron-wallet/package.json

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,16 @@
4242
]
4343
},
4444
"dependencies": {
45-
"@ckb-lumos/base": "0.20.0-alpha.3",
46-
"@ckb-lumos/bi": "0.20.0-alpha.3",
47-
"@ckb-lumos/ckb-indexer": "0.20.0-alpha.3",
48-
"@ckb-lumos/codec": "0.20.0-alpha.3",
49-
"@ckb-lumos/config-manager": "0.20.0-alpha.3",
50-
"@ckb-lumos/hd": "0.20.0-alpha.3",
51-
"@ckb-lumos/helpers": "0.20.0-alpha.3",
52-
"@ckb-lumos/rpc": "0.20.0-alpha.3",
45+
"@ckb-lumos/base": "^0.21.0-next.1",
46+
"@ckb-lumos/bi": "^0.21.0-next.1",
47+
"@ckb-lumos/ckb-indexer": "^0.21.0-next.1",
48+
"@ckb-lumos/codec": "^0.21.0-next.1",
49+
"@ckb-lumos/config-manager": "^0.21.0-next.1",
50+
"@ckb-lumos/hd": "^0.21.0-next.1",
51+
"@ckb-lumos/helpers": "^0.21.0-next.1",
52+
"@ckb-lumos/rpc": "^0.21.0-next.1",
5353
"@iarna/toml": "2.2.5",
5454
"@ledgerhq/hw-transport-node-hid": "6.27.16",
55-
"@nervosnetwork/ckb-sdk-core": "0.109.0",
5655
"archiver": "5.3.0",
5756
"async": "3.2.4",
5857
"bn.js": "4.12.0",

packages/neuron-wallet/src/block-sync-renderer/sync/queue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export default class Queue {
146146
const headers = await rpc
147147
.createBatchRequest<'getHeader', string[], CKBComponents.BlockHeader[]>(blockHashes.map(v => ['getHeader', v]))
148148
.exec()
149-
headers.forEach((blockHeader, idx) => {
149+
headers.forEach((blockHeader: CKBComponents.BlockHeader, idx: number) => {
150150
if (blockHeader) {
151151
const header = BlockHeader.fromSDK(blockHeader)
152152
txs[idx].timestamp = header.timestamp

packages/neuron-wallet/src/controllers/offline-sign.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ export default class OfflineSignController {
4040
const rpc = generateRPC(NodeService.getInstance().nodeUrl)
4141

4242
if (context === undefined) {
43-
const rawTx = rpc.paramsFormatter.toRawTransaction(tx.toSDKRawTransaction())
43+
const rawSdkTx = tx.toSDKRawTransaction()
44+
const rawTx = rpc.paramsFormatter.toRawTransaction(rawSdkTx)
4445
const txs = await Promise.all(rawTx.inputs.map(i => rpc.getTransaction(i.previous_output!.tx_hash)))
4546
context = txs.map(i => rpc.paramsFormatter.toRawTransaction(i.transaction))
4647
}

packages/neuron-wallet/src/models/chain/block.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ export default class Block {
1414
const header = BlockHeader.fromSDK(block.header)
1515
return new Block(
1616
header,
17-
block.transactions.map(tx => Transaction.fromSDK(tx, header))
17+
block.transactions.map((tx: CKBComponents.RawTransaction | CKBComponents.Transaction) =>
18+
Transaction.fromSDK(tx, header)
19+
)
1820
)
1921
}
2022
}

packages/neuron-wallet/src/models/chain/transaction.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ export default class Transaction {
298298
blockHeader?: BlockHeader
299299
): Transaction {
300300
const txHash: string | undefined = (tx as CKBComponents.Transaction).hash
301-
const outputs = tx.outputs.map((o, i) => {
301+
const outputs = tx.outputs.map((o: CKBComponents.CellOutput, i: number) => {
302302
const output = Output.fromSDK(o)
303303
if (txHash) {
304304
output.setOutPoint(new OutPoint(txHash, i.toString()))
@@ -307,9 +307,9 @@ export default class Transaction {
307307
})
308308
return new Transaction(
309309
tx.version,
310-
tx.cellDeps.map(cd => CellDep.fromSDK(cd)),
310+
tx.cellDeps.map((cd: CKBComponents.CellDep) => CellDep.fromSDK(cd)),
311311
tx.headerDeps,
312-
tx.inputs.map(i => Input.fromSDK(i)),
312+
tx.inputs.map((i: CKBComponents.CellInput) => Input.fromSDK(i)),
313313
outputs,
314314
tx.outputsData,
315315
tx.witnesses,

packages/neuron-wallet/src/services/sdk-core.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import CKB from '@nervosnetwork/ckb-sdk-core'
1+
import { CKBRPC } from '@ckb-lumos/rpc'
22
import https from 'https'
33
import http from 'http'
44

@@ -19,14 +19,14 @@ const getHttpAgent = () => {
1919
return httpAgent
2020
}
2121

22-
export const generateCKB = (url: string): CKB => {
23-
const ckb = new CKB(url)
22+
export const generateCKB = (url: string): CKBRPC => {
23+
const rpc = new CKBRPC(url)
2424
if (url.startsWith('https')) {
25-
ckb.rpc.setNode({ url, httpsAgent: getHttpsAgent() })
25+
rpc.setNode({ url, httpsAgent: getHttpsAgent() })
2626
} else {
27-
ckb.rpc.setNode({ url, httpAgent: getHttpAgent() })
27+
rpc.setNode({ url, httpAgent: getHttpAgent() })
2828
}
29-
return ckb
29+
return rpc
3030
}
3131

3232
export default {

packages/neuron-wallet/src/services/transaction-sender.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import signWitnesses from '@nervosnetwork/ckb-sdk-core/lib/signWitnesses'
21
import NodeService from './node'
32
import { serializeWitnessArgs } from '../utils/serialization'
43
import { scriptToAddress } from '../utils/scriptAndAddress'
@@ -21,6 +20,7 @@ import Script from '../models/chain/script'
2120
import Multisig from '../models/multisig'
2221
import Blake2b from '../models/blake2b'
2322
import logger from '../utils/logger'
23+
import { signWitnesses } from '../utils/signWitnesses'
2424
import { bytes as byteUtils, bytes, number } from '@ckb-lumos/codec'
2525
import SystemScriptInfo from '../models/system-script-info'
2626
import AddressParser from '../models/address-parser'
@@ -42,7 +42,7 @@ import { getMultisigStatus } from '../utils/multisig'
4242
import { SignStatus } from '../models/offline-sign'
4343
import NetworksService from './networks'
4444
import { generateRPC } from '../utils/ckb-rpc'
45-
import CKB from '@nervosnetwork/ckb-sdk-core'
45+
import { CKBRPC } from '@ckb-lumos/rpc'
4646
import CellsService from './cells'
4747
import hd from '@ckb-lumos/hd'
4848

@@ -216,7 +216,8 @@ export default class TransactionSender {
216216
wit.lock = serializedMultisig + wit.lock!.slice(2)
217217
signed[0] = serializeWitnessArgs(wit.toSDK())
218218
} else {
219-
signed = signWitnesses(privateKey)({
219+
signed = signWitnesses({
220+
privateKey,
220221
transactionHash: txHash,
221222
witnesses: serializedWitnesses.map(wit => {
222223
if (typeof wit === 'string') {
@@ -791,9 +792,8 @@ export default class TransactionSender {
791792
depositOutPoint: OutPoint,
792793
withdrawBlockHash: string
793794
): Promise<bigint> => {
794-
const ckb = new CKB(NodeService.getInstance().nodeUrl)
795+
const ckb = new CKBRPC(NodeService.getInstance().nodeUrl)
795796
const result = await ckb.calculateDaoMaximumWithdraw(depositOutPoint.toSDK(), withdrawBlockHash)
796-
797797
return BigInt(result)
798798
}
799799

packages/neuron-wallet/src/services/tx/transaction-service.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getConnection } from 'typeorm'
2-
import CKB from '@nervosnetwork/ckb-sdk-core'
2+
import { CKBRPC } from '@ckb-lumos/rpc'
33
import TransactionEntity from '../../database/chain/entities/transaction'
44
import OutputEntity from '../../database/chain/entities/output'
55
import Transaction, {
@@ -507,14 +507,14 @@ export class TransactionsService {
507507
const inputTxHashes = inputs.map(v => v.previousOutput?.txHash).filter((v): v is string => !!v)
508508
if (!inputTxHashes.length) return inputs
509509
const url: string = NetworksService.getInstance().getCurrent().remote
510-
const ckb = new CKB(url)
511-
const inputTxs = await ckb.rpc
510+
const ckb = new CKBRPC(url)
511+
const inputTxs = await ckb
512512
.createBatchRequest<'getTransaction', string[], CKBComponents.TransactionWithStatus[]>(
513513
inputTxHashes.map(v => ['getTransaction', v])
514514
)
515515
.exec()
516516
const inputTxMap = new Map<string, CKBComponents.Transaction>()
517-
inputTxs.forEach((v, idx) => {
517+
inputTxs.forEach((v: { transaction: CKBComponents.Transaction }, idx: number) => {
518518
inputTxMap.set(inputTxHashes[idx], v.transaction)
519519
})
520520
return inputs.map(v => {

0 commit comments

Comments
 (0)