@ -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