pub struct Stamp { /* private fields */ }Expand description
A postage stamp represents proof of payment for storing a chunk.
Stamps are created by signing a message containing the chunk address, batch ID, stamp index, and timestamp with the batch owner’s private key.
§Wire Format
A serialized stamp is 113 bytes:
- Batch ID: 32 bytes
- Bucket (x): 4 bytes, big-endian
- Index (y): 4 bytes, big-endian
- Timestamp: 8 bytes, big-endian
- Signature: 65 bytes (r || s || v)
Implementations§
Source§impl Stamp
impl Stamp
Sourcepub const fn new(
batch: BatchId,
bucket: u32,
index: u32,
timestamp: u64,
sig: Signature,
) -> Self
pub const fn new( batch: BatchId, bucket: u32, index: u32, timestamp: u64, sig: Signature, ) -> Self
Creates a new stamp with the given parameters.
Sourcepub const fn with_index(
batch: BatchId,
index: StampIndex,
timestamp: u64,
sig: Signature,
) -> Self
pub const fn with_index( batch: BatchId, index: StampIndex, timestamp: u64, sig: Signature, ) -> Self
Creates a new stamp from a stamp index.
Sourcepub const fn stamp_index(&self) -> StampIndex
pub const fn stamp_index(&self) -> StampIndex
Returns the stamp index.
Sourcepub fn to_bytes(&self) -> StampBytes
pub fn to_bytes(&self) -> StampBytes
Serializes the stamp to a 113-byte array.
Sourcepub fn from_bytes(bytes: &StampBytes) -> Result<Self, StampError>
pub fn from_bytes(bytes: &StampBytes) -> Result<Self, StampError>
Deserializes a stamp from a 113-byte array.
Returns an error if the signature bytes are invalid.
Sourcepub fn try_from_slice(bytes: &[u8]) -> Result<Self, StampError>
pub fn try_from_slice(bytes: &[u8]) -> Result<Self, StampError>
Attempts to deserialize a stamp from a byte slice.
Returns an error if the slice is not exactly 113 bytes or if the signature is invalid.
Sourcepub fn recover_signer(
&self,
chunk_address: &SwarmAddress,
) -> Result<Address, StampError>
pub fn recover_signer( &self, chunk_address: &SwarmAddress, ) -> Result<Address, StampError>
Recovers the signer address from this stamp using EIP-191 message recovery.
This computes the stamp digest from the chunk address and stamp fields, then recovers the Ethereum address that signed it.
§Arguments
chunk_address- The address of the chunk this stamp is for
§Returns
The Ethereum address of the signer, or an error if recovery fails.
§Example
let stamp = Stamp::try_from_slice(&bytes)?;
let signer = stamp.recover_signer(&chunk_address)?;
println!("Stamp signed by: {}", signer);Sourcepub fn verify(
&self,
chunk_address: &SwarmAddress,
owner: Address,
) -> Result<(), StampError>
pub fn verify( &self, chunk_address: &SwarmAddress, owner: Address, ) -> Result<(), StampError>
Verifies this stamp was signed by the expected owner.
This is a convenience method that calls recover_signer
and compares the result to the expected owner address.
§Arguments
chunk_address- The address of the chunk this stamp is forowner- The expected owner/signer address
§Returns
Ok(()) if the stamp was signed by the expected owner,
or an error if signature recovery fails or the signer doesn’t match.
§Example
let stamp = Stamp::try_from_slice(&bytes)?;
stamp.verify(&chunk_address, batch.owner())?;Sourcepub fn recover_pubkey(
&self,
chunk_address: &SwarmAddress,
) -> Result<VerifyingKey, StampError>
pub fn recover_pubkey( &self, chunk_address: &SwarmAddress, ) -> Result<VerifyingKey, StampError>
Recovers the public key from this stamp.
This is useful for caching the public key after the first verification
of a batch. Subsequent stamps from the same batch can then use
verify_with_pubkey which is approximately
10x faster than full signature recovery.
§Arguments
chunk_address- The address of the chunk this stamp is for
§Returns
The public key of the signer, or an error if recovery fails.
§Example
// First stamp: recover public key and cache it
let pubkey = first_stamp.recover_pubkey(&first_chunk_address)?;
// Subsequent stamps: fast verification with cached pubkey
for (stamp, addr) in remaining_stamps {
stamp.verify_with_pubkey(&addr, &pubkey)?;
}Sourcepub fn verify_with_pubkey(
&self,
chunk_address: &SwarmAddress,
pubkey: &VerifyingKey,
) -> Result<(), StampError>
pub fn verify_with_pubkey( &self, chunk_address: &SwarmAddress, pubkey: &VerifyingKey, ) -> Result<(), StampError>
Verifies this stamp using a known public key.
This is approximately 10x faster than verify or
recover_signer because it avoids the expensive
ECDSA public key recovery operation.
Use this when you’ve already recovered the owner’s public key from a
previous stamp in the same batch (via recover_pubkey).
§Arguments
chunk_address- The address of the chunk this stamp is forpubkey- The expected signer’s public key (cached from previous recovery)
§Returns
Ok(()) if the signature is valid for the given public key,
or an error if verification fails.
§Example
// First stamp: recover and cache the public key
let pubkey = first_stamp.recover_pubkey(&first_address)?;
let owner = alloy_signer::utils::public_key_to_address(&pubkey);
// Fast verification for remaining stamps in the same batch
second_stamp.verify_with_pubkey(&second_address, &pubkey)?;
third_stamp.verify_with_pubkey(&third_address, &pubkey)?;Trait Implementations§
Source§impl<'a> Arbitrary<'a> for Stamp
Available on crate feature arbitrary only.
impl<'a> Arbitrary<'a> for Stamp
arbitrary only.Source§fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
Self from the given unstructured data. Read moreSource§fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
Self from the entirety of the given
unstructured data. Read moreSource§fn size_hint(depth: usize) -> (usize, Option<usize>)
fn size_hint(depth: usize) -> (usize, Option<usize>)
Unstructured this type
needs to construct itself. Read moreSource§fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), MaxRecursionReached>
fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>
Unstructured this type
needs to construct itself. Read moreSource§impl<'de> Deserialize<'de> for Stamp
impl<'de> Deserialize<'de> for Stamp
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl Eq for Stamp
Source§impl From<Stamp> for StampBytes
impl From<Stamp> for StampBytes
impl StructuralPartialEq for Stamp
Auto Trait Implementations§
impl Freeze for Stamp
impl RefUnwindSafe for Stamp
impl Send for Stamp
impl Sync for Stamp
impl Unpin for Stamp
impl UnsafeUnpin for Stamp
impl UnwindSafe for Stamp
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<'de, T> BorrowedRpcObject<'de> for T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more