Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ The library supports v2 registry descriptor features:
- **Unit SI prefix**: `prefix: true` enables k/M/G/T notation
- **Maps `keyPath`**: Cross-field key resolution for map lookups
- **`excluded` paths**: Deprecated v1 field now functional in rendering
- **`mustMatch`/`ifNotIn` value comparison**: Decimal strings and JSON numbers compare numerically against decoded words (signed ints as two's complement); hex literals compare case-insensitively so checksummed addresses match
- **Hidden fields in `bundled` groups**: A hidden array element keeps an empty slot, so `mustMatch`/`never` fields inside a bundled group do not break bundle alignment
- **Intent as object**: `intent` can be string or `{"label": "..."}` object
- **Interpolation escape sequences**: `{{` and `}}` produce literal braces
- **Encryption (`encryption`)**: wallet-delegated decryption via `DataProvider::resolve_decrypted_value` (calldata and EIP-712 alike, sharing `encryption.rs`; behaviour mirrors the TypeScript library). The plaintext is re-read as the declared `plaintextType` — integers normalized to a 32-byte two's-complement word so `intN` keeps its sign, negatives required at full declared width, only `0x00` padding stripped — then rendered with the field's normal format. Decrypted once per value per render (memoized, so entry / `interpolatedIntent` / conditional visibility agree and the wallet prompts once); conditional `visible` rules see the plaintext, never the handle. A wallet that cannot decrypt renders `fallbackLabel` (or `[Encrypted]`) with a `decryption_failed` diagnostic, and `DisplayItem.raw_encrypted_value` reports the handle either way (spec RECOMMENDS showing it). A malformed annotation (no `scheme`/`plaintextType`, non-canonical type) is a descriptor bug: `Error::Descriptor` → `FormatFailure::InvalidDescriptor`, validated even for hidden fields
Expand Down
2 changes: 2 additions & 0 deletions crates/clear-signing/src/eip712.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,8 @@ fn render_typed_group_field_kind<'a>(
)
.await?
{
// Keep the slot so bundled groups stay aligned.
bundles.push(Vec::new());
continue;
}
let rendered = if matches!(format.as_ref(), Some(FieldFormat::Calldata))
Expand Down
13 changes: 12 additions & 1 deletion crates/clear-signing/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,8 @@ fn render_group_field_kind<'a>(
)
.await?
{
// Keep the slot so bundled groups stay aligned.
bundles.push(Vec::new());
continue;
}
let rendered = if matches!(format.as_ref(), Some(FieldFormat::Calldata))
Expand Down Expand Up @@ -1201,6 +1203,15 @@ fn visibility_context(label: &str, path: &str) -> String {
}
}

/// JSON value used for `visible` comparisons. Signed integers become
/// decimal strings so `mustMatch: ["-1"]` matches a negative value.
fn visibility_json_value(val: &ArgumentValue) -> serde_json::Value {
match val {
ArgumentValue::Int(bytes) => serde_json::Value::String(int_to_bigint(bytes).to_string()),
_ => val.to_json_value(),
}
}

/// Check if a field should be visible based on the visibility rule and decoded value.
fn check_visibility(
rule: &VisibleRule,
Expand All @@ -1226,7 +1237,7 @@ fn check_visibility(
return Ok(true);
};

let json_val = val.to_json_value();
let json_val = visibility_json_value(val);
if cond.hides_for_if_not_in(&json_val) {
return Ok(false);
}
Expand Down
107 changes: 100 additions & 7 deletions crates/clear-signing/src/types/display.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Display configuration types: field formats, visibility rules, and layout groups.

use num_bigint::BigUint;
use num_bigint::{BigInt, BigUint, Sign};
use serde::{de, Deserialize, Deserializer, Serialize};
use std::collections::HashMap;

Expand Down Expand Up @@ -238,15 +238,108 @@ pub struct VisibleCondition {

impl VisibleCondition {
pub fn hides_for_if_not_in(&self, value: &serde_json::Value) -> bool {
self.if_not_in
.as_ref()
.is_some_and(|excluded| excluded.contains(value))
self.if_not_in.as_ref().is_some_and(|excluded| {
excluded
.iter()
.any(|expected| visibility_values_equal(expected, value))
})
}

pub fn matches_must_match(&self, value: &serde_json::Value) -> bool {
self.must_match
.as_ref()
.is_none_or(|required| required.contains(value))
self.must_match.as_ref().is_none_or(|required| {
required
.iter()
.any(|expected| visibility_values_equal(expected, value))
})
}
}

/// Integer parsed from a visibility value.
enum VisibilityInt {
/// Decimal literal, possibly negative.
Decimal(BigInt),
/// Hex literal with its bit width.
Hex(BigUint, u64),
}

fn parse_visibility_int(text: &str) -> Option<VisibilityInt> {
if let Some(hex_digits) = text.strip_prefix("0x").or_else(|| text.strip_prefix("0X")) {
let bytes = hex::decode(hex_digits).ok()?;
return Some(VisibilityInt::Hex(
BigUint::from_bytes_be(&bytes),
hex_digits.len() as u64 * 4,
));
}
if !text
.strip_prefix('-')
.unwrap_or(text)
.bytes()
.all(|b| b.is_ascii_digit())
{
return None;
}
text.parse::<BigInt>().ok().map(VisibilityInt::Decimal)
}

fn visibility_scalar_text(value: &serde_json::Value) -> Option<String> {
match value {
serde_json::Value::String(s) => Some(s.clone()),
serde_json::Value::Number(n) => Some(n.to_string()),
serde_json::Value::Bool(b) => Some(b.to_string()),
_ => None,
}
}

fn is_hex_literal(text: &str) -> bool {
text.starts_with("0x") || text.starts_with("0X")
}

/// Interpret a hex literal as two's complement of its own bit width.
fn hex_as_signed(magnitude: &BigUint, bits: u64) -> BigInt {
let signed = BigInt::from(magnitude.clone());
if bits > 0 && magnitude.bit(bits - 1) {
signed - (BigInt::from(1) << bits)
} else {
signed
}
}

/// Compare a descriptor `mustMatch` / `ifNotIn` value with a decoded value.
///
/// Hex literals compare case-insensitively, so checksummed addresses match
/// lowercase decoded addresses. A decimal literal (string or JSON number)
/// compares as an integer against a hex word or another decimal. A negative
/// decimal reads a hex word as two's complement.
fn visibility_values_equal(expected: &serde_json::Value, actual: &serde_json::Value) -> bool {
if expected == actual {
return true;
}
let (Some(expected), Some(actual)) = (
visibility_scalar_text(expected),
visibility_scalar_text(actual),
) else {
return false;
};
if is_hex_literal(&expected) && is_hex_literal(&actual) {
return expected.eq_ignore_ascii_case(&actual);
}
let (Some(expected), Some(actual)) = (
parse_visibility_int(&expected),
parse_visibility_int(&actual),
) else {
return false;
};
match (expected, actual) {
(VisibilityInt::Decimal(a), VisibilityInt::Decimal(b)) => a == b,
(VisibilityInt::Decimal(dec), VisibilityInt::Hex(mag, bits))
| (VisibilityInt::Hex(mag, bits), VisibilityInt::Decimal(dec)) => {
if dec.sign() == Sign::Minus {
hex_as_signed(&mag, bits) == dec
} else {
BigInt::from(mag) == dec
}
}
(VisibilityInt::Hex(..), VisibilityInt::Hex(..)) => false,
}
}

Expand Down
Loading
Loading