Waterdan on Nostr: mod keys { const BYTE_LEN: usize = 32; #[derive(Debug, PartialEq, Eq, PartialOrd, ...
mod keys {
const BYTE_LEN: usize = 32;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PrivateKey([u8; BYTE_LEN]);
#[derive(Debug)]
pub enum Error {
FromHexError(hex::FromHexError),
Not64Length(usize),
}
impl PrivateKey {
// Public constructor
pub fn new(id: &str) -> Result<PrivateKey, Error> {
if id.starts_with("npub") {}
if id.len() != BYTE_LEN * 2 {
return Result::Err(Error::Not64Length(id.len()));
}
let h = hex::decode(id);
let bytes = match h {
Ok(bytes) => bytes,
Err(err) => return Result::Err(Error::FromHexError(err)),
};
let mut array: [u8; BYTE_LEN] = [0; BYTE_LEN];
for (i, b) in bytes.iter().enumerate() {
array[i] = b.to_owned();
}
Result::Ok(PrivateKey(array))
}
pub fn hex(&self) -> String {
hex::encode(&self.0).clone()
}
}
}
Is this good Rust?
Published at
2024-06-21 20:08:19Event JSON
{
"id": "b1192dcb42b384050ee8ae4729e9fcd1402ea78ff83d06af2c4e3f9c2cb05778",
"pubkey": "6b9da920c4b6ecbf2c12018a7a2d143b4dfdf9878c3beac69e39bb597841cc6e",
"created_at": 1718993299,
"kind": 1,
"tags": [
[
"t",
""
],
[
"t",
""
]
],
"content": "mod keys {\n\n const BYTE_LEN: usize = 32;\n\n #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]\n pub struct PrivateKey([u8; BYTE_LEN]);\n\n #[derive(Debug)]\n pub enum Error {\n FromHexError(hex::FromHexError),\n Not64Length(usize),\n }\n\n impl PrivateKey {\n // Public constructor\n pub fn new(id: \u0026str) -\u003e Result\u003cPrivateKey, Error\u003e {\n if id.starts_with(\"npub\") {}\n if id.len() != BYTE_LEN * 2 {\n return Result::Err(Error::Not64Length(id.len()));\n }\n let h = hex::decode(id);\n let bytes = match h {\n Ok(bytes) =\u003e bytes,\n Err(err) =\u003e return Result::Err(Error::FromHexError(err)),\n };\n let mut array: [u8; BYTE_LEN] = [0; BYTE_LEN];\n for (i, b) in bytes.iter().enumerate() {\n array[i] = b.to_owned();\n }\n Result::Ok(PrivateKey(array))\n }\n\n pub fn hex(\u0026self) -\u003e String {\n hex::encode(\u0026self.0).clone()\n }\n }\n}\n\nIs this good Rust?",
"sig": "59f794e852426724472ae3a92c1f493cd10975202797ab47ef7282cc5311e70de815892bcfeb3f6bfaaeb1a1446b1f3ef76e4620ec535562847b21effc6ef52b"
}