fix(dubp_rs): replace buggy err handling by custom err handling system
This commit is contained in:
parent
c2d373b721
commit
1d9e6d0753
|
@ -1,20 +1,5 @@
|
||||||
# This file is automatically @generated by Cargo.
|
# This file is automatically @generated by Cargo.
|
||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
[[package]]
|
|
||||||
name = "addr2line"
|
|
||||||
version = "0.14.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "a55f82cfe485775d02112886f4169bde0c5894d75e79ead7eafe7e40a25e45f7"
|
|
||||||
dependencies = [
|
|
||||||
"gimli",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "adler"
|
|
||||||
version = "0.2.3"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "ee2a4ec343196209d6594e19543ae87a39f96d5534d7174822a3ad825dd6ed7e"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "aes"
|
name = "aes"
|
||||||
version = "0.4.0"
|
version = "0.4.0"
|
||||||
|
@ -85,26 +70,6 @@ dependencies = [
|
||||||
"winapi",
|
"winapi",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "autocfg"
|
|
||||||
version = "1.0.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "backtrace"
|
|
||||||
version = "0.3.55"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "ef5140344c85b01f9bbb4d4b7288a8aa4b3287ccef913a14bcc78a1063623598"
|
|
||||||
dependencies = [
|
|
||||||
"addr2line",
|
|
||||||
"cfg-if 1.0.0",
|
|
||||||
"libc",
|
|
||||||
"miniz_oxide",
|
|
||||||
"object",
|
|
||||||
"rustc-demangle",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "base64"
|
name = "base64"
|
||||||
version = "0.12.3"
|
version = "0.12.3"
|
||||||
|
@ -280,7 +245,6 @@ dependencies = [
|
||||||
"cbindgen",
|
"cbindgen",
|
||||||
"dart-bindgen",
|
"dart-bindgen",
|
||||||
"dup-crypto",
|
"dup-crypto",
|
||||||
"ffi_helpers",
|
|
||||||
"thiserror",
|
"thiserror",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -306,39 +270,6 @@ dependencies = [
|
||||||
"zeroize",
|
"zeroize",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "failure"
|
|
||||||
version = "0.1.8"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "d32e9bd16cc02eae7db7ef620b392808b89f6a5e16bb3497d159c6b92a0f4f86"
|
|
||||||
dependencies = [
|
|
||||||
"backtrace",
|
|
||||||
"failure_derive",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "failure_derive"
|
|
||||||
version = "0.1.8"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "aa4da3c766cd7a0db8242e326e9e4e081edd567072893ed320008189715366a4"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"syn",
|
|
||||||
"synstructure",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "ffi_helpers"
|
|
||||||
version = "0.2.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "36a21261040ffbc3999698b39170d6e951f2823dd9ad7b50469309156060a50c"
|
|
||||||
dependencies = [
|
|
||||||
"failure",
|
|
||||||
"failure_derive",
|
|
||||||
"libc",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "generic-array"
|
name = "generic-array"
|
||||||
version = "0.14.4"
|
version = "0.14.4"
|
||||||
|
@ -362,12 +293,6 @@ dependencies = [
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "gimli"
|
|
||||||
version = "0.23.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "f6503fe142514ca4799d4c26297c4248239fe8838d827db6bd6065c6ed29a6ce"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "glob"
|
name = "glob"
|
||||||
version = "0.3.0"
|
version = "0.3.0"
|
||||||
|
@ -428,22 +353,6 @@ dependencies = [
|
||||||
"cfg-if 0.1.10",
|
"cfg-if 0.1.10",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "miniz_oxide"
|
|
||||||
version = "0.4.3"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "0f2d26ec3309788e423cfbf68ad1800f061638098d76a83681af979dc4eda19d"
|
|
||||||
dependencies = [
|
|
||||||
"adler",
|
|
||||||
"autocfg",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "object"
|
|
||||||
version = "0.22.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "8d3b63360ec3cb337817c2dbd47ab4a0f170d285d8e5a2064600f3def1402397"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "once_cell"
|
name = "once_cell"
|
||||||
version = "1.5.2"
|
version = "1.5.2"
|
||||||
|
@ -551,12 +460,6 @@ dependencies = [
|
||||||
"winapi",
|
"winapi",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "rustc-demangle"
|
|
||||||
version = "0.1.18"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "6e3bad0ee36814ca07d7968269dd4b7ec89ec2da10c4bb613928d3077083c232"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ryu"
|
name = "ryu"
|
||||||
version = "1.0.5"
|
version = "1.0.5"
|
||||||
|
|
|
@ -11,7 +11,6 @@ crate-type = ["rlib"]
|
||||||
[dependencies]
|
[dependencies]
|
||||||
allo-isolate = "0.1.6"
|
allo-isolate = "0.1.6"
|
||||||
dup-crypto = { version = "0.36.0", features = ["dewif", "mnemonic", "mnemonic_french", "rand", "scrypt"] }
|
dup-crypto = { version = "0.36.0", features = ["dewif", "mnemonic", "mnemonic_french", "rand", "scrypt"] }
|
||||||
ffi_helpers = "0.2"
|
|
||||||
thiserror = "1.0.23"
|
thiserror = "1.0.23"
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
|
|
|
@ -35,11 +35,16 @@ pub(super) fn gen_pin10() -> Result<String, DubpError> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn change_pin(
|
pub(super) fn change_pin(
|
||||||
currency: &str,
|
currency: *const raw::c_char,
|
||||||
dewif: &str,
|
dewif: *const raw::c_char,
|
||||||
old_pin: &str,
|
old_pin: *const raw::c_char,
|
||||||
new_pin: &str,
|
new_pin: *const raw::c_char,
|
||||||
) -> Result<String, DubpError> {
|
) -> Result<String, DubpError> {
|
||||||
|
let currency = char_ptr_to_str(currency)?;
|
||||||
|
let dewif = char_ptr_to_str(dewif)?;
|
||||||
|
let old_pin = char_ptr_to_str(old_pin)?;
|
||||||
|
let new_pin = char_ptr_to_str(new_pin)?;
|
||||||
|
|
||||||
let currency = parse_currency(currency)?;
|
let currency = parse_currency(currency)?;
|
||||||
let mut keypairs = dup_crypto::dewif::read_dewif_file_content(
|
let mut keypairs = dup_crypto::dewif::read_dewif_file_content(
|
||||||
ExpectedCurrency::Specific(currency),
|
ExpectedCurrency::Specific(currency),
|
||||||
|
@ -57,11 +62,15 @@ pub(super) fn change_pin(
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn gen_dewif(
|
pub(super) fn gen_dewif(
|
||||||
currency: &str,
|
currency: *const raw::c_char,
|
||||||
language: u32,
|
language: u32,
|
||||||
mnemonic: &str,
|
mnemonic: *const raw::c_char,
|
||||||
pin: &str,
|
pin: *const raw::c_char,
|
||||||
) -> Result<String, DubpError> {
|
) -> Result<String, DubpError> {
|
||||||
|
let currency = char_ptr_to_str(currency)?;
|
||||||
|
let mnemonic = char_ptr_to_str(mnemonic)?;
|
||||||
|
let pin = char_ptr_to_str(pin)?;
|
||||||
|
|
||||||
let currency = parse_currency(currency)?;
|
let currency = parse_currency(currency)?;
|
||||||
let mnemonic = Mnemonic::from_phrase(mnemonic, u32_to_language(language)?)
|
let mnemonic = Mnemonic::from_phrase(mnemonic, u32_to_language(language)?)
|
||||||
.map_err(|_| DubpError::WrongLanguage)?;
|
.map_err(|_| DubpError::WrongLanguage)?;
|
||||||
|
@ -72,12 +81,20 @@ pub(super) fn gen_dewif(
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn get_pubkey(currency: &str, dewif: &str, pin: &str) -> Result<String, DubpError> {
|
pub(super) fn get_pubkey(
|
||||||
|
currency: *const raw::c_char,
|
||||||
|
dewif: *const raw::c_char,
|
||||||
|
pin: *const raw::c_char,
|
||||||
|
) -> Result<String, DubpError> {
|
||||||
|
let currency = char_ptr_to_str(currency)?;
|
||||||
|
let dewif = char_ptr_to_str(dewif)?;
|
||||||
|
let pin = char_ptr_to_str(pin)?;
|
||||||
|
|
||||||
let currency = parse_currency(currency)?;
|
let currency = parse_currency(currency)?;
|
||||||
let mut keypairs = dup_crypto::dewif::read_dewif_file_content(
|
let mut keypairs = dup_crypto::dewif::read_dewif_file_content(
|
||||||
ExpectedCurrency::Specific(currency),
|
ExpectedCurrency::Specific(currency),
|
||||||
dewif,
|
dewif,
|
||||||
&pin,
|
&pin.to_ascii_uppercase(),
|
||||||
)
|
)
|
||||||
.map_err(DubpError::DewifReadError)?;
|
.map_err(DubpError::DewifReadError)?;
|
||||||
if let Some(KeyPairEnum::Ed25519(keypair)) = keypairs.next() {
|
if let Some(KeyPairEnum::Ed25519(keypair)) = keypairs.next() {
|
||||||
|
@ -87,12 +104,22 @@ pub(super) fn get_pubkey(currency: &str, dewif: &str, pin: &str) -> Result<Strin
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn sign(currency: &str, dewif: &str, pin: &str, msg: &str) -> Result<String, DubpError> {
|
pub(super) fn sign(
|
||||||
|
currency: *const raw::c_char,
|
||||||
|
dewif: *const raw::c_char,
|
||||||
|
pin: *const raw::c_char,
|
||||||
|
msg: *const raw::c_char,
|
||||||
|
) -> Result<String, DubpError> {
|
||||||
|
let currency = char_ptr_to_str(currency)?;
|
||||||
|
let dewif = char_ptr_to_str(dewif)?;
|
||||||
|
let pin = char_ptr_to_str(pin)?;
|
||||||
|
let msg = char_ptr_to_str(msg)?;
|
||||||
|
|
||||||
let currency = parse_currency(currency)?;
|
let currency = parse_currency(currency)?;
|
||||||
let mut keypairs = dup_crypto::dewif::read_dewif_file_content(
|
let mut keypairs = dup_crypto::dewif::read_dewif_file_content(
|
||||||
ExpectedCurrency::Specific(currency),
|
ExpectedCurrency::Specific(currency),
|
||||||
dewif,
|
dewif,
|
||||||
&pin,
|
&pin.to_ascii_uppercase(),
|
||||||
)
|
)
|
||||||
.map_err(DubpError::DewifReadError)?;
|
.map_err(DubpError::DewifReadError)?;
|
||||||
if let Some(KeyPairEnum::Ed25519(keypair)) = keypairs.next() {
|
if let Some(KeyPairEnum::Ed25519(keypair)) = keypairs.next() {
|
||||||
|
@ -103,16 +130,27 @@ pub(super) fn sign(currency: &str, dewif: &str, pin: &str, msg: &str) -> Result<
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn sign_several(
|
pub(super) fn sign_several(
|
||||||
currency: &str,
|
currency: *const raw::c_char,
|
||||||
dewif: &str,
|
dewif: *const raw::c_char,
|
||||||
pin: &str,
|
pin: *const raw::c_char,
|
||||||
msgs: &[&str],
|
msgs_len: usize,
|
||||||
|
msgs: *const *const raw::c_char,
|
||||||
) -> Result<Vec<String>, DubpError> {
|
) -> Result<Vec<String>, DubpError> {
|
||||||
|
let currency = char_ptr_to_str(currency)?;
|
||||||
|
let dewif = char_ptr_to_str(dewif)?;
|
||||||
|
let pin = char_ptr_to_str(pin)?;
|
||||||
|
|
||||||
|
let msgs_slice: &[*const raw::c_char] = unsafe { std::slice::from_raw_parts(msgs, msgs_len) };
|
||||||
|
let mut msgs = Vec::with_capacity(msgs_len);
|
||||||
|
for ptr_c_char in msgs_slice {
|
||||||
|
msgs.push(char_ptr_to_str(*ptr_c_char)?);
|
||||||
|
}
|
||||||
|
|
||||||
let currency = parse_currency(currency)?;
|
let currency = parse_currency(currency)?;
|
||||||
let mut keypairs = dup_crypto::dewif::read_dewif_file_content(
|
let mut keypairs = dup_crypto::dewif::read_dewif_file_content(
|
||||||
ExpectedCurrency::Specific(currency),
|
ExpectedCurrency::Specific(currency),
|
||||||
dewif,
|
dewif,
|
||||||
&pin,
|
&pin.to_ascii_uppercase(),
|
||||||
)
|
)
|
||||||
.map_err(DubpError::DewifReadError)?;
|
.map_err(DubpError::DewifReadError)?;
|
||||||
if let Some(KeyPairEnum::Ed25519(keypair)) = keypairs.next() {
|
if let Some(KeyPairEnum::Ed25519(keypair)) = keypairs.next() {
|
||||||
|
|
|
@ -18,7 +18,7 @@
|
||||||
mod dewif;
|
mod dewif;
|
||||||
mod mnemonic;
|
mod mnemonic;
|
||||||
|
|
||||||
use allo_isolate::Isolate;
|
use allo_isolate::{IntoDart, Isolate};
|
||||||
use dup_crypto::{
|
use dup_crypto::{
|
||||||
bases::b58::ToBase58,
|
bases::b58::ToBase58,
|
||||||
dewif::{Currency, DewifReadError, ExpectedCurrency, G1_CURRENCY, G1_TEST_CURRENCY},
|
dewif::{Currency, DewifReadError, ExpectedCurrency, G1_CURRENCY, G1_TEST_CURRENCY},
|
||||||
|
@ -28,7 +28,6 @@ use dup_crypto::{
|
||||||
},
|
},
|
||||||
mnemonic::{Language, Mnemonic, MnemonicType},
|
mnemonic::{Language, Mnemonic, MnemonicType},
|
||||||
};
|
};
|
||||||
use ffi_helpers::null_pointer_check;
|
|
||||||
use std::{ffi::CStr, io, os::raw};
|
use std::{ffi::CStr, io, os::raw};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
|
@ -39,12 +38,16 @@ pub enum DubpError {
|
||||||
DewifReadError(DewifReadError),
|
DewifReadError(DewifReadError),
|
||||||
#[error("I/O error: {0}")]
|
#[error("I/O error: {0}")]
|
||||||
IoErr(io::Error),
|
IoErr(io::Error),
|
||||||
|
#[error("A given parameter is null")]
|
||||||
|
NullParamErr,
|
||||||
#[error("fail to generate random bytes")]
|
#[error("fail to generate random bytes")]
|
||||||
RandErr,
|
RandErr,
|
||||||
#[error("Unknown currency name")]
|
#[error("Unknown currency name")]
|
||||||
UnknownCurrencyName,
|
UnknownCurrencyName,
|
||||||
#[error("Unknown language")]
|
#[error("Unknown language")]
|
||||||
UnknownLanguage,
|
UnknownLanguage,
|
||||||
|
#[error("{0}")]
|
||||||
|
Utf8Error(std::str::Utf8Error),
|
||||||
#[error("Wrong language")]
|
#[error("Wrong language")]
|
||||||
WrongLanguage,
|
WrongLanguage,
|
||||||
}
|
}
|
||||||
|
@ -55,39 +58,41 @@ impl From<io::Error> for DubpError {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! error {
|
struct DartRes(allo_isolate::ffi::DartCObject);
|
||||||
($result:expr) => {
|
impl IntoDart for DartRes {
|
||||||
error!($result, 0);
|
fn into_dart(self) -> allo_isolate::ffi::DartCObject {
|
||||||
};
|
self.0.into_dart()
|
||||||
($result:expr, $error:expr) => {
|
}
|
||||||
match $result {
|
}
|
||||||
Ok(value) => value,
|
impl<E> From<Result<String, E>> for DartRes
|
||||||
Err(e) => {
|
where
|
||||||
ffi_helpers::update_last_error(e);
|
E: ToString,
|
||||||
return $error;
|
{
|
||||||
}
|
fn from(res: Result<String, E>) -> Self {
|
||||||
|
match res {
|
||||||
|
Ok(string) => Self(vec![string].into_dart()),
|
||||||
|
Err(e) => Self(vec![String::from("_"), e.to_string()].into_dart()),
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
}
|
||||||
|
impl<E> From<Result<Vec<String>, E>> for DartRes
|
||||||
|
where
|
||||||
|
E: ToString,
|
||||||
|
{
|
||||||
|
fn from(res: Result<Vec<String>, E>) -> Self {
|
||||||
|
match res {
|
||||||
|
Ok(vec_string) => Self(vec_string.into_dart()),
|
||||||
|
Err(e) => Self(vec![String::with_capacity(0), e.to_string()].into_dart()),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! cstr {
|
pub(crate) fn char_ptr_to_str<'a>(c_char_ptr: *const raw::c_char) -> Result<&'a str, DubpError> {
|
||||||
($ptr:expr) => {
|
if c_char_ptr.is_null() {
|
||||||
cstr!($ptr, 0);
|
Err(DubpError::NullParamErr)
|
||||||
};
|
} else {
|
||||||
($ptr:expr, $error:expr) => {{
|
unsafe { CStr::from_ptr(c_char_ptr).to_str() }.map_err(DubpError::Utf8Error)
|
||||||
null_pointer_check!($ptr);
|
}
|
||||||
error!(unsafe { CStr::from_ptr($ptr).to_str() }, $error)
|
|
||||||
}};
|
|
||||||
}
|
|
||||||
|
|
||||||
#[no_mangle]
|
|
||||||
pub unsafe extern "C" fn last_error_length() -> i32 {
|
|
||||||
ffi_helpers::error_handling::last_error_length()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[no_mangle]
|
|
||||||
pub unsafe extern "C" fn error_message_utf8(buf: *mut raw::c_char, length: i32) -> i32 {
|
|
||||||
ffi_helpers::error_handling::error_message_utf8(buf, length)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
|
@ -97,13 +102,10 @@ pub extern "C" fn change_dewif_pin(
|
||||||
dewif: *const raw::c_char,
|
dewif: *const raw::c_char,
|
||||||
old_pin: *const raw::c_char,
|
old_pin: *const raw::c_char,
|
||||||
new_pin: *const raw::c_char,
|
new_pin: *const raw::c_char,
|
||||||
) -> i32 {
|
) {
|
||||||
let currency = cstr!(currency);
|
Isolate::new(port).post(DartRes::from(dewif::change_pin(
|
||||||
let dewif = cstr!(dewif);
|
currency, dewif, old_pin, new_pin,
|
||||||
let old_pin = cstr!(old_pin);
|
)));
|
||||||
let new_pin = cstr!(new_pin);
|
|
||||||
Isolate::new(port).post(error!(dewif::change_pin(currency, dewif, old_pin, new_pin)));
|
|
||||||
1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
|
@ -113,36 +115,30 @@ pub extern "C" fn gen_dewif(
|
||||||
language: u32,
|
language: u32,
|
||||||
mnemonic: *const raw::c_char,
|
mnemonic: *const raw::c_char,
|
||||||
pin: *const raw::c_char,
|
pin: *const raw::c_char,
|
||||||
) -> i32 {
|
) {
|
||||||
let currency = cstr!(currency);
|
Isolate::new(port).post(DartRes::from(dewif::gen_dewif(
|
||||||
let mnemonic = cstr!(mnemonic);
|
currency, language, mnemonic, pin,
|
||||||
let pin = cstr!(pin);
|
)));
|
||||||
Isolate::new(port).post(error!(dewif::gen_dewif(currency, language, mnemonic, pin)));
|
|
||||||
1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn gen_mnemonic(port: i64, language: u32) -> i32 {
|
pub extern "C" fn gen_mnemonic(port: i64, language: u32) {
|
||||||
Isolate::new(port).post(error!(mnemonic::gen_mnemonic(language)));
|
Isolate::new(port).post(DartRes::from(mnemonic::gen_mnemonic(language)));
|
||||||
1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn gen_pin6(port: i64) -> i32 {
|
pub extern "C" fn gen_pin6(port: i64) {
|
||||||
Isolate::new(port).post(error!(dewif::gen_pin6()));
|
Isolate::new(port).post(DartRes::from(dewif::gen_pin6()));
|
||||||
1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn gen_pin8(port: i64) -> i32 {
|
pub extern "C" fn gen_pin8(port: i64) {
|
||||||
Isolate::new(port).post(error!(dewif::gen_pin8()));
|
Isolate::new(port).post(DartRes::from(dewif::gen_pin8()));
|
||||||
1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn gen_pin10(port: i64) -> i32 {
|
pub extern "C" fn gen_pin10(port: i64) {
|
||||||
Isolate::new(port).post(error!(dewif::gen_pin10()));
|
Isolate::new(port).post(DartRes::from(dewif::gen_pin10()));
|
||||||
1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
|
@ -151,16 +147,8 @@ pub extern "C" fn get_dewif_pubkey(
|
||||||
currency: *const raw::c_char,
|
currency: *const raw::c_char,
|
||||||
dewif: *const raw::c_char,
|
dewif: *const raw::c_char,
|
||||||
pin: *const raw::c_char,
|
pin: *const raw::c_char,
|
||||||
) -> i32 {
|
) {
|
||||||
let currency = cstr!(currency);
|
Isolate::new(port).post(DartRes::from(dewif::get_pubkey(currency, dewif, pin)));
|
||||||
let dewif = cstr!(dewif);
|
|
||||||
let pin = cstr!(pin);
|
|
||||||
Isolate::new(port).post(error!(dewif::get_pubkey(
|
|
||||||
currency,
|
|
||||||
dewif,
|
|
||||||
&pin.to_ascii_uppercase()
|
|
||||||
)));
|
|
||||||
1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
|
@ -168,13 +156,11 @@ pub extern "C" fn mnemonic_to_pubkey(
|
||||||
port: i64,
|
port: i64,
|
||||||
language: u32,
|
language: u32,
|
||||||
mnemonic_phrase: *const raw::c_char,
|
mnemonic_phrase: *const raw::c_char,
|
||||||
) -> i32 {
|
) {
|
||||||
let mnemonic_phrase = cstr!(mnemonic_phrase);
|
Isolate::new(port).post(DartRes::from(mnemonic::mnemonic_to_pubkey(
|
||||||
Isolate::new(port).post(error!(mnemonic::mnemonic_to_pubkey(
|
|
||||||
language,
|
language,
|
||||||
mnemonic_phrase
|
mnemonic_phrase,
|
||||||
)));
|
)));
|
||||||
1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
|
@ -184,18 +170,8 @@ pub extern "C" fn sign(
|
||||||
dewif: *const raw::c_char,
|
dewif: *const raw::c_char,
|
||||||
pin: *const raw::c_char,
|
pin: *const raw::c_char,
|
||||||
msg: *const raw::c_char,
|
msg: *const raw::c_char,
|
||||||
) -> i32 {
|
) {
|
||||||
let currency = cstr!(currency);
|
Isolate::new(port).post(DartRes::from(dewif::sign(currency, dewif, pin, msg)));
|
||||||
let dewif = cstr!(dewif);
|
|
||||||
let pin = cstr!(pin);
|
|
||||||
let msg = cstr!(msg);
|
|
||||||
Isolate::new(port).post(error!(dewif::sign(
|
|
||||||
currency,
|
|
||||||
dewif,
|
|
||||||
&pin.to_ascii_uppercase(),
|
|
||||||
msg
|
|
||||||
)));
|
|
||||||
1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
|
@ -206,24 +182,8 @@ pub extern "C" fn sign_several(
|
||||||
pin: *const raw::c_char,
|
pin: *const raw::c_char,
|
||||||
msgs_len: usize,
|
msgs_len: usize,
|
||||||
msgs: *const *const raw::c_char,
|
msgs: *const *const raw::c_char,
|
||||||
) -> i32 {
|
) {
|
||||||
let currency = cstr!(currency);
|
Isolate::new(port).post(dewif::sign_several(currency, dewif, pin, msgs_len, msgs));
|
||||||
let dewif = cstr!(dewif);
|
|
||||||
let pin = cstr!(pin);
|
|
||||||
|
|
||||||
let msgs_slice: &[*const raw::c_char] = unsafe { std::slice::from_raw_parts(msgs, msgs_len) };
|
|
||||||
let mut vec = Vec::with_capacity(msgs_len);
|
|
||||||
for ptr_c_char in msgs_slice {
|
|
||||||
vec.push(cstr!(*ptr_c_char));
|
|
||||||
}
|
|
||||||
|
|
||||||
Isolate::new(port).post(error!(dewif::sign_several(
|
|
||||||
currency,
|
|
||||||
dewif,
|
|
||||||
&pin.to_ascii_uppercase(),
|
|
||||||
&vec
|
|
||||||
)));
|
|
||||||
1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn u32_to_language(i: u32) -> Result<Language, DubpError> {
|
fn u32_to_language(i: u32) -> Result<Language, DubpError> {
|
||||||
|
|
|
@ -21,7 +21,12 @@ pub(super) fn gen_mnemonic(language: u32) -> Result<String, DubpError> {
|
||||||
Ok(mnemonic.phrase().to_owned())
|
Ok(mnemonic.phrase().to_owned())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn mnemonic_to_pubkey(language: u32, mnemonic: &str) -> Result<String, DubpError> {
|
pub(super) fn mnemonic_to_pubkey(
|
||||||
|
language: u32,
|
||||||
|
mnemonic: *const raw::c_char,
|
||||||
|
) -> Result<String, DubpError> {
|
||||||
|
let mnemonic = char_ptr_to_str(mnemonic)?;
|
||||||
|
|
||||||
let mnemonic = Mnemonic::from_phrase(mnemonic, u32_to_language(language)?)
|
let mnemonic = Mnemonic::from_phrase(mnemonic, u32_to_language(language)?)
|
||||||
.map_err(|_| DubpError::WrongLanguage)?;
|
.map_err(|_| DubpError::WrongLanguage)?;
|
||||||
let seed = dup_crypto::mnemonic::mnemonic_to_seed(&mnemonic);
|
let seed = dup_crypto::mnemonic::mnemonic_to_seed(&mnemonic);
|
||||||
|
|
|
@ -54,45 +54,35 @@ class DubpRust {
|
||||||
/// Generate a random mnemonic
|
/// Generate a random mnemonic
|
||||||
static Future<String> genMnemonic({Language language = Language.english}) {
|
static Future<String> genMnemonic({Language language = Language.english}) {
|
||||||
final completer = Completer<String>();
|
final completer = Completer<String>();
|
||||||
final sendPort = singleCompletePort<String, String>(completer);
|
final sendPort =
|
||||||
final res = native.gen_mnemonic(
|
singleCompletePort<String, List>(completer, callback: _handleErr);
|
||||||
|
native.gen_mnemonic(
|
||||||
sendPort.nativePort,
|
sendPort.nativePort,
|
||||||
language.index,
|
language.index,
|
||||||
);
|
);
|
||||||
if (res != 1) {
|
|
||||||
_throwError();
|
|
||||||
}
|
|
||||||
return completer.future;
|
return completer.future;
|
||||||
}
|
}
|
||||||
|
|
||||||
static Future<String> _genPin(PinLength pinLength) {
|
static Future<String> _genPin(PinLength pinLength) {
|
||||||
final completer = Completer<String>();
|
final completer = Completer<String>();
|
||||||
final sendPort = singleCompletePort<String, String>(completer);
|
final sendPort =
|
||||||
|
singleCompletePort<String, List>(completer, callback: _handleErr);
|
||||||
switch (pinLength) {
|
switch (pinLength) {
|
||||||
case PinLength.ten:
|
case PinLength.ten:
|
||||||
final res = native.gen_pin10(
|
native.gen_pin10(
|
||||||
sendPort.nativePort,
|
sendPort.nativePort,
|
||||||
);
|
);
|
||||||
if (res != 1) {
|
|
||||||
DubpRust._throwError();
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case PinLength.eight:
|
case PinLength.eight:
|
||||||
final res = native.gen_pin8(
|
native.gen_pin8(
|
||||||
sendPort.nativePort,
|
sendPort.nativePort,
|
||||||
);
|
);
|
||||||
if (res != 1) {
|
|
||||||
DubpRust._throwError();
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case PinLength.six:
|
case PinLength.six:
|
||||||
default:
|
default:
|
||||||
final res = native.gen_pin6(
|
native.gen_pin6(
|
||||||
sendPort.nativePort,
|
sendPort.nativePort,
|
||||||
);
|
);
|
||||||
if (res != 1) {
|
|
||||||
DubpRust._throwError();
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return completer.future;
|
return completer.future;
|
||||||
|
@ -110,33 +100,29 @@ class DubpRust {
|
||||||
String newDewif;
|
String newDewif;
|
||||||
{
|
{
|
||||||
final completer = Completer<String>();
|
final completer = Completer<String>();
|
||||||
final sendPort = singleCompletePort<String, String>(completer);
|
final sendPort =
|
||||||
final res = native.change_dewif_pin(
|
singleCompletePort<String, List>(completer, callback: _handleErr);
|
||||||
|
native.change_dewif_pin(
|
||||||
sendPort.nativePort,
|
sendPort.nativePort,
|
||||||
Utf8.toUtf8(currency),
|
Utf8.toUtf8(currency),
|
||||||
Utf8.toUtf8(dewif),
|
Utf8.toUtf8(dewif),
|
||||||
Utf8.toUtf8(oldPin),
|
Utf8.toUtf8(oldPin),
|
||||||
Utf8.toUtf8(newPin),
|
Utf8.toUtf8(newPin),
|
||||||
);
|
);
|
||||||
if (res != 1) {
|
|
||||||
DubpRust._throwError();
|
|
||||||
}
|
|
||||||
newDewif = await completer.future;
|
newDewif = await completer.future;
|
||||||
}
|
}
|
||||||
// publicKey
|
// publicKey
|
||||||
String publicKey;
|
String publicKey;
|
||||||
{
|
{
|
||||||
final completer = Completer<String>();
|
final completer = Completer<String>();
|
||||||
final sendPort = singleCompletePort<String, String>(completer);
|
final sendPort =
|
||||||
final res = native.get_dewif_pubkey(
|
singleCompletePort<String, List>(completer, callback: _handleErr);
|
||||||
|
native.get_dewif_pubkey(
|
||||||
sendPort.nativePort,
|
sendPort.nativePort,
|
||||||
Utf8.toUtf8(currency),
|
Utf8.toUtf8(currency),
|
||||||
Utf8.toUtf8(newDewif),
|
Utf8.toUtf8(newDewif),
|
||||||
Utf8.toUtf8(newPin),
|
Utf8.toUtf8(newPin),
|
||||||
);
|
);
|
||||||
if (res != 1) {
|
|
||||||
_throwError();
|
|
||||||
}
|
|
||||||
publicKey = await completer.future;
|
publicKey = await completer.future;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -161,32 +147,28 @@ class DubpRust {
|
||||||
String publicKey;
|
String publicKey;
|
||||||
{
|
{
|
||||||
final completer = Completer<String>();
|
final completer = Completer<String>();
|
||||||
final sendPort = singleCompletePort<String, String>(completer);
|
final sendPort =
|
||||||
final res = native.mnemonic_to_pubkey(
|
singleCompletePort<String, List>(completer, callback: _handleErr);
|
||||||
|
native.mnemonic_to_pubkey(
|
||||||
sendPort.nativePort,
|
sendPort.nativePort,
|
||||||
language.index,
|
language.index,
|
||||||
Utf8.toUtf8(mnemonic),
|
Utf8.toUtf8(mnemonic),
|
||||||
);
|
);
|
||||||
if (res != 1) {
|
|
||||||
DubpRust._throwError();
|
|
||||||
}
|
|
||||||
publicKey = await completer.future;
|
publicKey = await completer.future;
|
||||||
}
|
}
|
||||||
// dewif
|
// dewif
|
||||||
String dewif;
|
String dewif;
|
||||||
{
|
{
|
||||||
final completer = Completer<String>();
|
final completer = Completer<String>();
|
||||||
final sendPort = singleCompletePort<String, String>(completer);
|
final sendPort =
|
||||||
final res = native.gen_dewif(
|
singleCompletePort<String, List>(completer, callback: _handleErr);
|
||||||
|
native.gen_dewif(
|
||||||
sendPort.nativePort,
|
sendPort.nativePort,
|
||||||
Utf8.toUtf8(currency),
|
Utf8.toUtf8(currency),
|
||||||
language.index,
|
language.index,
|
||||||
Utf8.toUtf8(mnemonic),
|
Utf8.toUtf8(mnemonic),
|
||||||
Utf8.toUtf8(pin),
|
Utf8.toUtf8(pin),
|
||||||
);
|
);
|
||||||
if (res != 1) {
|
|
||||||
DubpRust._throwError();
|
|
||||||
}
|
|
||||||
dewif = await completer.future;
|
dewif = await completer.future;
|
||||||
}
|
}
|
||||||
return Future.value(NewWallet._(dewif, pin, publicKey));
|
return Future.value(NewWallet._(dewif, pin, publicKey));
|
||||||
|
@ -194,18 +176,16 @@ class DubpRust {
|
||||||
|
|
||||||
/// Get pulblic key (in base 58) of `dewif` keypair.
|
/// Get pulblic key (in base 58) of `dewif` keypair.
|
||||||
static Future<String> getDewifPublicKey(
|
static Future<String> getDewifPublicKey(
|
||||||
{String currency = "g1", String dewif, String pin}) {
|
{String currency = "g1", String dewif, String pin}) async {
|
||||||
final completer = Completer<String>();
|
final completer = Completer<String>();
|
||||||
final sendPort = singleCompletePort<String, String>(completer);
|
final sendPort =
|
||||||
final res = native.get_dewif_pubkey(
|
singleCompletePort<String, List>(completer, callback: _handleErr);
|
||||||
|
native.get_dewif_pubkey(
|
||||||
sendPort.nativePort,
|
sendPort.nativePort,
|
||||||
Utf8.toUtf8(currency),
|
Utf8.toUtf8(currency),
|
||||||
Utf8.toUtf8(dewif),
|
Utf8.toUtf8(dewif),
|
||||||
Utf8.toUtf8(pin),
|
Utf8.toUtf8(pin),
|
||||||
);
|
);
|
||||||
if (res != 1) {
|
|
||||||
_throwError();
|
|
||||||
}
|
|
||||||
return completer.future;
|
return completer.future;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -215,17 +195,15 @@ class DubpRust {
|
||||||
static Future<String> sign(
|
static Future<String> sign(
|
||||||
{String currency = "g1", String dewif, String pin, String message}) {
|
{String currency = "g1", String dewif, String pin, String message}) {
|
||||||
final completer = Completer<String>();
|
final completer = Completer<String>();
|
||||||
final sendPort = singleCompletePort<String, String>(completer);
|
final sendPort =
|
||||||
final res = native.sign(
|
singleCompletePort<String, List>(completer, callback: _handleErr);
|
||||||
|
native.sign(
|
||||||
sendPort.nativePort,
|
sendPort.nativePort,
|
||||||
Utf8.toUtf8(currency),
|
Utf8.toUtf8(currency),
|
||||||
Utf8.toUtf8(dewif),
|
Utf8.toUtf8(dewif),
|
||||||
Utf8.toUtf8(pin),
|
Utf8.toUtf8(pin),
|
||||||
Utf8.toUtf8(message),
|
Utf8.toUtf8(message),
|
||||||
);
|
);
|
||||||
if (res != 1) {
|
|
||||||
_throwError();
|
|
||||||
}
|
|
||||||
return completer.future;
|
return completer.future;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -241,9 +219,10 @@ class DubpRust {
|
||||||
String pin,
|
String pin,
|
||||||
List<String> messages}) {
|
List<String> messages}) {
|
||||||
final completer = Completer<List<String>>();
|
final completer = Completer<List<String>>();
|
||||||
final sendPort = singleCompletePort<List<String>, List<String>>(completer);
|
final sendPort = singleCompletePort<List<String>, List>(completer,
|
||||||
|
callback: _handleErrList);
|
||||||
|
|
||||||
final res = native.sign_several(
|
native.sign_several(
|
||||||
sendPort.nativePort,
|
sendPort.nativePort,
|
||||||
Utf8.toUtf8(currency),
|
Utf8.toUtf8(currency),
|
||||||
Utf8.toUtf8(dewif),
|
Utf8.toUtf8(dewif),
|
||||||
|
@ -251,9 +230,7 @@ class DubpRust {
|
||||||
messages.length,
|
messages.length,
|
||||||
_listStringToPtr(messages),
|
_listStringToPtr(messages),
|
||||||
);
|
);
|
||||||
if (res != 1) {
|
|
||||||
_throwError();
|
|
||||||
}
|
|
||||||
return completer.future;
|
return completer.future;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -266,12 +243,25 @@ class DubpRust {
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _throwError() {
|
static String _handleErr(List res) {
|
||||||
final length = native.last_error_length();
|
final List<String> arr = res.cast();
|
||||||
final Pointer<Utf8> message = allocate(count: length);
|
if (arr.length == 1) {
|
||||||
native.error_message_utf8(message, length);
|
return arr[0];
|
||||||
final error = Utf8.fromUtf8(message);
|
} else {
|
||||||
print(error);
|
final error = arr[1];
|
||||||
throw error;
|
print(error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static List<String> _handleErrList(List res) {
|
||||||
|
final List<String> arr = res.cast();
|
||||||
|
if (arr.isNotEmpty && arr[0].isEmpty) {
|
||||||
|
final error = arr[1];
|
||||||
|
print(error);
|
||||||
|
throw error;
|
||||||
|
} else {
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue