Function deriveHdPath

  • Derive a child HD node from a master node given an absolute derivation path. The resulting node is the same type as the parent node – private nodes return private nodes, public nodes return public nodes. (To prevent implementation errors, this function will not internally derive a public node from any private node; for public derivation, use deriveHdPublicNode at the desired BIP32 account level and provide the HD public key to this function.)

    The derivation path uses the notation specified in BIP32: the first character must be either m for private derivation or M for public derivation, followed by sets of / and a number representing the child index used in the derivation at that depth. Hardened derivation is represented by a trailing ', and may only appear in private derivation paths (hardened derivation requires knowledge of the private key). Hardened child indexes are represented with the hardened index offset (2147483648) subtracted.

    For example, m/0/1'/2 uses private derivation (m), with child indexes in the following order:

    derivePrivate(derivePrivate(derivePrivate(node, 0), 2147483648 + 1), 2)

    Likewise, M/3/4/5 uses public derivation (M), with child indexes in the following order:

    derivePublic(derivePublic(derivePublic(node, 3), 4), 5)

    Because hardened derivation requires a private node, paths that specify public derivation (M) using hardened derivation (') will return an error. To derive the public node associated with a child private node that requires hardened derivation, begin with private derivation, then provide the result to deriveHdPublicNode or deriveHdPathRelative.

    By default, this function throws an Error rather than returning the error as string when the provided path is invalid or cannot be derived from the provided HD node (e.g. the path requests an excessive child index, a hardened path is requested from a public node, or an astronomically rare HMAC-SHA512 result produces and invalid node).

    While the throwing behavior is reasonable for the common case of deriving known, fixed paths (e.g. the BCH account as standardized by SLIP44 at m/44'/145'/0'), it is recommended that throwErrors be set to false for use cases where dynamic or user-specified paths might be derived. In these cases, deliberate error handling is recommended, e.g. saving any data and safely shutting down, displaying troubleshooting information to the user, etc.

    Type Parameters

    Parameters

    • node: NodeType

      The HD node from which to begin the derivation – for paths beginning with m, an HdPrivateNodeValid; for paths beginning with M, an HdPublicNodeValid.

    • path: Path

      The BIP32 derivation path, e.g. m/0/1'/2 or M/3/4/5.

    • __namedParameters: {
          crypto?: {
              ripemd160: {
                  hash: ((input) => Uint8Array) & ((input) => Uint8Array);
              };
              secp256k1: {
                  addTweakPrivateKey: ((privateKey, tweakValue) => string | Uint8Array);
                  addTweakPublicKeyCompressed: ((publicKey, tweakValue) => string | Uint8Array);
                  derivePublicKeyCompressed: ((privateKey) => string | Uint8Array);
              };
              sha256: {
                  hash: ((input) => Uint8Array) & ((input) => Uint8Array);
              };
              sha512: {
                  hash: ((input) => Uint8Array) & ((input) => Uint8Array);
              };
          };
          throwErrors?: ThrowErrors;
      } = {}
      • Optional crypto?: {
            ripemd160: {
                hash: ((input) => Uint8Array) & ((input) => Uint8Array);
            };
            secp256k1: {
                addTweakPrivateKey: ((privateKey, tweakValue) => string | Uint8Array);
                addTweakPublicKeyCompressed: ((publicKey, tweakValue) => string | Uint8Array);
                derivePublicKeyCompressed: ((privateKey) => string | Uint8Array);
            };
            sha256: {
                hash: ((input) => Uint8Array) & ((input) => Uint8Array);
            };
            sha512: {
                hash: ((input) => Uint8Array) & ((input) => Uint8Array);
            };
        }

        An optional object containing implementations of sha256 hashing, sha512 hashing, ripemd160 hashing, and secp256k1 derivation functions.

        • ripemd160: {
              hash: ((input) => Uint8Array) & ((input) => Uint8Array);
          }
          • hash: ((input) => Uint8Array) & ((input) => Uint8Array)
        • secp256k1: {
              addTweakPrivateKey: ((privateKey, tweakValue) => string | Uint8Array);
              addTweakPublicKeyCompressed: ((publicKey, tweakValue) => string | Uint8Array);
              derivePublicKeyCompressed: ((privateKey) => string | Uint8Array);
          }
          • addTweakPrivateKey: ((privateKey, tweakValue) => string | Uint8Array)
              • (privateKey, tweakValue): string | Uint8Array
              • Parameters

                • privateKey: Uint8Array
                • tweakValue: Uint8Array

                Returns string | Uint8Array

          • addTweakPublicKeyCompressed: ((publicKey, tweakValue) => string | Uint8Array)
              • (publicKey, tweakValue): string | Uint8Array
              • Parameters

                • publicKey: Uint8Array
                • tweakValue: Uint8Array

                Returns string | Uint8Array

          • derivePublicKeyCompressed: ((privateKey) => string | Uint8Array)
              • (privateKey): string | Uint8Array
              • Parameters

                • privateKey: Uint8Array

                Returns string | Uint8Array

        • sha256: {
              hash: ((input) => Uint8Array) & ((input) => Uint8Array);
          }
          • hash: ((input) => Uint8Array) & ((input) => Uint8Array)
        • sha512: {
              hash: ((input) => Uint8Array) & ((input) => Uint8Array);
          }
          • hash: ((input) => Uint8Array) & ((input) => Uint8Array)
      • Optional throwErrors?: ThrowErrors

        If true, this function will throw an Error rather than returning the error as a string when the provided path is invalid or cannot be derived from the provided HD node (defaults to true).

    Returns ThrowErrors extends true
        ? AbsoluteDerivation<NodeType, Path>
        : string | AbsoluteDerivation<NodeType, Path>

Generated using TypeDoc