Skip to content

Commit

Permalink
feat: add ability to skip public key generation (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
kewde authored Nov 27, 2024
1 parent 5543631 commit 4c87915
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
32 changes: 32 additions & 0 deletions features/keychain/api/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,38 @@ describe('keychain api', () => {
})
})

test('export privateKey without publicKey', async () => {
await expect(
api.exportKey({ seedId, keyId: solanaKeyId, exportPrivate: true, exportPublic: false })
).resolves.toEqual({
privateKey: Buffer.from(
'89e0d7573648f0e08d62491cf8cf01404b198e5316789089845e0f9349f06e21',
'hex'
),
publicKey: null,
xpriv:
'xprvA3aSZco2ba6qU2FmVowiad82ZHahVhAo6RA9XX7APo9aCMhaJhEjJxRRAwCB6mojPMJKvuZmG1nKmuLZb2VgZF8WpjNA9QtBgAxnNshT1iN',
xpub: null,
})
await expect(
api.exportKey({
seedId: otherSeedId,
keyId: solanaKeyId,
exportPrivate: true,
exportPublic: false,
})
).resolves.toEqual({
privateKey: Buffer.from(
'107bfcb5d268f0c8a6d5ade844607a96e42796fcc4410e49f5438f517f82ea1a',
'hex'
),
publicKey: null,
xpriv:
'xprvA3ZsPdqNsXe4VXohMHUmVcfUDqUG2et8Nia5wTQQGERE8S4uipLuNQjYzhMrN32rquhFhCoBZwHB1HuYw9p16UxF36DwJmiaDL3xDjfAZVD',
xpub: null,
})
})

test('export publicKey', async () => {
await expect(api.exportKey({ seedId, keyId: solanaKeyId })).resolves.toEqual({
privateKey: null,
Expand Down
1 change: 1 addition & 0 deletions features/keychain/api/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface KeychainApi {
exportKey(params: KeySource): Promise<PublicKeys>
exportKey(params: { exportPrivate: false } & KeySource): Promise<PublicKeys>
exportKey(params: { exportPrivate: true } & KeySource): Promise<PublicKeys & PrivateKeys>
exportKey(params: { exportPrivate: true; exportPublic: false } & KeySource): Promise<PrivateKeys>
arePrivateKeysLocked(seeds: Buffer[]): boolean
removeSeeds(seeds: Buffer[]): string[]
sodium: {
Expand Down
31 changes: 17 additions & 14 deletions features/keychain/module/keychain.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export class Keychain {
return this.#masters[seedId][derivationAlgorithm].derive(derivationPath)
}

async exportKey({ seedId, keyId, exportPrivate }) {
async exportKey({ seedId, keyId, exportPrivate, exportPublic = true }) {
assert(typeof seedId === 'string', 'seedId must be a string')

if (exportPrivate) {
Expand All @@ -157,24 +157,27 @@ export class Keychain {
getPrivateHDKeySymbol: this.#getPrivateHDKeySymbol,
})
const privateKey = hdkey.privateKey
let publicKey = hdkey.publicKey

if (keyId.keyType === 'legacy') {
if (keyId.assetName in this.#legacyPrivToPub) {
const legacyPrivToPub = this.#legacyPrivToPub[keyId.assetName]
publicKey = await legacyPrivToPub(privateKey)
} else {
throw new Error(`asset name ${keyId.assetName} has no legacyPrivToPub mapper`)
let publicKey = null

if (exportPublic) {
publicKey = hdkey.publicKey
if (keyId.keyType === 'legacy') {
if (keyId.assetName in this.#legacyPrivToPub) {
const legacyPrivToPub = this.#legacyPrivToPub[keyId.assetName]
publicKey = await legacyPrivToPub(privateKey)
} else {
throw new Error(`asset name ${keyId.assetName} has no legacyPrivToPub mapper`)
}
} else if (keyId.derivationAlgorithm !== 'SLIP10' && keyId.keyType === 'nacl') {
// SLIP10 already produces the correct public key for curve ed25119
// so we can safely skip using the privToPub mapper.
publicKey = await sodium.privToPub(privateKey)
}
} else if (keyId.derivationAlgorithm !== 'SLIP10' && keyId.keyType === 'nacl') {
// SLIP10 already produces the correct public key for curve ed25119
// so we can safely skip using the privToPub mapper.
publicKey = await sodium.privToPub(privateKey)
}

const { xpriv, xpub } = hdkey.toJSON()
return {
xpub,
xpub: exportPublic ? xpub : null,
xpriv: exportPrivate ? xpriv : null,
publicKey,
privateKey: exportPrivate ? privateKey : null,
Expand Down

0 comments on commit 4c87915

Please sign in to comment.