Function deriveHdPath

  • Derive a child HD node from a parent node given a derivation path. The resulting node is the same type as the parent node (private nodes return private nodes, public nodes return public nodes).

    Remarks

    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.

    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 HdPublicNode)

    • path: string

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

    • crypto: {
          ripemd160: {
              hash: ((input: Uint8Array) => Uint8Array);
          };
          secp256k1: {
              addTweakPrivateKey: ((privateKey: Uint8Array, tweakValue: Uint8Array) => string | Uint8Array);
              addTweakPublicKeyCompressed: ((publicKey: Uint8Array, tweakValue: Uint8Array) => string | Uint8Array);
              derivePublicKeyCompressed: ((privateKey: Uint8Array) => string | Uint8Array);
          };
          sha256: {
              hash: ((input: Uint8Array) => Uint8Array);
          };
          sha512: {
              hash: ((input: Uint8Array) => Uint8Array);
          };
      } = ...

      an optional object containing implementations of sha256, sha512, ripemd160, and secp256k1 derivation functions

      • ripemd160: {
            hash: ((input: Uint8Array) => Uint8Array);
        }
        • hash: ((input: Uint8Array) => Uint8Array)
            • (input: Uint8Array): Uint8Array
            • Returns the ripemd160 hash of the provided input.

              To incrementally construct a ripemd160 hash (e.g. for streaming), use init, update, and final.

              Parameters

              • input: Uint8Array

                a Uint8Array to be hashed using ripemd160

              Returns Uint8Array

      • secp256k1: {
            addTweakPrivateKey: ((privateKey: Uint8Array, tweakValue: Uint8Array) => string | Uint8Array);
            addTweakPublicKeyCompressed: ((publicKey: Uint8Array, tweakValue: Uint8Array) => string | Uint8Array);
            derivePublicKeyCompressed: ((privateKey: Uint8Array) => string | Uint8Array);
        }
        • addTweakPrivateKey: ((privateKey: Uint8Array, tweakValue: Uint8Array) => string | Uint8Array)
            • (privateKey: Uint8Array, tweakValue: Uint8Array): string | Uint8Array
            • Tweak a privateKey by adding tweakValue to it.

              Returns an error message if the private key is invalid or if the addition fails.

              Parameters

              • privateKey: Uint8Array

                a valid secp256k1 private key

              • tweakValue: Uint8Array

                256 bit value to tweak by (BE)

              Returns string | Uint8Array

        • addTweakPublicKeyCompressed: ((publicKey: Uint8Array, tweakValue: Uint8Array) => string | Uint8Array)
            • (publicKey: Uint8Array, tweakValue: Uint8Array): string | Uint8Array
            • Tweak a publicKey by adding tweakValue times the generator to it.

              Returns an error message if the provided public key could not be parsed or is not valid, or if the addition failed.

              The returned public key will be in compressed format.

              Parameters

              • publicKey: Uint8Array

                a public key.

              • tweakValue: Uint8Array

                256 bit value to tweak by (BE)

              Returns string | Uint8Array

        • derivePublicKeyCompressed: ((privateKey: Uint8Array) => string | Uint8Array)
            • (privateKey: Uint8Array): string | Uint8Array
            • Derive a compressed public key from a valid secp256k1 private key.

              Returns an error message if the provided private key is too large (see validatePrivateKey).

              Parameters

              • privateKey: Uint8Array

                a valid secp256k1, 32-byte private key

              Returns string | Uint8Array

      • sha256: {
            hash: ((input: Uint8Array) => Uint8Array);
        }
        • hash: ((input: Uint8Array) => Uint8Array)
            • (input: Uint8Array): Uint8Array
            • Returns the sha256 hash of the provided input.

              To incrementally construct a sha256 hash (e.g. for streaming), use init, update, and final.

              Parameters

              • input: Uint8Array

                a Uint8Array to be hashed using sha256

              Returns Uint8Array

      • sha512: {
            hash: ((input: Uint8Array) => Uint8Array);
        }
        • hash: ((input: Uint8Array) => Uint8Array)
            • (input: Uint8Array): Uint8Array
            • Returns the sha512 hash of the provided input.

              To incrementally construct a sha512 hash (e.g. for streaming), use init, update, and final.

              Parameters

              • input: Uint8Array

                a Uint8Array to be hashed using sha512

              Returns Uint8Array

    Returns invalidDerivationPath | invalidPrivateDerivationPrefix | invalidPublicDerivationPrefix | ReductionResults<NodeType>

Generated using TypeDoc