|
| 1 | +#![feature(alloc_error_handler)] |
| 2 | +#![feature(panic_info_message)] |
| 3 | +#![no_std] |
| 4 | +#![no_main] |
| 5 | + |
| 6 | +extern crate alloc; |
| 7 | +extern crate bitcoin; |
| 8 | + |
| 9 | +use alloc::string::ToString; |
| 10 | +use alloc::vec; |
| 11 | +use core::alloc::Layout; |
| 12 | +use core::panic::PanicInfo; |
| 13 | + |
| 14 | +use alloc_cortex_m::CortexMHeap; |
| 15 | +// use panic_halt as _; |
| 16 | +use bitcoin::{Address, Network, PrivateKey}; |
| 17 | +use bitcoin::secp256k1::ffi::types::AlignedType; |
| 18 | +use bitcoin::secp256k1::Secp256k1; |
| 19 | + |
| 20 | +use cortex_m::asm; |
| 21 | +use cortex_m_rt::entry; |
| 22 | +use cortex_m_semihosting::{debug, hprintln}; |
| 23 | + |
| 24 | +// this is the allocator the application will use |
| 25 | +#[global_allocator] |
| 26 | +static ALLOCATOR: CortexMHeap = CortexMHeap::empty(); |
| 27 | + |
| 28 | +const HEAP_SIZE: usize = 1024 * 512; // 512 KB |
| 29 | + |
| 30 | +#[entry] |
| 31 | +fn main() -> ! { |
| 32 | + hprintln!("heap size {}", HEAP_SIZE).unwrap(); |
| 33 | + |
| 34 | + unsafe { ALLOCATOR.init(cortex_m_rt::heap_start() as usize, HEAP_SIZE) } |
| 35 | + |
| 36 | + let size = Secp256k1::preallocate_size(); |
| 37 | + hprintln!("secp buf size {}", size*16).unwrap(); |
| 38 | + |
| 39 | + // Load a private key |
| 40 | + let raw = "L1HKVVLHXiUhecWnwFYF6L3shkf1E12HUmuZTESvBXUdx3yqVP1D"; |
| 41 | + let pk = PrivateKey::from_wif(raw).unwrap(); |
| 42 | + hprintln!("Seed WIF: {}", pk).unwrap(); |
| 43 | + |
| 44 | + let mut buf_ful = vec![AlignedType::zeroed(); size]; |
| 45 | + let secp = Secp256k1::preallocated_new(&mut buf_ful).unwrap(); |
| 46 | + |
| 47 | + // Derive address |
| 48 | + let pubkey = pk.public_key(&secp); |
| 49 | + let address = Address::p2wpkh(&pubkey, Network::Bitcoin).unwrap(); |
| 50 | + hprintln!("Address: {}", address).unwrap(); |
| 51 | + |
| 52 | + assert_eq!(address.to_string(), "bc1qpx9t9pzzl4qsydmhyt6ctrxxjd4ep549np9993".to_string()); |
| 53 | + // exit QEMU |
| 54 | + // NOTE do not run this on hardware; it can corrupt OpenOCD state |
| 55 | + debug::exit(debug::EXIT_SUCCESS); |
| 56 | + |
| 57 | + loop {} |
| 58 | +} |
| 59 | + |
| 60 | +// define what happens in an Out Of Memory (OOM) condition |
| 61 | +#[alloc_error_handler] |
| 62 | +fn alloc_error(_layout: Layout) -> ! { |
| 63 | + hprintln!("alloc error").unwrap(); |
| 64 | + debug::exit(debug::EXIT_FAILURE); |
| 65 | + asm::bkpt(); |
| 66 | + |
| 67 | + loop {} |
| 68 | +} |
| 69 | + |
| 70 | +#[inline(never)] |
| 71 | +#[panic_handler] |
| 72 | +fn panic(info: &PanicInfo) -> ! { |
| 73 | + hprintln!("panic {:?}", info.message()).unwrap(); |
| 74 | + debug::exit(debug::EXIT_FAILURE); |
| 75 | + loop {} |
| 76 | +} |
0 commit comments