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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use candid::CandidType;
use serde::{Deserialize, Serialize};

/// 10^8 Satoshi = 1 Bitcoin.
pub type Satoshi = u64;

/// Bitcoin Network.
#[derive(
    CandidType, Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy,
)]
pub enum BitcoinNetwork {
    /// Mainnet.
    #[serde(rename = "mainnet")]
    Mainnet,
    /// Testnet.
    #[serde(rename = "testnet")]
    Testnet,
    /// Regtest.
    ///
    /// This is only available when developing with local replica.
    #[serde(rename = "regtest")]
    Regtest,
}

impl Default for BitcoinNetwork {
    fn default() -> Self {
        Self::Regtest
    }
}

/// Bitcoin Address.
pub type BitcoinAddress = String;

/// Block Hash.
pub type BlockHash = Vec<u8>;

/// Element in the Response of [bitcoin_get_current_fee_percentiles](super::bitcoin_get_current_fee_percentiles).
pub type MillisatoshiPerByte = u64;

/// Identifier of [Utxo].
#[derive(
    CandidType, Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Default,
)]
pub struct Outpoint {
    /// Transaction Identifier.
    pub txid: Vec<u8>,
    /// A implicit index number.
    pub vout: u32,
}

/// Unspent transaction output (UTXO).
#[derive(
    CandidType, Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Default,
)]
pub struct Utxo {
    /// See [Outpoint].
    pub outpoint: Outpoint,
    /// Value in the units of satoshi.
    pub value: Satoshi,
    /// Height in the chain.
    pub height: u32,
}

/// Filter for requesting UTXOs.
#[derive(
    CandidType, Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone,
)]
pub enum UtxoFilter {
    /// Minimum number of confirmations. There is an upper bound of 144. Typically set to a value around 6 in practice.
    #[serde(rename = "min_confirmations")]
    MinConfirmations(u32),
    /// Page reference.
    ///
    /// DON'T construct it from scratch.
    /// Only get it from the `next_page` field of [GetUtxosResponse].
    #[serde(rename = "page")]
    Page(Vec<u8>),
}

/// Argument type of [bitcoin_get_balance](super::bitcoin_get_balance).
#[derive(
    CandidType, Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Default,
)]
pub struct GetBalanceRequest {
    /// See [BitcoinAddress].
    pub address: BitcoinAddress,
    /// See [BitcoinNetwork].
    pub network: BitcoinNetwork,
    /// Minimum number of confirmations. There is an upper bound of 144. Typically set to a value around 6 in practice.
    pub min_confirmations: Option<u32>,
}

/// Argument type of [bitcoin_get_utxos](super::bitcoin_get_utxos).
#[derive(
    CandidType, Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Default,
)]
pub struct GetUtxosRequest {
    /// See [BitcoinAddress].
    pub address: BitcoinAddress,
    /// See [BitcoinNetwork].
    pub network: BitcoinNetwork,
    /// See [UtxoFilter].
    pub filter: Option<UtxoFilter>,
}

/// Response type of [bitcoin_get_utxos](super::bitcoin_get_utxos).
#[derive(
    CandidType, Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Default,
)]
pub struct GetUtxosResponse {
    /// List of UTXOs.
    pub utxos: Vec<Utxo>,
    /// Hash of the tip block.
    pub tip_block_hash: BlockHash,
    /// Height of the tip height.
    pub tip_height: u32,
    /// Page reference when the response needs to be paginated.
    ///
    /// To be used in [UtxoFilter::Page].
    pub next_page: Option<Vec<u8>>,
}

/// Argument type of [bitcoin_send_transaction](super::bitcoin_send_transaction).
#[derive(
    CandidType, Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Default,
)]
pub struct SendTransactionRequest {
    /// The serialized transaction data.
    ///
    /// Several checks are performed.
    /// See [IC method `bitcoin_send_transaction`](https://internetcomputer.org/docs/current/references/ic-interface-spec/#ic-bitcoin_send_transaction).
    pub transaction: Vec<u8>,
    /// See [BitcoinNetwork].
    pub network: BitcoinNetwork,
}

/// Argument type of [bitcoin_get_current_fee_percentiles](super::bitcoin_get_current_fee_percentiles).
#[derive(
    CandidType,
    Serialize,
    Deserialize,
    Debug,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
    Clone,
    Copy,
    Default,
)]
pub struct GetCurrentFeePercentilesRequest {
    /// See [BitcoinNetwork].
    pub network: BitcoinNetwork,
}