Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/encrypt/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ fn is_mnemonic(s: &str) -> bool {

#[inline(always)]
fn is_private_key(s: &str) -> bool {
s.starts_with(['K', 'L', '5']) && s.len() == 52 && bitcoin::base58::decode(s).is_ok()
(match s.len() {
51 if s.starts_with('5') => true,
52 if s.starts_with(['K', 'L']) => true,
_ => false,
}) && (bitcoin::base58::decode(s).is_ok())
}

/// # Reference:
Expand All @@ -56,10 +60,13 @@ fn is_private_key(s: &str) -> bool {
/// > non-EC-multiplied keys with compression (prefix 6PY)
/// > EC-multiplied keys without compression (prefix 6Pf)
/// > EC-multiplied keys with compression (prefix 6Pn)
/// # Notice:
/// Ignore the second prefix char for disguised key.
/// So, disguised key can be decrypted.
#[inline(always)]
fn is_encrypted_key(s: &str) -> bool {
s.starts_with("6P")
&& matches!(s.as_bytes()[2], b'R' | b'Y' | b'f' | b'n')
// && matches!(s.as_bytes()[2], b'R' | b'Y' | b'f' | b'n')
&& s.len() == 58
&& bitcoin::base58::decode(s).is_ok()
}
3 changes: 2 additions & 1 deletion src/encrypt/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ trait Bip38 {
impl Bip38 for str {
#[inline(always)]
fn is_private(&self) -> bool {
self.starts_with(['K', 'L', '5']) && self.len() == 52
(self.starts_with(['K', 'L']) && self.len() == 52)
|| (self.starts_with('5') && self.len() == 51)
}

#[inline(always)]
Expand Down
35 changes: 29 additions & 6 deletions tests/encrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,36 @@ fn test_encrypt_full() {

#[test]
fn test_encrypt_key() {
let output = cli_execute!("encrypt KyyXeMvCn36KuedmVX727NYQ35YEeF4z1ZjXGyqgFpmZM4AcY8ay");
let output = output.trim();
assert!(output.ends_with("6PYPVwvgux4mN96iwj1RGvbiGmmPWpkiQimpkP1fvFGGhT38XxZed6Kdth"));
const TEST_DATA: &[&str] = &[
// compression
"KyyXeMvCn36KuedmVX727NYQ35YEeF4z1ZjXGyqgFpmZM4AcY8ay",
"6PYPVwvgux4mN96iwj1RGvbiGmmPWpkiQimpkP1fvFGGhT38XxZed6Kdth",
// no compression
"5KN7MzqK5wt2TP1fQCYyHBtDrXdJuXbUzm4A9rKAteGu3Qi5CVR",
"6PRVWUbkztvBzJXKGDvQ6ZmJQ2BGEF4h1rs9BDfw4C52bE4tUeZWzZ6Qwp",
];

let output = cli_execute!("decrypt 6PYPVwvgux4mN96iwj1RGvbiGmmPWpkiQimpkP1fvFGGhT38XxZed6Kdth");
let output = output.trim();
assert!(output.ends_with("KyyXeMvCn36KuedmVX727NYQ35YEeF4z1ZjXGyqgFpmZM4AcY8ay"));
for data in TEST_DATA.chunks(2) {
let output = cli_execute!("encrypt", data[0]);
assert!(output.trim().ends_with(data[1]));

let decrypted = cli_execute!("decrypt", data[1]);
assert!(decrypted.trim().ends_with(data[0]));
}

// disguised bip38 wif can be decrypted.
const DISGUISE_DATA: &[&str] = &[
// compression
"L44B5gGEpqEDRS9vVPz7QT35jcBG2r3CZwSwQ4fCewXAhAhqGVpP",
"6Pbf7wys4KxBfH7AAjh1LAuSXBC6qzcJuxenig8cJhiD7dQjDZwPUP3emh",
// no compression
"5KN7MzqK5wt2TP1fQCYyHBtDrXdJuXbUzm4A9rKAteGu3Qi5CVR",
"6PUnJsaPthDRfjZH2fAhsmxLiRevGjVN9RRrSLKqohtSAW4sBPaYoAzHk9",
];
for data in DISGUISE_DATA.chunks(2) {
let decrypted = cli_execute!("decrypt", data[1]);
assert!(decrypted.trim().ends_with(data[0]));
}
}

#[test]
Expand Down