Browse Source

ref(dubp): bind an array of string directly and use it in signSeveral

dubp_rs-doc
librelois 2 months ago
parent
commit
2492465d46
4 changed files with 61 additions and 15 deletions
  1. +2
    -2
      native/dubp_rs/src/dewif.rs
  2. +10
    -3
      native/dubp_rs/src/lib.rs
  3. +37
    -9
      packages/dubp_rs/ios/Classes/binding.h
  4. +12
    -1
      packages/dubp_rs/lib/dubp.dart

+ 2
- 2
native/dubp_rs/src/dewif.rs View File

@ -106,7 +106,7 @@ pub(super) fn sign_several(
currency: &str,
dewif: &str,
pin: &str,
msgs: &str,
msgs: &[&str],
) -> Result<Vec<String>, DubpError> {
let currency = parse_currency(currency)?;
let mut keypairs = dup_crypto::dewif::read_dewif_file_content(
@ -118,7 +118,7 @@ pub(super) fn sign_several(
if let Some(KeyPairEnum::Ed25519(keypair)) = keypairs.next() {
let signator = keypair.generate_signator();
Ok(msgs
.split("\n\t\n")
.iter()
.map(|msg| signator.sign(msg.as_bytes()).to_base64())
.collect())
} else {


+ 10
- 3
native/dubp_rs/src/lib.rs View File

@ -204,17 +204,24 @@ pub extern "C" fn sign_several(
currency: *const raw::c_char,
dewif: *const raw::c_char,
pin: *const raw::c_char,
msgs: *const raw::c_char,
msgs_len: usize,
msgs: *const *const raw::c_char,
) -> i32 {
let currency = cstr!(currency);
let dewif = cstr!(dewif);
let pin = cstr!(pin);
let msgs = cstr!(msgs);
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(),
msgs
&vec
)));
1
}


+ 37
- 9
packages/dubp_rs/ios/Classes/binding.h View File

@ -161,21 +161,49 @@ extern "C"
{
#endif // __cplusplus
/**
* Destroy the Tokio Runtime, and return 1 if everything is okay
*/
int32_t destroy_runtime(RuntimePtr runtime);
int32_t error_message_utf8(char *buf, int32_t length);
int32_t last_error_length(void);
int32_t change_dewif_pin(int64_t port,
const char *currency,
const char *dewif,
const char *old_pin,
const char *new_pin);
int32_t error_message_utf8(char *buf, int32_t length);
int32_t gen_dewif(int64_t port,
const char *currency,
uint32_t language,
const char *mnemonic,
const char *pin);
int32_t gen_mnemonic(int64_t port, uint32_t language);
int32_t gen_pin10(int64_t port);
int32_t gen_pin6(int64_t port);
int32_t gen_pin8(int64_t port);
int32_t get_dewif_pubkey(int64_t port, const char *currency, const char *dewif, const char *pin);
int32_t last_error_length(void);
int32_t load_page(RuntimePtr runtime, const char *url, DartPort port_id);
int32_t mnemonic_to_pubkey(int64_t port, uint32_t language, const char *mnemonic_phrase);
/**
* Setup a new Tokio Runtime and return a pointer to it so it could be used later to run tasks
*/
RuntimePtr setup_runtime(DartPostCObjectFnPtr post_cobject);
int32_t sign(int64_t port,
const char *currency,
const char *dewif,
const char *pin,
const char *msg);
int32_t sign_several(int64_t port,
const char *currency,
const char *dewif,
const char *pin,
const char *msgs);
#ifdef __cplusplus
} // extern "C"


+ 12
- 1
packages/dubp_rs/lib/dubp.dart View File

@ -242,12 +242,14 @@ class DubpRust {
List<String> messages}) {
final completer = Completer<List<String>>();
final sendPort = singleCompletePort<List<String>, List<String>>(completer);
final res = native.sign_several(
sendPort.nativePort,
Utf8.toUtf8(currency),
Utf8.toUtf8(dewif),
Utf8.toUtf8(pin),
Utf8.toUtf8(messages.join('\n\t\n')),
messages.length,
_listStringToPtr(messages),
);
if (res != 1) {
_throwError();
@ -255,6 +257,15 @@ class DubpRust {
return completer.future;
}
static Pointer<Pointer<Utf8>> _listStringToPtr(List<String> list) {
final listUtf8 = list.map(Utf8.toUtf8).toList();
final Pointer<Pointer<Utf8>> ptr = allocate(count: listUtf8.length);
for (var i = 0; i < listUtf8.length; i++) {
ptr[i] = listUtf8[i];
}
return ptr;
}
static void _throwError() {
final length = native.last_error_length();
final Pointer<Utf8> message = allocate(count: length);


Loading…
Cancel
Save