wookong_solo/
lib.rs

1// -*- mode: rust; -*-
2//
3// This file is part of subhd/solo, substrate.
4// Copyright (c) 2017-2019 Chester Li and extropies.com
5// See LICENSE for licensing information.
6//
7// Authors:
8// - Chester Li<chester@lichester.com>
9
10//! C API to RUST for solo
11extern {
12    fn solo_pubkey( pk: *mut u8, path:*const u8, path_level:usize ) -> u32;
13    fn solo_sign( message: *const u8, len: usize, path:*const u8, path_level:usize, sig: *mut u8 ) -> u32;
14    fn solo_generate( seed:*mut u8 ) -> u32;
15    fn solo_format() -> u32;
16    fn solo_import( seed:*const u8, len:usize ) -> u32;
17}
18
19
20/// get pulic key by path
21pub fn wk_getpub( pk: &mut [u8], path:&Vec<&[u8;32]>) -> u32 {
22    if !path.is_empty(){
23        let v_bytes: &[u8] = unsafe {
24            std::slice::from_raw_parts(
25                path.get(0).unwrap().as_ptr(),
26                path.len() * 32,
27            )
28        };
29         unsafe { solo_pubkey(pk.as_mut_ptr(),v_bytes.as_ptr(),path.len()*32) }
30    }else{
31        let empty:[u8;32] = [0u8;32];
32         unsafe { solo_pubkey(pk.as_mut_ptr(),empty.as_ptr(),0) }
33    }
34}
35/// sign the message
36pub fn wk_sign( message:&[u8], sig: &mut [u8], path:&Vec<&[u8;32]>) -> u32 {
37    if !path.is_empty(){
38        let v_bytes: &[u8] = unsafe {
39            std::slice::from_raw_parts(
40                path.get(0).unwrap().as_ptr(),
41                path.len() * 32,
42            )
43        };
44        unsafe { solo_sign( message.as_ptr(), message.len(), v_bytes.as_ptr(), path.len() * 32, sig.as_mut_ptr()) }
45    }else{
46        let empty:[u8;32] = [0u8;32];
47        unsafe { solo_sign( message.as_ptr(), message.len(), empty.as_ptr(), 0, sig.as_mut_ptr()) }
48    }
49        
50}
51    
52/// gen key paird and return seed
53pub fn wk_generate( seed: &mut [u8] ) -> u32 {
54    unsafe { solo_generate( seed.as_mut_ptr() ) }
55}
56///clear all data
57pub fn wk_format() -> u32{
58    unsafe { solo_format() }
59}
60///import seed to device
61pub fn wk_import( seed:&[u8] ) -> u32 {
62     unsafe { solo_import( seed.as_ptr(), seed.len() ) }
63}
64
65