Module dup_crypto::keys::ed25519::bip32[][src]

Expand description

Implement BIP32-Ed25519 specifications.

Generate an HD wallet

use dup_crypto::seeds::Seed32;
use dup_crypto::keys::KeyPair as _;
use dup_crypto::keys::ed25519::bip32::KeyPair;

let seed = Seed32::random().expect("fail to generate random seed");

let master_key_pair = KeyPair::from_seed(seed);

let master_public_key = master_key_pair.public_key();

dup_crypto::keys::ed25519::bip32::KeyPair implement the dup_crypto::keys::KeyPair trait, so sign and verify like a classic ed25519 keypair.

Derive private key and public key

use dup_crypto::seeds::Seed32;
use dup_crypto::keys::KeyPair as _;
use dup_crypto::keys::ed25519::bip32::{PrivateDerivationPath, KeyPair, PublicKeyWithChainCode};
use dup_crypto::utils::U31;

let master_keypair = KeyPair::from_seed(Seed32::random().expect("fail to generate random seed"));

let account_index = U31::new(2)?;
let address_index = U31::new(3)?;

// Derive master external keypair
let derivation_path = PrivateDerivationPath::opaque(account_index, true, None)?;
let external_keypair = master_keypair.derive(derivation_path);

// Get master external public key and chain code
let external_public_key = external_keypair.public_key();
let external_chain_code = external_keypair.chain_code();

// Derive a specific address with public derivation
let address = PublicKeyWithChainCode {
    public_key: external_public_key,
    chain_code: external_chain_code,
}
.derive(address_index)?
.public_key;

// Verify that the private derivation give us the same address
assert_eq!(
    address,
    master_keypair
        .derive(PrivateDerivationPath::opaque(
                account_index,
                true,
                Some(address_index)
        )?).public_key()
);

Structs

InvalidAccountIndex

Invalid account index

KeyPair

HDWallet extended key pair (Ed25519 extended private key + BIP32 ChainCode)

PrivateDerivationPath

Private Derivation path

PublicKeyWithChainCode

HDWallet extended public key (Ed25519 public key + BIP32 ChainCode)

Signator

HDWallet signator

Enums

PublicDerivationError

Derivation error

Type Definitions

ChainCode

BIP32 Chain code