Skip to content

Commit f6b9749

Browse files
Add bitcoin_get_block_headers (#537)
1 parent b3090c3 commit f6b9749

File tree

3 files changed

+78
-17
lines changed

3 files changed

+78
-17
lines changed

src/ic-cdk/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
### Added
1010

11+
- Add method `bitcoin_get_block_headers`.
1112
- Support management canister method: `subnet_info`. (#532)
1213
- Add types: `SubnetInfoArgs` and `SubnetInfoResult`.
1314

src/ic-cdk/src/api/management_canister/bitcoin/mod.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ const SEND_TRANSACTION_SUBMISSION_TESTNET: u128 = 2_000_000_000;
2323
const SEND_TRANSACTION_PAYLOAD_MAINNET: u128 = 20_000_000;
2424
const SEND_TRANSACTION_PAYLOAD_TESTNET: u128 = 8_000_000;
2525

26+
const GET_BLOCK_HEADERS_MAINNET: u128 = 4_000_000_000;
27+
const GET_BLOCK_HEADERS_TESTNET: u128 = 4_000_000_000;
28+
2629
/// See [IC method `bitcoin_get_balance`](https://internetcomputer.org/docs/current/references/ic-interface-spec/#ic-bitcoin_get_balance).
2730
///
2831
/// This call requires cycles payment.
@@ -115,3 +118,25 @@ pub async fn bitcoin_get_current_fee_percentiles(
115118
)
116119
.await
117120
}
121+
122+
/// See [IC method `bitcoin_get_block_headers`](https://internetcomputer.org/docs/current/references/ic-interface-spec#ic-bitcoin_get_block_headers).
123+
///
124+
/// This call requires cycles payment.
125+
/// This method handles the cycles cost under the hood.
126+
/// Check [API fees & Pricing](https://internetcomputer.org/docs/current/developer-docs/integrations/bitcoin/bitcoin-how-it-works/#api-fees--pricing) for more details.
127+
pub async fn bitcoin_get_block_headers(
128+
arg: GetBlockHeadersRequest,
129+
) -> CallResult<(GetBlockHeadersResponse,)> {
130+
let cycles = match arg.network {
131+
BitcoinNetwork::Mainnet => GET_BLOCK_HEADERS_MAINNET,
132+
BitcoinNetwork::Testnet => GET_BLOCK_HEADERS_TESTNET,
133+
BitcoinNetwork::Regtest => 0,
134+
};
135+
call_with_payment128(
136+
Principal::management_canister(),
137+
"bitcoin_get_block_headers",
138+
(arg,),
139+
cycles,
140+
)
141+
.await
142+
}

src/ic-cdk/src/api/management_canister/bitcoin/types.rs

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ pub type BitcoinAddress = String;
3434
/// Block Hash.
3535
pub type BlockHash = Vec<u8>;
3636

37-
/// Element in the Response of [bitcoin_get_current_fee_percentiles](super::bitcoin_get_current_fee_percentiles).
37+
/// Element in the Response of [`bitcoin_get_current_fee_percentiles`](super::bitcoin_get_current_fee_percentiles).
3838
pub type MillisatoshiPerByte = u64;
3939

40-
/// Identifier of [Utxo].
40+
/// Identifier of [`Utxo`].
4141
#[derive(
4242
CandidType, Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Default,
4343
)]
@@ -53,7 +53,7 @@ pub struct Outpoint {
5353
CandidType, Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Default,
5454
)]
5555
pub struct Utxo {
56-
/// See [Outpoint].
56+
/// See [`Outpoint`].
5757
pub outpoint: Outpoint,
5858
/// Value in the units of satoshi.
5959
pub value: Satoshi,
@@ -72,38 +72,38 @@ pub enum UtxoFilter {
7272
/// Page reference.
7373
///
7474
/// DON'T construct it from scratch.
75-
/// Only get it from the `next_page` field of [GetUtxosResponse].
75+
/// Only get it from the `next_page` field of [`GetUtxosResponse`].
7676
#[serde(rename = "page")]
7777
Page(Vec<u8>),
7878
}
7979

80-
/// Argument type of [bitcoin_get_balance](super::bitcoin_get_balance).
80+
/// Argument type of [`bitcoin_get_balance`](super::bitcoin_get_balance).
8181
#[derive(
8282
CandidType, Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Default,
8383
)]
8484
pub struct GetBalanceRequest {
85-
/// See [BitcoinAddress].
85+
/// See [`BitcoinAddress`].
8686
pub address: BitcoinAddress,
87-
/// See [BitcoinNetwork].
87+
/// See [`BitcoinNetwork`].
8888
pub network: BitcoinNetwork,
8989
/// Minimum number of confirmations. There is an upper bound of 144. Typically set to a value around 6 in practice.
9090
pub min_confirmations: Option<u32>,
9191
}
9292

