• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bitcoindevkit / bdk / 10032626530

22 Jul 2024 12:36AM UTC coverage: 81.881% (-1.6%) from 83.434%
10032626530

Pull #1506

github

web-flow
Merge 515f252f2 into d99b3ef4b
Pull Request #1506: Standardize API ownership in `KeychainTxOutIndex`

893 of 1170 new or added lines in 14 files covered. (76.32%)

33 existing lines in 7 files now uncovered.

10950 of 13373 relevant lines covered (81.88%)

16589.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

58.33
/crates/wallet/src/types.rs
1
// Bitcoin Dev Kit
2
// Written in 2020 by Alekos Filini <alekos.filini@gmail.com>
3
//
4
// Copyright (c) 2020-2021 Bitcoin Dev Kit Developers
5
//
6
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
7
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
8
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
9
// You may not use this file except in accordance with one or both of these
10
// licenses.
11

12
use alloc::boxed::Box;
13
use core::convert::AsRef;
14

15
use bdk_chain::ConfirmationTime;
16
use bitcoin::transaction::{OutPoint, Sequence, TxOut};
17
use bitcoin::{psbt, Weight};
18

19
use serde::{Deserialize, Serialize};
20

21
/// Types of keychains
UNCOV
22
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
×
23
pub enum KeychainKind {
24
    /// External keychain, used for deriving recipient addresses.
25
    External = 0,
26
    /// Internal keychain, used for deriving change addresses.
27
    Internal = 1,
28
}
29

30
impl KeychainKind {
31
    /// Return [`KeychainKind`] as a byte
32
    pub fn as_byte(&self) -> u8 {
×
33
        match self {
×
34
            KeychainKind::External => b'e',
×
35
            KeychainKind::Internal => b'i',
×
36
        }
37
    }
×
38
}
39

40
impl AsRef<[u8]> for KeychainKind {
41
    fn as_ref(&self) -> &[u8] {
×
42
        match self {
×
43
            KeychainKind::External => b"e",
×
44
            KeychainKind::Internal => b"i",
×
45
        }
46
    }
×
47
}
48

49
/// An unspent output owned by a [`Wallet`].
50
///
51
/// [`Wallet`]: crate::Wallet
52
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
×
53
pub struct LocalOutput {
54
    /// Reference to a transaction output
55
    pub outpoint: OutPoint,
56
    /// Transaction output
57
    pub txout: TxOut,
58
    /// Type of keychain
59
    pub keychain: KeychainKind,
60
    /// Whether this UTXO is spent or not
61
    pub is_spent: bool,
62
    /// The derivation index for the script pubkey in the wallet
63
    pub derivation_index: u32,
64
    /// The confirmation time for transaction containing this utxo
65
    pub confirmation_time: ConfirmationTime,
66
}
67

68
/// A [`Utxo`] with its `satisfaction_weight`.
69
#[derive(Debug, Clone, PartialEq, Eq)]
70
pub struct WeightedUtxo {
71
    /// The weight of the witness data and `scriptSig` expressed in [weight units]. This is used to
72
    /// properly maintain the feerate when adding this input to a transaction during coin selection.
73
    ///
74
    /// [weight units]: https://en.bitcoin.it/wiki/Weight_units
75
    pub satisfaction_weight: Weight,
76
    /// The UTXO
77
    pub utxo: Utxo,
78
}
79

80
#[derive(Debug, Clone, PartialEq, Eq)]
81
/// An unspent transaction output (UTXO).
82
pub enum Utxo {
83
    /// A UTXO owned by the local wallet.
84
    Local(LocalOutput),
85
    /// A UTXO owned by another wallet.
86
    Foreign {
87
        /// The location of the output.
88
        outpoint: OutPoint,
89
        /// The nSequence value to set for this input.
90
        sequence: Option<Sequence>,
91
        /// The information about the input we require to add it to a PSBT.
92
        // Box it to stop the type being too big.
93
        psbt_input: Box<psbt::Input>,
94
    },
95
}
96

97
impl Utxo {
98
    /// Get the location of the UTXO
99
    pub fn outpoint(&self) -> OutPoint {
4,117✔
100
        match &self {
4,117✔
101
            Utxo::Local(local) => local.outpoint,
3,925✔
102
            Utxo::Foreign { outpoint, .. } => *outpoint,
192✔
103
        }
104
    }
4,117✔
105

106
    /// Get the `TxOut` of the UTXO
107
    pub fn txout(&self) -> &TxOut {
115,974✔
108
        match &self {
115,974✔
109
            Utxo::Local(local) => &local.txout,
115,878✔
110
            Utxo::Foreign {
111
                outpoint,
96✔
112
                psbt_input,
96✔
113
                ..
114
            } => {
115
                if let Some(prev_tx) = &psbt_input.non_witness_utxo {
96✔
116
                    return &prev_tx.output[outpoint.vout as usize];
16✔
117
                }
80✔
118

119
                if let Some(txout) = &psbt_input.witness_utxo {
80✔
120
                    return txout;
80✔
121
                }
×
122

×
123
                unreachable!("Foreign UTXOs will always have one of these set")
×
124
            }
125
        }
126
    }
115,974✔
127

128
    /// Get the sequence number if an explicit sequence number has to be set for this input.
129
    pub fn sequence(&self) -> Option<Sequence> {
1,232✔
130
        match self {
1,232✔
131
            Utxo::Local(_) => None,
1,184✔
132
            Utxo::Foreign { sequence, .. } => *sequence,
48✔
133
        }
134
    }
1,232✔
135
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc