Skip to content

Keys & Crypto API Reference

Keys

PubKey

Construction Examples:

  • xpub661MyMwAqRbcFvXwqbgwigDczeocqeBEibKMCkoc31RiyB464Ybc1z8sWMnR38JdeCBJPPkSM7mKahcBX2nPX9KYVTz3cotpLmSkMxrp99L
  • pubkey(03b2123025f45648c3f31fd4b7d3e1ec3344769ab3f53dec5af9b8a9a95385cbd5)
  • [0f9e8d7c/3]b2123025f45648c3f31fd4b7d3e1ec3344769ab3f53dec5af9b8a9a95385cbd5

→ See Key Construction and Key Structure for more details.

PubKey Variations:

Coerces from:

To fully preserve key origin information, use the str()/repr() encoding instead of Bytes.

Underlying type: miniscript::DescriptorPublicKey

pubkey()

(Not to be confused with pk())

identifier Hash

The key identifier as defined in BIP 32 (HASH160 of the public key).

fingerprint Hash

The key fingerprint as defined in BIP 32 (first four bytes of the identifier).

is_xpub Bool

Aliased as: PubKey->is_xkey

is_definite Bool

is_wildcard Bool

Whether this key has an underived /* wildcard modifier.

is_multipath Bool

Whether this is a multi-path key.

is_xonly Bool

Whether this is an X-only key (X-only are always SingleKey).

master_fingerprint Hash

0x00000000 if there's none.

derivation_path Array<Int> opt

The full derivation path from the master_fingerprint.

Only available for single-path keys.

derivation_paths Array<Array<Int>>

For multi-path keys, an array of all expanded derivation paths.

For single-path keys, an array with a single element equal to derivation_path.

chain_code Hash opt

The chain code as defined in BIP 32.

Only available for definite Xpub keys.

SecKey

Construction Examples:

  • xprv9s21ZrQH143K37CBUxq5bSXTyHwKc5KwYWsm6onCYDmiPBBDyiz3bYwvHYH9NXzsY6mDiXmhf77Ym2EJkGreLHB3s6MH5tRkfKQT9uDQ4r2
  • L4oC7AMPJkKPwuVipfFaKcJKBhsR3kmXy89f7oAFhSQSPWLNXocJ (WIF)
  • seckey(e215fcbcb22c3118fb1974b7188922b760b14a24f94d06d5b45d127c278aba77)

→ See Key Construction and Key Structure for more details.

SecKey Variations:

Coerces from:

To fully preserve key origin information, use the str()/repr() encoding instead of Bytes.

Underlying type: miniscript::DescriptorSecretKey

seckey()

seckey(Bytes|SecKey) -> SecKey

pubkey PubKey

is_xpriv Bool

Aliased as: SecKey->is_xkey

...

With the exception of is_xpub and is_xonly, all of the PubKey fields are available on SecKey too. They are not repeated here for brevity.

seckey::rand()

seckey::rand(network: Network = testnet) -> SecKey<SingleKey>

Generate a random single secret key.

xpriv::rand()

xpriv::rand(network: Network = testnet) -> SecKey<Xpriv>

Generate a random Xpriv.

xpriv::from_seed()

xpriv::from_seed(seed: Bytes, network: Network = testnet) -> SecKey<Xpriv>

Construct Xpriv from seed bytes.

xpriv::from_mnemonic()

xpriv::from_mnemonic(mnemonic: String, network: Network = testnet, passphrase: String = "", language: String = "english") -> SecKey<Xpriv>

Construct Xpriv from a BIP 39 mnemonic phrase.

Alias for bip39::to_xpriv() and also possible using bip39().

See the BIP 39 reference for more information.

xonly()

singles()

This should also work with Policy<MultiPath>, but currently doesn't.

derived()

derived(PubKey<Xpub>) -> PubKey<SingleKey>
derived(SecKey<Xpriv>) -> SecKey<SingleKey>

xderived()

xderived(PubKey<Xpub>) -> PubKey<Xpub>
xderived(SecKey<Xpriv>) -> SecKey<Xpriv>

xNUMS()

xNUMS(seed: Bytes, version: Bytes = BIP32_MAIN) -> PubKey<Xpub>

Derive a unique, deterministic, provably unspendable Xpub based on the given seed. Unlike NUMS, outside observers cannot tell its unspendable without access to the seed.

NUMS PubKey<XOnly>

NUMS = 0x50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0

The provably unspendable NUMS key suggested in BIP 341 for use as a Taproot internal key.

BIP32_MAIN Bytes

BIP32_MAIN = 0x0488b21e

BIP32_TEST Bytes

BIP32_TEST = 0x043587cf

KeySource

Hashing

hash::sha256()

hash::sha256(Bytes) -> Hash

hash::sha256d()

hash::sha256d(Bytes) -> Hash

hash::ripemd160()

hash::ripemd160(Bytes) -> Hash

hash::hash160()

hash::hash160(Bytes) -> Hash

Hash

Signing

ecdsa::sign()

ecdsa::sign(sk: SecKey<Definite>, msg: Hash, compact_sig: Bool = false) -> Bytes

ecdsa::verify()

ecdsa::verify(pk: PubKey<Definite>, msg: Hash, signature: Bytes) -> Bool

schnorr::sign()

schnorr::sign(sk: SecKey<Definite>, msg: Hash, aux_rand: Bool = false) -> Bytes

schnorr::verify()

schnorr::verify(pk: PubKey<Definite>, msg: Hash, signature: Bytes) -> Bool

RNG

Minsc provides a CSPRNG based on the rand crate's default ThreadRng, which uses ChaCha12 with OS RNG seeding. See its Security section for more details.

Deterministic randomness based on an explicit seed is supported by providing the seed parameter. The seed can be any Bytes sequence (or Bytes-coercible, including Strings and Ints), and will be hashed using SHA256 to seed ChaCha12.

When used without a seed, the RNG is the only possible source of non-determinism in Minsc.

rand::bytes()

rand::bytes(size: Int, seed: Bytes = None) -> Bytes

Generate a random Bytes sequence of length size

rand::i64()

rand::i64(seed: Bytes = None) -> Int

Generate a random signed 64-bit integer in the [−2⁶³ , 2⁶³−1] range

rand::f64()

rand::f64(seed: Bytes = None) -> Float

Generate a random 64-bit float in the [0.0 , 1.0) range

rand::u32()

rand::u32(seed: Bytes = None) -> Int

Generate a random unsigned 32-bit integer in the [0 , 2³²−1] range

rand::i32()

rand::i32(seed: Bytes = None) -> Int

Generate a random signed 32-bit integer in the [−2³¹ , 2³¹−1] range

rand::range()

rand::range(min: Int, max: Int, seed: Bytes = None) -> Int

Generate a random integer in the [min , max] range (min <= N <= max)

rand::frange()

rand::frange(min: Float, max: Float, seed: Bytes = None) -> Float

Generate a random float in the [min, max) range (min <= N < max)