Skip to content

Commit 2352fbc

Browse files
authored
Merge pull request zcash#7030 from daira/nicer-bip0039-language-handling
Make the handling of bip0039 languages nicer
2 parents 4256156 + 78695a7 commit 2352fbc

File tree

4 files changed

+50
-119
lines changed

4 files changed

+50
-119
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ secrecy = "0.8"
9898
thiserror = "2"
9999
time = { version = "0.3", features = ["formatting", "macros"] }
100100

101+
# Macros
102+
macro_find_and_replace = "1"
103+
101104
[dev-dependencies]
102105
incrementalmerkletree = { version = "0.7", features = ["test-dependencies"] }
103106
proptest = "1.0.0"

qa/supply-chain/audits.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,6 +1569,12 @@ who = "Daira-Emma Hopwood <[email protected]>"
15691569
criteria = "safe-to-deploy"
15701570
delta = "0.4.20 -> 0.4.21"
15711571

1572+
[[audits.macro_find_and_replace]]
1573+
who = "Daira-Emma Hopwood <[email protected]>"
1574+
criteria = "safe-to-deploy"
1575+
version = "1.0.0"
1576+
notes = "Fully reviewed. No problems found other than a few typos in documentation (filed https://github.com/lord-ne/rust-macro-find-and-replace/pull/1 )."
1577+
15721578
[[audits.maybe-rayon]]
15731579
who = "Sean Bowe <[email protected]>"
15741580
criteria = "safe-to-deploy"

src/rust/src/zip339_ffi.rs

Lines changed: 34 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use libc::{c_char, size_t};
2+
use macro_find_and_replace::replace_token_sequence;
23
use std::{
34
borrow::Cow,
45
ffi::{CStr, CString},
@@ -44,65 +45,37 @@ impl Language {
4445
Language(_) => None,
4546
}
4647
}
48+
}
49+
50+
macro_rules! all_languages {
51+
($self:expr, $ctx:expr, $e:expr) => {
52+
$self.handle(
53+
$ctx,
54+
replace_token_sequence!{[LANGUAGE], [bip0039::English], $e},
55+
replace_token_sequence!{[LANGUAGE], [bip0039::ChineseSimplified], $e},
56+
replace_token_sequence!{[LANGUAGE], [bip0039::ChineseTraditional], $e},
57+
replace_token_sequence!{[LANGUAGE], [bip0039::Czech], $e},
58+
replace_token_sequence!{[LANGUAGE], [bip0039::French], $e},
59+
replace_token_sequence!{[LANGUAGE], [bip0039::Italian], $e},
60+
replace_token_sequence!{[LANGUAGE], [bip0039::Japanese], $e},
61+
replace_token_sequence!{[LANGUAGE], [bip0039::Korean], $e},
62+
replace_token_sequence!{[LANGUAGE], [bip0039::Portuguese], $e},
63+
replace_token_sequence!{[LANGUAGE], [bip0039::Spanish], $e},
64+
)
65+
};
66+
}
4767

