sonicapi/state/data.rs
1// SONIC: Standard library for formally-verifiable distributed contracts
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Designed in 2019-2025 by Dr Maxim Orlovsky <orlovsky@ubideco.org>
6// Written in 2024-2025 by Dr Maxim Orlovsky <orlovsky@ubideco.org>
7//
8// Copyright (C) 2019-2024 LNP/BP Standards Association, Switzerland.
9// Copyright (C) 2024-2025 Laboratories for Ubiquitous Deterministic Computing (UBIDECO),
10// Institute for Distributed and Cognitive Systems (InDCS), Switzerland.
11// Copyright (C) 2019-2025 Dr Maxim Orlovsky.
12// All rights under the above copyrights are reserved.
13//
14// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
15// in compliance with the License. You may obtain a copy of the License at
16//
17// http://www.apache.org/licenses/LICENSE-2.0
18//
19// Unless required by applicable law or agreed to in writing, software distributed under the License
20// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
21// or implied. See the License for the specific language governing permissions and limitations under
22// the License.
23
24use amplify::num::u256;
25use strict_types::StrictVal;
26use ultrasonic::{AuthToken, CellLock};
27
28pub type StateTy = u256;
29
30#[derive(Clone, Eq, PartialEq, Debug)]
31#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(rename_all = "camelCase"))]
32pub struct StateAtom {
33 pub verified: StrictVal,
34 pub unverified: Option<StrictVal>,
35}
36
37impl StateAtom {
38 #[inline]
39 pub fn new_verified(val: impl Into<StrictVal>) -> Self { Self { verified: val.into(), unverified: None } }
40
41 #[inline]
42 pub fn new_unverified(val: impl Into<StrictVal>) -> Self {
43 Self { verified: StrictVal::Unit, unverified: Some(val.into()) }
44 }
45
46 #[inline]
47 pub fn with(verified: impl Into<StrictVal>, unverified: impl Into<StrictVal>) -> Self {
48 Self {
49 verified: verified.into(),
50 unverified: Some(unverified.into()),
51 }
52 }
53}
54
55#[derive(Clone, Eq, PartialEq, Debug)]
56#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(rename_all = "camelCase"))]
57pub struct DataCell {
58 pub data: StrictVal,
59 pub auth: AuthToken,
60 pub lock: Option<CellLock>,
61}
62
63impl DataCell {
64 #[inline]
65 pub fn new_unlocked(auth: impl Into<AuthToken>, data: impl Into<StrictVal>) -> Self {
66 Self { data: data.into(), auth: auth.into(), lock: None }
67 }
68}