[][src]Module dup_crypto::dewif

Handle DEWIF format

See DEWIF format specifications.

Summary

Write ed25519 key-pair in DEWIF file

use dup_crypto::dewif::{Currency, G1_TEST_CURRENCY, write_dewif_v1_content};
use dup_crypto::keys::ed25519::{KeyPairFromSaltedPasswordGenerator, SaltedPassword};
use std::num::NonZeroU32;

// Get user credentials (from cli prompt or gui)
let credentials = SaltedPassword::new("user salt".to_owned(), "user password".to_owned());

// Generate ed25519 keypair
let keypair = KeyPairFromSaltedPasswordGenerator::with_default_parameters().generate(credentials);

// Get user passphrase for DEWIF encryption
let encryption_passphrase = "toto titi tata";

// Serialize keypair in DEWIF format
let dewif_content = write_dewif_v1_content(Currency::from(G1_TEST_CURRENCY), &keypair, encryption_passphrase);

assert_eq!(
    "AAAAARAAAAEx3yd707xD3F5ttjcISbZzXRrko4pKUmCDIF/emfcVU9MvBqCJQS9R2sWlqbtI1Q37sLQhkj/W7tqY+hxm7mFQ",
    dewif_content
)

Read DEWIF file

use dup_crypto::dewif::{Currency, ExpectedCurrency, read_dewif_file_content};
use dup_crypto::keys::{KeyPair, Signator};
use std::num::NonZeroU32;
use std::str::FromStr;

// Get DEWIF file content (Usually from disk)
let dewif_file_content = "AAAAARAAAAEx3yd707xD3F5ttjcISbZzXRrko4pKUmCDIF/emfcVU9MvBqCJQS9R2sWlqbtI1Q37sLQhkj/W7tqY+hxm7mFQ";

// Get user passphrase for DEWIF decryption (from cli prompt or gui)
let encryption_passphrase = "toto titi tata";

// Expected currency
let expected_currency = ExpectedCurrency::Specific(Currency::from_str("g1-test").expect("unknown currency"));

// Read DEWIF file content
// If the file content is correct, we get a key-pair iterator.
let mut key_pair_iter = read_dewif_file_content(expected_currency, dewif_file_content, encryption_passphrase)?;

// Get first key-pair
let key_pair = key_pair_iter
    .next()
    .expect("DEWIF file must contain at least one keypair");

assert_eq!(
    "2cC9FrvRiN3uHHcd8S7wuureDS8CAmD5y4afEgSCLHtU",
    &key_pair.public_key().to_string()
);

// Generate signator
// `Signator` is a non-copiable and non-clonable type,
// so only generate it when you are in the scope where you effectively sign.
let signator = key_pair.generate_signator();

// Sign a message with keypair
let sig = signator.sign(b"message");

assert_eq!(
    "nCWl7jtCa/nCMKKnk2NJN7daVxd/ER+e1wsFbofdh/pUvDuHxFaa7S5eUMGiqPTJ4uJQOvrmF/BOfOsYIoI2Bg==",
    &sig.to_string()
);

Structs

Currency

DEWIF currency

Enums

DewifReadError

Error when try to read DEWIF file content

ExpectedCurrency

Expected DEWIF currency

Constants

G1_CURRENCY

Ğ1 currency

G1_TEST_CURRENCY

Ğ1-Test currency

Functions

read_dewif_file_content

read dewif file content with user passphrase

write_dewif_v1_content

Write dewif v1 file content with user passphrase

write_dewif_v2_content

Write dewif v2 file content with user passphrase