Commit 253d5899 authored by Marek Kotewicz's avatar Marek Kotewicz
Browse files

fixed rust string ownership

parent 0b94f2b5
......@@ -10,17 +10,27 @@ import Foundation
@objc(EthkeyBridge)
class EthkeyBridge: NSObject {
@objc func show(_ name: String) {
NSLog("%@", name);
}
@objc func brainWalletAddress(_ seed: String, callback: RCTResponseSenderBlock) -> Void {
var seed_ptr = seed.asPtr()
let keypair_ptr = ethkey_keypair_brainwallet(&seed_ptr)
let address_ptr = ethkey_keypair_address(keypair_ptr)
let address = String.fromStringPtr(ptr: address_ptr!.pointee)
let address_rust_str = ethkey_keypair_address(keypair_ptr)
let address_rust_str_ptr = rust_string_ptr(address_rust_str)
let address = String.fromStringPtr(ptr: address_rust_str_ptr!.pointee)
rust_string_ptr_destroy(address_rust_str_ptr)
rust_string_destroy(address_rust_str)
ethkey_keypair_destroy(keypair_ptr)
ethkey_string_destroy(address_ptr)
callback([address])
}
@objc func brainWalletSecret(_ seed: String, callback: RCTResponseSenderBlock) -> Void {
var seed_ptr = seed.asPtr()
let keypair_ptr = ethkey_keypair_brainwallet(&seed_ptr)
let secret_rust_str = ethkey_keypair_secret(keypair_ptr)
let secret_rust_str_ptr = rust_string_ptr(secret_rust_str)
let secret = String.fromStringPtr(ptr: secret_rust_str_ptr!.pointee)
rust_string_ptr_destroy(secret_rust_str_ptr)
rust_string_destroy(secret_rust_str)
ethkey_keypair_destroy(keypair_ptr)
callback([secret])
}
}
......@@ -10,7 +10,7 @@
@interface RCT_EXTERN_MODULE(EthkeyBridge, NSObject)
RCT_EXTERN_METHOD(show:(NSString *)name)
RCT_EXTERN_METHOD(brainWalletAddress:(NSString*)seed callback:(RCTResponseSenderBlock)callback)
RCT_EXTERN_METHOD(brainWalletSecret:(NSString*)seed callback:(RCTResponseSenderBlock)callback)
@end
......@@ -9,13 +9,13 @@
import Foundation
extension String {
static func fromStringPtr(ptr: string_ptr) -> String {
static func fromStringPtr(ptr: rust_string_ptr) -> String {
let data = NSData(bytes: UnsafeRawPointer(ptr.ptr), length: ptr.len)
return String(data: data as Data, encoding: String.Encoding.utf8)!
}
func asPtr() -> string_ptr {
func asPtr() -> rust_string_ptr {
let data = self.data(using: String.Encoding.utf8, allowLossyConversion: false)!
return string_ptr(ptr: (data as NSData).bytes.bindMemory(to: UInt8.self, capacity: data.count), len: data.count)
return rust_string_ptr(ptr: (data as NSData).bytes.bindMemory(to: UInt8.self, capacity: data.count), len: data.count)
}
}
......@@ -2,26 +2,40 @@
#include <stdint.h>
// rust ffi
// string
struct string_ptr {
struct rust_string;
// string_ptr
struct rust_string_ptr {
const uint8_t* ptr;
size_t len;
};
// return ptr to rust_str
struct rust_string_ptr* rust_string_ptr(const struct rust_string* s);
// removes rust string
void rust_string_destroy(struct rust_string* s);
// removes string pointer
void ethkey_string_destroy(struct string_ptr* s);
void rust_string_ptr_destroy(struct rust_string_ptr* s);
// ethkey ffi
// keypair pointer
struct keypair_ptr;
// creates new brainwallet keypair from seed
struct keypair_ptr* ethkey_keypair_brainwallet(const struct string_ptr* seed);
// removes keypair pointer
void ethkey_keypair_destroy(struct keypair_ptr* keypair);
// creates new brainwallet keypair from seed
struct keypair_ptr* ethkey_keypair_brainwallet(const struct rust_string_ptr* seed);
// returns keypair secret
struct string_ptr* ethkey_keypair_secret(const struct keypair_ptr* keypair);
struct rust_string* ethkey_keypair_secret(const struct keypair_ptr* keypair);
// return keypair address
struct string_ptr* ethkey_keypair_address(const struct keypair_ptr* keypair);
struct rust_string* ethkey_keypair_address(const struct keypair_ptr* keypair);
......@@ -6,42 +6,45 @@ mod string;
use ethkey::{KeyPair, Generator, Brain};
use string::StringPtr;
// string ffi
#[no_mangle]
pub unsafe extern fn ethkey_string_destroy(s: *mut StringPtr) {
pub unsafe extern fn rust_string_ptr(s: *mut String) -> *mut StringPtr {
Box::into_raw(Box::new(StringPtr::from(&**s)))
}
#[no_mangle]
pub unsafe extern fn rust_string_destroy(s: *mut String) {
let _ = Box::from_raw(s);
}
#[no_mangle]
pub extern fn ethkey_keypair_brainwallet(seed: *mut StringPtr) -> *mut KeyPair {
let seed = unsafe { Box::from_raw(seed) };
let generator = Brain::new(seed.as_str().to_owned());
let keypair = generator.generate().unwrap();
let boxed_keypair = Box::new(keypair);
let _ = Box::into_raw(seed);
Box::into_raw(boxed_keypair)
pub unsafe extern fn rust_string_ptr_destroy(s: *mut StringPtr) {
let _ = Box::from_raw(s);
}
// ethkey ffi
#[no_mangle]
pub unsafe extern fn ethkey_keypair_destroy(keypair: *mut KeyPair) {
let _ = Box::from_raw(keypair);
}
#[no_mangle]
pub extern fn ethkey_keypair_secret(keypair: *mut KeyPair) -> *mut StringPtr {
let keypair = unsafe { Box::from_raw(keypair) };
let secret = keypair.secret().to_string();
let secret_ptr = StringPtr::from(&*secret);
let _ = Box::into_raw(keypair);
let _ = Box::into_raw(Box::new(secret));
Box::into_raw(Box::new(secret_ptr))
pub unsafe extern fn ethkey_keypair_brainwallet(seed: *mut StringPtr) -> *mut KeyPair {
let generator = Brain::new((*seed).as_str().to_owned());
Box::into_raw(Box::new(generator.generate().unwrap()))
}
#[no_mangle]
pub extern fn ethkey_keypair_address(keypair: *mut KeyPair) -> *mut StringPtr {
let keypair = unsafe { Box::from_raw(keypair) };
let address = keypair.address().to_string();
let address_ptr = StringPtr::from(&*address);
let _ = Box::into_raw(keypair);
let _ = Box::into_raw(Box::new(address));
Box::into_raw(Box::new(address_ptr))
pub unsafe extern fn ethkey_keypair_secret(keypair: *mut KeyPair) -> *mut String {
let secret = (*keypair).secret().to_string();
Box::into_raw(Box::new(secret))
}
#[no_mangle]
pub unsafe extern fn ethkey_keypair_address(keypair: *mut KeyPair) -> *mut String {
let address = (*keypair).address().to_string();
Box::into_raw(Box::new(address))
}
......@@ -7,11 +7,9 @@ pub struct StringPtr {
pub len: size_t,
}
// TODO: fix ownership of string
impl<'a> From<&'a str> for StringPtr {
fn from(s: &'a str) -> Self {
StringPtr{
StringPtr {
ptr: s.as_ptr(),
len: s.len() as size_t,
}
......@@ -20,8 +18,7 @@ impl<'a> From<&'a str> for StringPtr {
impl StringPtr {
pub fn as_str(&self) -> &str {
use std::slice;
use std::str;
use std::{slice, str};
unsafe {
let slice = slice::from_raw_parts(self.ptr, self.len);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment