bc/
stl.rs

1// Bitcoin protocol consensus library.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Written in 2019-2024 by
6//     Dr Maxim Orlovsky <orlovsky@lnp-bp.org>
7//
8// Copyright (C) 2019-2024 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// Coding conventions
22
23use strict_types::{CompileError, LibBuilder, TypeLib};
24
25use crate::{
26    Bip340Sig, Block, ByteStr, CompressedPk, ControlBlock, FutureLeafVer, InternalPk, LeafScript,
27    LegacyPk, LegacySig, LockHeight, LockTimestamp, OpCode, OutputPk, PubkeyHash, RedeemScript,
28    ScriptHash, TapCode, TapLeafHash, TapNodeHash, TapScript, TimeLockInterval, Tx, UncompressedPk,
29    VBytes, VarInt, WPubkeyHash, WScriptHash, WeightUnits, WitnessProgram, WitnessScript,
30    WitnessVer, Wtxid, LIB_NAME_BITCOIN,
31};
32
33pub const LIB_ID_BP_TX: &str =
34    "stl:9WwTYiP2-OadKCZP-cR0bJ_Y-qruINYX-bXZFj8Y-fsQoGgo#signal-color-cipher";
35pub const LIB_ID_BP_CONSENSUS: &str =
36    "stl:CaK1Puml-gQpFSBK-E06s9O3-W27jJMu-fcpa21K-6AQ2FZc#extend-crimson-cable";
37
38#[allow(clippy::result_large_err)]
39#[deprecated(since = "0.10.8", note = "use _bp_tx_stl instead")]
40fn _bitcoin_stl() -> Result<TypeLib, CompileError> { _bp_tx_stl() }
41
42#[allow(clippy::result_large_err)]
43fn _bp_tx_stl() -> Result<TypeLib, CompileError> {
44    LibBuilder::with(libname!(LIB_NAME_BITCOIN), None).transpile::<Tx>().compile()
45}
46
47#[allow(clippy::result_large_err)]
48fn _bp_consensus_stl() -> Result<TypeLib, CompileError> {
49    LibBuilder::with(libname!(LIB_NAME_BITCOIN), [
50        strict_types::stl::std_stl().to_dependency_types()
51    ])
52    .transpile::<LegacySig>()
53    .transpile::<Bip340Sig>()
54    .transpile::<OpCode>()
55    .transpile::<PubkeyHash>()
56    .transpile::<WPubkeyHash>()
57    .transpile::<ScriptHash>()
58    .transpile::<WScriptHash>()
59    .transpile::<WitnessScript>()
60    .transpile::<RedeemScript>()
61    .transpile::<Wtxid>()
62    .transpile::<WitnessProgram>()
63    .transpile::<WitnessVer>()
64    .transpile::<CompressedPk>()
65    .transpile::<UncompressedPk>()
66    .transpile::<LegacyPk>()
67    .transpile::<InternalPk>()
68    .transpile::<OutputPk>()
69    .transpile::<TapNodeHash>()
70    .transpile::<TapLeafHash>()
71    .transpile::<FutureLeafVer>()
72    .transpile::<LeafScript>()
73    .transpile::<TapCode>()
74    .transpile::<TapScript>()
75    .transpile::<ControlBlock>()
76    .transpile::<Block>()
77    .transpile::<TimeLockInterval>()
78    .transpile::<LockTimestamp>()
79    .transpile::<LockHeight>()
80    .transpile::<Tx>()
81    .transpile::<VarInt>()
82    .transpile::<ByteStr>()
83    .transpile::<WeightUnits>()
84    .transpile::<VBytes>()
85    .compile()
86}
87
88#[deprecated(since = "0.10.8", note = "use bp_tx_stl instead")]
89pub fn bitcoin_stl() -> TypeLib { bp_tx_stl() }
90
91pub fn bp_tx_stl() -> TypeLib {
92    _bp_tx_stl().expect("invalid strict type Bitcoin transaction library")
93}
94
95pub fn bp_consensus_stl() -> TypeLib {
96    _bp_consensus_stl().expect("invalid strict type Bitcoin consensus library")
97}
98
99#[cfg(test)]
100mod test {
101    #![cfg_attr(coverage_nightly, coverage(off))]
102
103    use super::*;
104
105    #[test]
106    fn lib_id_tx() {
107        let lib = bp_tx_stl();
108        assert_eq!(lib.id().to_string(), LIB_ID_BP_TX);
109    }
110
111    #[test]
112    fn lib_id_consensus() {
113        let lib = bp_consensus_stl();
114        assert_eq!(lib.id().to_string(), LIB_ID_BP_CONSENSUS);
115    }
116}