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 5 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
1 change: 0 additions & 1 deletion packages/neuron-wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
"@ckb-lumos/rpc": "0.20.0-alpha.3",
"@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
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
46 changes: 46 additions & 0 deletions packages/neuron-wallet/src/utils/signWitnesses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { bytes, number } from '@ckb-lumos/codec'
import { serializeWitnessArgs } from './serialization'
import { CKBHasher } from '@ckb-lumos/base/lib/utils'
import { key } from '@ckb-lumos/hd'

type StructuredWitness = CKBComponents.WitnessArgs | CKBComponents.Witness

// https://github.com/nervosnetwork/ckb-system-scripts/wiki/How-to-sign-transaction#signing
export const signWitnesses = ({
yanguoyu marked this conversation as resolved.
Show resolved Hide resolved
witnesses,
transactionHash,
privateKey,
}: {
witnesses: StructuredWitness[]
transactionHash: string
privateKey: string
}): StructuredWitness[] => {
if (witnesses.length === 0) {
throw new Error('witnesses cannot be empty')
}
if (typeof witnesses[0] !== 'object') {
throw new Error('The first witness in the group should be type of WitnessArgs')
}

const emptyWitness = {
...witnesses[0],
lock: `0x${'00'.repeat(65)}`,
}
const serializedEmptyWitnessBytes = bytes.bytify(serializeWitnessArgs(emptyWitness))
const serializedEmptyWitnessSize = serializedEmptyWitnessBytes.byteLength

const hasher = new CKBHasher()
hasher.update(transactionHash)
hasher.update(number.Uint64LE.pack(serializedEmptyWitnessSize))
hasher.update(serializedEmptyWitnessBytes)

witnesses.slice(1).forEach(witness => {
const witnessBytes = bytes.bytify(typeof witness === 'string' ? witness : serializeWitnessArgs(witness))
hasher.update(number.Uint64LE.pack(witnessBytes.byteLength))
hasher.update(witnessBytes)
})
const message = hasher.digestHex()

emptyWitness.lock = key.signRecoverable(message, privateKey)
return [serializeWitnessArgs(emptyWitness), ...witnesses.slice(1)]
}
6 changes: 3 additions & 3 deletions packages/neuron-wallet/tests/services/tx-wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import Keystore from '../../src/models/keys/keystore'
import Keychain from '../../src/models/keys/keychain'
import { mnemonicToSeedSync } from '../../src/models/keys/mnemonic'
import { ExtendedPrivateKey, AccountExtendedPublicKey } from '../../src/models/keys/key'
import CKB from '@nervosnetwork/ckb-sdk-core'
import TransactionSender from '../../src/services/transaction-sender'
import { signWitnesses } from '../../src/utils/signWitnesses'

describe('sign witness', () => {
const witness = {
Expand All @@ -19,8 +19,8 @@ describe('sign witness', () => {
]

it('success', () => {
const ckb = new CKB('')
const newWitness = ckb.signWitnesses(privateKey)({
const newWitness = signWitnesses({
privateKey,
witnesses: [witness],
transactionHash: txHash,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,16 @@ jest.doMock('services/hardware', () => ({
}),
}))

jest.doMock('@nervosnetwork/ckb-sdk-core', () => {
return function () {
return {
calculateDaoMaximumWithdraw: stubbedCalculateDaoMaximumWithdraw,
}
jest.doMock('@ckb-lumos/rpc', () => {
return {
CKBRPC: class CKBRPC {
url: string
constructor(url: string) {
this.url = url
}

calculateDaoMaximumWithdraw = stubbedCalculateDaoMaximumWithdraw
},
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,20 @@ jest.mock('../../../src/services/rpc-service', () => {
})

const ckbRpcExecMock = jest.fn()
jest.mock('@ckb-lumos/rpc', () => {
return {
CKBRPC: class CKBRPC {
url: string
constructor(url: string) {
this.url = url
}

jest.mock('@nervosnetwork/ckb-sdk-core', () => {
return function () {
return {
rpc: {
createBatchRequest() {
return {
exec: ckbRpcExecMock,
}
},
},
}
createBatchRequest() {
return {
exec: ckbRpcExecMock,
}
}
},
}
})

Expand Down