93-
/// Argument type of [bitcoin_get_utxos](super::bitcoin_get_utxos).
93+
/// Argument type of [`bitcoin_get_utxos`](super::bitcoin_get_utxos).
9494
#[derive(
9595
CandidType, Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Default,
9696
)]
9797
pub struct GetUtxosRequest {
98-
/// See [BitcoinAddress].
98+
/// See [`BitcoinAddress`].
9999
pub address: BitcoinAddress,
100-
/// See [BitcoinNetwork].
100+
/// See [`BitcoinNetwork`].
101101
pub network: BitcoinNetwork,
102-
/// See [UtxoFilter].
102+
/// See [`UtxoFilter`].
103103
pub filter: Option<UtxoFilter>,
104104
}
105105

106-
/// Response type of [bitcoin_get_utxos](super::bitcoin_get_utxos).
106+
/// Response type of [`bitcoin_get_utxos`](super::bitcoin_get_utxos).
107107
#[derive(
108108
CandidType, Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Default,
109109
)]
@@ -116,11 +116,11 @@ pub struct GetUtxosResponse {
116116
pub tip_height: u32,
117117
/// Page reference when the response needs to be paginated.
118118
///
119-
/// To be used in [UtxoFilter::Page].
119+
/// To be used in [`UtxoFilter::Page`].
120120
pub next_page: Option<Vec<u8>>,
121121
}
122122

123-
/// Argument type of [bitcoin_send_transaction](super::bitcoin_send_transaction).
123+
/// Argument type of [`bitcoin_send_transaction`](super::bitcoin_send_transaction).
124124
#[derive(
125125
CandidType, Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Default,
126126
)]
@@ -130,11 +130,11 @@ pub struct SendTransactionRequest {
130130
/// Several checks are performed.
131131
/// See [IC method `bitcoin_send_transaction`](https://internetcomputer.org/docs/current/references/ic-interface-spec/#ic-bitcoin_send_transaction).
132132
pub transaction: Vec<u8>,
133-
/// See [BitcoinNetwork].
133+
/// See [`BitcoinNetwork`].
134134
pub network: BitcoinNetwork,
135135
}
136136

137-
/// Argument type of [bitcoin_get_current_fee_percentiles](super::bitcoin_get_current_fee_percentiles).
137+
/// Argument type of [`bitcoin_get_current_fee_percentiles`](super::bitcoin_get_current_fee_percentiles).
138138
#[derive(
139139
CandidType,
140140
Serialize,
@@ -150,6 +150,41 @@ pub struct SendTransactionRequest {
150150
Default,
151151
)]
152152
pub struct GetCurrentFeePercentilesRequest {
153-
/// See [BitcoinNetwork].
153+
/// See [`BitcoinNetwork`].
154154
pub network: BitcoinNetwork,
155155
}
156+
157+
/// Argument type of [`bitcoin_get_block_headers`](super::bitcoin_get_block_headers).
158+
#[derive(
159+
CandidType,
160+
Serialize,
161+
Deserialize,
162+
Debug,
163+
PartialEq,
164+
Eq,
165+
PartialOrd,
166+
Ord,
167+
Hash,
168+
Clone,
169+
Copy,
170+
Default,
171+
)]
172+
pub struct GetBlockHeadersRequest {
173+
/// The starting block height for the request.
174+
pub start_height: u32,
175+
/// The ending block height for the request, or `None` for the current tip.
176+
pub end_height: Option<u32>,
177+
/// See [`BitcoinNetwork`].
178+
pub network: BitcoinNetwork,
179+
}
180+
181+
/// Response type of [`bitcoin_get_block_headers`](super::bitcoin_get_block_headers).
182+
#[derive(
183+
CandidType, Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Default,
184+
)]
185+
pub struct GetBlockHeadersResponse {
186+
/// The tip of the blockchain when this request was filled.
187+
pub tip_height: u32,
188+
/// The requested block headers.
189+
pub block_headers: Vec<Vec<u8>>,
190+
}

0 commit comments

Comments
 (0)