DescriptorSecretKey

class DescriptorSecretKey(    network: Network,     mnemonic: Mnemonic,     password: String?)

An extended secret key.

Samples


fun main() { 
   //sampleStart 
   // The DescriptorSecretKey.derive() method allows you to derive an extended key for a given
// node in the derivation tree (for example to create an xpub for a particular account)

val mnemonic: Mnemonic = Mnemonic("scene change clap smart together mind wheel knee clip normal trial unusual")
val bip32RootKey: DescriptorSecretKey = DescriptorSecretKey(
    network = Network.TESTNET,
    mnemonic = mnemonic,
    password = ""
)

val bip84Account0: DerivationPath = DerivationPath("m/84h/1h/0h")
val xpubAccount0: DescriptorSecretKey = bip32RootKey.derive(bip84Account0)
println(xpubAccount0.asString())
// [5512949b/84'/1'/0']tprv8ghw3FWfWTeLCEXcr8f8Q8Lz4QPCELYv3jhBXjAm7XagA6R5hreeWLTJeLBfMj7Ni6Q3PdV1o8NbvNBHE59W97EkRJSU4JkvTQjaNUmQubE/*

val internalPath: DerivationPath = DerivationPath("m/0")
val externalExtendedKey = xpubAccount0.extend(internalPath).asString()
println(externalExtendedKey)
// [5512949b/84'/1'/0']tprv8ghw3FWfWTeLCEXcr8f8Q8Lz4QPCELYv3jhBXjAm7XagA6R5hreeWLTJeLBfMj7Ni6Q3PdV1o8NbvNBHE59W97EkRJSU4JkvTQjaNUmQubE/0/*

// to create the descriptor you'll need to use this extended key in a descriptor function,
// i.e. wpkh(), tr(), etc.
val externalDescriptor = "wpkh($externalExtendedKey)" 
   //sampleEnd
}

fun main() { 
   //sampleStart 
   // The `DescriptorSecretKey.extend()` method allows you to extend a key to any given path.

// val mnemonic: String = generateMnemonic(WordCount.WORDS12)
val mnemonic: Mnemonic = Mnemonic("scene change clap smart together mind wheel knee clip normal trial unusual")

// the initial DescriptorSecretKey will always be at the "master" node,
// i.e. the derivation path is empty
val bip32RootKey: DescriptorSecretKey = DescriptorSecretKey(
    network = Network.TESTNET,
    mnemonic = mnemonic,
    password = ""
)
println(bip32RootKey.asString())
// tprv8ZgxMBicQKsPfM8Trx2apvdEkmxbJkYY3ZsmcgKb2bfnLNcBhtCstqQTeFesMRLEJXpjGDinAUJUHprXMwph8dQBdS1HAoxEis8Knimxovf/*

// the derive method will also automatically apply the wildcard (*) to your path,
// i.e the following will generate the typical testnet BIP84 external wallet path
// m/84h/1h/0h/0/*
val bip84ExternalPath: DerivationPath = DerivationPath("m/84h/1h/0h/0")
val externalExtendedKey: DescriptorSecretKey = bip32RootKey.extend(bip84ExternalPath).asString()
println(externalExtendedKey)
// tprv8ZgxMBicQKsPfM8Trx2apvdEkmxbJkYY3ZsmcgKb2bfnLNcBhtCstqQTeFesMRLEJXpjGDinAUJUHprXMwph8dQBdS1HAoxEis8Knimxovf/84'/1'/0'/0/*

// to create the descriptor you'll need to use this extended key in a descriptor function,
// i.e. wpkh(), tr(), etc.
val externalDescriptor = "wpkh($externalExtendedKey)" 
   //sampleEnd
}

Parameters

network

The network this DescriptorSecretKey is to be used on.

mnemonic

The mnemonic.

password

The optional passphrase that can be provided as per BIP-39.

Constructors

Link copied to clipboard
fun DescriptorSecretKey(    network: Network,     mnemonic: Mnemonic,     password: String?)

Functions

Link copied to clipboard
fun asPublic(): DescriptorPublicKey

Return the public version of the descriptor.

Link copied to clipboard
fun asString(): String

Return the private descriptor as a string.

Link copied to clipboard
fun derive(path: DerivationPath): DescriptorSecretKey

Derive a private descriptor at a given path.

Link copied to clipboard
fun extend(path: DerivationPath): DescriptorSecretKey

Extend the private descriptor with a custom path.

Link copied to clipboard
fun fromString(secretKey: String): DescriptorSecretKey

Build a DescriptorSecretKey from a String

Link copied to clipboard
fun secretBytes(): List<UByte>

Return the raw private key as bytes.