Trait bip38::Decrypt [−][src]
Expand description
Allow decryption of bitcoin encrypted private keys in srt
format.
Required methods
Decrypt an encrypted bitcoin private key in str
format (both non-ec and ec).
This function targets strings of 58 base58 characters with the version prefix 6P
and
returns a tuple containing the decrypted private key ([u8; 32]
) and a boolean indication
of if this private key is intended to result in a compressed public key or not. So, if the
flag is true
, create an compressed public key (33 bytes), in case of false
, use the full
65 bytes of the public key.
Examples
use bip38::Decrypt; // decryption of non elliptic curve multiplication assert_eq!( "6PYMgbeR6XCsX4yJx8E52vW4PJDoTiu1QeFLn81KoW6Shye5DZ4ZnDauno".decrypt("weakPass"), Ok(([0x11; 32], true)) // indication to compress the public key of this private key ); assert_eq!( "6PRVo8whL3QbdrXpKk3gP2dGuxDbuvMsMqUq2imVigrm8oyRbvBoRUsbB3".decrypt("weakPass"), Ok(([0x11; 32], false)) // indication do not compress the public key ); // decryption of elliptic curve multiplication assert!( "6PnPQGcDuPhCMmXzTebiryx8zHxr8PZvUJccSxarn9nLHVLX7yVj6Wcoj9".decrypt("weakPass").is_ok() ); assert!( "6PfVV4eYCodt6tRiHbHH356MX818xZvcN54oNd1rCr8Cbme3273xWAgBhx".decrypt("notWeak?").is_ok() );
Errors
-
Error::Pass
is returned if an invalid passphrase is inserted. -
Error::EncKey
is returned if the targetstr
is not an valid encrypted private key. -
Error::Checksum
is returned if the targetstr
has valid encrypted private key format but invalid checksum. -
Error::Base58
is returned if an nonbase58
character is found.
use bip38::{Decrypt, Error}; assert!( "6PRNFFkZc2NZ6dJqFfhRoFNMR9Lnyj7dYGrzdgXXVMXcxoKTePPX1dWByq".decrypt("Satoshi").is_ok() ); assert_eq!( "6PRNFFkZc2NZ6dJqFfhRoFNMR9Lnyj7dYGrzdgXXVMXcxoKTePPX1dWByq".decrypt("Nakamoto"), Err(Error::Pass) ); assert_eq!( "6PRNFFkZc2NZ6dJqFfhRoFNMR9Lnyj7dYGrzdgXXVMXcxoKTePPX1dWByQ".decrypt("Satoshi"), // <- Q Err(Error::Checksum) ); assert_eq!( "6PRNFFkZc2NZ6dJqFfhRoFNMR9Lnyj7dYGrzdgXXVMXcxoKTePPX1dWBy".decrypt("Satoshi"), // <- q? Err(Error::EncKey) ); assert_eq!( "6PRNFFkZc2NZ6dJqFfhRoFNMR9Lnyj7dYGrzdgXXVMXcxoKTePPX1dWBy!".decrypt("Satoshi"), // <- ! Err(Error::Base58) );
Passphrase
This function handle the normalization (nfc
) of the passphrase as specified on bip-0038
.
use bip38::Decrypt; assert!( "6PRW5o9FLp4gJDDVqJQKJFTpMvdsSGJxMYHtHaQBF3ooa8mwD69bapcDQn" .decrypt("\u{03d2}\u{0301}\u{0000}\u{010400}\u{01f4a9}").is_ok() );