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
// Modern, minimalistic & standard-compliant cold wallet library.
//
// SPDX-License-Identifier: Apache-2.0
//
// Written in 2020-2023 by
//     Dr Maxim Orlovsky <orlovsky@lnp-bp.org>
//
// Copyright (C) 2020-2023 LNP/BP Standards Association. All rights reserved.
// Copyright (C) 2020-2023 Dr Maxim Orlovsky. 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.

#[macro_use]
extern crate amplify;
#[cfg(feature = "strict_encoding")]
#[macro_use]
extern crate strict_encoding;
#[cfg(feature = "serde")]
#[macro_use]
extern crate serde_crate as serde;

mod data;
mod keys;
mod maps;
mod coders;
#[cfg(feature = "client-side-validation")]
mod csval;

pub use coders::{Decode, DecodeError, Encode, PsbtError};
#[cfg(feature = "client-side-validation")]
pub use csval::*;
pub use data::{
    Input, ModifiableFlags, Output, Prevout, Psbt, PsbtParseError, UnsignedTx, UnsignedTxIn,
};
pub use keys::{GlobalKey, InputKey, KeyPair, KeyType, OutputKey, PropKey};
pub use maps::{KeyAlreadyPresent, KeyData, KeyMap, Map, MapName, ValueData};

#[cfg(feature = "strict_encoding")]
pub const LIB_NAME_PSBT: &str = "Psbt";

#[derive(Copy, Clone, Eq, PartialEq, Debug, Display, Error)]
#[display("unsupported version of PSBT v{0}")]
pub struct PsbtUnsupportedVer(u32);

#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display)]
#[cfg_attr(
    feature = "serde",
    derive(Serialize),
    serde(crate = "serde_crate", rename_all = "camelCase")
)]
pub enum PsbtVer {
    #[display("v0")]
    V0 = 0,
    #[display("v2")]
    V2 = 2,
}

impl PsbtVer {
    pub const fn try_from_standard_u32(v: u32) -> Result<Self, PsbtUnsupportedVer> {
        Ok(match v {
            0 => Self::V0,
            2 => Self::V2,
            wrong => return Err(PsbtUnsupportedVer(wrong)),
        })
    }

    pub const fn to_standard_u32(&self) -> u32 { *self as u32 }

    pub const fn max() -> Self {
        // this is a special syntax construct to get compiler error each time we add a new version
        // and not to forget upgrade the result of this method
        match Self::V0 {
            PsbtVer::V0 | PsbtVer::V2 => PsbtVer::V2,
        }
    }
}