-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
104 lines (82 loc) · 3.82 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#![feature(alloc_error_handler)]
#![no_std]
#![no_main]
// pick a panicking behavior
use panic_halt as _; // you can put a breakpoint on `rust_begin_unwind` to catch panics
// use panic_abort as _; // requires nightly
// use panic_itm as _; // logs messages over ITM; requires ITM support
// use panic_semihosting as _; // logs messages to the host stderr; requires a debugger
// use cortex_m::asm;
use alloc_cortex_m::CortexMHeap;
use core::alloc::Layout;
use cortex_m_rt::entry;
use cortex_m_semihosting::{debug, hprintln};
use cardano_embedded_sdk::api as embedano;
use cardano_embedded_sdk::bip::bip39::{dictionary, Entropy, Mnemonics};
use cardano_embedded_sdk::types::{harden, TxId, XPrvKey};
use derivation_path::DerivationPath;
#[global_allocator]
static ALLOCATOR: CortexMHeap = CortexMHeap::empty();
const HEAP_SIZE: usize = 1024; // in bytes
#[entry]
fn main() -> ! {
hprintln!("Test: Generating keys");
// from example/allocator.rs
unsafe { ALLOCATOR.init(cortex_m_rt::heap_start() as usize, HEAP_SIZE) }
let mnemonics = "aim wool into nose tell ball arm expand design push elevator multiply glove lonely minimum";
let mnemonics = Mnemonics::from_string(&dictionary::ENGLISH, mnemonics).unwrap();
let password = b"embedano";
let entropy = Entropy::from_mnemonics(&mnemonics).unwrap();
// Make derivation path for account 0 and address 0 according to CIP-1852
let path: DerivationPath = "m/1852'/1815'/0'/0/0".parse().unwrap();
// Try to parse transaction id and sign it
let tx_id =
TxId::from_hex("bb1eb401cd03b0cd8caa08997df0a2ab226772c4d3a08adfb5a60ba34de12dfb").unwrap();
let signature = embedano::sign_tx_id(&tx_id, &entropy, password, &path);
// Derive key pair using same path ant try to verify signature from `sign_tx_id`
let (_prv_key, pub_key) = embedano::derive_key_pair(&entropy, password, &path);
assert!(pub_key.verify(tx_id.to_bytes(), &signature));
// Check if public key can be derived from given entropy by signing nonce
let nonce = b"some nonce";
// Define what key type of public key we pass to prove function.
// This will affect what address derivation will be used and how many indexes will be checked.
// As earlier we used derivation path "m/1852'/1815'/0'/0/0" to make keys,
// `pub_key` corresponds to address 0 of account 0 so as `key_type`.
let key_type = embedano::KeyType::AddressKey {
account_gap: 5,
address_gap: 5,
};
let proof_sig =
embedano::prove_ownership(nonce, &pub_key, &entropy, password, key_type).unwrap();
// If we got Some(signature), then we can verify proof with public key we tested.
assert!(pub_key.verify(nonce, &proof_sig));
// Function above defined on top of types in `types.rs` that can provide more fine grinded control.
// E.g.:
// Create root private key from entropy
let root_key = XPrvKey::from_entropy(&entropy, password);
hprintln!("Root key: {}", root_key.to_hex());
// Derive private key for same path that was used in `derive_key_pair` above
let prv_key = root_key
.derive(harden(1852))
.derive(harden(1815))
.derive(harden(0))
.derive(0)
.derive(0);
hprintln!("Private key: {}", prv_key.to_hex());
// Derive corresponding public key
let pub_key = prv_key.to_public();
hprintln!("Public key: {}", pub_key.to_hex());
// Sign and verify using derived keys
let some_data = b"some data";
let signature = prv_key.sign(some_data);
hprintln!("Verify: {}", pub_key.verify(some_data, &signature));
debug::exit(debug::EXIT_SUCCESS);
loop {}
}
#[alloc_error_handler]
fn alloc_error(_layout: Layout) -> ! {
hprintln!("ALLOC ERROR");
debug::exit(debug::EXIT_FAILURE);
//let f: [u32] = todo!();
loop {}
}