From 276c826b614724ccf65a6de41d1d7d59e2851cc4 Mon Sep 17 00:00:00 2001 From: Derek Guenther Date: Wed, 24 Apr 2024 13:14:41 -0400 Subject: [PATCH] Add spendingKeyToWords to ironfish-native-module --- .../IronfishNativeModule.kt | 27 +++++++++++++++++++ .../ios/IronfishNativeModule.swift | 5 ++++ .../rust_lib/src/lib.rs | 13 +++++++++ packages/ironfish-native-module/src/index.ts | 7 +++++ .../mobile-app/shims/ironfish-rust-nodejs.js | 1 + 5 files changed, 53 insertions(+) diff --git a/packages/ironfish-native-module/android/src/main/java/expo/modules/ironfishnativemodule/IronfishNativeModule.kt b/packages/ironfish-native-module/android/src/main/java/expo/modules/ironfishnativemodule/IronfishNativeModule.kt index 10cbd4e..4dca9cc 100644 --- a/packages/ironfish-native-module/android/src/main/java/expo/modules/ironfishnativemodule/IronfishNativeModule.kt +++ b/packages/ironfish-native-module/android/src/main/java/expo/modules/ironfishnativemodule/IronfishNativeModule.kt @@ -41,6 +41,24 @@ class IronfishNativeModule : Module() { ) } + Function("spendingKeyToWords") { privateKey: String, languageCode: Long -> + try { + return spendingKeyToWords(privateKey: privateKey, languageCode: languageCode) + } catch (error: Exception) { + error.printStackTrace() + throw error + } + } + + Function("wordsToSpendingKey") { words: String, languageCode: Long -> + try { + return wordsToSpendingKey(words: words, languageCode: languageCode) + } catch (error: Exception) { + error.printStackTrace() + throw error + } + } + AsyncFunction("generateKeyFromPrivateKey") { privateKey: String -> val k = uniffi.rust_lib.generateKeyFromPrivateKey(privateKey) @@ -53,5 +71,14 @@ class IronfishNativeModule : Module() { k.proofAuthorizingKey ) } + + Function("isValidPublicAddress") { hexAddress: String -> + try { + uniffi.rust_lib.isValidPublicAddress(hexAddress: hexAddress) + } catch (error: Exception) { + error.printStackTrace() + throw error + } + } } } diff --git a/packages/ironfish-native-module/ios/IronfishNativeModule.swift b/packages/ironfish-native-module/ios/IronfishNativeModule.swift index f9affd7..82e1dea 100644 --- a/packages/ironfish-native-module/ios/IronfishNativeModule.swift +++ b/packages/ironfish-native-module/ios/IronfishNativeModule.swift @@ -43,6 +43,11 @@ public class IronfishNativeModule: Module { ) } + Function("spendingKeyToWords") { (privateKey: String, languageCode: Int32) throws -> String in + let phrase = try spendingKeyToWords(privateKey: privateKey, languageCode: languageCode) + return phrase + } + Function("wordsToSpendingKey") { (words: String, languageCode: Int32) throws -> String in let k = try wordsToSpendingKey(words: words, languageCode: languageCode) return k diff --git a/packages/ironfish-native-module/rust_lib/src/lib.rs b/packages/ironfish-native-module/rust_lib/src/lib.rs index 6dd9e2c..5593807 100644 --- a/packages/ironfish-native-module/rust_lib/src/lib.rs +++ b/packages/ironfish-native-module/rust_lib/src/lib.rs @@ -66,6 +66,19 @@ fn generate_key() -> Key { } } +#[uniffi::export] +pub fn spending_key_to_words( + private_key: String, + language_code: i32, +) -> Result { + let key = SaplingKey::from_hex(&private_key).map_err(|e| EnumError::Error { msg: e.to_string() })?; + let language_code_enum: LanguageCode = LanguageCode::from_i32(language_code).ok_or_else(|| EnumError::Error { msg: "Invalid language code".to_string() })?; + let language = Language::from(language_code_enum); + + let mnemonic = key.to_words(language).map_err(|e| EnumError::Error { msg: e.to_string() })?; + Ok(mnemonic.into_phrase()) +} + #[uniffi::export] pub fn words_to_spending_key( words: String, diff --git a/packages/ironfish-native-module/src/index.ts b/packages/ironfish-native-module/src/index.ts index f075550..7d553fa 100644 --- a/packages/ironfish-native-module/src/index.ts +++ b/packages/ironfish-native-module/src/index.ts @@ -15,6 +15,13 @@ export function generateKey(): Key { return IronfishNativeModule.generateKey(); } +export function spendingKeyToWords( + privateKey: string, + languageCode: number, +): string { + return IronfishNativeModule.spendingKeyToWords(privateKey, languageCode); +} + export function wordsToSpendingKey( words: string, languageCode: number, diff --git a/packages/mobile-app/shims/ironfish-rust-nodejs.js b/packages/mobile-app/shims/ironfish-rust-nodejs.js index e11ba89..84691b7 100644 --- a/packages/mobile-app/shims/ironfish-rust-nodejs.js +++ b/packages/mobile-app/shims/ironfish-rust-nodejs.js @@ -33,6 +33,7 @@ const mockIronfishRustNodejs = { throw new Error(message); } }), + spendingKeyToWords: IronfishNativeModule.spendingKeyToWords, wordsToSpendingKey: IronfishNativeModule.wordsToSpendingKey, generateKeyFromPrivateKey: IronfishNativeModule.generateKeyFromPrivateKey, isValidPublicAddress: IronfishNativeModule.isValidPublicAddress,