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-js from dependency list #2817

Merged
merged 16 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from 15 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
2 changes: 1 addition & 1 deletion packages/neuron-wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@
"@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",
"@iarna/toml": "2.2.5",
"@ledgerhq/hw-transport-node-hid": "6.27.16",
"@nervosnetwork/ckb-sdk-core": "0.109.0",
"@nervosnetwork/ckb-sdk-utils": "0.109.0",
"archiver": "5.3.0",
"async": "3.2.4",
"bn.js": "4.12.0",
Expand Down
11 changes: 6 additions & 5 deletions packages/neuron-wallet/src/models/blake2b.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { blake2b, PERSONAL, hexToBytes } from '@nervosnetwork/ckb-sdk-utils'
import { CKBHasher } from '@ckb-lumos/base/lib/utils'
import { bytes } from '@ckb-lumos/codec'

export default class Blake2b {
private blake2b: any
private blake2b: CKBHasher

constructor() {
this.blake2b = blake2b(32, null, null, PERSONAL)
this.blake2b = new CKBHasher()
}

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

public updateBuffer = (message: Buffer): void => {
this.blake2b.update(message)
}

public digest = (): string => {
return `0x${this.blake2b.digest('hex')}`
return this.blake2b.digestHex()
}

public static digest = (message: string): string => {
Expand Down
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 @@ -3,12 +3,12 @@ import Input from './input'
import Output from './output'
import WitnessArgs from './witness-args'
import { BI } from '@ckb-lumos/bi'
import { rawTransactionToHash } from '@nervosnetwork/ckb-sdk-utils'
import { serializeWitnessArgs } from '../../utils/serialization'
import { serializeRawTransaction, serializeWitnessArgs } from '../../utils/serialization'
import BlockHeader from './block-header'
import TypeCheckerUtils from '../../utils/type-checker'
import OutPoint from './out-point'
import { Signatures } from '../../models/offline-sign'
import { utils } from '@ckb-lumos/base'

export enum TransactionStatus {
Pending = 'pending',
Expand Down Expand Up @@ -269,7 +269,7 @@ export default class Transaction {
}

public computeHash(): string {
return rawTransactionToHash(this.toSDKRawTransaction())
return utils.ckbHash(serializeRawTransaction(this.toSDKRawTransaction()))
}

public toSDKRawTransaction(): CKBComponents.RawTransaction {
Expand Down
5 changes: 2 additions & 3 deletions packages/neuron-wallet/src/models/keys/address.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { AddressPrefix, blake160, bytesToHex } from '@nervosnetwork/ckb-sdk-utils'
import { scriptToAddress } from '../../utils/scriptAndAddress'
import { AccountExtendedPublicKey } from './key'
import { systemScripts } from '../../utils/systemScripts'
export { AddressPrefix }
import hd from '@ckb-lumos/hd'

export enum AddressType {
Receiving = 0, // External chain
Expand All @@ -15,7 +14,7 @@ export const publicKeyToAddress = (publicKey: string, isMainnet = false) => {
{
codeHash: systemScripts.SECP256K1_BLAKE160.CODE_HASH,
hashType: systemScripts.SECP256K1_BLAKE160.HASH_TYPE,
args: bytesToHex(blake160(pubkey)),
args: hd.key.publicKeyToBlake160(pubkey),
},
isMainnet
)
Expand Down
17 changes: 12 additions & 5 deletions packages/neuron-wallet/src/models/transaction-size.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { serializeOutput, serializeWitnessArgs } from '../utils/serialization'
import { serializeFixVec } from '@nervosnetwork/ckb-sdk-utils/lib/serialization'
import Output from './chain/output'
import WitnessArgs from './chain/witness-args'
import Transaction from './chain/transaction'
Expand All @@ -9,6 +8,8 @@ import BufferUtils from '../utils/buffer'
import { bytes as byteUtils } from '@ckb-lumos/codec'

export default class TransactionSize {
// https://github.com/zhangsoledad/rfcs/blob/zhangsoledad/ckb2023-overview/rfcs/0008-serialization/0008-serialization.md#fixvec---fixed-vector
public static SERIALIZED_ITEMS_COUNT_BYTESIZE = 4
public static SERIALIZED_OFFSET_BYTESIZE = 4

public static base(): number {
Expand Down Expand Up @@ -55,8 +56,11 @@ export default class TransactionSize {
}

public static outputData(data: string): number {
const bytes = serializeFixVec(data)
return byteUtils.bytify(bytes).byteLength + TransactionSize.SERIALIZED_OFFSET_BYTESIZE
return (
byteUtils.bytify(data).byteLength +
TransactionSize.SERIALIZED_ITEMS_COUNT_BYTESIZE +
TransactionSize.SERIALIZED_OFFSET_BYTESIZE
)
}

// TODO: and here
Expand All @@ -67,8 +71,11 @@ export default class TransactionSize {

public static witness(witness: WitnessArgs | string): number {
const wit: string = typeof witness === 'string' ? witness : serializeWitnessArgs(witness.toSDK())
const bytes = serializeFixVec(wit)
return byteUtils.bytify(bytes).byteLength + TransactionSize.SERIALIZED_OFFSET_BYTESIZE
return (
byteUtils.bytify(wit).byteLength +
TransactionSize.SERIALIZED_ITEMS_COUNT_BYTESIZE +
TransactionSize.SERIALIZED_OFFSET_BYTESIZE
)
}

public static secpLockWitness(): number {
Expand Down
5 changes: 2 additions & 3 deletions packages/neuron-wallet/src/services/sign-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import AddressService from './addresses'
import WalletService, { Wallet } from './wallets'
import Keychain from '../models/keys/keychain'
import Blake2b from '../models/blake2b'
import ECPair from '@nervosnetwork/ckb-sdk-utils/lib/ecpair'
import hd from '@ckb-lumos/hd'
import { ec as EC } from 'elliptic'
import { AddressNotFound } from '../exceptions'
import HardwareWalletService from './hardware'
Expand Down Expand Up @@ -40,8 +40,7 @@ export default class SignMessage {

private static signByPrivateKey(privateKey: string, message: string): string {
const digest = SignMessage.signatureHash(message)
const ecPair = new ECPair(privateKey)
const signature = ecPair.signRecoverable(digest)
const signature = hd.key.signRecoverable(digest, privateKey)
return signature
}

Expand Down
7 changes: 4 additions & 3 deletions packages/neuron-wallet/src/services/transaction-sender.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import ECPair from '@nervosnetwork/ckb-sdk-utils/lib/ecpair'
import signWitnesses from '@nervosnetwork/ckb-sdk-core/lib/signWitnesses'
import NodeService from './node'
import { serializeWitnessArgs } from '../utils/serialization'
Expand Down Expand Up @@ -45,6 +44,7 @@ import NetworksService from './networks'
import { generateRPC } from '../utils/ckb-rpc'
import CKB from '@nervosnetwork/ckb-sdk-core'
import CellsService from './cells'
import hd from '@ckb-lumos/hd'

interface SignInfo {
witnessArgs: WitnessArgs
Expand Down Expand Up @@ -417,8 +417,9 @@ export default class TransactionSender {
const message = blake2b.digest()

if (!wallet.isHardware()) {
const keyPair = new ECPair(privateKeyOrPath)
emptyWitness.lock = keyPair.signRecoverable(message)
// `privateKeyOrPath` variable here is a private key because wallet is not a hardware one. Otherwise, it will be a private key path.
const privateKey = privateKeyOrPath
emptyWitness.lock = hd.key.signRecoverable(message, privateKey)
}

return [emptyWitness, ...restWitnesses]
Expand Down
4 changes: 2 additions & 2 deletions packages/neuron-wallet/tests/services/address.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,10 @@ describe('integration tests for AddressService', () => {
})
})
})

describe('#generateAndSaveForPublicKey', () => {
describe('with public key info exist for the public key', () => {
const publicKey = 'public key'
// public key is a valid 33 byte hex string
const publicKey = '0x' + '00'.repeat(33)
const addressType = AddressType.Receiving
const addressIndex = 0
beforeEach(async () => {
Expand Down
60 changes: 37 additions & 23 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1373,10 +1373,10 @@
dependencies:
regenerator-runtime "^0.13.11"

"@babel/runtime@^7.20.6":
version "7.22.10"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.22.10.tgz#ae3e9631fd947cb7e3610d3e9d8fef5f76696682"
integrity sha512-21t/fkKLMZI4pqP2wlmsQAWnYW1PDyKyyUV4vCi+B25ydmdaYTKXPwCj0BzSUnZf4seIiYvSA3jcZ3gdsMFkLQ==
"@babel/runtime@^7.22.5":
version "7.22.11"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.22.11.tgz#7a9ba3bbe406ad6f9e8dd4da2ece453eb23a77a4"
integrity sha512-ee7jVNlWN09+KftVOu9n7S8gQzD/Z6hN/I8VBRXW4P1+Xe7kJGXMwu8vds4aGIMHZnNbdpSWCfZZtinytpcAvA==
dependencies:
regenerator-runtime "^0.14.0"

Expand Down Expand Up @@ -1500,6 +1500,19 @@
"@types/deep-freeze-strict" "^1.1.0"
deep-freeze-strict "^1.1.1"

"@ckb-lumos/hd@0.20.0-alpha.3":
version "0.20.0-alpha.3"
resolved "https://registry.yarnpkg.com/@ckb-lumos/hd/-/hd-0.20.0-alpha.3.tgz#608bded07aed41f71bda40e4f66af365a9ab18db"
integrity sha512-ladOBv4CPdW3q6fZorMIBpavzKII12ClAs7jvjtub8/CPziJAdTW0o62UFBvP1zt51eG0nP/NObuHzAos3pJnw==
dependencies:
"@ckb-lumos/base" "0.20.0-alpha.3"
"@ckb-lumos/bi" "0.20.0-alpha.3"
bn.js "^5.1.3"
elliptic "^6.5.4"
scrypt-js "^3.0.1"
sha3 "^2.1.3"
uuid "^8.3.0"

"@ckb-lumos/helpers@0.20.0-alpha.3":
version "0.20.0-alpha.3"
resolved "https://registry.yarnpkg.com/@ckb-lumos/helpers/-/helpers-0.20.0-alpha.3.tgz#17e715664b871fdb0b20cc9d146b0ccb3a7e288e"
Expand Down Expand Up @@ -4550,7 +4563,7 @@
"@types/history" "^4.7.11"
"@types/react" "*"

"@types/react@*", "@types/react@17.0.62", "@types/react@>=16", "@types/react@^17":
"@types/react@*", "@types/react@17.0.62", "@types/react@>=16", "@types/react@^16", "@types/react@^17":
version "17.0.62"
resolved "https://registry.yarnpkg.com/@types/react/-/react-17.0.62.tgz#2efe8ddf8533500ec44b1334dd1a97caa2f860e3"
integrity sha512-eANCyz9DG8p/Vdhr0ZKST8JV12PhH2ACCDYlFw6DIO+D+ca+uP4jtEDEpVqXZrh/uZdXQGwk7whJa3ah5DtyLw==
Expand All @@ -4559,15 +4572,6 @@
"@types/scheduler" "*"
csstype "^3.0.2"

"@types/react@^16":
version "16.14.45"
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.14.45.tgz#c505c96adf959971fc97b4687187c871765a6ae4"
integrity sha512-XFtKkY3yuPO5VJSE6Lru9yLkVQvYE+l6NbmLp6IWCg4jo5S8Ijbpke8wC9q4NmQ5pJErT8KKboG5eY7n5n718A==
dependencies:
"@types/prop-types" "*"
"@types/scheduler" "*"
csstype "^3.0.2"

"@types/resolve@1.17.1":
version "1.17.1"
resolved "https://registry.yarnpkg.com/@types/resolve/-/resolve-1.17.1.tgz#3afd6ad8967c77e4376c598a82ddd58f46ec45d6"
Expand Down Expand Up @@ -6127,6 +6131,11 @@ bn.js@4.12.0, bn.js@^4.11.9:
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.12.0.tgz#775b3f278efbb9718eec7361f483fb36fbbfea88"
integrity sha512-c98Bf3tPniI+scsdk237ku1Dc3ujXQTSgyiPUDEOe7tRkhrqridvh8klBv0HCEso1OLOYcHuCv/cS6DNxKH+ZA==

bn.js@^5.1.3:
version "5.2.1"
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-5.2.1.tgz#0bc527a6a0d18d0aa8d5b0538ce4a77dccfa7b70"
integrity sha512-eXRvHzWyYPBuB4NBy0cmYQjGitUrtqwbvlzP3G6VFnNRbsZQIxQ10PbKKHt8gZ/HW/D/747aDl+QkDqg3KQLMQ==

body-parser@1.20.1:
version "1.20.1"
resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.20.1.tgz#b1812a8912c195cd371a3ee5e66faa2338a5c668"
Expand Down Expand Up @@ -8194,7 +8203,7 @@ electron@24.6.0:
"@types/node" "^18.11.18"
extract-zip "^2.0.1"

elliptic@6.5.4:
elliptic@6.5.4, elliptic@^6.5.4:
version "6.5.4"
resolved "https://registry.yarnpkg.com/elliptic/-/elliptic-6.5.4.tgz#da37cebd31e79a1367e941b592ed1fbebd58abbb"
integrity sha512-iLhC6ULemrljPZb+QutR5TQGB+pdW6KGD5RSegS+8sorOZT+rdQFbsQFJgvN3eRqNALqJer4oQ16YvJHlU8hzQ==
Expand Down Expand Up @@ -15253,12 +15262,12 @@ react-error-overlay@^6.0.11:
resolved "https://registry.yarnpkg.com/react-error-overlay/-/react-error-overlay-6.0.11.tgz#92835de5841c5cf08ba00ddd2d677b6d17ff9adb"
integrity sha512-/6UZ2qgEyH2aqzYZgQPxEnz33NJ2gNsnHA2o5+o4wW9bLM/JYQitNP9xPhsXwC08hMMovfGe/8retsdDsczPRg==

react-i18next@12.1.5:
version "12.1.5"
resolved "https://registry.yarnpkg.com/react-i18next/-/react-i18next-12.1.5.tgz#b65f5733dd2f96188a9359c009b7dbe27443f009"
integrity sha512-7PQAv6DA0TcStG96fle+8RfTwxVbHVlZZJPoEszwUNvDuWpGldJmNWa3ZPesEsZQZGF6GkzwvEh6p57qpFD2gQ==
react-i18next@12.1.5, react-i18next@>=11.16.4:
version "13.2.0"
resolved "https://registry.yarnpkg.com/react-i18next/-/react-i18next-13.2.0.tgz#9d7a574144738e46dd4c66573907f56942435ced"
integrity sha512-YD8xMc+I0jkHHLotd8ERMzZ53hMaCBVLCndbcbBveJn3FbppRP4jyYOAkmR8XItN01sTD9ilAjoEjpH1i42IgA==
dependencies:
"@babel/runtime" "^7.20.6"
"@babel/runtime" "^7.22.5"
html-parse-stringify "^3.0.1"

react-inspector@^6.0.0, react-inspector@^6.0.1:
Expand Down Expand Up @@ -16078,6 +16087,11 @@ schema-utils@^4.0.0:
ajv-formats "^2.1.1"
ajv-keywords "^5.1.0"

scrypt-js@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/scrypt-js/-/scrypt-js-3.0.1.tgz#d314a57c2aef69d1ad98a138a21fe9eafa9ee312"
integrity sha512-cdwTTnqPu0Hyvf5in5asVdZocVDTNRmR7XEcJuIzMjJeSHybHl7vpB66AzwTaIg6CLSbtjcxc8fqcySfnTkccA==

select-hose@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/select-hose/-/select-hose-2.0.0.tgz#625d8658f865af43ec962bfc376a37359a4994ca"
Expand Down Expand Up @@ -16236,7 +16250,7 @@ sha.js@^2.4.11:
inherits "^2.0.1"
safe-buffer "^5.0.1"

sha3@2.1.4:
sha3@2.1.4, sha3@^2.1.3:
version "2.1.4"
resolved "https://registry.yarnpkg.com/sha3/-/sha3-2.1.4.tgz#000fac0fe7c2feac1f48a25e7a31b52a6492cc8f"
integrity sha512-S8cNxbyb0UGUM2VhRD4Poe5N58gJnJsLJ5vC7FYWGUmGhcsj4++WaIOBFVDxlG0W3To6xBuiRh+i0Qp2oNCOtg==
Expand Down Expand Up @@ -17803,7 +17817,7 @@ url-parse@^1.5.3:
querystringify "^2.1.1"
requires-port "^1.0.0"

usb@^1.7.0:
usb@1.8.8, usb@^1.7.0:
version "1.8.8"
resolved "https://registry.yarnpkg.com/usb/-/usb-1.8.8.tgz#54de33f9e57dc4efc1b5b5f72b6624a275775e80"
integrity sha512-xpRAoek268RE3ATqK8l6LjrF4ADHn/A3V3cXEFbYo3/D83ZCLSO0A5tFKO093F4w5IbDfBVlB9VsYzoGz6EJGw==
Expand Down Expand Up @@ -17864,7 +17878,7 @@ utils-merge@1.0.1:
resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713"
integrity sha512-pMZTvIkT1d+TFGvDOqodOclx0QWkkgi6Tdoa8gC8ffGAAqz9pzPTZWAybbsHHoED/ztMtkv/VoYTYyShUn81hA==

uuid@8.3.2, uuid@^8.3.2:
uuid@8.3.2, uuid@^8.3.0, uuid@^8.3.2:
version "8.3.2"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==
Expand Down