Skip to content

Commit

Permalink
Refactoring bindings (#234)
Browse files Browse the repository at this point in the history
* chore: Refactoring bindings

* fix: Rust errors for iOS builds

* chore: Cleanup

* feat: QR codes generated in Rust :)

* chore: Make FFI between iOS and Android equivalent

* feat: Error handling in iOS

* feat: Add bip39 boolean when recovering from a phrase

* fix(): valid if bip39 and show seed type

* fix(): phrasing

* question mark

* remove "seed"

* remove quotes

* chore: Restore EthkeyBridge changes

* chore: Remove added code to String.swift

* fix: Update blockies, fix tests

* chore: Handle rust errors for blockies

* chore: use c_uint, update deps

* fix: Attempt to fix the error on randomPhrase in iOS

* fix: Hopefully fix the String allocation issues

* fix: Expose EthkeyBridge.qrCode to the iOS build

* fix: Propagate errors properly
  • Loading branch information
maciejhirsz authored Jul 8, 2019
1 parent ea76a1a commit eb55ff6
Show file tree
Hide file tree
Showing 25 changed files with 953 additions and 1,348 deletions.
20 changes: 20 additions & 0 deletions android/app/src/main/java/io/parity/signer/EthkeyBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ public void brainWalletAddress(String seed, Promise promise) {
promise.resolve(ethkeyBrainwalletAddress(seed));
}

@ReactMethod
public void brainWalletBIP39Address(String seed, Promise promise) {
try {
promise.resolve(ethkeyBrainwalletBIP39Address(seed));
} catch (Exception e) {
promise.reject("invalid phrase", null, null);
}
}

@ReactMethod
public void brainWalletSign(String seed, String message, Promise promise) {
promise.resolve(ethkeyBrainwalletSign(seed, message));
Expand Down Expand Up @@ -93,7 +102,17 @@ public void decryptData(String data, String password, Promise promise) {
}
}

@ReactMethod
public void qrCode(String data, Promise promise) {
try {
promise.resolve(ethkeyQrCode(data));
} catch (Exception e) {
promise.reject("failed to create QR code", null, null);
}
}

private static native String ethkeyBrainwalletAddress(String seed);
private static native String ethkeyBrainwalletBIP39Address(String seed);
private static native String ethkeyBrainwalletSign(String seed, String message);
private static native String ethkeyRlpItem(String data, int position);
private static native String ethkeyKeccak(String data);
Expand All @@ -102,4 +121,5 @@ public void decryptData(String data, String password, Promise promise) {
private static native String ethkeyRandomPhrase();
private static native String ethkeyEncryptData(String data, String password);
private static native String ethkeyDecryptData(String data, String password);
private static native String ethkeyQrCode(String data);
}
133 changes: 0 additions & 133 deletions ios/EthkeyBridge.1.swift

This file was deleted.

