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::timelocks::TimeLockInterval;
26use crate::{
27    Bip340Sig, BlockHeader, ByteStr, CompressedPk, ControlBlock, FutureLeafVer, InternalPk,
28    LeafScript, LegacyPk, LegacySig, LockHeight, LockTimestamp, OpCode, OutputPk, PubkeyHash,
29    RedeemScript, ScriptHash, TapCode, TapLeafHash, TapNodeHash, TapScript, Tx, UncompressedPk,
30    VBytes, VarInt, WPubkeyHash, WScriptHash, WeightUnits, WitnessProgram, WitnessScript,
31    WitnessVer, Wtxid, LIB_NAME_BITCOIN,
32};
33
34pub const LIB_ID_BP_TX: &str =
35    "stl:9WwTYiP2-OadKCZP-cR0bJ!Y-qruINYX-bXZFj8Y-fsQoGgo#signal-color-cipher";
36pub const LIB_ID_BP_CONSENSUS: &str =
37    "stl:q7G95wzt-SxT2BMV-t!PokBt-wNYgZTu-AaYAtM3-rYjlzs4#agenda-wolf-pagoda";
38
39#[deprecated(since = "0.10.8", note = "use _bp_tx_stl instead")]
40fn _bitcoin_stl() -> Result<TypeLib, CompileError> { _bp_tx_stl() }
41
42fn _bp_tx_stl() -> Result<TypeLib, CompileError> {
43    LibBuilder::new(libname!(LIB_NAME_BITCOIN), None).transpile::<Tx>().compile()
44}
45
46fn _bp_consensus_stl() -> Result<TypeLib, CompileError> {
47    LibBuilder::new(libname!(LIB_NAME_BITCOIN), tiny_bset! {
48        strict_types::stl::std_stl().to_dependency(),
49    })
50    .transpile::<LegacySig>()
51    .transpile::<Bip340Sig>()
52    .transpile::<OpCode>()
53    .transpile::<PubkeyHash>()
54    .transpile::<WPubkeyHash>()
55    .transpile::<ScriptHash>()
56    .transpile::<WScriptHash>()
57    .transpile::<WitnessScript>()
58    .transpile::<RedeemScript>()
59    .transpile::<Wtxid>()
60    .transpile::<WitnessProgram>()
61    .transpile::<WitnessVer>()
62    .transpile::<CompressedPk>()
63    .transpile::<UncompressedPk>()
64    .transpile::<LegacyPk>()
65    .transpile::<InternalPk>()
66    .transpile::<OutputPk>()
67    .transpile::<TapNodeHash>()
68    .transpile::<TapLeafHash>()
69    .transpile::<FutureLeafVer>()
70    .transpile::<LeafScript>()
71    .transpile::<TapCode>()
72    .transpile::<TapScript>()
73    .transpile::<ControlBlock>()
74    .transpile::<BlockHeader>()
75    .transpile::<TimeLockInterval>()
76    .transpile::<LockTimestamp>()
77    .transpile::<LockHeight>()
78    .transpile::<Tx>()
79    .transpile::<VarInt>()
80    .transpile::<ByteStr>()
81    .transpile::<WeightUnits>()
82    .transpile::<VBytes>()
83    .compile()
84}
85
86#[deprecated(since = "0.10.8", note = "use bp_tx_stl instead")]
87pub fn bitcoin_stl() -> TypeLib { bp_tx_stl() }
88
89pub fn bp_tx_stl() -> TypeLib {
90    _bp_tx_stl().expect("invalid strict type Bitcoin transaction library")
91}
92
93pub fn bp_consensus_stl() -> TypeLib {
94    _bp_consensus_stl().expect("invalid strict type Bitcoin consensus library")
95}
96
97#[cfg(test)]
98mod test {
99    use super::*;
100
101    #[test]
102    fn lib_id_tx() {
103        let lib = bp_tx_stl();
104        assert_eq!(lib.id().to_string(), LIB_ID_BP_TX);
105    }
106
107    #[test]
108    fn lib_id_consensus() {
109        let lib = bp_consensus_stl();
110        assert_eq!(lib.id().to_string(), LIB_ID_BP_CONSENSUS);
111    }
112}