Skip to content

Commit

Permalink
refactor: replace models/keys with lumos hd (#3152)
Browse files Browse the repository at this point in the history
* refactor: replace models/keys/mnemonic with lumos hd

* refactor: replace models/keys/keychain with lumos hd

* refactor: replace models/keys/keystore with lumos hd

* refactor: replace models/keys/key with lumos hd

* refactor: replace models/keys/address with lumos hd

* refactor: rename Ox() to prefixWith0x()

* refactor: create Buffer / HexString with bytify / hexify
  • Loading branch information
twhy authored May 15, 2024
1 parent 9060738 commit b8bad46
Show file tree
Hide file tree
Showing 40 changed files with 120 additions and 3,427 deletions.
2 changes: 1 addition & 1 deletion packages/neuron-wallet/src/controllers/hardware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DeviceInfo, ExtendedPublicKey, PublicKey } from '../services/hardware/c
import { ResponseCode } from '../utils/const'
import HardwareWalletService from '../services/hardware'
import { connectDeviceFailed } from '../exceptions'
import { AccountExtendedPublicKey } from '../models/keys/key'
import { AccountExtendedPublicKey } from '@ckb-lumos/hd'

export default class HardwareController {
public async connectDevice(deviceInfo: DeviceInfo): Promise<Controller.Response<void>> {
Expand Down
22 changes: 11 additions & 11 deletions packages/neuron-wallet/src/controllers/wallets.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import fs from 'fs'
import { t } from 'i18next'
import { prefixWith0x } from '../utils/scriptAndAddress'
import { dialog, SaveDialogReturnValue, BrowserWindow, OpenDialogReturnValue } from 'electron'
import WalletsService, { Wallet, WalletProperties, FileKeystoreWallet } from '../services/wallets'
import NetworksService from '../services/networks'
import Keystore from '../models/keys/keystore'
import Keychain from '../models/keys/keychain'
import { validateMnemonic, mnemonicToSeedSync } from '../models/keys/mnemonic'
import { AccountExtendedPublicKey, ExtendedPrivateKey, generateMnemonic } from '../models/keys/key'
import { bytes } from '@ckb-lumos/codec'
import { Keychain, Keystore, ExtendedPrivateKey, AccountExtendedPublicKey } from '@ckb-lumos/hd'
import { generateMnemonic, validateMnemonic, mnemonicToSeedSync } from '@ckb-lumos/hd/lib/mnemonic'
import CommandSubject from '../models/subjects/command'
import { ResponseCode } from '../utils/const'
import {
Expand Down Expand Up @@ -106,15 +106,15 @@ export default class WalletsController {
throw new InvalidMnemonic()
}
const extendedKey = new ExtendedPrivateKey(
masterKeychain.privateKey.toString('hex'),
masterKeychain.chainCode.toString('hex')
bytes.hexify(masterKeychain.privateKey),
bytes.hexify(masterKeychain.chainCode)
)
const keystore = Keystore.create(extendedKey, password)

const accountKeychain = masterKeychain.derivePath(AccountExtendedPublicKey.ckbAccountPath)
const accountExtendedPublicKey = new AccountExtendedPublicKey(
accountKeychain.publicKey.toString('hex'),
accountKeychain.chainCode.toString('hex')
bytes.hexify(accountKeychain.publicKey),
bytes.hexify(accountKeychain.chainCode)
)

const walletsService = WalletsService.getInstance()
Expand Down Expand Up @@ -173,8 +173,8 @@ export default class WalletsController {
)
const accountKeychain = masterKeychain.derivePath(AccountExtendedPublicKey.ckbAccountPath)
const accountExtendedPublicKey = new AccountExtendedPublicKey(
accountKeychain.publicKey.toString('hex'),
accountKeychain.chainCode.toString('hex')
bytes.hexify(accountKeychain.publicKey),
bytes.hexify(accountKeychain.chainCode)
)

const walletsService = WalletsService.getInstance()
Expand Down Expand Up @@ -273,7 +273,7 @@ export default class WalletsController {
walletName,
}: ExtendedPublicKey & { walletName: string }): Promise<Controller.Response<Wallet>> {
const device = HardwareWalletService.getInstance().getCurrent()!
const accountExtendedPublicKey = new AccountExtendedPublicKey(publicKey, chainCode)
const accountExtendedPublicKey = new AccountExtendedPublicKey(prefixWith0x(publicKey), prefixWith0x(chainCode))
const walletsService = WalletsService.getInstance()
const wallet = walletsService.create({
device: device.deviceInfo,
Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-wallet/src/database/address/meta.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { bytes } from '@ckb-lumos/codec'
import { Address, AddressVersion } from '../../models/address'
import { AddressType } from '../../models/keys/address'
import { AddressType } from '@ckb-lumos/hd'
import Script from '../../models/chain/script'
import SystemScriptInfo from '../../models/system-script-info'
import AssetAccountInfo from '../../models/asset-account-info'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Entity, Column, PrimaryGeneratedColumn, Index, CreateDateColumn } from 'typeorm'
import HdPublicKeyInfoModel from '../../../models/keys/hd-public-key-info'
import { AddressType } from '../../../models/keys/address'
import { AddressType } from '@ckb-lumos/hd'

@Entity()
export default class HdPublicKeyInfo {
Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-wallet/src/models/address.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AddressType } from '../models/keys/address'
import { AddressType } from '@ckb-lumos/hd'

export enum AddressVersion {
Testnet = 'testnet',
Expand Down
51 changes: 0 additions & 51 deletions packages/neuron-wallet/src/models/keys/address.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/neuron-wallet/src/models/keys/hd-public-key-info.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AddressType, AccountExtendedPublicKey } from '@ckb-lumos/hd'
import { scriptToAddress } from '../../utils/scriptAndAddress'
import SystemScriptInfo from '../../models/system-script-info'
import NetworksService from '../../services/networks'
import Address, { AddressType } from './address'

export default class HdPublicKeyInfoModel {
public walletId: string
Expand All @@ -22,7 +22,7 @@ export default class HdPublicKeyInfoModel {
}

public get path(): string {
return Address.pathFor(this.addressType, this.addressIndex)
return AccountExtendedPublicKey.pathFor(this.addressType, this.addressIndex)
}

constructor(
Expand Down
114 changes: 0 additions & 114 deletions packages/neuron-wallet/src/models/keys/key.ts

This file was deleted.

Loading

3 comments on commit b8bad46

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Packaging for test is done in 9094919128

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Packaging for test is done in 9094920194

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Packaging for test is done in 9256213918

Please sign in to comment.