Hierarchical Deterministic Node


The HDNode class in js-moi-sdk represents a Hierarchical Deterministic (HD) Node used for cryptographic key generation and derivation. It follows the BIP-32 standard and allows the creation of hierarchical deterministic wallets. With HDNode, developers can generate and derive child keys, create private and public keys, derive MOI account addresses, and serialize/deserialize HDNode instances. It simplifies the management of multiple addresses and keys within a single wallet, enhancing security and organization.

HDNode

A class representing a Hierarchical Deterministic (HD) Node used in cryptographic key generation and derivation.

Methods

static HDNode.fromSeed(seed)

Generates an HDNode from a seed buffer.

Arguments:
  • seed (Buffer) – The seed buffer.

Throws:

Error – If an error occurs during the HDNode generation.

// Example
const mnemonic = "behind wish visual father ...";
const seed = await mnemonicToSeed(mnemonic);
const hdNode = HDNode.fromSeed(seed);
console.log(hdNode)

>> HDNode
static HDNode.fromExtendedKey(extendedKey)

Generates an HDNode from an extended key.

Arguments:
  • extendedKey (string) – The extended key.

Throws:

Error – If an error occurs during the HDNode generation.

// Example
const hdNode = HDNode.fromExtendedKey(...);
console.log(hdNode)

>> HDNode
HDNode.derivePath(path)

Derives a child HDNode from the current HDNode using the specified path.

Arguments:
  • path (string) – The derivation path for the child HDNode.

Throws:

Error – If the HDNode is not initialized.

Returns:

HDNode – The derived child HDNode.

// Example
const hdNode = HDNode.fromSeed(...);
const childHDNode = hdNode.derivePath("m/44'/7567'/0'/0/1");
console.log(childHDNode)

>> HDNode
HDNode.deriveChild(index)

Derives a child HDNode from the current HDNode using the specified index.

Arguments:
  • index (number) – The child index.

Throws:

Error – If the HDNode is not initialized.

Returns:

HDNode – The derived child HDNode.

// Example
const hdNode = HDNode.fromSeed(...);
const childHDNode = hdNode.deriveChild(1);
console.log(childHDNode)

>> HDNode
HDNode.publicKey()

Retrieves the public key associated with the HDNode.

Throws:

Error – If the HDNode is not initialized.

Returns:

Buffer – The public key.

// Example
const hdNode = HDNode.fromSeed(...);
const publicKey = hdNode.publicKey();
console.log(publicKey)

>> Buffer
HDNode.privateKey()

Retrieves the private key associated with the HDNode.

Throws:

Error – If the HDNode is not initialized or private key is not available.

Returns:

Buffer – The private key.

// Example
const hdNode = HDNode.fromSeed(...);
const privateKey = hdNode.privateKey();
console.log(privateKey)

>> Buffer