Skip to content

Commit

Permalink
feat: custom hash
Browse files Browse the repository at this point in the history
  • Loading branch information
palozano committed Jul 2, 2024
1 parent ed830cf commit ddfa748
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
1 change: 1 addition & 0 deletions contracts/blob-registry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ crate-type = ["cdylib" ]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
borsh = { workspace = true }
hex = { workspace = true }
near-sdk = "5.1.0"
near-sdk-contract-tools = "3.0.2"

Expand Down
58 changes: 56 additions & 2 deletions contracts/blob-registry/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
use events::EventLog;
use near_sdk::{assert_one_yocto, env, near, AccountId, NearToken, PanicOnDefault};
use near_sdk::{
assert_one_yocto, env, near,
serde::de::{self, Visitor},
serde::{Deserialize, Deserializer, Serialize, Serializer},
AccountId, NearToken, PanicOnDefault,
};

use near_sdk_contract_tools::{
owner::{Owner, OwnerExternal},
Owner,
Expand Down Expand Up @@ -36,7 +42,7 @@ pub struct Metadata {
type Namespace = u32;
type Priority = u32;
type Maintainer = Vec<u8>;
type TransactionId = [u8; 32];
type TransactionId = Hash;

#[near]
impl Contract {
Expand Down Expand Up @@ -148,3 +154,51 @@ impl Contract {
}
}
}

/// Hash type for represennting the transaction id.
#[derive(Debug)]
pub struct Hash([u8; 32]);

impl Serialize for Hash {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
// Convert the byte array to a hex string for serialization
let hex_string = hex::encode(self.0);
serializer.serialize_str(&hex_string)
}
}

impl<'de> Deserialize<'de> for Hash {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct MyHashVisitor;

impl<'de> Visitor<'de> for MyHashVisitor {
type Value = Hash;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a hex string representing a hash")
}

fn visit_str<E>(self, v: &str) -> Result<Hash, E>
where
E: de::Error,
{
// Convert the hex string back to a byte array
let bytes = hex::decode(v).map_err(de::Error::custom)?;
if bytes.len() != 32 {
return Err(de::Error::custom("expected a 32-byte hash"));
}
let mut hash = [0u8; 32];
hash.copy_from_slice(&bytes);
Ok(Hash(hash))
}
}

deserializer.deserialize_str(MyHashVisitor)
}
}
4 changes: 2 additions & 2 deletions contracts/blob-registry/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ async fn submit() -> anyhow::Result<()> {
.into_result()?;

let mut tx_ids = vec![];
for _ in 0..30 {
tx_ids.push([0u8; 32]);
for _ in 0..100 {
tx_ids.push(hex::encode([1u8; 32]));
}

eprintln!("Submitting {} TX IDs...", tx_ids.len());
Expand Down

0 comments on commit ddfa748

Please sign in to comment.