Trait bip38::EncryptWif[][src]

pub trait EncryptWif {
    fn encrypt_wif(&self, pass: &str) -> Result<String, Error>;
}
Expand description

Allow encryption of bitcoin private keys in the wif format.

Required methods

Encrypt a bitcoin private key in the wif format (without elliptic curve multiplication) into a String of 58 base58 characters according to the wif indication of a compressed or uncompressed future public key (the prefix of the wif affects the prefix of the encrypted private key).

Examples

use bip38::EncryptWif;

assert_eq!(
    "KwntMbt59tTsj8xqpqYqRRWufyjGunvhSyeMo3NTYpFYzZbXJ5Hp".encrypt_wif("weakPass"),
    Ok(String::from("6PYMgbeR6XCsX4yJx8E52vW4PJDoTiu1QeFLn81KoW6Shye5DZ4ZnDauno"))
);
assert_eq!(
    "5HwoXVkHoRM8sL2KmNRS217n1g8mPPBomrY7yehCuXC1115WWsh".encrypt_wif("weakPass"),
    Ok(String::from("6PRVo8whL3QbdrXpKk3gP2dGuxDbuvMsMqUq2imVigrm8oyRbvBoRUsbB3"))
);

Errors

This function can fail due to decoding process of the target wif private key and if the decoded private key can’t result in a bitcoin address.

  • Error::Base58 is returned if an non base58 character is found.

  • Error::Checksum is returned if the target str has valid wif format but invalid checksum.

  • Error::PrvKey is returned if the decoded private key could not generate a bitcoin address

  • Error::WifKey is returned if the target str is not an valid wif private key.

use bip38::{EncryptWif, Error};

assert_eq!(
    "5HtasZ6ofTHP6HCwTqTkLDuLQisYPah7aUnSKfC7h4hMUVw2gi5".encrypt_wif("Satoshi"),
    Ok(String::from("6PRNFFkZc2NZ6dJqFfhRoFNMR9Lnyj7dYGrzdgXXVMXcxoKTePPX1dWByq"))
);
assert_eq!(
    "5HtasZ6ofTHP6HCwTqTkLDuLQisYPah7aUnSKfC7h4hMUVw2gi!".encrypt_wif("Satoshi"), // !
    Err(Error::Base58)
);
assert_eq!(
    "5HtasZ6ofTHP6HCwTqTkLDuLQisYPah7aUnSKfC7h4hMUVw2gi6".encrypt_wif("Satoshi"), // 6
    Err(Error::Checksum)
);
assert_eq!(
    "5HtasZ6ofTHP6HCwTqTkLDuLQisYPah7aUnSKfC7h4hMUVw2gi55".encrypt_wif("Satoshi"), // 55
    Err(Error::WifKey)
);
assert_eq!( // the payload of this wif is [0x00; 32]
    "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73NUBByJr".encrypt_wif("Satoshi"),
    Err(Error::PrvKey)
);

Passphrase

This function handle the normalization (nfc) of the passphrase as specified on bip-0038.

use bip38::EncryptWif;

assert_eq!(
    "5Jajm8eQ22H3pGWLEVCXyvND8dQZhiQhoLJNKjYXk9roUFTMSZ4"
        .encrypt_wif("\u{03d2}\u{0301}\u{0000}\u{010400}\u{01f4a9}"),
    Ok(String::from("6PRW5o9FLp4gJDDVqJQKJFTpMvdsSGJxMYHtHaQBF3ooa8mwD69bapcDQn"))
);

Implementations on Foreign Types

Implementors