-
Notifications
You must be signed in to change notification settings - Fork 4
feat: add shared seedqr decoding #155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,5 +7,6 @@ pub mod lnurl; | |
| pub mod onchain; | ||
| pub mod pubky; | ||
| pub mod scanner; | ||
| pub mod seedqr; | ||
| pub mod trezor; | ||
| pub mod ur; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| use thiserror::Error; | ||
|
|
||
| #[derive(uniffi::Error, Debug, Error, PartialEq, Eq)] | ||
| #[non_exhaustive] | ||
| pub enum SeedQrError { | ||
| #[error("Invalid Standard SeedQR payload")] | ||
| InvalidStandardPayload, | ||
| #[error("Invalid Compact SeedQR payload")] | ||
| InvalidCompactPayload, | ||
| #[error("SeedQR contains an invalid BIP39 mnemonic")] | ||
| InvalidMnemonic, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| use super::SeedQrError; | ||
| use crate::onchain::BitcoinAddressValidator; | ||
|
|
||
| const COMPACT_ENTROPY_LENGTH: usize = 16; | ||
| const STANDARD_PAYLOAD_LENGTH: usize = 48; | ||
| const WORD_COUNT: usize = 12; | ||
| const WORD_INDEX_LENGTH: usize = 4; | ||
|
|
||
| #[uniffi::export] | ||
| pub fn decode_standard_seed_qr(payload: String) -> Result<String, SeedQrError> { | ||
| if payload.len() != STANDARD_PAYLOAD_LENGTH | ||
| || !payload.bytes().all(|byte| byte.is_ascii_digit()) | ||
| { | ||
| return Err(SeedQrError::InvalidStandardPayload); | ||
| } | ||
|
|
||
| let wordlist = BitcoinAddressValidator::get_bip39_wordlist(); | ||
| let mut words = Vec::with_capacity(WORD_COUNT); | ||
| for chunk in payload.as_bytes().chunks_exact(WORD_INDEX_LENGTH) { | ||
| let index = std::str::from_utf8(chunk) | ||
| .ok() | ||
| .and_then(|value| value.parse::<usize>().ok()) | ||
| .filter(|index| *index < wordlist.len()) | ||
| .ok_or(SeedQrError::InvalidStandardPayload)?; | ||
| words.push(wordlist[index].as_str()); | ||
| } | ||
|
|
||
| let mnemonic = words.join(" "); | ||
| BitcoinAddressValidator::validate_mnemonic(&mnemonic) | ||
| .map(|()| mnemonic) | ||
| .map_err(|_| SeedQrError::InvalidMnemonic) | ||
| } | ||
|
|
||
| #[uniffi::export] | ||
| pub fn decode_compact_seed_qr(entropy: Vec<u8>) -> Result<String, SeedQrError> { | ||
| if entropy.len() != COMPACT_ENTROPY_LENGTH { | ||
| return Err(SeedQrError::InvalidCompactPayload); | ||
| } | ||
|
|
||
| BitcoinAddressValidator::entropy_to_mnemonic(&entropy) | ||
| .map_err(|_| SeedQrError::InvalidCompactPayload) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| //! SeedQR decoding shared by the platform applications. | ||
| //! | ||
| //! Camera scanning and normalization of platform-specific barcode payloads | ||
| //! remain application concerns. This module accepts the canonical Standard | ||
| //! SeedQR text or Compact SeedQR entropy described by the SeedQR format. | ||
|
|
||
| mod errors; | ||
| mod implementation; | ||
| #[cfg(test)] | ||
| mod tests; | ||
|
|
||
| pub use errors::SeedQrError; | ||
| pub use implementation::{decode_compact_seed_qr, decode_standard_seed_qr}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| use super::{decode_compact_seed_qr, decode_standard_seed_qr, SeedQrError}; | ||
|
|
||
| const EXPECTED_MNEMONIC: &str = | ||
| "forum undo fragile fade shy sign arrest garment culture tube off merit"; | ||
| const STANDARD_PAYLOAD: &str = "073318950739065415961602009907670428187212261116"; | ||
|
|
||
| #[test] | ||
| fn decodes_standard_seedqr() { | ||
| assert_eq!( | ||
| decode_standard_seed_qr(STANDARD_PAYLOAD.to_string()).unwrap(), | ||
| EXPECTED_MNEMONIC | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn rejects_standard_seedqr_with_short_length() { | ||
| let payload = STANDARD_PAYLOAD.strip_suffix('6').unwrap().to_string(); | ||
|
|
||
| assert_eq!( | ||
| decode_standard_seed_qr(payload), | ||
| Err(SeedQrError::InvalidStandardPayload) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn rejects_standard_seedqr_with_overlong_length() { | ||
| let payload = format!("{STANDARD_PAYLOAD}0"); | ||
|
|
||
| assert_eq!( | ||
| decode_standard_seed_qr(payload), | ||
| Err(SeedQrError::InvalidStandardPayload) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn rejects_standard_seedqr_with_invalid_checksum() { | ||
| let payload = "0000".repeat(12); | ||
|
|
||
| assert_eq!( | ||
| decode_standard_seed_qr(payload), | ||
| Err(SeedQrError::InvalidMnemonic) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn rejects_standard_seedqr_with_non_digit() { | ||
| let payload = format!("{}x", "0000".repeat(12).strip_suffix('0').unwrap()); | ||
|
|
||
| assert_eq!( | ||
| decode_standard_seed_qr(payload), | ||
| Err(SeedQrError::InvalidStandardPayload) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn rejects_standard_seedqr_with_out_of_range_index() { | ||
| let payload = format!("2048{}", "0000".repeat(11)); | ||
|
|
||
| assert_eq!( | ||
| decode_standard_seed_qr(payload), | ||
| Err(SeedQrError::InvalidStandardPayload) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn decodes_compact_seedqr() { | ||
| let entropy = hex::decode("5bbd9d71a8ec7990831aff359d426545").unwrap(); | ||
|
|
||
| assert_eq!(decode_compact_seed_qr(entropy).unwrap(), EXPECTED_MNEMONIC); | ||
| } | ||
|
|
||
| #[test] | ||
| fn decodes_compact_seedqr_containing_null_bytes() { | ||
| assert_eq!( | ||
| decode_compact_seed_qr(vec![0; 16]).unwrap(), | ||
| "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn rejects_compact_seedqr_with_invalid_length() { | ||
| assert_eq!( | ||
| decode_compact_seed_qr(vec![0; 15]), | ||
| Err(SeedQrError::InvalidCompactPayload) | ||
| ); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.