|
| 1 | +#![no_std] |
| 2 | +#![no_main] |
| 3 | +#![feature(alloc_error_handler)] |
| 4 | +#![feature(panic_info_message)] |
| 5 | + |
| 6 | +extern crate alloc; |
| 7 | + |
| 8 | +use alloc::string::ToString; |
| 9 | +use core::alloc::Layout; |
| 10 | +use core::panic::PanicInfo; |
| 11 | + |
| 12 | +use alloc_cortex_m::CortexMHeap; |
| 13 | + |
| 14 | +use core::str::FromStr; |
| 15 | + |
| 16 | +use cortex_m::asm; |
| 17 | +use cortex_m_rt::entry; |
| 18 | +use cortex_m_semihosting::{debug, hprintln}; |
| 19 | + |
| 20 | +// this is the allocator the application will use |
| 21 | +#[global_allocator] |
| 22 | +static ALLOCATOR: CortexMHeap = CortexMHeap::empty(); |
| 23 | + |
| 24 | +const HEAP_SIZE: usize = 1024 * 256; // 256 KB |
| 25 | + |
| 26 | +#[entry] |
| 27 | +fn main() -> ! { |
| 28 | + hprintln!("heap size {}", HEAP_SIZE).unwrap(); |
| 29 | + |
| 30 | + unsafe { ALLOCATOR.init(cortex_m_rt::heap_start() as usize, HEAP_SIZE) } |
| 31 | + |
| 32 | + // begin miniscript test |
| 33 | + let descriptor = "sh(wsh(or_d(\ |
| 34 | + c:pk_k(020e0338c96a8870479f2396c373cc7696ba124e8635d41b0ea581112b67817261),\ |
| 35 | + c:pk_k(0250863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352)\ |
| 36 | + )))"; |
| 37 | + hprintln!("descriptor {}", descriptor).unwrap(); |
| 38 | + let desc = |
| 39 | + miniscript::Descriptor::<miniscript::bitcoin::PublicKey>::from_str(descriptor).unwrap(); |
| 40 | + |
| 41 | + // Derive the P2SH address |
| 42 | + let p2sh_addr = desc |
| 43 | + .address(miniscript::bitcoin::Network::Bitcoin) |
| 44 | + .unwrap() |
| 45 | + .to_string(); |
| 46 | + hprintln!("p2sh address {}", p2sh_addr).unwrap(); |
| 47 | + assert_eq!(p2sh_addr, "3CJxbQBfWAe1ZkKiGQNEYrioV73ZwvBWns"); |
| 48 | + |
| 49 | + // Check whether the descriptor is safe |
| 50 | + // This checks whether all spend paths are accessible in bitcoin network. |
| 51 | + // It maybe possible that some of the spend require more than 100 elements in Wsh scripts |
| 52 | + // Or they contain a combination of timelock and heightlock. |
| 53 | + assert!(desc.sanity_check().is_ok()); |
| 54 | + |
| 55 | + // Estimate the satisfaction cost |
| 56 | + assert_eq!(desc.max_satisfaction_weight().unwrap(), 293); |
| 57 | + // end miniscript test |
| 58 | + |
| 59 | + // exit QEMU |
| 60 | + // NOTE do not run this on hardware; it can corrupt OpenOCD state |
| 61 | + debug::exit(debug::EXIT_SUCCESS); |
| 62 | + |
| 63 | + loop {} |
| 64 | +} |
| 65 | + |
| 66 | +// define what happens in an Out Of Memory (OOM) condition |
| 67 | +#[alloc_error_handler] |
| 68 | +fn alloc_error(_layout: Layout) -> ! { |
| 69 | + hprintln!("alloc error").unwrap(); |
| 70 | + debug::exit(debug::EXIT_FAILURE); |
| 71 | + asm::bkpt(); |
| 72 | + |
| 73 | + loop {} |
| 74 | +} |
| 75 | + |
| 76 | +#[inline(never)] |
| 77 | +#[panic_handler] |
| 78 | +fn panic(info: &PanicInfo) -> ! { |
| 79 | + hprintln!("panic {:?}", info.message()).unwrap(); |
| 80 | + debug::exit(debug::EXIT_FAILURE); |
| 81 | + loop {} |
| 82 | +} |
0 commit comments