From 83961b5178b352ea938008c70f8a611656f01abd Mon Sep 17 00:00:00 2001
From: Chris Duncan <chris@zoso.dev>
Date: Fri, 29 Nov 2024 02:07:25 -0800
Subject: [PATCH] Simplify ckd implementation with fewer allowed typings.

---
 src/lib/wallet.ts | 35 ++++++++++++++++++-----------------
 1 file changed, 18 insertions(+), 17 deletions(-)

diff --git a/src/lib/wallet.ts b/src/lib/wallet.ts
index 43e74ed..d8c4693 100644
--- a/src/lib/wallet.ts
+++ b/src/lib/wallet.ts
@@ -49,7 +49,7 @@ abstract class Wallet {
 		return ''
 	}
 
-	abstract ckd (index: number | number[]): Promise<KeyPair> | Promise<KeyPair[]>
+	abstract ckd (index: number[]): Promise<KeyPair[]>
 
 	constructor (seed?: string, mnemonic?: Bip39Mnemonic, id?: string) {
 		if (this.constructor === Wallet) {
@@ -96,7 +96,6 @@ abstract class Wallet {
 		}
 		if (indexes.length > 0) {
 			let results = await this.ckd(indexes)
-			if (!Array.isArray(results)) results = [results]
 			const data: any = []
 			results.forEach(r => data.push({ privateKey: hex.toBytes(r.privateKey as string), index: r.index }))
 			let now = performance.now()
@@ -426,13 +425,12 @@ export class Bip44Wallet extends Wallet {
 	/**
 	* Derives BIP-44 Nano account private keys.
 	*
-	* @param {number} index - Index of the account
+	* @param {number[]} indexes - Indexes of the accounts
 	* @returns {Promise<Account>}
 	*/
-	async ckd (index: number | number[]): Promise<KeyPair[]> {
-		if (!Array.isArray(index)) index = [index]
+	async ckd (indexes: number[]): Promise<KeyPair[]> {
 		const data: any = []
-		index.forEach(i => data.push({ seed: this.seed, index: i }))
+		indexes.forEach(i => data.push({ seed: this.seed, index: i }))
 		let now = performance.now()
 		const privateKeys: KeyPair[] = await this.#pool.work(data)
 		console.log(`ckd: ${-now + (now = performance.now())} ms`)
@@ -573,14 +571,12 @@ export class Blake2bWallet extends Wallet {
 	/**
 	* Derives BLAKE2b account private keys.
 	*
-	* @param {number} index - Index of the account
+	* @param {number[]} indexes - Indexes of the accounts
 	* @returns {Promise<Account>}
 	*/
-	async ckd (index: number | number[]): Promise<KeyPair[]> {
-		if (!Array.isArray(index)) index = [index]
-		const data: any = []
+	async ckd (indexes: number[]): Promise<KeyPair[]> {
 		let now = performance.now()
-		const results = index.map(index => {
+		const results = indexes.map(index => {
 			const indexHex = index.toString(16).padStart(8, '0').toUpperCase()
 			const inputHex = `${this.seed}${indexHex}`.padStart(72, '0')
 			const inputArray = (inputHex.match(/.{1,2}/g) ?? []).map(h => parseInt(h, 16))
@@ -651,15 +647,20 @@ export class LedgerWallet extends Wallet {
 	/**
 	* Gets the public key for an account from the Ledger device.
 	*
-	* @param {number} index - Index of the account
+	* @param {number[]} indexes - Indexes of the accounts
 	* @returns {Promise<Account>}
 	*/
-	async ckd (index: number): Promise<KeyPair> {
-		const { status, publicKey } = await this.ledger.account(index)
-		if (status === 'OK' && publicKey != null) {
-			return { publicKey, index }
+	async ckd (indexes: number[]): Promise<KeyPair[]> {
+		const results: KeyPair[] = []
+		for (const index of indexes) {
+			const { status, publicKey } = await this.ledger.account(index)
+			if (status === 'OK' && publicKey != null) {
+				results.push({ publicKey, index })
+			} else {
+				throw new Error(`Error getting Ledger account: ${status}`)
+			}
 		}
-		throw new Error(`Error getting Ledger account: ${status}`)
+		return results
 	}
 
 	/**
-- 
2.34.1