coin.rs 788 B

123456789101112131415161718192021222324252627282930313233343536
  1. use std::io;
  2. use pasta_curves::{group::ff::PrimeField, pallas};
  3. use crate::{
  4. util::serial::{Decodable, Encodable, ReadExt, WriteExt},
  5. Result,
  6. };
  7. #[derive(Clone, Copy, PartialEq, Debug)]
  8. pub struct Coin(pub pallas::Base);
  9. impl Coin {
  10. pub fn from_bytes(bytes: [u8; 32]) -> Self {
  11. pallas::Base::from_repr(bytes).map(Coin).unwrap()
  12. }
  13. pub fn to_bytes(self) -> [u8; 32] {
  14. self.0.to_repr()
  15. }
  16. }
  17. impl Encodable for Coin {
  18. fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
  19. s.write_slice(&self.to_bytes()[..])?;
  20. Ok(32)
  21. }
  22. }
  23. impl Decodable for Coin {
  24. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  25. let mut bytes = [0u8; 32];
  26. d.read_slice(&mut bytes)?;
  27. Ok(Self::from_bytes(bytes))
  28. }
  29. }