Skip to content

Commit

Permalink
Add spendingKeyToWords to ironfish-native-module (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
dguenther authored Apr 24, 2024
1 parent 6351031 commit 81765cc
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions packages/ironfish-native-module/rust_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ fn generate_key() -> Key {
}
}

#[uniffi::export]
pub fn spending_key_to_words(
private_key: String,
language_code: i32,
) -> Result<String, EnumError> {
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,
Expand Down
7 changes: 7 additions & 0 deletions packages/ironfish-native-module/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions packages/mobile-app/shims/ironfish-rust-nodejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const mockIronfishRustNodejs = {
throw new Error(message);
}
}),
spendingKeyToWords: IronfishNativeModule.spendingKeyToWords,
wordsToSpendingKey: IronfishNativeModule.wordsToSpendingKey,
generateKeyFromPrivateKey: IronfishNativeModule.generateKeyFromPrivateKey,
isValidPublicAddress: IronfishNativeModule.isValidPublicAddress,
Expand Down

0 comments on commit 81765cc

Please sign in to comment.