恢复签名方法
This commit is contained in:
parent
da1d9c7fce
commit
dd6b3b60c4
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
use std::ffi::CStr;
|
use std::ffi::CStr;
|
||||||
//use rustylib::gen::{CWallet};
|
//use rustylib::gen::{CWallet};
|
||||||
use rustwallet::{fetch_cwallet, free_cwallet, sss_sign, CWallet};
|
use rustwallet::{fetch_cwallet, free_cwallet, sign, sss_sign, CWallet};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
unsafe {
|
unsafe {
|
||||||
@ -21,7 +21,7 @@ fn main() {
|
|||||||
println!("---- fetching the saved wallet to be exposed to C-side ----");
|
println!("---- fetching the saved wallet to be exposed to C-side ----");
|
||||||
let fetched = fetch_cwallet();
|
let fetched = fetch_cwallet();
|
||||||
print_wallet(&fetched);
|
print_wallet(&fetched);
|
||||||
// sign();
|
sign();
|
||||||
let sign_str = "111";
|
let sign_str = "111";
|
||||||
let cstr = std::ffi::CString::new(sign_str).unwrap();
|
let cstr = std::ffi::CString::new(sign_str).unwrap();
|
||||||
sss_sign(cstr.into_raw());
|
sss_sign(cstr.into_raw());
|
||||||
|
32
src/lib.rs
32
src/lib.rs
@ -67,22 +67,22 @@ pub unsafe extern "C" fn fetch_cwallet() -> CWallet {
|
|||||||
Ok(w) => return convert_to_cwallet(w),
|
Ok(w) => return convert_to_cwallet(w),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
// #[no_mangle]
|
#[no_mangle]
|
||||||
// pub unsafe extern "C" fn sign() {
|
pub unsafe extern "C" fn sign() {
|
||||||
// match wallet_impl::Wallet::retrieve_keys("wallet.json") {
|
match wallet_impl::Wallet::retrieve_keys("wallet.json") {
|
||||||
// Err(_) => {
|
Err(_) => {
|
||||||
// println!("error sign");
|
println!("error sign");
|
||||||
// }
|
}
|
||||||
// Ok(w) => match w.sign("111") {
|
Ok(w) => match w.sign("111") {
|
||||||
// Err(err) => {
|
Err(err) => {
|
||||||
// println!("error sign: {:?}", err);
|
println!("error sign: {:?}", err);
|
||||||
// }
|
}
|
||||||
// Ok(sig) => {
|
Ok(sig) => {
|
||||||
// println!("sig result: {:?}", sig);
|
println!("sig result: {:?}", sig);
|
||||||
// }
|
}
|
||||||
// },
|
},
|
||||||
// };
|
};
|
||||||
// }
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern "C" fn sss_sign(msg: *const std::os::raw::c_char) {
|
pub unsafe extern "C" fn sss_sign(msg: *const std::os::raw::c_char) {
|
||||||
|
@ -1,15 +1,18 @@
|
|||||||
extern crate hex;
|
extern crate hex;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
use core::fmt::Write;
|
||||||
use secp256k1::rand::rngs::OsRng;
|
use secp256k1::rand::rngs::OsRng;
|
||||||
use secp256k1::{PublicKey, Secp256k1, SecretKey};
|
use secp256k1::{Message, PublicKey, Secp256k1, SecretKey};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use shamir_secret_sharing::num_bigint::BigInt;
|
use shamir_secret_sharing::num_bigint::BigInt;
|
||||||
use shamir_secret_sharing::ShamirSecretSharing as SSS;
|
use shamir_secret_sharing::ShamirSecretSharing as SSS;
|
||||||
use std::io::BufWriter;
|
use std::io::BufWriter;
|
||||||
use std::str;
|
use std::str;
|
||||||
|
use std::str::FromStr;
|
||||||
use std::{fs::OpenOptions, io::BufReader};
|
use std::{fs::OpenOptions, io::BufReader};
|
||||||
use tiny_keccak::keccak256;
|
use tiny_keccak::keccak256;
|
||||||
|
use web3::signing::{hash_message, Key, SecretKeyRef};
|
||||||
use web3::types::Address;
|
use web3::types::Address;
|
||||||
|
|
||||||
pub fn generate_keypair() -> (SecretKey, PublicKey) {
|
pub fn generate_keypair() -> (SecretKey, PublicKey) {
|
||||||
@ -100,58 +103,58 @@ impl Wallet {
|
|||||||
println!("recover: {:?}", secret_b.to_str_radix(16));
|
println!("recover: {:?}", secret_b.to_str_radix(16));
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub fn sign<S>(&self, msg: S) -> Result<()>
|
pub fn sign<S>(&self, msg: S) -> Result<()>
|
||||||
// where
|
where
|
||||||
// S: AsRef<[u8]>,
|
S: AsRef<[u8]>,
|
||||||
// {
|
{
|
||||||
// let secp = Secp256k1::new();
|
let secp = Secp256k1::new();
|
||||||
// println!("secret key str: {:?}", self.secret_key);
|
println!("secret key str: {:?}", self.secret_key);
|
||||||
// let message = msg.as_ref();
|
let message = msg.as_ref();
|
||||||
// let message_hash = hash_message(message.as_ref());
|
let message_hash = hash_message(message.as_ref());
|
||||||
// let pk = SecretKey::from_str(&self.secret_key).expect("32 bytes, within curve order");
|
let pk = SecretKey::from_str(&self.secret_key).expect("32 bytes, within curve order");
|
||||||
// println!("secret key: {:?}", pk);
|
println!("secret key: {:?}", pk);
|
||||||
|
|
||||||
// let key = SecretKeyRef::new(&pk);
|
let key = SecretKeyRef::new(&pk);
|
||||||
// let signature = key
|
let signature = key
|
||||||
// .sign(message_hash.as_bytes(), None)
|
.sign(message_hash.as_bytes(), None)
|
||||||
// .expect("hash is non-zero 32-bytes; qed");
|
.expect("hash is non-zero 32-bytes; qed");
|
||||||
// let v = signature
|
let v = signature
|
||||||
// .v
|
.v
|
||||||
// .try_into()
|
.try_into()
|
||||||
// .expect("signature recovery in electrum notation always fits in a u8");
|
.expect("signature recovery in electrum notation always fits in a u8");
|
||||||
|
|
||||||
// // let signature_bytes = Bytes({
|
// let signature_bytes = Bytes({
|
||||||
// // let mut bytes = Vec::with_capacity(65);
|
// let mut bytes = Vec::with_capacity(65);
|
||||||
// // bytes.extend_from_slice(signature.r.as_bytes());
|
// bytes.extend_from_slice(signature.r.as_bytes());
|
||||||
// // bytes.extend_from_slice(signature.s.as_bytes());
|
// bytes.extend_from_slice(signature.s.as_bytes());
|
||||||
// // bytes.push(v);
|
// bytes.push(v);
|
||||||
// // bytes
|
// bytes
|
||||||
// // });
|
// });
|
||||||
|
|
||||||
// let mut bytes = Vec::with_capacity(65);
|
let mut bytes = Vec::with_capacity(65);
|
||||||
// bytes.extend_from_slice(signature.r.as_bytes());
|
bytes.extend_from_slice(signature.r.as_bytes());
|
||||||
// bytes.extend_from_slice(signature.s.as_bytes());
|
bytes.extend_from_slice(signature.s.as_bytes());
|
||||||
// bytes.push(v);
|
bytes.push(v);
|
||||||
|
|
||||||
// let str_sign = hex::encode(bytes);
|
let str_sign = hex::encode(bytes);
|
||||||
// println!("web3 sign: {:?}", str_sign);
|
println!("web3 sign: {:?}", str_sign);
|
||||||
|
|
||||||
// let message_to_hash = Message::from_slice(message_hash.as_ref()).unwrap();
|
let message_to_hash = Message::from_slice(message_hash.as_ref()).unwrap();
|
||||||
// let (recovery_id, signature) = secp
|
let (recovery_id, signature) = secp
|
||||||
// .sign_ecdsa_recoverable(&message_to_hash, &pk)
|
.sign_ecdsa_recoverable(&message_to_hash, &pk)
|
||||||
// .serialize_compact();
|
.serialize_compact();
|
||||||
// // let mut s = String::with_capacity(2 * 65);
|
// let mut s = String::with_capacity(2 * 65);
|
||||||
// // for i in signature {
|
// for i in signature {
|
||||||
// // write!(s, "{:02x}", i).unwrap();
|
// write!(s, "{:02x}", i).unwrap();
|
||||||
// // }
|
// }
|
||||||
// let mut s = hex::encode(signature);
|
let mut s = hex::encode(signature);
|
||||||
// let standard_v = recovery_id.to_i32() as u64 + 27;
|
let standard_v = recovery_id.to_i32() as u64 + 27;
|
||||||
// let rv: u8 = standard_v
|
let rv: u8 = standard_v
|
||||||
// .try_into()
|
.try_into()
|
||||||
// .expect("signature recovery in electrum notation always fits in a u8");
|
.expect("signature recovery in electrum notation always fits in a u8");
|
||||||
// write!(s, "{:02x}", rv).unwrap();
|
write!(s, "{:02x}", rv).unwrap();
|
||||||
// println!("normal sigx: {:?}", s);
|
println!("normal sigx: {:?}", s);
|
||||||
|
|
||||||
// Ok(())
|
Ok(())
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user