derive/sign.rs
1// Modern, minimalistic & standard-compliant cold wallet library.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Written in 2020-2024 by
6// Dr Maxim Orlovsky <orlovsky@lnp-bp.org>
7//
8// Copyright (C) 2020-2024 LNP/BP Standards Association. All rights reserved.
9// Copyright (C) 2020-2024 Dr Maxim Orlovsky. All rights reserved.
10//
11// Licensed under the Apache License, Version 2.0 (the "License");
12// you may not use this file except in compliance with the License.
13// You may obtain a copy of the License at
14//
15// http://www.apache.org/licenses/LICENSE-2.0
16//
17// Unless required by applicable law or agreed to in writing, software
18// distributed under the License is distributed on an "AS IS" BASIS,
19// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20// See the License for the specific language governing permissions and
21// limitations under the License.
22
23use bc::secp256k1::{ecdsa, schnorr as bip340};
24use bc::{
25 InternalPk, LegacyPk, Sighash, TapLeafHash, TapMerklePath, TapNodeHash, TapSighash, XOnlyPk,
26};
27
28use crate::KeyOrigin;
29
30/// Trait used for signing transactions.
31pub trait Sign {
32 /// Create signature with a given key for inputs requiring ECDSA signatures (bare, pre-segwit
33 /// and segwit v0).
34 fn sign_ecdsa(
35 &self,
36 message: Sighash,
37 pk: LegacyPk,
38 origin: Option<&KeyOrigin>,
39 ) -> Option<ecdsa::Signature>;
40
41 /// Create signature with a given internal key using Schnorr signatures with BIP-340 signing
42 /// scheme (taproot).
43 fn sign_bip340_key_only(
44 &self,
45 message: TapSighash,
46 pk: InternalPk,
47 origin: Option<&KeyOrigin>,
48 merkle_root: Option<TapNodeHash>,
49 ) -> Option<bip340::Signature>;
50
51 /// Create signature with a given script path and x-only public key using Schnorr signatures
52 /// with BIP-340 signing scheme (taproot).
53 fn sign_bip340_script_path(
54 &self,
55 message: TapSighash,
56 pk: XOnlyPk,
57 origin: Option<&KeyOrigin>,
58 ) -> Option<bip340::Signature>;
59
60 /// Detect whether a given taproot script spending path should be signed for a given input
61 /// `index`.
62 #[must_use]
63 fn should_sign_script_path(
64 &self,
65 index: usize,
66 merkle_path: &TapMerklePath,
67 leaf: TapLeafHash,
68 ) -> bool;
69
70 /// Detect whether taproot key spending path should be signed for a given input `index`.
71 #[must_use]
72 fn should_sign_key_path(&self, index: usize) -> bool;
73}