Skip to content

Commit

Permalink
fix: Fix import hard wallet.
Browse files Browse the repository at this point in the history
  • Loading branch information
yanguoyu committed May 28, 2024
1 parent 3373f2d commit b81b534
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
17 changes: 16 additions & 1 deletion packages/neuron-wallet/src/services/hardware/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ import logger from '../../utils/logger'
import NetworksService from '../../services/networks'
import { generateRPC } from '../../utils/ckb-rpc'

const UNCOMPRESSED_KEY_LENGTH = 130
const compressPublicKey = (key: string) => {
if (key.length !== UNCOMPRESSED_KEY_LENGTH) {
return key
}

const publicKey = Buffer.from(key, 'hex')
const compressedPublicKey = Buffer.alloc(33)
// '03' for odd value, '02' for even value
.fill(publicKey[64] & 1 ? '03' : '02', 0, 1, 'hex')
.fill(publicKey.subarray(1, 33), 1, 33)
return compressedPublicKey.toString('hex')
}

export default class Ledger extends Hardware {
private ledgerCKB: LedgerCKB | null = null
private transport: Transport | null = null
Expand Down Expand Up @@ -41,8 +55,9 @@ export default class Ledger extends Hardware {

public async getExtendedPublicKey(): Promise<ExtendedPublicKey> {
const { public_key, chain_code } = await this.ledgerCKB!.getWalletExtendedPublicKey(this.defaultPath)
// The ledger wallet's public key is unzipped, so zip it to 33 bytes https://en.bitcoin.it/wiki/BIP_0032 serP(P)

Check warning on line 58 in packages/neuron-wallet/src/services/hardware/ledger.ts

View workflow job for this annotation

GitHub Actions / Check spell

"ser" should be "set".
return {
publicKey: public_key,
publicKey: compressPublicKey(public_key),
chainCode: chain_code,
}
}
Expand Down
39 changes: 39 additions & 0 deletions packages/neuron-wallet/tests/services/hardware/ledger.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Ledger from '../../../src/services/hardware/ledger'

const getWalletExtendedPublicKeyMock = jest.fn()

jest.mock('hw-app-ckb', () => {
return function () {
return {
getWalletExtendedPublicKey: getWalletExtendedPublicKeyMock,
}
}
})

jest.mock('@ledgerhq/hw-transport-node-hid', () => ({
open: jest.fn(),
}))

const pk =
'04d061e9c5891f579fd548cfd22ff29f5c642714cc7e7a9215f0071ef5a5723f691757b28e31be71f09f24673eed52348e58d53bcfd26f4d96ec6bf1489eab429d'
const zipPk = '03d061e9c5891f579fd548cfd22ff29f5c642714cc7e7a9215f0071ef5a5723f69'

describe('test for ledger', () => {
describe('test getExtendedPublicKey', () => {
it('if the return pk is unzip', async () => {
getWalletExtendedPublicKeyMock.mockResolvedValueOnce({ public_key: pk, chain_code: '0x' })
const ledger = new Ledger({} as any)
await ledger.connect()
const res = await ledger.getExtendedPublicKey()
expect(res.publicKey).toBe(zipPk)
})

it('if the return pk is zip', async () => {
getWalletExtendedPublicKeyMock.mockResolvedValueOnce({ public_key: zipPk, chain_code: '0x' })
const ledger = new Ledger({} as any)
await ledger.connect()
const res = await ledger.getExtendedPublicKey()
expect(res.publicKey).toBe(zipPk)
})
})
})

1 comment on commit b81b534

@github-actions
Copy link

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 9265716889

Please sign in to comment.