68+
impl Language {
4869
fn with_mnemonic_phrase_from_entropy<E: Into<Vec<u8>>, T>(
4970
self,
5071
entropy: E,
5172
f: impl FnOnce(&str) -> Option<T>,
5273
) -> Option<T> {
53-
self.handle(
54-
(entropy, f),
55-
|(entropy, f)| {
56-
bip0039::Mnemonic::<bip0039::English>::from_entropy(entropy)
57-
.ok()
58-
.and_then(|mnemonic| f(mnemonic.phrase()))
59-
},
60-
|(entropy, f)| {
61-
bip0039::Mnemonic::<bip0039::ChineseSimplified>::from_entropy(entropy)
62-
.ok()
63-
.and_then(|mnemonic| f(mnemonic.phrase()))
64-
},
65-
|(entropy, f)| {
66-
bip0039::Mnemonic::<bip0039::ChineseTraditional>::from_entropy(entropy)
67-
.ok()
68-
.and_then(|mnemonic| f(mnemonic.phrase()))
69-
},
70-
|(entropy, f)| {
71-
bip0039::Mnemonic::<bip0039::Czech>::from_entropy(entropy)
72-
.ok()
73-
.and_then(|mnemonic| f(mnemonic.phrase()))
74-
},
75-
|(entropy, f)| {
76-
bip0039::Mnemonic::<bip0039::French>::from_entropy(entropy)
77-
.ok()
78-
.and_then(|mnemonic| f(mnemonic.phrase()))
79-
},
80-
|(entropy, f)| {
81-
bip0039::Mnemonic::<bip0039::Italian>::from_entropy(entropy)
82-
.ok()
83-
.and_then(|mnemonic| f(mnemonic.phrase()))
84-
},
85-
|(entropy, f)| {
86-
bip0039::Mnemonic::<bip0039::Japanese>::from_entropy(entropy)
87-
.ok()
88-
.and_then(|mnemonic| f(mnemonic.phrase()))
89-
},
90-
|(entropy, f)| {
91-
bip0039::Mnemonic::<bip0039::Korean>::from_entropy(entropy)
92-
.ok()
93-
.and_then(|mnemonic| f(mnemonic.phrase()))
94-
},
95-
|(entropy, f)| {
96-
bip0039::Mnemonic::<bip0039::Portuguese>::from_entropy(entropy)
97-
.ok()
98-
.and_then(|mnemonic| f(mnemonic.phrase()))
99-
},
100-
|(entropy, f)| {
101-
bip0039::Mnemonic::<bip0039::Spanish>::from_entropy(entropy)
102-
.ok()
103-
.and_then(|mnemonic| f(mnemonic.phrase()))
104-
},
105-
)
74+
all_languages!(self, (entropy, f), |(entropy, f)| {
75+
bip0039::Mnemonic::<LANGUAGE>::from_entropy(entropy)
76+
.ok()
77+
.and_then(|mnemonic| f(mnemonic.phrase()))
78+
})
10679
}
10780

10881
fn with_seed_from_mnemonic_phrase<'a, P: Into<Cow<'a, str>>, T>(
@@ -111,75 +84,17 @@ impl Language {
11184
passphrase: &str,
11285
f: impl FnOnce([u8; 64]) -> Option<T>,
11386
) -> Option<T> {
114-
self.handle(
115-
(phrase, passphrase, f),
116-
|(phrase, passphrase, f)| {
117-
bip0039::Mnemonic::<bip0039::English>::from_phrase(phrase)
118-
.ok()
119-
.and_then(|mnemonic| f(mnemonic.to_seed(passphrase)))
120-
},
121-
|(phrase, passphrase, f)| {
122-
bip0039::Mnemonic::<bip0039::ChineseSimplified>::from_phrase(phrase)
123-
.ok()
124-
.and_then(|mnemonic| f(mnemonic.to_seed(passphrase)))
125-
},
126-
|(phrase, passphrase, f)| {
127-
bip0039::Mnemonic::<bip0039::ChineseTraditional>::from_phrase(phrase)
128-
.ok()
129-
.and_then(|mnemonic| f(mnemonic.to_seed(passphrase)))
130-
},
131-
|(phrase, passphrase, f)| {
132-
bip0039::Mnemonic::<bip0039::Czech>::from_phrase(phrase)
133-
.ok()
134-
.and_then(|mnemonic| f(mnemonic.to_seed(passphrase)))
135-
},
136-
|(phrase, passphrase, f)| {
137-
bip0039::Mnemonic::<bip0039::French>::from_phrase(phrase)
138-
.ok()
139-
.and_then(|mnemonic| f(mnemonic.to_seed(passphrase)))
140-
},
141-
|(phrase, passphrase, f)| {
142-
bip0039::Mnemonic::<bip0039::Italian>::from_phrase(phrase)
143-
.ok()
144-
.and_then(|mnemonic| f(mnemonic.to_seed(passphrase)))
145-
},
146-
|(phrase, passphrase, f)| {
147-
bip0039::Mnemonic::<bip0039::Japanese>::from_phrase(phrase)
148-
.ok()
149-
.and_then(|mnemonic| f(mnemonic.to_seed(passphrase)))
150-
},
151-
|(phrase, passphrase, f)| {
152-
bip0039::Mnemonic::<bip0039::Korean>::from_phrase(phrase)
153-
.ok()
154-
.and_then(|mnemonic| f(mnemonic.to_seed(passphrase)))
155-
},
156-
|(phrase, passphrase, f)| {
157-
bip0039::Mnemonic::<bip0039::Portuguese>::from_phrase(phrase)
158-
.ok()
159-
.and_then(|mnemonic| f(mnemonic.to_seed(passphrase)))
160-
},
161-
|(phrase, passphrase, f)| {
162-
bip0039::Mnemonic::<bip0039::Spanish>::from_phrase(phrase)
163-
.ok()
164-
.and_then(|mnemonic| f(mnemonic.to_seed(passphrase)))
165-
},
166-
)
87+
all_languages!(self, (phrase, passphrase, f), |(phrase, passphrase, f)| {
88+
bip0039::Mnemonic::<LANGUAGE>::from_phrase(phrase)
89+
.ok()
90+
.and_then(|mnemonic| f(mnemonic.to_seed(passphrase)))
91+
})
16792
}
16893

16994
fn validate_mnemonic<'a, P: Into<Cow<'a, str>>>(self, phrase: P) -> Option<()> {
170-
self.handle(
171-
phrase,
172-
|phrase| bip0039::Mnemonic::<bip0039::English>::validate(phrase).ok(),
173-
|phrase| bip0039::Mnemonic::<bip0039::ChineseSimplified>::validate(phrase).ok(),
174-
|phrase| bip0039::Mnemonic::<bip0039::ChineseTraditional>::validate(phrase).ok(),
175-
|phrase| bip0039::Mnemonic::<bip0039::Czech>::validate(phrase).ok(),
176-
|phrase| bip0039::Mnemonic::<bip0039::French>::validate(phrase).ok(),
177-
|phrase| bip0039::Mnemonic::<bip0039::Italian>::validate(phrase).ok(),
178-
|phrase| bip0039::Mnemonic::<bip0039::Japanese>::validate(phrase).ok(),
179-
|phrase| bip0039::Mnemonic::<bip0039::Korean>::validate(phrase).ok(),
180-
|phrase| bip0039::Mnemonic::<bip0039::Portuguese>::validate(phrase).ok(),
181-
|phrase| bip0039::Mnemonic::<bip0039::Spanish>::validate(phrase).ok(),
182-
)
95+
all_languages!(self, phrase, |phrase| {
96+
bip0039::Mnemonic::<LANGUAGE>::validate(phrase).ok()
97+
})
18398
}
18499
}
185100

0 commit comments

Comments
 (0)