Skip to main content

ClaimBody

Struct ClaimBody 

Source
pub struct ClaimBody { /* private fields */ }
Expand description

An immutable bootstrap claim body together with its exact JSON bytes.

The server builds the body once with ClaimBody::new, stores it as the outstanding challenge and hands out its exact bytes. The client signs a request whose payload tag is the SHA-256 of those bytes and sends them back verbatim. Nothing is ever re-serialised for hashing, so the bytes that were signed are the bytes that are checked.

The JSON shape of version CLAIM_BODY_VERSION is an object with exactly these fields: version, nonce, node, claimant, expires_at, base_revision and config_sha256. Unknown fields, duplicate fields and other versions are rejected.

Equality compares the bytes. The type has no serde implementations on purpose: transport the bytes from as_bytes and parse them with parse.

§Accepting a claim

ClaimBody::check is only one step, and on its own it proves nothing about where a body came from. It looks at expiry, node and claimant. A client can write a body of its own with any nonce, a later expiry, another base revision or another configuration digest, sign it, and it will pass check. A signature shows what the claimant signed, not that the server issued it. To accept a claim the server must do all of the following, and commit nothing unless every step succeeds:

  1. Verify the signed request with verify_request, passing the received body as ExpectedRequest::body. This checks the signature and that the payload tag is the SHA-256 of exactly those bytes.
  2. Parse the same bytes with ClaimBody::parse.
  3. Look up the outstanding challenge the server issued for nonce, and compare its stored bytes with the received bytes for equality (== on two ClaimBody values compares bytes). A nonce the server never issued, or any difference in the bytes, is a refusal.
  4. Call check with the server’s clock, its own node DID and the request signer from VerifiedRequest::signer.
  5. In one transaction, consume the challenge so that the nonce can never be accepted again, record the request’s event ID for replay protection, bind the administrator and commit the initial configuration. Commit only values bound by the body: the configuration text must hash to config_sha256 and apply on top of base_revision. Values taken from anywhere else in the request must not affect what is committed.

§Examples

use stratum_node_core::{
    ChallengeId, ClaimBody, ClaimFields, DidNostr, NostrSecretKey, Revision, Sha256Digest,
    UnixTime,
};

let node = NostrSecretKey::from_hex(&"06".repeat(32))?.public();
let admin = NostrSecretKey::from_hex(&"07".repeat(32))?.public();
// The server issues the body and keeps it as the outstanding challenge.
let issued = ClaimBody::new(ClaimFields {
    nonce: ChallengeId::from_bytes([9; 16]),
    node,
    claimant: admin,
    expires_at: UnixTime::from_secs(1_700_000_300),
    base_revision: Revision::new(0),
    config_sha256: Sha256Digest::of(b"[node]\n"),
});

// The client returns the bytes verbatim (inside a signed request, which
// the server verifies first). The server parses them and finds the
// challenge it issued under that nonce.
let returned = ClaimBody::parse(issued.as_bytes())?;
assert_eq!(returned.nonce(), issued.nonce());

// Security check, not a formality: the returned bytes must be exactly the
// bytes the server issued. `check` alone would accept a body the client
// wrote itself.
assert_eq!(returned, issued);
returned.check(UnixTime::from_secs(1_700_000_000), &node, &admin)?;

// Then, atomically: consume the nonce, record the event ID, bind the
// administrator and commit the configuration the body names.

Implementations§

Source§

impl ClaimBody

Source

pub fn new(fields: ClaimFields) -> Self

Serialises fields once, as version CLAIM_BODY_VERSION, and keeps the bytes.

§Panics

Does not panic: serde_json has no failure path for this struct of strings and integers.

Source

pub fn parse(bytes: &[u8]) -> Result<Self, ParseClaimError>

Parses claim-body bytes and keeps them exactly as given.

§Errors

Returns ParseClaimError::UnsupportedVersion when version is anything other than CLAIM_BODY_VERSION, and ParseClaimError::Malformed for any other deviation from the format, including a missing version, unknown or duplicate fields and invalid field values.

Source

pub fn check( &self, now: UnixTime, expected_node: &DidNostr, expected_claimant: &DidNostr, ) -> Result<(), CheckClaimError>

Checks the claim’s expiry, node and claimant against the server’s time and expectations.

This is a partial check. It does not show that the server issued this body, that the nonce is outstanding or unused, or that the request carrying it was signed; see Accepting a claim for the full procedure.

The claim is live while now is strictly before expires_at.

§Errors

Returns CheckClaimError::Expired at or after the expiry, CheckClaimError::NodeMismatch when the body names another node and CheckClaimError::ClaimantMismatch when it names another claimant, checked in that order.

Source

pub fn as_bytes(&self) -> &[u8] ⓘ

Returns the exact bytes this body was built or parsed from.

Source

pub fn into_bytes(self) -> Vec<u8> ⓘ

Consumes the body and returns its exact bytes.

Source

pub fn sha256(&self) -> Sha256Digest

Returns the SHA-256 of as_bytes: the value the claim request’s payload tag must carry.

Source

pub fn fields(&self) -> &ClaimFields

Returns all fields.

Source

pub fn nonce(&self) -> ChallengeId

Returns the challenge this claim answers.

Source

pub fn node(&self) -> DidNostr

Returns the node being claimed.

Source

pub fn claimant(&self) -> DidNostr

Returns the claimant.

Source

pub fn expires_at(&self) -> UnixTime

Returns the first second at which the claim is no longer accepted.

Source

pub fn base_revision(&self) -> Revision

Returns the base configuration revision.

Source

pub fn config_sha256(&self) -> Sha256Digest

Returns the SHA-256 of the initial configuration text.

Trait Implementations§

Source§

impl Clone for ClaimBody

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ClaimBody

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for ClaimBody

Source§

impl Hash for ClaimBody

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for ClaimBody

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for ClaimBody

Auto Trait Implementations§

§

impl Forget for ClaimBody

§

impl Freeze for ClaimBody
where *const u8 is !null: Freeze, PhantomData<u8>: Freeze,

§

impl Move for ClaimBody

§

impl RefUnwindSafe for ClaimBody

§

impl Send for ClaimBody
where RawVecInner: Send,

§

impl Sync for ClaimBody
where RawVecInner: Sync,

§

impl Unpin for ClaimBody
where *const u8 is !null: Unpin,

§

impl UnsafeUnpin for ClaimBody

§

impl UnwindSafe for ClaimBody
where RawVecInner: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.