Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit bc0f0a8

Browse files
committed
Move pubkey utils to tests/utils.rs
.. as they are only used in tests.
1 parent 0e70831 commit bc0f0a8

File tree

4 files changed

+53
-52
lines changed

4 files changed

+53
-52
lines changed

src/lsps0/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ mod tests {
115115
use alloc::sync::Arc;
116116

117117
use crate::lsps0::msgs::{LSPSMessage, RequestId};
118-
use crate::tests::utils::TestEntropy;
118+
use crate::tests::utils::{self, TestEntropy};
119119

120120
use super::*;
121121

src/lsps0/service.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl ProtocolMessageHandler for LSPS0ServiceHandler {
8282
mod tests {
8383

8484
use crate::lsps0::msgs::{LSPSMessage, ListProtocolsRequest};
85-
use crate::utils;
85+
use crate::tests::utils;
8686
use alloc::string::ToString;
8787
use alloc::sync::Arc;
8888

src/tests/utils.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use crate::prelude::Vec;
2+
use bitcoin::secp256k1::PublicKey;
3+
use lightning::io;
14
use lightning::sign::EntropySource;
25

36
pub struct TestEntropy {}
@@ -6,3 +9,50 @@ impl EntropySource for TestEntropy {
69
[0; 32]
710
}
811
}
12+
13+
pub fn to_vec(hex: &str) -> Option<Vec<u8>> {
14+
let mut out = Vec::with_capacity(hex.len() / 2);
15+
16+
let mut b = 0;
17+
for (idx, c) in hex.as_bytes().iter().enumerate() {
18+
b <<= 4;
19+
match *c {
20+
b'A'..=b'F' => b |= c - b'A' + 10,
21+
b'a'..=b'f' => b |= c - b'a' + 10,
22+
b'0'..=b'9' => b |= c - b'0',
23+
_ => return None,
24+
}
25+
if (idx & 1) == 1 {
26+
out.push(b);
27+
b = 0;
28+
}
29+
}
30+
31+
Some(out)
32+
}
33+
34+
pub fn to_compressed_pubkey(hex: &str) -> Option<PublicKey> {
35+
if hex.len() != 33 * 2 {
36+
return None;
37+
}
38+
let data = match to_vec(&hex[0..33 * 2]) {
39+
Some(bytes) => bytes,
40+
None => return None,
41+
};
42+
match PublicKey::from_slice(&data) {
43+
Ok(pk) => Some(pk),
44+
Err(_) => None,
45+
}
46+
}
47+
48+
pub fn parse_pubkey(pubkey_str: &str) -> Result<PublicKey, io::Error> {
49+
let pubkey = to_compressed_pubkey(pubkey_str);
50+
if pubkey.is_none() {
51+
return Err(io::Error::new(
52+
io::ErrorKind::Other,
53+
"ERROR: unable to parse given pubkey for node",
54+
));
55+
}
56+
57+
Ok(pubkey.unwrap())
58+
}

src/utils.rs

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
use bitcoin::secp256k1::PublicKey;
21
use core::{fmt::Write, ops::Deref};
3-
use lightning::io;
42
use lightning::sign::EntropySource;
53

64
use crate::lsps0::msgs::RequestId;
7-
use crate::prelude::{String, Vec};
5+
use crate::prelude::String;
86

97
/// Maximum transaction index that can be used in a `short_channel_id`.
108
/// This value is based on the 3-bytes available for tx index.
@@ -56,53 +54,6 @@ pub fn hex_str(value: &[u8]) -> String {
5654
res
5755
}
5856

59-
pub fn to_vec(hex: &str) -> Option<Vec<u8>> {
60-
let mut out = Vec::with_capacity(hex.len() / 2);
61-
62-
let mut b = 0;
63-
for (idx, c) in hex.as_bytes().iter().enumerate() {
64-
b <<= 4;
65-
match *c {
66-
b'A'..=b'F' => b |= c - b'A' + 10,
67-
b'a'..=b'f' => b |= c - b'a' + 10,
68-
b'0'..=b'9' => b |= c - b'0',
69-
_ => return None,
70-
}
71-
if (idx & 1) == 1 {
72-
out.push(b);
73-
b = 0;
74-
}
75-
}
76-
77-
Some(out)
78-
}
79-
80-
pub fn to_compressed_pubkey(hex: &str) -> Option<PublicKey> {
81-
if hex.len() != 33 * 2 {
82-
return None;
83-
}
84-
let data = match to_vec(&hex[0..33 * 2]) {
85-
Some(bytes) => bytes,
86-
None => return None,
87-
};
88-
match PublicKey::from_slice(&data) {
89-
Ok(pk) => Some(pk),
90-
Err(_) => None,
91-
}
92-
}
93-
94-
pub fn parse_pubkey(pubkey_str: &str) -> Result<PublicKey, io::Error> {
95-
let pubkey = to_compressed_pubkey(pubkey_str);
96-
if pubkey.is_none() {
97-
return Err(io::Error::new(
98-
io::ErrorKind::Other,
99-
"ERROR: unable to parse given pubkey for node",
100-
));
101-
}
102-
103-
Ok(pubkey.unwrap())
104-
}
105-
10657
#[cfg(test)]
10758
mod tests {
10859
use super::*;

0 commit comments

Comments
 (0)