|
| 1 | +use anyhow::{Context, Result, bail}; |
| 2 | +use blake2::{ |
| 3 | + Blake2bVar, |
| 4 | + digest::{Update, VariableOutput}, |
| 5 | +}; |
| 6 | +use bs58; |
| 7 | +use serde::{Deserialize, Serialize, Serializer}; |
| 8 | + |
| 9 | +use crate::common::{djb2, xor_byte}; |
| 10 | + |
| 11 | +const TOTAL_NUMBER_OF_GROUPS: u8 = 4; |
| 12 | + |
| 13 | +#[repr(u8)] |
| 14 | +#[allow(dead_code)] |
| 15 | +pub enum AddressType { |
| 16 | + P2PKH = 0x00, |
| 17 | + P2MPKH = 0x01, |
| 18 | + P2SH = 0x02, |
| 19 | + P2C = 0x03, |
| 20 | + P2PK = 0x04, |
| 21 | + P2HMPK = 0x05, |
| 22 | +} |
| 23 | + |
| 24 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 25 | +pub struct Address { |
| 26 | + pub key: String, |
| 27 | + pub full_bytes: Vec<u8>, |
| 28 | + pub bytes: Vec<u8>, |
| 29 | +} |
| 30 | + |
| 31 | +impl Serialize for Address { |
| 32 | + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 33 | + where |
| 34 | + S: Serializer, |
| 35 | + { |
| 36 | + serializer.serialize_str(&self.key) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +impl<'de> Deserialize<'de> for Address { |
| 41 | + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 42 | + where |
| 43 | + D: serde::Deserializer<'de>, |
| 44 | + { |
| 45 | + let key = String::deserialize(deserializer)?; |
| 46 | + Self::new(&key).map_err(serde::de::Error::custom) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl Address { |
| 51 | + pub fn new(public_key_str: &str) -> Result<Self> { |
| 52 | + let public_key_bytes = hex::decode(public_key_str).context("Invalid hex")?; |
| 53 | + let mut hasher = Blake2bVar::new(32).context("Failed to create Blake2bVar")?; |
| 54 | + hasher.update(&public_key_bytes); |
| 55 | + let mut hash_bytes = [0u8; 32]; |
| 56 | + hasher |
| 57 | + .finalize_variable(&mut hash_bytes) |
| 58 | + .context("Failed to finalize Blake2bVar")?; |
| 59 | + let hash_bytes = &hash_bytes[..32]; |
| 60 | + |
| 61 | + let mut address_bytes = Vec::with_capacity(1 + 32); |
| 62 | + address_bytes.push(AddressType::P2PKH as u8); |
| 63 | + address_bytes.extend_from_slice(hash_bytes); |
| 64 | + |
| 65 | + Ok(Self { |
| 66 | + key: bs58::encode(&address_bytes).into_string(), |
| 67 | + full_bytes: address_bytes, |
| 68 | + bytes: hash_bytes.into(), |
| 69 | + }) |
| 70 | + } |
| 71 | + |
| 72 | + pub fn new_b58(b58_str: &str) -> Result<Self> { |
| 73 | + let address_bytes = bs58::decode(b58_str) |
| 74 | + .into_vec() |
| 75 | + .context("Invalid base58 string")?; |
| 76 | + if address_bytes.len() < 33 { |
| 77 | + bail!("Invalid address length"); |
| 78 | + } |
| 79 | + |
| 80 | + let key = b58_str.to_string(); |
| 81 | + let full_bytes = address_bytes.clone(); |
| 82 | + let bytes = address_bytes[1..].to_vec(); // The length > 1 was already checked |
| 83 | + |
| 84 | + Ok(Self { |
| 85 | + key, |
| 86 | + full_bytes, |
| 87 | + bytes, |
| 88 | + }) |
| 89 | + } |
| 90 | + |
| 91 | + pub fn group_from_bytes(&self) -> u8 { |
| 92 | + let hint = djb2(&self.bytes) | 1; |
| 93 | + let hash = xor_byte(hint); |
| 94 | + hash % TOTAL_NUMBER_OF_GROUPS |
| 95 | + } |
| 96 | +} |
0 commit comments