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 ckb-sdk-core and replace with lumos #2843

Merged
merged 16 commits into from
Oct 20, 2023
Merged
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
4 changes: 1 addition & 3 deletions _typos.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
[default.extend-words]
thur = "thur"
numer = "numer"
Keith-CY marked this conversation as resolved.
Show resolved Hide resolved

# defined in database schema
lastest = "lastest"

[files]
extend-exclude = ["CHANGELOG.md", "**/migrations/*.ts"]



17 changes: 8 additions & 9 deletions packages/neuron-wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,16 @@
]
},
"dependencies": {
"@ckb-lumos/base": "0.20.0-alpha.3",
"@ckb-lumos/bi": "0.20.0-alpha.3",
"@ckb-lumos/ckb-indexer": "0.20.0-alpha.3",
"@ckb-lumos/codec": "0.20.0-alpha.3",
"@ckb-lumos/config-manager": "0.20.0-alpha.3",
"@ckb-lumos/hd": "0.20.0-alpha.3",
"@ckb-lumos/helpers": "0.20.0-alpha.3",
"@ckb-lumos/rpc": "0.20.0-alpha.3",
"@ckb-lumos/base": "^0.21.0-next.1",
"@ckb-lumos/bi": "^0.21.0-next.1",
"@ckb-lumos/ckb-indexer": "^0.21.0-next.1",
"@ckb-lumos/codec": "^0.21.0-next.1",
"@ckb-lumos/config-manager": "^0.21.0-next.1",
"@ckb-lumos/hd": "^0.21.0-next.1",
"@ckb-lumos/helpers": "^0.21.0-next.1",
"@ckb-lumos/rpc": "^0.21.0-next.1",
"@iarna/toml": "2.2.5",
"@ledgerhq/hw-transport-node-hid": "6.27.16",
"@nervosnetwork/ckb-sdk-core": "0.109.0",
"archiver": "5.3.0",
"async": "3.2.4",
"bn.js": "4.12.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export default class Queue {
const headers = await rpc
.createBatchRequest<'getHeader', string[], CKBComponents.BlockHeader[]>(blockHashes.map(v => ['getHeader', v]))
.exec()
headers.forEach((blockHeader, idx) => {
headers.forEach((blockHeader: CKBComponents.BlockHeader, idx: number) => {
if (blockHeader) {
const header = BlockHeader.fromSDK(blockHeader)
txs[idx].timestamp = header.timestamp
Expand Down
3 changes: 2 additions & 1 deletion packages/neuron-wallet/src/controllers/offline-sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export default class OfflineSignController {
const rpc = generateRPC(NodeService.getInstance().nodeUrl)

if (context === undefined) {
const rawTx = rpc.paramsFormatter.toRawTransaction(tx.toSDKRawTransaction())
const rawSdkTx = tx.toSDKRawTransaction()
const rawTx = rpc.paramsFormatter.toRawTransaction(rawSdkTx)
const txs = await Promise.all(rawTx.inputs.map(i => rpc.getTransaction(i.previous_output!.tx_hash)))
context = txs.map(i => rpc.paramsFormatter.toRawTransaction(i.transaction))
}
Expand Down
4 changes: 3 additions & 1 deletion packages/neuron-wallet/src/models/chain/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export default class Block {
const header = BlockHeader.fromSDK(block.header)
return new Block(
header,
block.transactions.map(tx => Transaction.fromSDK(tx, header))
block.transactions.map((tx: CKBComponents.RawTransaction | CKBComponents.Transaction) =>
Transaction.fromSDK(tx, header)
)
)
}
}
6 changes: 3 additions & 3 deletions packages/neuron-wallet/src/models/chain/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ export default class Transaction {
blockHeader?: BlockHeader
): Transaction {
const txHash: string | undefined = (tx as CKBComponents.Transaction).hash
const outputs = tx.outputs.map((o, i) => {
const outputs = tx.outputs.map((o: CKBComponents.CellOutput, i: number) => {
const output = Output.fromSDK(o)
if (txHash) {
output.setOutPoint(new OutPoint(txHash, i.toString()))
Expand All @@ -307,9 +307,9 @@ export default class Transaction {
})
return new Transaction(
tx.version,
tx.cellDeps.map(cd => CellDep.fromSDK(cd)),
tx.cellDeps.map((cd: CKBComponents.CellDep) => CellDep.fromSDK(cd)),
tx.headerDeps,
tx.inputs.map(i => Input.fromSDK(i)),
tx.inputs.map((i: CKBComponents.CellInput) => Input.fromSDK(i)),
outputs,
tx.outputsData,
tx.witnesses,
Expand Down
12 changes: 6 additions & 6 deletions packages/neuron-wallet/src/services/sdk-core.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import CKB from '@nervosnetwork/ckb-sdk-core'
import { CKBRPC } from '@ckb-lumos/rpc'
import https from 'https'
import http from 'http'

Expand All @@ -19,14 +19,14 @@ const getHttpAgent = () => {
return httpAgent
}

export const generateCKB = (url: string): CKB => {
const ckb = new CKB(url)
export const generateCKB = (url: string): CKBRPC => {
const rpc = new CKBRPC(url)
if (url.startsWith('https')) {
ckb.rpc.setNode({ url, httpsAgent: getHttpsAgent() })
rpc.setNode({ url, httpsAgent: getHttpsAgent() })
} else {
ckb.rpc.setNode({ url, httpAgent: getHttpAgent() })
rpc.setNode({ url, httpAgent: getHttpAgent() })
}
return ckb
return rpc
}

export default {
Expand Down
10 changes: 5 additions & 5 deletions packages/neuron-wallet/src/services/transaction-sender.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import signWitnesses from '@nervosnetwork/ckb-sdk-core/lib/signWitnesses'
import NodeService from './node'
import { serializeWitnessArgs } from '../utils/serialization'
import { scriptToAddress } from '../utils/scriptAndAddress'
Expand All @@ -21,6 +20,7 @@ import Script from '../models/chain/script'
import Multisig from '../models/multisig'
import Blake2b from '../models/blake2b'
import logger from '../utils/logger'
import { signWitnesses } from '../utils/signWitnesses'
import { bytes as byteUtils, bytes, number } from '@ckb-lumos/codec'
import SystemScriptInfo from '../models/system-script-info'
import AddressParser from '../models/address-parser'
Expand All @@ -42,7 +42,7 @@ import { getMultisigStatus } from '../utils/multisig'
import { SignStatus } from '../models/offline-sign'
import NetworksService from './networks'
import { generateRPC } from '../utils/ckb-rpc'
import CKB from '@nervosnetwork/ckb-sdk-core'
import { CKBRPC } from '@ckb-lumos/rpc'
import CellsService from './cells'
import hd from '@ckb-lumos/hd'

Expand Down Expand Up @@ -216,7 +216,8 @@ export default class TransactionSender {
wit.lock = serializedMultisig + wit.lock!.slice(2)
signed[0] = serializeWitnessArgs(wit.toSDK())
} else {
signed = signWitnesses(privateKey)({
signed = signWitnesses({
privateKey,
transactionHash: txHash,
witnesses: serializedWitnesses.map(wit => {
if (typeof wit === 'string') {
Expand Down Expand Up @@ -791,9 +792,8 @@ export default class TransactionSender {
depositOutPoint: OutPoint,
withdrawBlockHash: string
): Promise<bigint> => {
const ckb = new CKB(NodeService.getInstance().nodeUrl)
const ckb = new CKBRPC(NodeService.getInstance().nodeUrl)
const result = await ckb.calculateDaoMaximumWithdraw(depositOutPoint.toSDK(), withdrawBlockHash)

return BigInt(result)
}

Expand Down
8 changes: 4 additions & 4 deletions packages/neuron-wallet/src/services/tx/transaction-service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getConnection } from 'typeorm'
import CKB from '@nervosnetwork/ckb-sdk-core'
import { CKBRPC } from '@ckb-lumos/rpc'
import TransactionEntity from '../../database/chain/entities/transaction'
import OutputEntity from '../../database/chain/entities/output'
import Transaction, {
Expand Down Expand Up @@ -507,14 +507,14 @@ export class TransactionsService {
const inputTxHashes = inputs.map(v => v.previousOutput?.txHash).filter((v): v is string => !!v)
if (!inputTxHashes.length) return inputs
const url: string = NetworksService.getInstance().getCurrent().remote
const ckb = new CKB(url)
const inputTxs = await ckb.rpc
const ckb = new CKBRPC(url)
const inputTxs = await ckb
.createBatchRequest<'getTransaction', string[], CKBComponents.TransactionWithStatus[]>(
inputTxHashes.map(v => ['getTransaction', v])
)
.exec()
const inputTxMap = new Map<string, CKBComponents.Transaction>()
inputTxs.forEach((v, idx) => {
inputTxs.forEach((v: { transaction: CKBComponents.Transaction }, idx: number) => {
inputTxMap.set(inputTxHashes[idx], v.transaction)
})
return inputs.map(v => {
Expand Down
Loading