Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.

public final class Bip32

  1. Object
  2. Bip32

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 reads it back.

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

Fields

public static final long HARDENED = 2147483648LThe offset that marks a hardened index.
public static final int XPRV = 76066276The mainnet private version bytes, which print as xprv.
public static final int XPUB = 76067358The mainnet public version bytes, which print as xpub.
public static final int TPRV = 70615956The testnet private version bytes, which print as tprv.
public static final int TPUB = 70617039The testnet public version bytes, which print as tpub.

Methods

public static Bip32 master(byte[] seed)The master node of a seed (16 to 64 bytes, normally Mnemonic.toSeed).
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.
public int version()The version bytes this node was parsed with, or 0 for a derived node.
public Bip32 child(long index)The child at index; add HARDENED for a hardened one, which needs the private key.
public Bip32 derive(String path)Derives along a path relative to this node, such as m/84'/0'/0'/0/7.
public Bip32 derive(DerivationPath path)Derives along a path relative to this node.
public Bip32 neuter()This node without its private key.
public boolean hasPrivateKey()Tells whether this node holds its private key.
public byte[] privateKey()A copy of the 32-byte private key.
public byte[] publicKey()A copy of the 33-byte compressed public key.
public byte[] chainCode()A copy of the 32-byte chain code.
public int depth()The distance from the master node.
public long childNumber()The index this node was derived with, including HARDENED.
public int parentFingerprint()The first four bytes of the parent’s key identifier, as a big-endian int; 0 for the master node.
public int fingerprint()The first four bytes of HASH160(publicKey), as a big-endian int.
public String toXprv()The mainnet xprv serialization.
public String toXpub()The mainnet xpub serialization.
public String serialize(int versionBytes, boolean includePrivate)The serialization under any version bytes (SLIP-132 zpub, testnet tpub, …).
public void destroy()Wipes the private key and chain code.

Inherited methods

Field details

HARDENED

public static final long HARDENED = 2147483648L
The offset that marks a hardened index.

XPRV

public static final int XPRV = 76066276
The mainnet private version bytes, which print as xprv.

XPUB

public static final int XPUB = 76067358
The mainnet public version bytes, which print as xpub.

TPRV

public static final int TPRV = 70615956
The testnet private version bytes, which print as tprv.

TPUB

public static final int TPUB = 70617039
The testnet public version bytes, which print as tpub.

Method details

master

public static Bip32 master(byte[] seed)
The master node of a seed (16 to 64 bytes, normally Mnemonic.toSeed).

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 parsed 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 int
the four version bytes as a big-endian int
includePrivate boolean
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.