stock.rs (6551B)
1 //! Exact owner-private pool creation data, without durable stock permission. 2 use super::{COMMAND_MAX_BYTES, CommandError, Context, check_frame, hash, parse_object}; 3 use alloc::{boxed::Box, string::String, vec::Vec}; 4 use core::fmt; 5 use radroots_domain::{ 6 ApprovalIntentDigest, CommandContextData, CommandDigest, Decimal, PoolId, Quantity, 7 listing::MeasurementUnit, 8 stock::{PoolUnit, StockTicks}, 9 }; 10 use serde::{Deserialize, Serialize, ser::SerializeStruct}; 11 use zeroize::Zeroizing; 12 13 /// Fixed unit/precision and initial quantity. This is a claim, not inventory. 14 pub struct CreatePoolInput { 15 id: PoolId, 16 label: Zeroizing<String>, 17 unit: PoolUnit, 18 quantity: Quantity, 19 ticks: StockTicks, 20 } 21 impl CreatePoolInput { 22 pub fn new( 23 id: PoolId, 24 label: &str, 25 unit: PoolUnit, 26 quantity: Quantity, 27 ) -> Result<Self, CommandError> { 28 if label.len() > 160 { 29 return Err(CommandError::LimitExceeded); 30 } 31 if label.chars().any(char::is_control) { 32 return Err(CommandError::InvalidInput); 33 } 34 let ticks = unit 35 .ticks_for(quantity) 36 .map_err(|_| CommandError::InvalidInput)?; 37 Ok(Self { 38 id, 39 label: Zeroizing::new(String::from(label)), 40 unit, 41 quantity, 42 ticks, 43 }) 44 } 45 pub const fn id(&self) -> PoolId { 46 self.id 47 } 48 pub fn label(&self) -> &str { 49 &self.label 50 } 51 pub const fn unit(&self) -> PoolUnit { 52 self.unit 53 } 54 pub const fn quantity(&self) -> Quantity { 55 self.quantity 56 } 57 pub const fn initial_ticks(&self) -> StockTicks { 58 self.ticks 59 } 60 } 61 impl fmt::Debug for CreatePoolInput { 62 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 63 f.write_str("CreatePoolInput(<redacted>)") 64 } 65 } 66 /// Complete bounded command commitment. No host or signer is acquired here. 67 pub struct CapturedPoolCreation { 68 context: CommandContextData, 69 input: CreatePoolInput, 70 wire: Zeroizing<Box<[u8]>>, 71 digest: CommandDigest, 72 approval: ApprovalIntentDigest, 73 } 74 impl CapturedPoolCreation { 75 pub fn new(context: CommandContextData, input: CreatePoolInput) -> Result<Self, CommandError> { 76 let encode = |approval, correlation| -> Result<Zeroizing<Vec<u8>>, CommandError> { 77 let bytes = Zeroizing::new( 78 serde_json::to_vec(&Wire { 79 context: &context, 80 input: &input, 81 approval, 82 correlation, 83 }) 84 .map_err(|_| CommandError::InvalidInput)?, 85 ); 86 if bytes.len() > COMMAND_MAX_BYTES { 87 return Err(CommandError::LimitExceeded); 88 } 89 Ok(bytes) 90 }; 91 let wire = Zeroizing::new(Box::from(encode(true, true)?.as_slice())); 92 let digest = 93 CommandDigest::from_bytes(hash(b"radroots.command.v1\0", &encode(true, false)?)); 94 let approval = ApprovalIntentDigest::from_bytes(hash( 95 b"radroots.approval.v1\0", 96 &encode(false, false)?, 97 )); 98 Ok(Self { 99 context, 100 input, 101 wire, 102 digest, 103 approval, 104 }) 105 } 106 pub fn parse(raw: &[u8]) -> Result<Self, CommandError> { 107 check_frame(raw)?; 108 #[derive(Deserialize)] 109 #[serde(deny_unknown_fields)] 110 struct Root<'a> { 111 #[serde(rename = "type")] 112 kind: String, 113 context: CommandContextData, 114 #[serde(borrow)] 115 input: &'a serde_json::value::RawValue, 116 } 117 #[derive(Deserialize)] 118 #[serde(deny_unknown_fields)] 119 struct Input { 120 id: PoolId, 121 label: String, 122 unit: MeasurementUnit, 123 tick_scale: String, 124 initial_quantity: Decimal, 125 } 126 let root: Root<'_> = parse_object(raw)?; 127 if root.kind != "stock.create_pool" { 128 return Err(CommandError::UnsupportedCommand); 129 } 130 let v: Input = parse_object(root.input.get().as_bytes())?; 131 let scale = match v.tick_scale.as_bytes() { 132 [byte @ b'0'..=b'6'] => *byte - b'0', 133 _ => return Err(CommandError::InvalidInput), 134 }; 135 let unit = PoolUnit::new(v.unit, scale).map_err(|_| CommandError::InvalidInput)?; 136 let quantity = 137 Quantity::new(v.initial_quantity, v.unit).map_err(|_| CommandError::InvalidInput)?; 138 Self::new( 139 root.context, 140 CreatePoolInput::new(v.id, &v.label, unit, quantity)?, 141 ) 142 } 143 pub const fn context(&self) -> &CommandContextData { 144 &self.context 145 } 146 pub const fn input(&self) -> &CreatePoolInput { 147 &self.input 148 } 149 pub fn protected_wire(&self) -> &[u8] { 150 &self.wire 151 } 152 pub const fn digest(&self) -> CommandDigest { 153 self.digest 154 } 155 pub const fn approval_intent_digest(&self) -> ApprovalIntentDigest { 156 self.approval 157 } 158 } 159 impl fmt::Debug for CapturedPoolCreation { 160 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 161 f.write_str("CapturedPoolCreation(<redacted>)") 162 } 163 } 164 struct Wire<'a> { 165 context: &'a CommandContextData, 166 input: &'a CreatePoolInput, 167 approval: bool, 168 correlation: bool, 169 } 170 impl Serialize for Wire<'_> { 171 fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> { 172 let mut o = s.serialize_struct("PoolCreation", 3)?; 173 o.serialize_field( 174 "context", 175 &Context(self.context, self.approval, self.correlation), 176 )?; 177 o.serialize_field("input", &InputWire(self.input))?; 178 o.serialize_field("type", "stock.create_pool")?; 179 o.end() 180 } 181 } 182 struct Scale(u8); 183 impl Serialize for Scale { 184 fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> { 185 s.collect_str(&self.0) 186 } 187 } 188 struct InputWire<'a>(&'a CreatePoolInput); 189 impl Serialize for InputWire<'_> { 190 fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> { 191 let v = self.0; 192 let mut o = s.serialize_struct("CreatePoolInput", 5)?; 193 o.serialize_field("id", &v.id)?; 194 o.serialize_field("initial_quantity", &v.quantity.amount())?; 195 o.serialize_field("label", v.label())?; 196 o.serialize_field("tick_scale", &Scale(v.unit.scale()))?; 197 o.serialize_field("unit", &v.unit.unit())?; 198 o.end() 199 } 200 }