1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// RGB standard library
// Written in 2020 by
//     Dr. Maxim Orlovsky <orlovsky@pandoracore.com>
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the MIT License
// along with this software.
// If not, see <https://opensource.org/licenses/MIT>.

use regex::Regex;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use lnpbp::bitcoin::util::psbt::PartiallySignedTransaction;
use lnpbp::bitcoin::OutPoint;
use lnpbp::bp::blind::OutpointReveal;
use lnpbp::rgb::{Consignment, ContractId};

use crate::fungible::{ConsealCoins, OutpointCoins, SealCoins};
use crate::DataFormat;

#[derive(Clone, Debug, Display, LnpApi)]
#[lnp_api(encoding = "strict")]
#[display(Debug)]
#[non_exhaustive]
pub enum Request {
    #[lnp_api(type = 0x0101)]
    Issue(crate::api::fungible::Issue),

    #[lnp_api(type = 0x0103)]
    Transfer(crate::api::fungible::TransferApi),

    #[lnp_api(type = 0x0105)]
    Validate(::lnpbp::rgb::Consignment),

    #[lnp_api(type = 0x0107)]
    Accept(crate::api::fungible::AcceptApi),

    #[lnp_api(type = 0x0109)]
    ImportAsset(::lnpbp::rgb::Genesis),

    #[lnp_api(type = 0x010b)]
    ExportAsset(::lnpbp::rgb::ContractId),

    #[lnp_api(type = 0x010d)]
    Forget(::lnpbp::bitcoin::OutPoint),

    #[lnp_api(type = 0xFF01)]
    Sync(DataFormat),

    #[lnp_api(type = 0xFF02)]
    Assets(OutPoint),

    #[lnp_api(type = 0xFF03)]
    Allocations(ContractId),
}

#[derive(
    Clap, Clone, PartialEq, StrictEncode, StrictDecode, Debug, Display,
)]
#[display(Debug)]
#[cfg_attr(
    feature = "serde",
    derive(Serialize, Deserialize,),
    serde(crate = "serde_crate")
)]
pub struct Issue {
    /// Asset ticker (up to 8 characters, always converted to uppercase)
    #[clap(validator=ticker_validator)]
    pub ticker: String,

    /// Asset name (up to 32 characters)
    pub name: String,

    /// Asset description
    #[clap(short, long)]
    pub description: Option<String>,

    /// Precision, i.e. number of digits reserved for fractional part
    #[clap(short, long, default_value = "0")]
    pub precision: u8,

    /// Asset allocation, in form of <amount>@<txid>:<vout>
    pub allocation: Vec<OutpointCoins>,

    /// Outputs controlling inflation (secondary issue);
    /// in form of <amount>@<txid>:<vout>
    #[clap(short, long)]
    pub inflation: Vec<OutpointCoins>,

    /// Enable renomination procedure; parameter takes argument in form of
    /// <txid>:<vout> specifying output controlling renomination right
    #[clap(short, long)]
    pub renomination: Option<OutPoint>,

    /// Enable epoch-based burn & replacement procedure; parameter takes
    /// argument in form of <txid>:<vout> specifying output controlling the
    /// right of opening the first epoch
    #[clap(short, long)]
    pub epoch: Option<OutPoint>,
}

#[derive(Clone, PartialEq, StrictEncode, StrictDecode, Debug, Display)]
#[display(Debug)]
pub struct TransferApi {
    /// Asset contract id
    pub contract_id: ContractId,

    /// Base layer transaction structure to use
    pub psbt: PartiallySignedTransaction,

    /// Asset input: unspent transaction outputs
    pub inputs: Vec<OutPoint>,

    /// Asset change allocations
    ///
    /// Here we always know an explicit outpoint that will contain the assets
    pub ours: Vec<SealCoins>,

    /// Receiver's allocations.
    ///
    /// They are kept separate from change allocations since here we do not
    /// know the actual seals and only know hashes derived from seal data and
    /// blinding entropy.
    pub theirs: Vec<ConsealCoins>,
}

#[derive(Clone, StrictEncode, StrictDecode, Debug, Display)]
#[display(Debug)]
pub struct AcceptApi {
    /// Raw consignment data
    pub consignment: Consignment,

    /// Reveal outpoints data used during invoice creation
    pub reveal_outpoints: Vec<OutpointReveal>,
}

fn ticker_validator(name: &str) -> Result<(), String> {
    let re = Regex::new(r"^[A-Z]{3,8}$").expect("Regex parse failure");
    if !re.is_match(&name) {
        Err(
            "Ticker name must be between 3 and 8 chars, contain no spaces and \
            consist only of capital letters\
            "
            .to_string(),
        )
    } else {
        Ok(())
    }
}