3 changes: 2 additions & 1 deletion ios/NativeSigner/EthkeyBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ @interface RCT_EXTERN_MODULE(EthkeyBridge, NSObject)
RCT_EXTERN_METHOD(keccak:(NSString*)data resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(ethSign:(NSString*)data resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(blockiesIcon:(NSString*)seed resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(randomPhrase:(NSUInteger)words resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(randomPhrase:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(encryptData:(NSString*)data password:(NSString*)password resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(decryptData:(NSString*)data password:(NSString*)password resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(qrCode:(NSString*)data resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)

@end
60 changes: 40 additions & 20 deletions ios/NativeSigner/EthkeyBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,38 @@ import Foundation
@objc(EthkeyBridge)
class EthkeyBridge: NSObject {

open static func requiresMainQueueSetup() -> Bool {
public static func requiresMainQueueSetup() -> Bool {
return true;
}

@objc func brainWalletAddress(_ seed: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
var error: UInt32 = 0
var seed_ptr = seed.asPtr()
let keypair_ptr = ethkey_keypair_brainwallet(&seed_ptr)
let address_rust_str = ethkey_keypair_address(keypair_ptr)
let address_rust_str = ethkey_brainwallet_address(&error, &seed_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)
resolve(address)
}

@objc func brainWalletSign(_ seed: String, message: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
var error: UInt32 = 0
print(seed, " + ", message)
var seed_ptr = seed.asPtr()
var message_ptr = message.asPtr()
let keypair_ptr = ethkey_keypair_brainwallet(&seed_ptr)
let signature_rust_str = ethkey_keypair_sign(keypair_ptr, &message_ptr)
let signature_rust_str = ethkey_brainwallet_sign(&error, &seed_ptr, &message_ptr)
let signature_rust_str_ptr = rust_string_ptr(signature_rust_str)
let signature = String.fromStringPtr(ptr: signature_rust_str_ptr!.pointee)
rust_string_ptr_destroy(signature_rust_str_ptr)
rust_string_destroy(signature_rust_str)
ethkey_keypair_destroy(keypair_ptr)
resolve(signature)
}

@objc func rlpItem(_ rlp: String, position: UInt32, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
var rlp_ptr = rlp.asPtr()
var error: UInt32 = 0
let item_rust_str = rlp_item(&rlp_ptr, position, &error)
var rlp_ptr = rlp.asPtr()
let item_rust_str = rlp_item(&error, &rlp_ptr, position)
let item_rust_str_ptr = rust_string_ptr(item_rust_str)
let item = String.fromStringPtr(ptr: item_rust_str_ptr!.pointee)
rust_string_ptr_destroy(item_rust_str_ptr)
Expand All @@ -57,8 +55,9 @@ class EthkeyBridge: NSObject {
}

@objc func keccak(_ data: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
var error: UInt32 = 0
var data_ptr = data.asPtr()
let hash_rust_str = keccak256(&data_ptr)
let hash_rust_str = keccak256(&error, &data_ptr)
let hash_rust_str_ptr = rust_string_ptr(hash_rust_str)
let hash = String.fromStringPtr(ptr: hash_rust_str_ptr!.pointee)
rust_string_ptr_destroy(hash_rust_str_ptr)
Expand All @@ -67,8 +66,9 @@ class EthkeyBridge: NSObject {
}

@objc func ethSign(_ data: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
var error: UInt32 = 0
var data_ptr = data.asPtr()
let hash_rust_str = eth_sign(&data_ptr)
let hash_rust_str = eth_sign(&error, &data_ptr)
let hash_rust_str_ptr = rust_string_ptr(hash_rust_str)
let hash = String.fromStringPtr(ptr: hash_rust_str_ptr!.pointee)
rust_string_ptr_destroy(hash_rust_str_ptr)
Expand All @@ -77,17 +77,23 @@ class EthkeyBridge: NSObject {
}

@objc func blockiesIcon(_ seed: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
var error: UInt32 = 0
var seed_ptr = seed.asPtr()
let icon_rust_str = blockies_icon(&seed_ptr)
let icon_rust_str = blockies_icon(&error, &seed_ptr)
let icon_rust_str_ptr = rust_string_ptr(icon_rust_str)
let icon = String.fromStringPtr(ptr: icon_rust_str_ptr!.pointee)
rust_string_ptr_destroy(icon_rust_str_ptr)
rust_string_destroy(icon_rust_str)
resolve(icon)
if error == 0 {
resolve(icon)
} else {
reject("Failed to generate blockies", nil, nil)
}
}

@objc func randomPhrase(resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
let words_rust_str = random_phrase()
@objc func randomPhrase(_ resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
var error: UInt32 = 0
let words_rust_str = random_phrase(&error)
let words_rust_str_ptr = rust_string_ptr(words_rust_str)
let words = String.fromStringPtr(ptr: words_rust_str_ptr!.pointee)
rust_string_ptr_destroy(words_rust_str_ptr)
Expand All @@ -96,9 +102,10 @@ class EthkeyBridge: NSObject {
}

@objc func encryptData(_ data: String, password: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
var error: UInt32 = 0
var data_ptr = data.asPtr()
var password_ptr = password.asPtr()
let encrypted_data_rust_str = encrypt_data(&data_ptr, &password_ptr)
let encrypted_data_rust_str = encrypt_data(&error, &data_ptr, &password_ptr)
let encrypted_data_rust_str_ptr = rust_string_ptr(encrypted_data_rust_str)
let encrypted_data = String.fromStringPtr(ptr: encrypted_data_rust_str_ptr!.pointee)
rust_string_ptr_destroy(encrypted_data_rust_str_ptr)
Expand All @@ -107,20 +114,33 @@ class EthkeyBridge: NSObject {
}

@objc func decryptData(_ data: String, password: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
var error: UInt32 = 0
var data_ptr = data.asPtr()
var password_ptr = password.asPtr()
var error: UInt32 = 0
let decrypted_data_rust_str = decrypt_data(&data_ptr, &password_ptr, &error)
let decrypted_data_rust_str = decrypt_data(&error, &data_ptr, &password_ptr)
let decrypted_data_rust_str_ptr = rust_string_ptr(decrypted_data_rust_str)
let decrypted_data = String.fromStringPtr(ptr: decrypted_data_rust_str_ptr!.pointee)
rust_string_ptr_destroy(decrypted_data_rust_str_ptr)
rust_string_destroy(decrypted_data_rust_str)
if error == 0 {
resolve(decrypted_data)
} else if error == 1{
reject("invalid data", nil, nil)
} else {
reject("invalid password", nil, nil)
}
}

@objc func qrCode(_ data: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
var error: UInt32 = 0
var data_ptr = data.asPtr()
let icon_rust_str = qrcode(&error, &data_ptr)
let icon_rust_str_ptr = rust_string_ptr(icon_rust_str)
let icon = String.fromStringPtr(ptr: icon_rust_str_ptr!.pointee)
rust_string_ptr_destroy(icon_rust_str_ptr)
rust_string_destroy(icon_rust_str)
if error == 0 {
resolve(icon)
} else {
reject("Failed to generate blockies", nil, nil)
}
}
}
2 changes: 1 addition & 1 deletion ios/NativeSigner/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extension String {
let data = NSData(bytes: UnsafeRawPointer(ptr.ptr), length: ptr.len)
return String(data: data as Data, encoding: String.Encoding.utf8)!
}

func asPtr() -> rust_string_ptr {
let data = self.data(using: String.Encoding.utf8, allowLossyConversion: false)!
return rust_string_ptr(ptr: (data as NSData).bytes.bindMemory(to: UInt8.self, capacity: data.count), len: data.count)
Expand Down
Loading

0 comments on commit eb55ff6

Please sign in to comment.