forked from trent_larson/crowd-funder-for-time-pwa
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.
This commit is contained in:
@@ -94,6 +94,10 @@ def derive_address(
|
|||||||
public_key = pk.public_key
|
public_key = pk.public_key
|
||||||
public_hex = public_key.to_hex()[2:] # Remove '0x' prefix
|
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
|
return address, private_hex, public_hex, derivation_path
|
||||||
|
|
||||||
def new_identifier(
|
def new_identifier(
|
||||||
|
|||||||
@@ -42,18 +42,26 @@ function generateMnemonic(): string {
|
|||||||
/**
|
/**
|
||||||
* Derive Ethereum address and keys from a mnemonic phrase
|
* Derive Ethereum address and keys from a mnemonic phrase
|
||||||
*/
|
*/
|
||||||
function deriveAddress(
|
async function deriveAddress(
|
||||||
mnemonic: string,
|
mnemonic: string,
|
||||||
derivationPath: string = DEFAULT_ROOT_DERIVATION_PATH
|
derivationPath: string = DEFAULT_ROOT_DERIVATION_PATH
|
||||||
): [string, string, string, string] {
|
): Promise<[string, string, string, string]> {
|
||||||
mnemonic = mnemonic.trim().toLowerCase();
|
mnemonic = mnemonic.trim().toLowerCase();
|
||||||
const hdnode: HDNode = HDNode.fromMnemonic(mnemonic);
|
const hdnode: HDNode = HDNode.fromMnemonic(mnemonic);
|
||||||
const rootNode: HDNode = hdnode.derivePath(derivationPath);
|
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 privateHex = rootNode.privateKey.substring(2); // remove '0x'
|
||||||
const publicHex = rootNode.publicKey.substring(2); // remove '0x'
|
const publicHex = rootNode.publicKey.substring(2); // remove '0x'
|
||||||
const address = rootNode.address;
|
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];
|
return [address, privateHex, publicHex, derivationPath];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,7 +97,7 @@ function newIdentifier(
|
|||||||
*/
|
*/
|
||||||
async function initializeAccount(): Promise<IIdentifier> {
|
async function initializeAccount(): Promise<IIdentifier> {
|
||||||
const mnemonic = generateMnemonic();
|
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);
|
const identity = newIdentifier(address, publicHex, privateHex, derivationPath);
|
||||||
|
|
||||||
// Format and display account data
|
// Format and display account data
|
||||||
|
|||||||
Reference in New Issue
Block a user