From 4f07e34555fe3351463f0042871e079a4d2116d5 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Wed, 20 Nov 2024 18:52:53 +0100 Subject: [PATCH] chore: move containers to SONIC. Add info about it to README --- Cargo.lock | 40 +++-- Cargo.toml | 28 +--- README.md | 1 + cli/src/cmd.rs | 2 +- inmem/Cargo.toml | 12 +- inmem/src/state.rs | 2 +- src/containers/contract.rs | 107 ------------ src/containers/kit.rs | 42 ----- src/containers/mod.rs | 29 ---- src/containers/transfer.rs | 38 ----- src/lib.rs | 14 +- src/persistence/stock.rs | 5 +- src/util/mod.rs | 26 --- src/util/refs.rs | 31 ---- src/util/sigs.rs | 119 ------------- src/util/suppl.rs | 331 ------------------------------------- 16 files changed, 52 insertions(+), 775 deletions(-) delete mode 100644 src/containers/contract.rs delete mode 100644 src/containers/kit.rs delete mode 100644 src/containers/mod.rs delete mode 100644 src/containers/transfer.rs delete mode 100644 src/util/mod.rs delete mode 100644 src/util/refs.rs delete mode 100644 src/util/sigs.rs delete mode 100644 src/util/suppl.rs diff --git a/Cargo.lock b/Cargo.lock index 05f245e..787553b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -577,15 +577,11 @@ checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" name = "sonare" version = "0.12.0-alpha.1" dependencies = [ - "aluvm", "amplify", - "baid64 0.4.0", - "commit_verify", "getrandom", "rand", "serde", - "strict_encoding", - "strict_types", + "sonic", "ultrasonic", "wasm-bindgen", "wasm-bindgen-test", @@ -606,21 +602,44 @@ dependencies = [ "amplify", "serde", "sonare", + "sonic", "strict_encoding", "strict_types", "ultrasonic", ] [[package]] -name = "sonare-request" +name = "sonic" version = "0.12.0-alpha.1" +source = "git+https://github.com/AluVM/sonic#a7407f311ec076694edac7f7ba0604b496ed8c73" dependencies = [ + "aluvm", "amplify", + "baid64 0.4.0", + "commit_verify", + "getrandom", "serde", - "sonare", + "sonic-api", + "strict_encoding", + "strict_types", + "ultrasonic", + "wasm-bindgen", +] + +[[package]] +name = "sonic-api" +version = "0.12.0-alpha.1" +source = "git+https://github.com/AluVM/sonic#a7407f311ec076694edac7f7ba0604b496ed8c73" +dependencies = [ + "aluvm", + "amplify", + "baid64 0.4.0", + "commit_verify", + "getrandom", "strict_encoding", "strict_types", "ultrasonic", + "wasm-bindgen", ] [[package]] @@ -731,7 +750,7 @@ checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" [[package]] name = "ultrasonic" version = "0.12.0-beta.1" -source = "git+https://github.com/AluVM/ultrasonic#3d31392e5bbe4fdeaadb37d895001661646b529f" +source = "git+https://github.com/AluVM/ultrasonic#c0a2ce6519a720cf40cd37c29f00760d34260dd9" dependencies = [ "amplify", "baid64 0.4.0", @@ -739,15 +758,16 @@ dependencies = [ "getrandom", "serde", "strict_encoding", + "strict_types", "wasm-bindgen", "zk-aluvm", ] [[package]] name = "unicode-ident" -version = "1.0.13" +version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" +checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" [[package]] name = "utf8parse" diff --git a/Cargo.toml b/Cargo.toml index a12e3e4..8618d7a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] -members = [".", "cli", "inmem", "request"] -default-members = [".", "inmem", "request"] +members = [".", "cli", "inmem"] +default-members = [".", "inmem"] [workspace.package] version = "0.12.0-alpha.1" @@ -19,6 +19,7 @@ strict_encoding = "~2.8.1" strict_types = "~2.8.1" aluvm = "0.12.0-beta.2" ultrasonic = "0.12.0-beta.1" +sonic = "0.12.0-alpha.1" serde = { version = "1", features = ["derive"] } [package] @@ -39,32 +40,12 @@ exclude = [".github"] [lib] name = "sonare" -[[example]] -name = "dao" - [dependencies] amplify.workspace = true -baid64 = "0.4.0" -strict_encoding.workspace = true -strict_types.workspace = true -commit_verify = "0.12.0-alpha.3" -aluvm.workspace = true ultrasonic.workspace = true +sonic.workspace = true serde = { workspace = true, optional = true } -[features] -default = [] -all = ["stl", "serde"] - -stl = ["commit_verify/stl", "ultrasonic/stl", "strict_types/armor"] -serde = [ - "dep:serde", - "amplify/serde", - "strict_encoding/serde", - "commit_verify/serde", - "ultrasonic/serde" -] - [target.'cfg(target_arch = "wasm32")'.dependencies] wasm-bindgen = "0.2" rand = { version = "0.8.4", optional = true } @@ -78,4 +59,5 @@ features = ["all"] [patch.crates-io] commit_verify = { git = "https://github.com/LNP-BP/client_side_validation", branch = "v0.12" } +sonic = { git = "https://github.com/AluVM/sonic" } ultrasonic = { git = "https://github.com/AluVM/ultrasonic" } diff --git a/README.md b/README.md index 0f55ca7..afd51ee 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,7 @@ SONARE is a part of a larger ecosystem used to build safe distributed software, uses a zk-STARK-compatible subset of its instruction set architecture (called zk-AluVM); - [UltraSONIC]: a transactional execution layer with capability-based memory access on top of zk-AluVM; +- [SONIC]: toolchain for building SONIC contracts; - [Cation]: a general-purpose high-level programming language made with category theory, which features strict types, termination analysis and can be formally verified; - [Contractum]: a domain-specific version of Cation for writing programs for SONARE. diff --git a/cli/src/cmd.rs b/cli/src/cmd.rs index f62f61b..742fb8e 100644 --- a/cli/src/cmd.rs +++ b/cli/src/cmd.rs @@ -23,7 +23,7 @@ use std::path::PathBuf; -use sonare::util::ContractRef; +use sonare::ContractRef; pub enum Command { /// Import kit diff --git a/inmem/Cargo.toml b/inmem/Cargo.toml index 959757c..7c41801 100644 --- a/inmem/Cargo.toml +++ b/inmem/Cargo.toml @@ -18,11 +18,21 @@ amplify.workspace = true strict_encoding.workspace = true strict_types = { workspace = true, optional = true } ultrasonic.workspace = true +sonic.workspace = true sonare = { version = "0.12.0-alpha.1", path = ".." } serde = { workspace = true, optional = true } [features] default = ["std"] -all = ["std"] +all = ["std", "stl", "serde"] std = [] + +stl = ["sonic/stl", "strict_types/armor"] +serde = [ + "dep:serde", + "amplify/serde", + "strict_encoding/serde", + "ultrasonic/serde", + "sonic/serde", +] diff --git a/inmem/src/state.rs b/inmem/src/state.rs index 88ca682..de5f1a0 100644 --- a/inmem/src/state.rs +++ b/inmem/src/state.rs @@ -31,7 +31,7 @@ //! persistence solution adapted for enterprise needs. use amplify::confinement::{SmallOrdMap, SmallVec, TinyOrdMap}; -use sonare::api::{ApiId, StateTy, StructData}; +use sonic::{ApiId, StateTy, StructData}; use ultrasonic::{CellAddr, StateCell, StateData}; /// The state as it is defined in the contract. Accessed during the validation. diff --git a/src/containers/contract.rs b/src/containers/contract.rs deleted file mode 100644 index 2c4d064..0000000 --- a/src/containers/contract.rs +++ /dev/null @@ -1,107 +0,0 @@ -// UltraSONIC: transactional execution layer with capability-based memory access for zk-AluVM -// -// SPDX-License-Identifier: Apache-2.0 -// -// Designed in 2019-2025 by Dr Maxim Orlovsky -// Written in 2024-2025 by Dr Maxim Orlovsky -// -// Copyright (C) 2019-2024 LNP/BP Standards Association, Switzerland. -// Copyright (C) 2024-2025 Laboratories for Ubiquitous Deterministic Computing (UBIDECO), -// Institute for Distributed and Cognitive Systems (InDCS), Switzerland. -// Copyright (C) 2019-2025 Dr Maxim Orlovsky. -// All rights under the above copyrights are reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except -// in compliance with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software distributed under the License -// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express -// or implied. See the License for the specific language governing permissions and limitations under -// the License. - -use core::fmt::{Debug, Display}; - -use amplify::confinement::SmallString; -use commit_verify::ReservedBytes; -use strict_encoding::{StrictDecode, StrictDumb, StrictEncode, TypeName}; -use ultrasonic::{Codex, ContractId, Operation}; - -use crate::LIB_NAME_SONARE; - -pub type ContractPrivate = Contract; - -#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Display)] -#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)] -#[strict_type(lib = LIB_NAME_SONARE, tags = custom)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(untagged))] -pub enum ContractName { - #[strict_type(tag = 0, dumb)] - #[display("~")] - Unnamed, - - #[strict_type(tag = 1)] - #[display(inner)] - Named(TypeName), -} - -#[derive(Clone, Eq, PartialEq, Debug)] -#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)] -#[strict_type(lib = LIB_NAME_SONARE)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(rename_all = "camelCase"))] -pub struct ContractMeta { - pub proof_of_publ: PoP, - // aligning to 16 byte edge - #[cfg_attr(feature = "serde", serde(skip))] - pub reserved: ReservedBytes<10>, - pub salt: u64, - pub timestamp: i64, - // ^^ above is a fixed-size contract header of 32 bytes - pub name: ContractName, - pub issuer: SmallString, -} - -#[derive(Clone, Eq, PartialEq, Debug)] -#[derive(CommitEncode)] -#[commit_encode(strategy = strict, id = ContractId)] -#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)] -#[strict_type(lib = LIB_NAME_SONARE)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(rename_all = "camelCase"))] -pub struct Contract { - pub version: Ffv, - pub meta: ContractMeta, - pub codex: Codex, - pub initial: Operation, -} - -/// Fast-forward version code -#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Default, Debug, Display)] -#[display("RGB/1.{0}")] -#[derive(StrictType, StrictEncode)] -#[strict_type(lib = LIB_NAME_SONARE)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(rename_all = "camelCase"))] -pub struct Ffv(u16); - -mod _ffv { - use alloc::string::{String, ToString}; - - use strict_encoding::{DecodeError, ReadTuple, StrictDecode, TypedRead}; - - use super::Ffv; - - impl StrictDecode for Ffv { - fn strict_decode(reader: &mut impl TypedRead) -> Result { - let ffv = reader.read_tuple(|r| r.read_field().map(Self))?; - if ffv != Ffv::default() { - let mut err = s!("unsupported fast-forward version code belonging to a future version. Please update \ - your software, or, if the problem persists, contact your vendor providing the \ - following version information: "); - err.push_str(&ffv.to_string()); - Err(DecodeError::DataIntegrityError(err)) - } else { - Ok(ffv) - } - } - } -} diff --git a/src/containers/kit.rs b/src/containers/kit.rs deleted file mode 100644 index fd2c5f5..0000000 --- a/src/containers/kit.rs +++ /dev/null @@ -1,42 +0,0 @@ -// SONARE: Runtime environment for formally-verifiable distributed software -// -// SPDX-License-Identifier: Apache-2.0 -// -// Designed in 2019-2024 by Dr Maxim Orlovsky -// Written in 2024-2025 by Dr Maxim Orlovsky -// -// Copyright (C) 2019-2025 LNP/BP Standards Association, Switzerland. -// Copyright (C) 2024-2025 Laboratories for Ubiquitous Deterministic Computing (UBIDECO), -// Institute for Distributed and Cognitive Systems (InDCS), Switzerland. -// Copyright (C) 2019-2025 Dr Maxim Orlovsky. -// All rights under the above copyrights are reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except -// in compliance with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software distributed under the License -// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express -// or implied. See the License for the specific language governing permissions and limitations under -// the License. - -use aluvm::Lib; -use amplify::confinement::SmallOrdSet; -use strict_encoding::TypeName; -use strict_types::TypeSystem; -use ultrasonic::Codex; - -use crate::api::Api; -use crate::containers::{Contract, ProofOfPubl}; - -pub struct Kit { - pub codex: Codex, - pub apis: SmallOrdSet, - pub libs: SmallOrdSet, - pub types: TypeSystem, -} - -impl Kit { - pub fn issue(&self, api: Option) -> Contract { todo!() } -} diff --git a/src/containers/mod.rs b/src/containers/mod.rs deleted file mode 100644 index c59b402..0000000 --- a/src/containers/mod.rs +++ /dev/null @@ -1,29 +0,0 @@ -// SONARE: Runtime environment for formally-verifiable distributed software -// -// SPDX-License-Identifier: Apache-2.0 -// -// Designed in 2019-2024 by Dr Maxim Orlovsky -// Written in 2024-2025 by Dr Maxim Orlovsky -// -// Copyright (C) 2019-2025 LNP/BP Standards Association, Switzerland. -// Copyright (C) 2024-2025 Laboratories for Ubiquitous Deterministic Computing (UBIDECO), -// Institute for Distributed and Cognitive Systems (InDCS), Switzerland. -// Copyright (C) 2019-2025 Dr Maxim Orlovsky. -// All rights under the above copyrights are reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except -// in compliance with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software distributed under the License -// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express -// or implied. See the License for the specific language governing permissions and limitations under -// the License. - -mod kit; -mod contract; -mod transfer; - -pub use contract::{Contract, ContractMeta, ContractName, ContractPrivate, Ffv, ProofOfPubl}; -pub use kit::Kit; diff --git a/src/containers/transfer.rs b/src/containers/transfer.rs deleted file mode 100644 index bd7f573..0000000 --- a/src/containers/transfer.rs +++ /dev/null @@ -1,38 +0,0 @@ -// SONARE: Runtime environment for formally-verifiable distributed software -// -// SPDX-License-Identifier: Apache-2.0 -// -// Designed in 2019-2024 by Dr Maxim Orlovsky -// Written in 2024-2025 by Dr Maxim Orlovsky -// -// Copyright (C) 2019-2025 LNP/BP Standards Association, Switzerland. -// Copyright (C) 2024-2025 Laboratories for Ubiquitous Deterministic Computing (UBIDECO), -// Institute for Distributed and Cognitive Systems (InDCS), Switzerland. -// Copyright (C) 2019-2025 Dr Maxim Orlovsky. -// All rights under the above copyrights are reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except -// in compliance with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software distributed under the License -// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express -// or implied. See the License for the specific language governing permissions and limitations under -// the License. - -use aluvm::Lib; -use amplify::confinement::{MediumVec, SmallOrdSet}; -use strict_types::TypeSystem; -use ultrasonic::Operation; - -use crate::api::Api; -use crate::containers::ContractPrivate; - -pub struct Transfer { - pub contract: ContractPrivate, - pub operations: MediumVec, - pub apis: SmallOrdSet, - pub libs: SmallOrdSet, - pub types: TypeSystem, -} diff --git a/src/lib.rs b/src/lib.rs index d71c9c0..f7ace60 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,19 +32,7 @@ extern crate alloc; #[macro_use] extern crate amplify; -#[macro_use] -extern crate strict_encoding; -#[macro_use] -extern crate commit_verify; - -#[cfg(feature = "serde")] -#[macro_use] -extern crate serde; +pub use sonic::*; pub use ultrasonic as base; -pub mod api; pub mod persistence; -pub mod containers; -pub mod util; - -pub const LIB_NAME_SONARE: &str = "SONARE"; diff --git a/src/persistence/stock.rs b/src/persistence/stock.rs index ec653a4..25aa835 100644 --- a/src/persistence/stock.rs +++ b/src/persistence/stock.rs @@ -24,11 +24,10 @@ use core::iter; use amplify::confinement::TinyOrdMap; -use ultrasonic::{CellAddr, Codex, Operation, Opid}; +use sonic::{ApiId, Contract, ContractMeta, DataCell, MethodName, StateName, StructData}; +use ultrasonic::{CellAddr, Codex, Operation, Opid, ProofOfPubl}; use super::{Repo, RepoProvider, Stash, StashProvider, State, StateProvider, Trace, TraceProvider}; -use crate::api::{ApiId, DataCell, MethodName, StateName, StructData}; -use crate::containers::{Contract, ContractMeta, ProofOfPubl}; pub struct ValidStash(Stash); diff --git a/src/util/mod.rs b/src/util/mod.rs deleted file mode 100644 index cbbe62b..0000000 --- a/src/util/mod.rs +++ /dev/null @@ -1,26 +0,0 @@ -// SONARE: Runtime environment for formally-verifiable distributed software -// -// SPDX-License-Identifier: Apache-2.0 -// -// Designed in 2019-2024 by Dr Maxim Orlovsky -// Written in 2024-2025 by Dr Maxim Orlovsky -// -// Copyright (C) 2019-2025 LNP/BP Standards Association, Switzerland. -// Copyright (C) 2024-2025 Laboratories for Ubiquitous Deterministic Computing (UBIDECO), -// Institute for Distributed and Cognitive Systems (InDCS), Switzerland. -// Copyright (C) 2019-2025 Dr Maxim Orlovsky. -// All rights under the above copyrights are reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except -// in compliance with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software distributed under the License -// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express -// or implied. See the License for the specific language governing permissions and limitations under -// the License. - -mod refs; - -pub use refs::ContractRef; diff --git a/src/util/refs.rs b/src/util/refs.rs deleted file mode 100644 index 32c6049..0000000 --- a/src/util/refs.rs +++ /dev/null @@ -1,31 +0,0 @@ -// SONARE: Runtime environment for formally-verifiable distributed software -// -// SPDX-License-Identifier: Apache-2.0 -// -// Designed in 2019-2024 by Dr Maxim Orlovsky -// Written in 2024-2025 by Dr Maxim Orlovsky -// -// Copyright (C) 2019-2025 LNP/BP Standards Association, Switzerland. -// Copyright (C) 2024-2025 Laboratories for Ubiquitous Deterministic Computing (UBIDECO), -// Institute for Distributed and Cognitive Systems (InDCS), Switzerland. -// Copyright (C) 2019-2025 Dr Maxim Orlovsky. -// All rights under the above copyrights are reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except -// in compliance with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software distributed under the License -// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express -// or implied. See the License for the specific language governing permissions and limitations under -// the License. - -use strict_encoding::TypeName; -use ultrasonic::ContractId; - -pub enum ContractRef { - Id(ContractId), - Name(TypeName), - // Mnemonic(), -} diff --git a/src/util/sigs.rs b/src/util/sigs.rs deleted file mode 100644 index 8d0d966..0000000 --- a/src/util/sigs.rs +++ /dev/null @@ -1,119 +0,0 @@ -// RGB standard library for working with smart contracts on Bitcoin & Lightning -// -// SPDX-License-Identifier: Apache-2.0 -// -// Written in 2019-2024 by -// Dr Maxim Orlovsky -// -// Copyright (C) 2019-2024 LNP/BP Standards Association. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -use alloc::collections::btree_map; - -use amplify::confinement::{NonEmptyBlob, NonEmptyOrdMap}; -use commit_verify::StrictHash; -use rgb::{ContractId, Identity, SchemaId}; -use strict_encoding::StrictDumb; - -use super::SupplId; -use crate::interface::{IfaceId, ImplId}; -use crate::LIB_NAME_RGB_STD; - -#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display, Default)] -#[derive(StrictType, StrictEncode, StrictDecode)] -#[strict_type(lib = LIB_NAME_RGB_STD, tags = repr, into_u8, try_from_u8)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(crate = "serde_crate", rename_all = "camelCase"))] -#[non_exhaustive] -#[repr(u8)] -pub enum ContainerVer { - // V0 and V1 was a previous version before v0.11, currently not supported. - #[default] - #[display("v2", alt = "2")] - V2 = 2, -} - -pub trait SigValidator { - fn validate_sig(&self, identity: &Identity, sig: SigBlob) -> bool; -} - -pub struct DumbValidator; -impl SigValidator for DumbValidator { - fn validate_sig(&self, _: &Identity, _: SigBlob) -> bool { false } -} - -#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display, Default)] -#[display(lowercase)] -#[derive(StrictType, StrictEncode, StrictDecode)] -#[strict_type(lib = LIB_NAME_RGB_STD, tags = repr, into_u8, try_from_u8)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(crate = "serde_crate", rename_all = "camelCase"))] -#[repr(u8)] -pub enum TrustLevel { - Malicious = 0x10, - #[default] - Unknown = 0x20, - Untrusted = 0x40, - Trusted = 0x80, - Ultimate = 0xC0, -} - -impl TrustLevel { - pub fn should_accept(self) -> bool { self >= Self::Unknown } - pub fn should_use(self) -> bool { self >= Self::Trusted } - pub fn must_use(self) -> bool { self >= Self::Ultimate } -} - -#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] -#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)] -#[strict_type(lib = LIB_NAME_RGB_STD, tags = order, dumb = ContentId::Schema(strict_dumb!()))] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(crate = "serde_crate", rename_all = "camelCase"))] -pub enum ContentId { - Schema(SchemaId), - Genesis(ContractId), - Iface(IfaceId), - IfaceImpl(ImplId), - Suppl(SupplId), -} - -#[derive(Wrapper, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, From, Display)] -#[wrapper(Deref, AsSlice, BorrowSlice, Hex)] -#[display(LowerHex)] -#[derive(StrictType, StrictEncode, StrictDecode)] -#[strict_type(lib = LIB_NAME_RGB_STD)] -#[derive(CommitEncode)] -#[commit_encode(strategy = strict, id = StrictHash)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(crate = "serde_crate", transparent))] -pub struct SigBlob(NonEmptyBlob<4096>); - -impl Default for SigBlob { - fn default() -> Self { SigBlob(NonEmptyBlob::with(0)) } -} - -#[derive(Wrapper, WrapperMut, Clone, PartialEq, Eq, Hash, Debug, From)] -#[wrapper(Deref)] -#[wrapper_mut(DerefMut)] -#[derive(StrictType, StrictEncode, StrictDecode)] -#[strict_type(lib = LIB_NAME_RGB_STD)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(crate = "serde_crate"))] -pub struct ContentSigs(NonEmptyOrdMap); - -impl StrictDumb for ContentSigs { - fn strict_dumb() -> Self { Self(NonEmptyOrdMap::with_key_value(strict_dumb!(), SigBlob::default())) } -} - -impl IntoIterator for ContentSigs { - type Item = (Identity, SigBlob); - type IntoIter = btree_map::IntoIter; - - fn into_iter(self) -> Self::IntoIter { self.0.into_iter() } -} diff --git a/src/util/suppl.rs b/src/util/suppl.rs deleted file mode 100644 index 629f055..0000000 --- a/src/util/suppl.rs +++ /dev/null @@ -1,331 +0,0 @@ -// RGB standard library for working with smart contracts on Bitcoin & Lightning -// -// SPDX-License-Identifier: Apache-2.0 -// -// Written in 2019-2024 by -// Dr Maxim Orlovsky -// -// Copyright (C) 2019-2024 LNP/BP Standards Association. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -use core::fmt; -use core::fmt::{Display, Formatter}; -use core::str::FromStr; - -use amplify::confinement::{SmallBlob, TinyOrdMap}; -use amplify::{ByteArray, Bytes32}; -use baid64::{Baid64ParseError, DisplayBaid64, FromBaid64Str}; -use chrono::Utc; -use commit_verify::{CommitId, CommitmentId, DigestExt, Sha256}; -use rgb::{AssignmentType, ContractId, GlobalStateType, Identity, SchemaId}; -use strict_encoding::stl::{AlphaCaps, AlphaNumDash}; -use strict_encoding::{ - DeserializeError, FieldName, RString, SerializeError, StrictDeserialize, StrictSerialize, TypeName, VariantName, -}; -use strict_types::value; - -use crate::interface::{IfaceId, ImplId}; -use crate::LIB_NAME_RGB_STD; - -pub const SUPPL_ANNOT_VELOCITY: &str = "Velocity"; -pub const SUPPL_ANNOT_IFACE_CLASS: &str = "Standard"; -pub const SUPPL_ANNOT_IFACE_FEATURES: &str = "Features"; - -/// Contract supplement identifier. -/// -/// Contract supplement identifier commits to all of the supplement data. -#[derive(Wrapper, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, From)] -#[wrapper(Deref, BorrowSlice, Hex, Index, RangeOps)] -#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)] -#[strict_type(lib = LIB_NAME_RGB_STD)] -pub struct SupplId( - #[from] - #[from([u8; 32])] - Bytes32, -); - -impl From for SupplId { - fn from(hasher: Sha256) -> Self { hasher.finish().into() } -} - -impl CommitmentId for SupplId { - const TAG: &'static str = "urn:lnp-bp:rgb:suppl#2024-03-11"; -} - -impl DisplayBaid64 for SupplId { - const HRI: &'static str = "rgb:sup"; - const CHUNKING: bool = false; - const PREFIX: bool = true; - const EMBED_CHECKSUM: bool = false; - const MNEMONIC: bool = false; - fn to_baid64_payload(&self) -> [u8; 32] { self.to_byte_array() } -} -impl FromBaid64Str for SupplId {} -impl FromStr for SupplId { - type Err = Baid64ParseError; - fn from_str(s: &str) -> Result { Self::from_baid64_str(s) } -} -impl Display for SupplId { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { self.fmt_baid64(f) } -} - -impl_serde_baid64!(SupplId); - -impl SupplId { - pub const fn from_array(id: [u8; 32]) -> Self { Self(Bytes32::from_array(id)) } -} - -#[derive(Wrapper, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, From, Display)] -#[wrapper(Deref, FromStr)] -#[display(inner)] -#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)] -#[strict_type(lib = LIB_NAME_RGB_STD)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(crate = "serde_crate", transparent))] -pub struct AnnotationName(RString); - -impl From<&'static str> for AnnotationName { - fn from(s: &'static str) -> Self { Self(RString::from(s)) } -} - -#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Display, From)] -#[display(inner)] -#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)] -#[strict_type(lib = LIB_NAME_RGB_STD, tags = order, dumb = ContentRef::Schema(strict_dumb!()))] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(crate = "serde_crate", rename_all = "camelCase"))] -pub enum ContentRef { - #[from] - Schema(SchemaId), - #[from] - Genesis(ContractId), - #[from] - Iface(IfaceId), - #[from] - IfaceImpl(ImplId), -} - -#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)] -#[derive(StrictType, StrictEncode, StrictDecode)] -#[strict_type(lib = LIB_NAME_RGB_STD, tags = repr, into_u8, try_from_u8)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(crate = "serde_crate", rename_all = "camelCase"))] -#[repr(u8)] -pub enum SupplSub { - #[default] - Itself = 0, - Meta = 1, - Global, - Owned, - Valency, - Assignment, - Genesis, - Transition, - Extension, - Exception, -} - -#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)] -#[derive(StrictType, StrictEncode, StrictDecode)] -#[strict_type(lib = LIB_NAME_RGB_STD, tags = custom)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(crate = "serde_crate", rename_all = "camelCase"))] -pub enum SupplItem { - #[default] - #[strict_type(tag = 0)] - Default, - #[strict_type(tag = 1)] - TypeNo(u16), - #[strict_type(tag = 0x11)] - TypeName(TypeName), - #[strict_type(tag = 0x12)] - FieldName(FieldName), - #[strict_type(tag = 0x13)] - VariantName(VariantName), -} - -#[derive(Wrapper, WrapperMut, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default, From)] -#[wrapper(Deref)] -#[wrapper_mut(DerefMut)] -#[derive(StrictType, StrictEncode, StrictDecode)] -#[strict_type(lib = LIB_NAME_RGB_STD)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(crate = "serde_crate"))] -pub struct SupplMap(TinyOrdMap); - -#[derive(Wrapper, WrapperMut, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default, From)] -#[wrapper(Deref)] -#[wrapper_mut(DerefMut)] -#[derive(StrictType, StrictEncode, StrictDecode)] -#[strict_type(lib = LIB_NAME_RGB_STD)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(crate = "serde_crate"))] -pub struct Annotations(TinyOrdMap); - -/// Contract supplement, providing non-consensus information about standard -/// way of working with the contract data. Each contract can have only a single -/// valid supplement; the supplement is attached to the contract via trusted -/// provider signature (providers are ordered by the priority). -#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] -#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)] -#[strict_type(lib = LIB_NAME_RGB_STD)] -#[derive(CommitEncode)] -#[commit_encode(strategy = strict, id = SupplId)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(crate = "serde_crate", rename_all = "camelCase"))] -pub struct Supplement { - pub content_id: ContentRef, - pub timestamp: i64, - pub creator: Identity, - /// Strict-encoded custom fields. - pub annotations: TinyOrdMap, -} - -impl StrictSerialize for Supplement {} -impl StrictDeserialize for Supplement {} - -impl Supplement { - pub fn suppl_id(&self) -> SupplId { self.commit_id() } - - pub fn new(content: impl Into, creator: impl Into) -> Self { - Supplement { - content_id: content.into(), - timestamp: Utc::now().timestamp(), - creator: creator.into(), - annotations: none!(), - } - } - - pub fn with(content: impl Into, creator: impl Into, timestamp: i64) -> Self { - Supplement { - content_id: content.into(), - timestamp, - creator: creator.into(), - annotations: none!(), - } - } - - pub fn get_default_opt(&self, sub: SupplSub, name: impl Into) -> Option { - self.get_default(sub, name).transpose().ok().flatten() - } - - pub fn get_default( - &self, - sub: SupplSub, - name: impl Into, - ) -> Option> { - let annotation = self - .annotations - .get(&sub)? - .get(&SupplItem::Default)? - .get(&name.into())?; - Some(T::from_strict_serialized(annotation.clone())) - } - - pub fn get( - &self, - sub: SupplSub, - item: SupplItem, - name: impl Into, - ) -> Option> { - let annotation = self.annotations.get(&sub)?.get(&item)?.get(&name.into())?; - Some(T::from_strict_serialized(annotation.clone())) - } - - pub fn annotate_itself( - &mut self, - name: impl Into, - data: &impl StrictSerialize, - ) -> Result { - self.annotate_default(SupplSub::Itself, name, data) - } - - pub fn annotate_default( - &mut self, - sub: SupplSub, - name: impl Into, - data: &impl StrictSerialize, - ) -> Result { - self.annotate(sub, SupplItem::Default, name, data) - } - - pub fn annotate( - &mut self, - sub: SupplSub, - item: SupplItem, - name: impl Into, - data: &impl StrictSerialize, - ) -> Result { - let mut a = self - .annotations - .remove(&sub) - .expect("zero items allowed") - .unwrap_or_default(); - let mut b = a - .remove(&item) - .expect("zero items allowed") - .unwrap_or_default(); - let prev = b.insert(name.into(), data.to_strict_serialized()?)?; - a.insert(item, b)?; - self.annotations.insert(sub, a)?; - Ok(prev.is_some()) - } -} - -#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] -#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)] -#[strict_type(lib = LIB_NAME_RGB_STD, tags = custom)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(crate = "serde_crate", rename_all = "camelCase"))] -pub enum TickerSuppl { - #[strict_type(tag = 0, dumb)] - Absent, - #[strict_type(tag = 1)] - Global(GlobalStateType, value::Path), - #[strict_type(tag = 2)] - Owned(AssignmentType, value::Path), -} - -impl StrictSerialize for TickerSuppl {} -impl StrictDeserialize for TickerSuppl {} - -#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Display, Default)] -#[derive(StrictType, StrictEncode, StrictDecode)] -#[strict_type(lib = LIB_NAME_RGB_STD, tags = repr, try_from_u8, into_u8)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(crate = "serde_crate", rename_all = "camelCase"))] -#[display(lowercase)] -#[repr(u8)] -pub enum VelocityHint { - #[default] - Unspecified = 0, - /// Should be used for thinks like secondary issuance for tokens which do - /// not inflate very often. - Seldom = 15, - /// Should be used for digital identity revocations. - Episodic = 31, - /// Should be used for digital art, shares, bonds etc. - Regular = 63, - /// Should be used for fungible tokens. - Frequent = 127, - /// Should be used for stablecoins and money. - HighFrequency = 255, -} - -impl StrictSerialize for VelocityHint {} -impl StrictDeserialize for VelocityHint {} - -impl VelocityHint { - pub fn with_value(value: &u8) -> Self { - match *value { - 0 => VelocityHint::Unspecified, - 1..=15 => VelocityHint::Seldom, - 16..=31 => VelocityHint::Episodic, - 32..=63 => VelocityHint::Regular, - 64..=127 => VelocityHint::Frequent, - 128..=255 => VelocityHint::HighFrequency, - } - } -}