-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove consensus-encoding for
Scope
and SessionId
Use custom conversion and more docs in coupl of places
- Loading branch information
Showing
4 changed files
with
99 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,44 @@ | ||
use core::fmt; | ||
|
||
use bitcoin::{ | ||
consensus::{encode::Error, Decodable, Encodable}, | ||
hashes::{sha256, Hash}, | ||
io::Cursor, | ||
}; | ||
use bitcoin::hashes::{sha256, Hash}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
/// A unique identifier of deposit setup made by hashing unique data related to it | ||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Deserialize, Serialize)] | ||
pub struct Scope(sha256::Hash); | ||
pub struct Scope([u8; Scope::SIZE]); | ||
|
||
impl fmt::Display for Scope { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
self.0.fmt(f) | ||
write!(f, "{}", hex::encode(self.0)) | ||
} | ||
} | ||
|
||
impl Scope { | ||
pub const fn hash(data: &[u8]) -> Self { | ||
Self(sha256::Hash::const_hash(data)) | ||
const SIZE: usize = 32; | ||
|
||
/// Construct scope from preimage by hashing it. | ||
pub fn hash(data: &[u8]) -> Self { | ||
Self(sha256::Hash::hash(data).to_byte_array()) | ||
} | ||
|
||
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> { | ||
let mut cursor = Cursor::new(bytes); | ||
let scope = Decodable::consensus_decode(&mut cursor)?; | ||
Ok(Self(scope)) | ||
/// Construct scope from array of bytes. | ||
pub fn from_bytes(bytes: [u8; Self::SIZE]) -> Self { | ||
Self(bytes) | ||
} | ||
|
||
pub fn to_vec(&self) -> Vec<u8> { | ||
self.0.to_byte_array().to_vec() | ||
self.0.to_vec() | ||
} | ||
} | ||
|
||
impl AsRef<[u8]> for Scope { | ||
fn as_ref(&self) -> &[u8] { | ||
self.0.as_byte_array().as_ref() | ||
self.0.as_ref() | ||
} | ||
} | ||
|
||
impl From<sha256::Hash> for Scope { | ||
fn from(value: sha256::Hash) -> Self { | ||
Self(value) | ||
} | ||
} | ||
|
||
impl Decodable for Scope { | ||
fn consensus_decode<R: bitcoin::io::Read + ?Sized>(reader: &mut R) -> Result<Self, Error> { | ||
Ok(Self(sha256::Hash::consensus_decode(reader)?)) | ||
} | ||
} | ||
|
||
impl Encodable for Scope { | ||
fn consensus_encode<W: bitcoin::io::Write + ?Sized>( | ||
&self, | ||
writer: &mut W, | ||
) -> Result<usize, bitcoin::io::Error> { | ||
self.0.consensus_encode(writer) | ||
Self(value.to_byte_array()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,44 @@ | ||
use core::fmt; | ||
|
||
use bitcoin::{ | ||
consensus::{encode::Error, Decodable, Encodable}, | ||
hashes::{sha256, Hash}, | ||
io::Cursor, | ||
}; | ||
use bitcoin::hashes::{sha256, Hash}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
/// A unique identifier of signatures and nonces exchange session made by | ||
/// hashing unique data related to it. | ||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Deserialize, Serialize)] | ||
pub struct SessionId(sha256::Hash); | ||
pub struct SessionId([u8; SessionId::SIZE]); | ||
|
||
impl SessionId { | ||
pub const fn hash(data: &[u8]) -> Self { | ||
Self(sha256::Hash::const_hash(data)) | ||
const SIZE: usize = 32; | ||
|
||
/// Construct session ID from preimage by hashing it. | ||
pub fn hash(data: &[u8]) -> Self { | ||
Self(sha256::Hash::const_hash(data).to_byte_array()) | ||
} | ||
|
||
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> { | ||
let mut cursor = Cursor::new(bytes); | ||
let session_id = Decodable::consensus_decode(&mut cursor)?; | ||
Ok(Self(session_id)) | ||
pub fn from_bytes(bytes: [u8; Self::SIZE]) -> Self { | ||
Self(bytes) | ||
} | ||
|
||
pub fn to_vec(&self) -> Vec<u8> { | ||
self.0.to_byte_array().to_vec() | ||
self.0.to_vec() | ||
} | ||
} | ||
|
||
impl fmt::Display for SessionId { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
self.0.fmt(f) | ||
write!(f, "{}", hex::encode(self.0)) | ||
} | ||
} | ||
|
||
impl AsRef<[u8]> for SessionId { | ||
fn as_ref(&self) -> &[u8] { | ||
self.0.as_byte_array().as_ref() | ||
self.0.as_ref() | ||
} | ||
} | ||
|
||
impl From<sha256::Hash> for SessionId { | ||
fn from(value: sha256::Hash) -> Self { | ||
Self(value) | ||
} | ||
} | ||
|
||
impl Decodable for SessionId { | ||
fn consensus_decode<R: bitcoin::io::Read + ?Sized>(reader: &mut R) -> Result<Self, Error> { | ||
Ok(Self(sha256::Hash::consensus_decode(reader)?)) | ||
} | ||
} | ||
|
||
impl Encodable for SessionId { | ||
fn consensus_encode<W: bitcoin::io::Write + ?Sized>( | ||
&self, | ||
writer: &mut W, | ||
) -> Result<usize, bitcoin::io::Error> { | ||
self.0.consensus_encode(writer) | ||
Self(value.to_byte_array()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters