improvement(NIP-19): identify and parse NIP-19 addresses
This commit is contained in:
committed by
Greg Heartsfield
parent
6f1a4e7d76
commit
c1152ce430
@@ -1,4 +1,5 @@
|
||||
//! Common utility functions
|
||||
use bech32::FromBase32;
|
||||
use std::time::SystemTime;
|
||||
|
||||
/// Seconds since 1970.
|
||||
@@ -14,6 +15,17 @@ use std::time::SystemTime;
|
||||
s.chars().all(|x| char::is_ascii_hexdigit(&x))
|
||||
}
|
||||
|
||||
/// Check if string is a nip19 string
|
||||
pub fn is_nip19(s: &str) -> bool {
|
||||
s.starts_with("npub") || s.starts_with("note")
|
||||
}
|
||||
|
||||
pub fn nip19_to_hex(s: &str) -> Result<String, bech32::Error> {
|
||||
let (_hrp, data, _checksum) = bech32::decode(s)?;
|
||||
let data = Vec::<u8>::from_base32(&data)?;
|
||||
Ok(hex::encode(data))
|
||||
}
|
||||
|
||||
/// Check if a string contains only lower-case hex chars.
|
||||
#[must_use] pub fn is_lower_hex(s: &str) -> bool {
|
||||
s.chars().all(|x| {
|
||||
@@ -30,4 +42,21 @@ mod tests {
|
||||
let hexstr = "abcd0123";
|
||||
assert_eq!(is_lower_hex(hexstr), true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nip19() {
|
||||
let hexkey = "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d";
|
||||
let nip19key = "npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6";
|
||||
assert_eq!(is_nip19(hexkey), false);
|
||||
assert_eq!(is_nip19(nip19key), true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nip19_hex() {
|
||||
let nip19key = "npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6";
|
||||
let expected = "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d";
|
||||
let got = nip19_to_hex(nip19key).unwrap();
|
||||
|
||||
assert_eq!(expected, got);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user