Commit f870ff57 authored by Marek Kotewicz's avatar Marek Kotewicz
Browse files

rust ffi exposed

parent 915dd554
......@@ -14,9 +14,11 @@ class EthkeyBridge: NSObject {
NSLog("%@", name);
}
@objc func brainWallet(_ seed: NSString, callback: RCTResponseSenderBlock) -> Void {
let s = tmp_string()
let tmp_s = String.fromStringPtr(ptr: s!.pointee)
callback([tmp_s as NSString])
@objc func brainWallet(_ seed: String, callback: RCTResponseSenderBlock) -> Void {
var seed_ptr = seed.asPtr()
let keypair = ethkey_keypair_brainwallet(&seed_ptr)
let address_ptr = ethkey_keypair_address(keypair)
let address = String.fromStringPtr(ptr: address_ptr!.pointee)
callback([address])
}
}
......@@ -260,12 +260,12 @@
821AED75338B40468A2A8B7A /* RCTCamera.xcodeproj */ = {isa = PBXFileReference; explicitFileType = undefined; fileEncoding = 9; includeInIndex = 0; lastKnownFileType = "wrapper.pb-project"; name = RCTCamera.xcodeproj; path = "../node_modules/react-native-camera/ios/RCTCamera.xcodeproj"; sourceTree = "<group>"; };
832341B01AAA6A8300B99B32 /* RCTText.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTText.xcodeproj; path = "../node_modules/react-native/Libraries/Text/RCTText.xcodeproj"; sourceTree = "<group>"; };
EA529FD11E5A00B2004DF471 /* EthkeyBridge.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = EthkeyBridge.m; path = NativeSigner/EthkeyBridge.m; sourceTree = "<group>"; };
EA529FF01E5A3FAB004DF471 /* signer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = signer.h; sourceTree = "<group>"; };
EA529FF11E5A4260004DF471 /* NativeSigner-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NativeSigner-Bridging-Header.h"; sourceTree = "<group>"; };
EA529FF21E5A4261004DF471 /* String.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = String.swift; path = NativeSigner/String.swift; sourceTree = "<group>"; };
EA529FF41E5A4EF2004DF471 /* EthkeyBridge.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EthkeyBridge.swift; sourceTree = "<group>"; };
EA529FF71E5A525F004DF471 /* libsigner.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libsigner.a; path = ../rust/signer/libsigner.a; sourceTree = "<group>"; };
EA529FF91E5A52AD004DF471 /* libsigner.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libsigner.a; path = ../rust/signer/libsigner.a; sourceTree = "<group>"; };
EA529FFB1E5A6F4B004DF471 /* signer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = signer.h; path = ../rust/signer/signer.h; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
......@@ -389,7 +389,7 @@
EA529FD11E5A00B2004DF471 /* EthkeyBridge.m */,
EA529FF21E5A4261004DF471 /* String.swift */,
EA529FF41E5A4EF2004DF471 /* EthkeyBridge.swift */,
EA529FF01E5A3FAB004DF471 /* signer.h */,
EA529FFB1E5A6F4B004DF471 /* signer.h */,
13B07FB51A68108700A75B9A /* Images.xcassets */,
13B07FB61A68108700A75B9A /* Info.plist */,
13B07FB11A68108700A75B9A /* LaunchScreen.xib */,
......
#pragma once
#include <stdint.h>
// string
struct string_ptr {
const uint8_t* ptr;
size_t len;
};
// removes string pointer
void ethstore_string_destroy(struct string_ptr* s);
// keypair pointer
struct keypair_ptr;
// creates new brainwallet keypair from seed
struct keypair_ptr* ethstore_keypair_brainwallet(const struct string_ptr* seed);
// removes keypair pointer
void ethstore_keypair_destroy(struct keypair_ptr* keypair);
struct string_ptr* ethstore_keypair_address(const struct keypair_ptr* keypair);
// test
struct string_ptr* tmp_string();
......@@ -20,7 +20,8 @@ struct keypair_ptr* ethkey_keypair_brainwallet(const struct string_ptr* seed);
// removes keypair pointer
void ethkey_keypair_destroy(struct keypair_ptr* keypair);
struct string_ptr* ethkey_keypair_address(const struct keypair_ptr* keypair);
// returns keypair secret
struct string_ptr* ethkey_keypair_secret(const struct keypair_ptr* keypair);
// test
struct string_ptr* tmp_string();
// return keypair address
struct string_ptr* ethkey_keypair_address(const struct keypair_ptr* keypair);
extern crate libc;
extern crate ethkey;
mod string;
use ethkey::{KeyPair, Generator, Brain};
use string::StringPtr;
#[no_mangle]
pub extern fn tmp_string() -> *mut StringPtr {
let hello: StringPtr = "hello from rust".into();
// let's put it on the heap
let boxed_hello = Box::new(hello);
// let's make a pointer to the heap, return it and forget about it
Box::into_raw(boxed_hello)
pub unsafe extern fn ethkey_string_destroy(s: *mut StringPtr) {
let _ = Box::from_raw(s);
}
#[no_mangle]
pub extern fn simple() -> i32 {
12345
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)
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
}
#[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))
}
#[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))
}
......@@ -7,6 +7,8 @@ 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{
......
......@@ -55,10 +55,10 @@ export class NewAccount extends Component {
<Text style={styles.hint}>brain wallet seed</Text>
<NewAccountInput seed={this.state.seed} onChangeText={
debounce((text) => {
var keypair = keypairFromPhrase(text)
toAddressAsync(keypair, (address) => {
//var keypair = keypairFromPhrase(text)
toAddressAsync(text, (address) => {
self.setState({
keypair: keypairFromPhrase(text),
//keypair:
address: address,
})
})
......
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