From fac868dd1b8792e22f2798cb0ad6a48acf16bdf0 Mon Sep 17 00:00:00 2001 From: Luis Covarrubias Date: Fri, 13 Feb 2026 10:06:25 -0800 Subject: [PATCH] refactor: address PR #160 review feedback - Replace signWithSecretKey(string) with signWithKeypair(Keypair) for type safety and to avoid needless base58 decoding at the call site - Remove duplicate toBase58() from Keypair (identical to getAddress()) - Add comments explaining casing conventions in enum JS conversions BTC-3025 --- packages/wasm-solana/js/keypair.ts | 8 ------- packages/wasm-solana/js/transaction.ts | 13 ++++++----- .../src/instructions/try_into_js_value.rs | 3 +++ packages/wasm-solana/src/wasm/keypair.rs | 6 ----- packages/wasm-solana/src/wasm/transaction.rs | 22 +++++++++---------- 5 files changed, 20 insertions(+), 32 deletions(-) diff --git a/packages/wasm-solana/js/keypair.ts b/packages/wasm-solana/js/keypair.ts index e171424..a3a1fe5 100644 --- a/packages/wasm-solana/js/keypair.ts +++ b/packages/wasm-solana/js/keypair.ts @@ -61,14 +61,6 @@ export class Keypair { return this._wasm.address(); } - /** - * Get the public key as a base58 string - * @returns The public key as a base58 string - */ - toBase58(): string { - return this._wasm.to_base58(); - } - /** * Sign a message with this keypair * @param message - The message bytes to sign diff --git a/packages/wasm-solana/js/transaction.ts b/packages/wasm-solana/js/transaction.ts index 85e835b..0fced38 100644 --- a/packages/wasm-solana/js/transaction.ts +++ b/packages/wasm-solana/js/transaction.ts @@ -1,4 +1,5 @@ import { WasmTransaction } from "./wasm/wasm_solana.js"; +import { Keypair } from "./keypair.js"; import { Pubkey } from "./pubkey.js"; /** @@ -225,15 +226,15 @@ export class Transaction { } /** - * Sign this transaction with a base58-encoded Ed25519 secret key. + * Sign this transaction with a Keypair. * - * Derives the public key from the secret, signs the transaction message, - * and places the signature at the correct signer index. + * Signs the transaction message and places the signature at the correct + * signer index. * - * @param secretKeyBase58 - The Ed25519 secret key (32-byte seed) as base58 + * @param keypair - A Keypair instance */ - signWithSecretKey(secretKeyBase58: string): void { - this._wasm.sign_with_secret_key(secretKeyBase58); + signWithKeypair(keypair: Keypair): void { + this._wasm.sign_with_keypair(keypair.wasm); } /** diff --git a/packages/wasm-solana/src/instructions/try_into_js_value.rs b/packages/wasm-solana/src/instructions/try_into_js_value.rs index e153418..8b1aa72 100644 --- a/packages/wasm-solana/src/instructions/try_into_js_value.rs +++ b/packages/wasm-solana/src/instructions/try_into_js_value.rs @@ -15,6 +15,7 @@ use crate::intent::{AuthorizeType, KeypairPurpose, StakingType}; // Enum → JS string conversions // ============================================================================= +// UPPER_CASE: matches BitGo platform enum format impl TryIntoJsValue for StakingType { fn try_to_js_value(&self) -> Result { let s = match self { @@ -26,6 +27,7 @@ impl TryIntoJsValue for StakingType { } } +// PascalCase: matches Solana SDK StakeAuthorize variant names impl TryIntoJsValue for AuthorizeType { fn try_to_js_value(&self) -> Result { let s = match self { @@ -36,6 +38,7 @@ impl TryIntoJsValue for AuthorizeType { } } +// camelCase: matches JS/TS field naming convention impl TryIntoJsValue for KeypairPurpose { fn try_to_js_value(&self) -> Result { let s = match self { diff --git a/packages/wasm-solana/src/wasm/keypair.rs b/packages/wasm-solana/src/wasm/keypair.rs index 7a24a85..5361eac 100644 --- a/packages/wasm-solana/src/wasm/keypair.rs +++ b/packages/wasm-solana/src/wasm/keypair.rs @@ -55,12 +55,6 @@ impl WasmKeypair { self.inner.address() } - /// Get the public key as a base58 string. - #[wasm_bindgen] - pub fn to_base58(&self) -> String { - self.inner.address() - } - /// Sign a message with this keypair and return the 64-byte Ed25519 signature. /// /// @param message - The message bytes to sign diff --git a/packages/wasm-solana/src/wasm/transaction.rs b/packages/wasm-solana/src/wasm/transaction.rs index 97af423..a6e571e 100644 --- a/packages/wasm-solana/src/wasm/transaction.rs +++ b/packages/wasm-solana/src/wasm/transaction.rs @@ -8,6 +8,7 @@ use crate::error::WasmSolanaError; use crate::transaction::{Transaction, TransactionExt}; use crate::versioned::{detect_transaction_version, TxVersion, VersionedTransactionExt}; +use crate::wasm::keypair::WasmKeypair; use solana_message::VersionedMessage; use solana_sdk::bs58; use solana_transaction::versioned::VersionedTransaction; @@ -135,24 +136,21 @@ impl WasmTransaction { self.inner.signer_index(pubkey) } - /// Sign this transaction with a base58-encoded Ed25519 secret key. + /// Sign this transaction with a `WasmKeypair`. /// - /// Derives the public key from the secret, signs the transaction message, - /// and places the signature at the correct signer index. + /// Signs the transaction message and places the signature at the correct + /// signer index. /// - /// @param secret_key_base58 - The Ed25519 secret key (32-byte seed) as base58 + /// @param keypair - A WasmKeypair instance #[wasm_bindgen] - pub fn sign_with_secret_key(&mut self, secret_key_base58: &str) -> Result<(), WasmSolanaError> { - use crate::keypair::{Keypair, KeypairExt}; + pub fn sign_with_keypair(&mut self, keypair: &WasmKeypair) -> Result<(), WasmSolanaError> { + use crate::keypair::KeypairExt; use solana_signer::Signer; - let secret_bytes: Vec = bs58::decode(secret_key_base58) - .into_vec() - .map_err(|e| WasmSolanaError::new(&format!("Failed to decode secret key: {}", e)))?; - let keypair = Keypair::from_secret_key_bytes(&secret_bytes)?; + let inner = keypair.inner(); let message_bytes = self.inner.message.serialize(); - let signature = keypair.sign_message(&message_bytes); - let address = keypair.address(); + let signature = inner.sign_message(&message_bytes); + let address = inner.address(); self.inner.add_signature(&address, signature.as_ref()) }