Skip to content

Commit 222fda2

Browse files
committed
Make few protocol clean ups
1 parent cfa79f1 commit 222fda2

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

protocol/src/constants.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ pub struct ChainAnchor {
1616
pub height: u32,
1717
}
1818

19+
pub const SPACES_SIGNED_MSG_PREFIX: &[u8] = b"\x17Spaces Signed Message:\n";
20+
1921
pub const RESERVED_SPACES: [&'static [u8]; 3] = [b"\x07example", b"\x04test", b"\x05local"];
2022

2123
/// The number of blocks between each rollout of new spaces for auction.
@@ -100,7 +102,8 @@ impl ChainAnchor {
100102

101103
#[cfg(feature = "bincode")]
102104
pub mod bincode_impl {
103-
use alloc::vec::Vec;
105+
use crate::alloc::borrow::ToOwned;
106+
use alloc::vec::Vec;
104107

105108
use bincode::{
106109
config,

protocol/src/hasher.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,15 @@ impl From<Hash> for SpaceKey {
5757
impl SpaceKey {
5858
#[inline(always)]
5959
pub fn from_raw(value: Hash) -> crate::errors::Result<Self> {
60-
if (value[0] & 0b1000_0000) == 0 && (value[31] & 0b0000_0001) == 0 {
60+
if Self::is_valid(&value) {
6161
return Ok(Self { 0: value });
6262
}
63-
return Err(crate::errors::Error::IO("bad space hash".to_string()));
63+
Err(crate::errors::Error::IO("bad space hash".to_string()))
64+
}
65+
66+
#[inline(always)]
67+
pub fn is_valid(value: &Hash) -> bool {
68+
value[0] & 0b1000_0000 == 0 && value[31] & 0b0000_0001 == 0
6469
}
6570

6671
pub fn from_slice_unchecked(slice: &[u8]) -> Self {
@@ -113,6 +118,11 @@ impl OutpointKey {
113118
let h = H::hash(&buffer);
114119
h.into()
115120
}
121+
122+
#[inline(always)]
123+
pub fn is_valid(value: &Hash) -> bool {
124+
value[0] & 0b1000_0000 == 0 && value[31] & 0b0000_0001 == 0b0000_0001
125+
}
116126
}
117127

118128
impl BidKey {

protocol/src/lib.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ use alloc::{vec, vec::Vec};
99

1010
#[cfg(feature = "bincode")]
1111
use bincode::{Decode, Encode};
12+
13+
#[cfg(feature = "serde")]
14+
use serde::{Deserialize, Serialize};
15+
1216
use bitcoin::{
1317
psbt,
1418
secp256k1::{schnorr, Message},
@@ -17,8 +21,6 @@ use bitcoin::{
1721
transaction::Version,
1822
Amount, OutPoint, ScriptBuf, Transaction, TxIn, TxOut, Txid, Witness,
1923
};
20-
#[cfg(feature = "serde")]
21-
use serde::{Deserialize, Serialize};
2224

2325
use crate::{
2426
constants::{BID_PSBT_INPUT_SEQUENCE, BID_PSBT_TX_LOCK_TIME, BID_PSBT_TX_VERSION},
@@ -33,6 +35,7 @@ pub mod script;
3335
pub mod slabel;
3436
pub mod validate;
3537

38+
3639
#[derive(Clone, PartialEq, Debug)]
3740
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3841
#[cfg_attr(feature = "bincode", derive(Encode, Decode))]
@@ -214,6 +217,7 @@ pub mod serde_bytes_impl {
214217

215218
#[cfg(feature = "bincode")]
216219
pub mod bincode_bytes_impl {
220+
use alloc::vec::Vec;
217221
use bincode::{
218222
de::Decoder,
219223
enc::Encoder,

protocol/src/prepare.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use alloc::vec::Vec;
1+
use alloc::{vec, vec::Vec};
22

33
use bitcoin::{
44
absolute::LockTime,

0 commit comments

Comments
 (0)