rgbstd/containers/
transfer.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 std::io;
23use std::str::FromStr;
24
25use amplify::confinement::SmallOrdSet;
26use amplify::{ByteArray, Bytes32};
27use baid58::{Baid58ParseError, Chunking, FromBaid58, ToBaid58, CHUNKING_32};
28use commit_verify::{CommitEncode, CommitmentId, Conceal};
29use strict_encoding::{StrictEncode, StrictWriter};
30
31use crate::containers::{TerminalSeal, Transfer};
32use crate::LIB_NAME_RGB_STD;
33
34/// Transfer identifier.
35#[derive(Wrapper, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display, From)]
36#[wrapper(Deref, BorrowSlice, Hex, Index, RangeOps)]
37#[display(Self::to_baid58_string)]
38#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
39#[strict_type(lib = LIB_NAME_RGB_STD)]
40#[cfg_attr(
41    feature = "serde",
42    derive(Serialize, Deserialize),
43    serde(crate = "serde_crate", transparent)
44)]
45pub struct TransferId(
46    #[from]
47    #[from([u8; 32])]
48    Bytes32,
49);
50
51impl ToBaid58<32> for TransferId {
52    const HRI: &'static str = "consign";
53    const CHUNKING: Option<Chunking> = CHUNKING_32;
54    fn to_baid58_payload(&self) -> [u8; 32] { self.to_byte_array() }
55    fn to_baid58_string(&self) -> String { self.to_string() }
56}
57impl FromBaid58<32> for TransferId {}
58impl FromStr for TransferId {
59    type Err = Baid58ParseError;
60    fn from_str(s: &str) -> Result<Self, Self::Err> { Self::from_baid58_chunked_str(s, ':', '#') }
61}
62#[allow(clippy::wrong_self_convention)] // We need the method that takes self by ref in order to have simplier APIs in iterators
63impl TransferId {
64    pub fn to_baid58_string(&self) -> String { format!("{::<#.2}", self.to_baid58()) }
65    pub fn to_mnemonic(&self) -> String { self.to_baid58().mnemonic() }
66}
67
68impl CommitEncode for Transfer {
69    fn commit_encode(&self, e: &mut impl io::Write) {
70        let write = || -> Result<_, io::Error> {
71            let mut writer = StrictWriter::with(usize::MAX, e);
72            writer = self.transfer.strict_encode(writer)?;
73            writer = self.contract_id().strict_encode(writer)?;
74            for (bundle_id, terminal) in &self.terminals {
75                writer = bundle_id.strict_encode(writer)?;
76                let seals =
77                    SmallOrdSet::try_from_iter(terminal.seals.iter().map(TerminalSeal::conceal))
78                        .expect("same size iterator");
79                writer = seals.strict_encode(writer)?;
80            }
81            for attach_id in self.attachments.keys() {
82                writer = attach_id.strict_encode(writer)?;
83            }
84            self.signatures.strict_encode(writer)?;
85            Ok(())
86        };
87        write().expect("hashers do not error");
88    }
89}
90
91impl CommitmentId for Transfer {
92    const TAG: [u8; 32] = *b"urn:lnpbp:rgb:transfer:v01#2303A";
93    type Id = TransferId;
94}
95
96impl Transfer {
97    #[inline]
98    pub fn transfer_id(&self) -> TransferId { self.commitment_id() }
99}