Skip to content

Commit

Permalink
Merge pull request #94 from getlipa/feature/words-by-prefix
Browse files Browse the repository at this point in the history
Implement `words_by_prefix()`
  • Loading branch information
danielgranhao authored Mar 23, 2023
2 parents fdaf835 + fc93cf5 commit ff91cdf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ pub use crate::auth::Auth;
pub use crate::errors::{Error as WalletError, WalletRuntimeErrorCode};
pub use crate::native_logger::init_native_logger_once;
pub use crate::secrets::{
derive_keys, generate_keypair, generate_mnemonic, Descriptors, KeyPair, WalletKeys,
derive_keys, generate_keypair, generate_mnemonic, words_by_prefix, Descriptors, KeyPair,
WalletKeys,
};
pub use crate::signing::sign;
pub use crate::wallet::{Config, Tx, TxDetails, TxStatus, Wallet};
Expand Down
4 changes: 4 additions & 0 deletions src/lipabusinesslib.udl
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,8 @@ namespace lipabusinesslib {

// Generate a new keypair. Used for authentication with the backend.
KeyPair generate_keypair();

// Return a list of valid BIP-39 English words starting with the prefix.
// Calling this function with empty prefix will return the full list of BIP-39 words.
sequence<string> words_by_prefix(string prefix);
};
21 changes: 20 additions & 1 deletion src/secrets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bdk::bitcoin::secp256k1::PublicKey;
use bdk::bitcoin::util::bip32::{DerivationPath, ExtendedPrivKey, KeySource};
use bdk::bitcoin::Network;
use bdk::descriptor::Segwitv0;
use bdk::keys::bip39::Mnemonic;
use bdk::keys::bip39::{Language, Mnemonic};
use bdk::keys::DescriptorKey::Secret;
use bdk::keys::{DerivableKey, DescriptorKey, ExtendedKey};
use bdk::miniscript::ToPublicKey;
Expand Down Expand Up @@ -210,6 +210,14 @@ pub fn generate_keypair() -> KeyPair {
}
}

pub fn words_by_prefix(prefix: String) -> Vec<String> {
Language::English
.words_by_prefix(&prefix)
.iter()
.map(|w| w.to_string())
.collect()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -327,4 +335,15 @@ mod tests {

check_keys_match(keypair);
}

#[test]
fn test_words_by_prefix() {
assert_eq!(words_by_prefix("".to_string()).len(), 2048);
assert_eq!(words_by_prefix("s".to_string()).len(), 250);
assert_eq!(words_by_prefix("sc".to_string()).len(), 15);
assert_eq!(words_by_prefix("sch".to_string()), vec!["scheme", "school"]);
assert_eq!(words_by_prefix("sche".to_string()), vec!["scheme"]);
assert_eq!(words_by_prefix("scheme".to_string()), vec!["scheme"]);
assert_eq!(words_by_prefix("schemelol".to_string()).len(), 0);
}
}

0 comments on commit ff91cdf

Please sign in to comment.