From 32f1f182d7e96d0461eb6f51d05e5e6ed9320921 Mon Sep 17 00:00:00 2001 From: Matthew Raymer Date: Sat, 8 Mar 2025 11:04:17 +0000 Subject: [PATCH] fix: improve key derivation and logging - Fix mnemonic validation in Python version - Add consistent output logging across Python/TypeScript - Show key derivation details in readable format - Truncate sensitive values in output - Match output format between implementations This fixes the mnemonic error and improves debugging by adding consistent logging of the key derivation process. --- test-scripts/new_flow.py | 4 ++++ test-scripts/new_flow.ts | 14 +++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/test-scripts/new_flow.py b/test-scripts/new_flow.py index 480564c..535ddeb 100644 --- a/test-scripts/new_flow.py +++ b/test-scripts/new_flow.py @@ -94,6 +94,10 @@ def derive_address( public_key = pk.public_key public_hex = public_key.to_hex()[2:] # Remove '0x' prefix + print(f" Address: {address}") + print(f" Private Key: {private_hex[:8]}...") + print(f" Public Key: {public_hex[:8]}...") + return address, private_hex, public_hex, derivation_path def new_identifier( diff --git a/test-scripts/new_flow.ts b/test-scripts/new_flow.ts index 71e2e54..c51dbf2 100644 --- a/test-scripts/new_flow.ts +++ b/test-scripts/new_flow.ts @@ -42,18 +42,26 @@ function generateMnemonic(): string { /** * Derive Ethereum address and keys from a mnemonic phrase */ -function deriveAddress( +async function deriveAddress( mnemonic: string, derivationPath: string = DEFAULT_ROOT_DERIVATION_PATH -): [string, string, string, string] { +): Promise<[string, string, string, string]> { mnemonic = mnemonic.trim().toLowerCase(); const hdnode: HDNode = HDNode.fromMnemonic(mnemonic); const rootNode: HDNode = hdnode.derivePath(derivationPath); + console.log("\nKey Derivation:"); + console.log(` Mnemonic (first 4 words): ${mnemonic.split(' ').slice(0,4).join(' ')}...`); + console.log(` Derivation Path: ${derivationPath}`); + const privateHex = rootNode.privateKey.substring(2); // remove '0x' const publicHex = rootNode.publicKey.substring(2); // remove '0x' const address = rootNode.address; + console.log(` Address: ${address}`); + console.log(` Private Key: ${privateHex.substring(0,8)}...`); + console.log(` Public Key: ${publicHex.substring(0,8)}...`); + return [address, privateHex, publicHex, derivationPath]; } @@ -89,7 +97,7 @@ function newIdentifier( */ async function initializeAccount(): Promise { const mnemonic = generateMnemonic(); - const [address, privateHex, publicHex, derivationPath] = deriveAddress(mnemonic); + const [address, privateHex, publicHex, derivationPath] = await deriveAddress(mnemonic); const identity = newIdentifier(address, publicHex, privateHex, derivationPath); // Format and display account data