rgbstd/stl/
stl.rs

1// RGB standard library for working with smart contracts on Bitcoin & Lightning
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Written in 2019-2023 by
6//     Dr Maxim Orlovsky <orlovsky@lnp-bp.org>
7//
8// Copyright (C) 2019-2023 LNP/BP Standards Association. All rights reserved.
9//
10// Licensed under the Apache License, Version 2.0 (the "License");
11// you may not use this file except in compliance with the License.
12// You may obtain a copy of the License at
13//
14//     http://www.apache.org/licenses/LICENSE-2.0
15//
16// Unless required by applicable law or agreed to in writing, software
17// distributed under the License is distributed on an "AS IS" BASIS,
18// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19// See the License for the specific language governing permissions and
20// limitations under the License.
21
22use bp::bc::stl::bp_tx_stl;
23use bp::stl::bp_core_stl;
24use commit_verify::stl::commit_verify_stl;
25pub use rgb::stl::{aluvm_stl, rgb_core_stl, LIB_ID_RGB};
26use strict_types::stl::{std_stl, strict_types_stl};
27use strict_types::typesys::SystemBuilder;
28use strict_types::{CompileError, LibBuilder, SemId, SymbolicSys, TypeLib, TypeSystem};
29
30use super::{
31    Amount, BurnMeta, ContractData, DivisibleAssetSpec, Error, IssueMeta, MediaType,
32    RicardianContract, Timestamp, LIB_NAME_RGB_CONTRACT,
33};
34use crate::containers::{Contract, Transfer};
35use crate::stl::ProofOfReserves;
36use crate::LIB_NAME_RGB_STD;
37
38/// Strict types id for the library providing standard data types which may be
39/// used in RGB smart contracts.
40pub const LIB_ID_RGB_CONTRACT: &str =
41    "urn:ubideco:stl:6vbr9ZrtsD9aBjo5qRQ36QEZPVucqvRRjKCPqE8yPeJr#choice-little-boxer";
42
43/// Strict types id for the library representing of RGB StdLib data types.
44pub const LIB_ID_RGB_STD: &str =
45    "urn:ubideco:stl:3KXsWZ6hSKRbPjSVwRGbwnwJp3ZNQ2tfe6QUwLJEDG6K#twist-paul-carlo";
46
47fn _rgb_std_stl() -> Result<TypeLib, CompileError> {
48    LibBuilder::new(libname!(LIB_NAME_RGB_STD), tiny_bset! {
49        std_stl().to_dependency(),
50        strict_types_stl().to_dependency(),
51        commit_verify_stl().to_dependency(),
52        bp_tx_stl().to_dependency(),
53        bp_core_stl().to_dependency(),
54        aluvm_stl().to_dependency(),
55        rgb_core_stl().to_dependency()
56    })
57    .transpile::<Transfer>()
58    .transpile::<Contract>()
59    .compile()
60}
61
62fn _rgb_contract_stl() -> Result<TypeLib, CompileError> {
63    LibBuilder::new(libname!(LIB_NAME_RGB_CONTRACT), tiny_bset! {
64        std_stl().to_dependency(),
65        bp_tx_stl().to_dependency()
66    })
67    .transpile::<Amount>()
68    .transpile::<Timestamp>()
69    .transpile::<DivisibleAssetSpec>()
70    .transpile::<RicardianContract>()
71    .transpile::<ContractData>()
72    .transpile::<MediaType>()
73    .transpile::<ProofOfReserves>()
74    .transpile::<BurnMeta>()
75    .transpile::<IssueMeta>()
76    .compile()
77}
78
79/// Generates strict type library representation of RGB StdLib data types.
80pub fn rgb_std_stl() -> TypeLib { _rgb_std_stl().expect("invalid strict type RGBStd library") }
81
82/// Generates strict type library providing standard data types which may be
83/// used in RGB smart contracts.
84pub fn rgb_contract_stl() -> TypeLib {
85    _rgb_contract_stl().expect("invalid strict type RGBContract library")
86}
87
88#[derive(Debug)]
89pub struct StandardTypes(SymbolicSys);
90
91impl Default for StandardTypes {
92    fn default() -> Self { StandardTypes::new() }
93}
94
95impl StandardTypes {
96    pub fn new() -> Self {
97        Self::try_with([std_stl(), bp_tx_stl(), rgb_contract_stl()])
98            .expect("error in standard RGBContract type system")
99    }
100
101    pub fn with(lib: TypeLib) -> Self {
102        Self::try_with([std_stl(), bp_tx_stl(), rgb_contract_stl(), lib])
103            .expect("error in standard RGBContract type system")
104    }
105
106    #[allow(clippy::result_large_err)]
107    fn try_with(libs: impl IntoIterator<Item = TypeLib>) -> Result<Self, Error> {
108        let mut builder = SystemBuilder::new();
109        for lib in libs.into_iter() {
110            builder = builder.import(lib)?;
111        }
112        let sys = builder.finalize()?;
113        Ok(Self(sys))
114    }
115
116    pub fn type_system(&self) -> TypeSystem { self.0.as_types().clone() }
117
118    pub fn get(&self, name: &'static str) -> SemId {
119        *self.0.resolve(name).unwrap_or_else(|| {
120            panic!("type '{name}' is absent in standard RGBContract type library")
121        })
122    }
123}
124
125#[cfg(test)]
126mod test {
127    use super::*;
128
129    #[test]
130    fn contract_lib_id() {
131        let lib = rgb_contract_stl();
132        assert_eq!(lib.id().to_string(), LIB_ID_RGB_CONTRACT);
133    }
134
135    #[test]
136    fn std_lib_id() {
137        let lib = rgb_std_stl();
138        assert_eq!(lib.id().to_string(), LIB_ID_RGB_STD);
139    }
140}