Class Bip32

java.lang.Object
com.codename1.crypto.hd.Bip32

public final class Bip32 extends Object

A BIP-32 hierarchical deterministic key on secp256k1: Bitcoin, every EVM chain and most others derive their accounts this way from one Mnemonic seed.

A node holds a private key, or only the public key once neuter() has dropped it; a public node still derives every non-hardened child, which is how a watch-only server hands out deposit addresses without holding a secret. toXprv() and toXpub() write the standard serialization and parse(String) reads it back.

Private key material lives in arrays this object owns; call destroy() to wipe them.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final long
    The offset that marks a hardened index.
    static final int
    The testnet private version bytes, which print as tprv.
    static final int
    The testnet public version bytes, which print as tpub.
    static final int
    The mainnet private version bytes, which print as xprv.
    static final int
    The mainnet public version bytes, which print as xpub.
  • Method Summary

    Modifier and Type
    Method
    Description
    byte[]
    A copy of the 32-byte chain code.
    child(long index)
    The child at index; add HARDENED for a hardened one, which needs the private key.
    long
    The index this node was derived with, including HARDENED.
    int
    The distance from the master node.
    Derives along a path relative to this node.
    derive(String path)
    Derives along a path relative to this node, such as m/84'/0'/0'/0/7.
    void
    Wipes the private key and chain code.
    int
    The first four bytes of HASH160(publicKey), as a big-endian int.
    boolean
    Tells whether this node holds its private key.
    static Bip32
    master(byte[] seed)
    The master node of a seed (16 to 64 bytes, normally Mnemonic.toSeed(String, String)).
    This node without its private key.
    int
    The first four bytes of the parent's key identifier, as a big-endian int; 0 for the master node.
    static Bip32
    parse(String extendedKey)
    Reads an extended key in its Base58Check form (xprv..., xpub..., tprv... or any other version bytes); version tells which.
    byte[]
    A copy of the 32-byte private key.
    byte[]
    A copy of the 33-byte compressed public key.
    serialize(int versionBytes, boolean includePrivate)
    The serialization under any version bytes (SLIP-132 zpub, testnet tpub, ...).
    The mainnet xprv serialization.
    The mainnet xpub serialization.
    int
    The version bytes this node was parse(String)d with, or 0 for a derived node.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • HARDENED

      public static final long HARDENED
      The offset that marks a hardened index.
      See Also:
    • XPRV

      public static final int XPRV
      The mainnet private version bytes, which print as xprv.
      See Also:
    • XPUB

      public static final int XPUB
      The mainnet public version bytes, which print as xpub.
      See Also:
    • TPRV

      public static final int TPRV
      The testnet private version bytes, which print as tprv.
      See Also:
    • TPUB

      public static final int TPUB
      The testnet public version bytes, which print as tpub.
      See Also:
  • Method Details

    • master

      public static Bip32 master(byte[] seed)

      The master node of a seed (16 to 64 bytes, normally Mnemonic.toSeed(String, String)).

      Throws
      • IllegalArgumentException: for a seed out of range, or one whose master key is invalid (probability below 2^-127)
    • parse

      public static Bip32 parse(String extendedKey)

      Reads an extended key in its Base58Check form (xprv..., xpub..., tprv... or any other version bytes); version tells which.

      Throws
      • IllegalArgumentException: for a malformed string, a bad checksum or a key that is not valid on the curve
    • version

      public int version()
      The version bytes this node was parse(String)d with, or 0 for a derived node.
    • child

      public Bip32 child(long index)
      The child at index; add HARDENED for a hardened one, which needs the private key. When an index yields an invalid key, as BIP-32 allows with probability below 2^-127, the next index is used.
    • derive

      public Bip32 derive(String path)
      Derives along a path relative to this node, such as m/84'/0'/0'/0/7.
    • derive

      public Bip32 derive(DerivationPath path)
      Derives along a path relative to this node.
    • neuter

      public Bip32 neuter()
      This node without its private key.
    • hasPrivateKey

      public boolean hasPrivateKey()
      Tells whether this node holds its private key.
    • privateKey

      public byte[] privateKey()

      A copy of the 32-byte private key. Wipe it after use.

      Throws
      • IllegalStateException: on a public-only node
    • publicKey

      public byte[] publicKey()
      A copy of the 33-byte compressed public key.
    • chainCode

      public byte[] chainCode()
      A copy of the 32-byte chain code.
    • depth

      public int depth()
      The distance from the master node.
    • childNumber

      public long childNumber()
      The index this node was derived with, including HARDENED.
    • parentFingerprint

      public int parentFingerprint()
      The first four bytes of the parent's key identifier, as a big-endian int; 0 for the master node.
    • fingerprint

      public int fingerprint()
      The first four bytes of HASH160(publicKey), as a big-endian int.
    • toXprv

      public String toXprv()
      The mainnet xprv serialization.
    • toXpub

      public String toXpub()
      The mainnet xpub serialization.
    • serialize

      public String serialize(int versionBytes, boolean includePrivate)

      The serialization under any version bytes (SLIP-132 zpub, testnet tpub, ...).

      Parameters
      • versionBytes: the four version bytes as a big-endian int

      • includePrivate: true for the private form, which needs the private key

    • destroy

      public void destroy()
      Wipes the private key and chain code. The node is unusable afterwards.