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:
- Verify the signed request with
verify_request, passing the received body asExpectedRequest::body. This checks the signature and that thepayloadtag is the SHA-256 of exactly those bytes. - Parse the same bytes with
ClaimBody::parse. - Look up the outstanding challenge the server issued for
nonce, and compare its stored bytes with the received bytes for equality (==on twoClaimBodyvalues compares bytes). A nonce the server never issued, or any difference in the bytes, is a refusal. - Call
checkwith the server’s clock, its own node DID and the request signer fromVerifiedRequest::signer. - 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_sha256and apply on top ofbase_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
impl ClaimBody
Sourcepub fn new(fields: ClaimFields) -> Self
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.
Sourcepub fn parse(bytes: &[u8]) -> Result<Self, ParseClaimError>
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.
Sourcepub fn check(
&self,
now: UnixTime,
expected_node: &DidNostr,
expected_claimant: &DidNostr,
) -> Result<(), CheckClaimError>
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.
Sourcepub fn into_bytes(self) -> Vec<u8> ⓘ
pub fn into_bytes(self) -> Vec<u8> ⓘ
Consumes the body and returns its exact bytes.
Sourcepub fn sha256(&self) -> Sha256Digest
pub fn sha256(&self) -> Sha256Digest
Returns the SHA-256 of as_bytes: the value the
claim request’s payload tag must carry.
Sourcepub fn fields(&self) -> &ClaimFields
pub fn fields(&self) -> &ClaimFields
Returns all fields.
Sourcepub fn nonce(&self) -> ChallengeId
pub fn nonce(&self) -> ChallengeId
Returns the challenge this claim answers.
Sourcepub fn expires_at(&self) -> UnixTime
pub fn expires_at(&self) -> UnixTime
Returns the first second at which the claim is no longer accepted.
Sourcepub fn base_revision(&self) -> Revision
pub fn base_revision(&self) -> Revision
Returns the base configuration revision.
Sourcepub fn config_sha256(&self) -> Sha256Digest
pub fn config_sha256(&self) -> Sha256Digest
Returns the SHA-256 of the initial configuration text.