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
use crate::{Api, Client, Error, Result};
use bee_message::prelude::{TransactionId, UtxoInput};
use bee_rest_api::types::responses::{BalanceAddressResponse, OutputsAddressResponse};
use std::convert::TryInto;
const OUTPUT_ID_LENGTH: usize = 68;
const TRANSACTION_ID_LENGTH: usize = 64;
#[derive(Clone, Debug)]
pub enum OutputType {
SignatureLockedSingle,
SignatureLockedDustAllowance,
}
impl From<OutputType> for u16 {
fn from(value: OutputType) -> Self {
match value {
OutputType::SignatureLockedSingle => 0,
OutputType::SignatureLockedDustAllowance => 1,
}
}
}
#[derive(Default, Clone)]
pub struct OutputsOptions {
pub include_spent: bool,
pub output_type: Option<OutputType>,
}
impl OutputsOptions {
fn into_query(self) -> Option<String> {
let mut params = Vec::new();
if self.include_spent {
params.push("include-spent=true".to_string());
}
if let Some(output_type) = self.output_type {
params.push(format!("type={}", u16::from(output_type)))
}
if params.is_empty() {
None
} else {
Some(params.join("&"))
}
}
}
pub struct GetAddressBuilder<'a> {
client: &'a Client,
}
impl<'a> GetAddressBuilder<'a> {
pub fn new(client: &'a Client) -> Self {
Self { client }
}
pub async fn balance(self, address: &str) -> Result<BalanceAddressResponse> {
let path = &format!("api/v1/addresses/{}", address);
#[derive(Debug, Serialize, Deserialize)]
struct ResponseWrapper {
data: BalanceAddressResponse,
}
let resp: ResponseWrapper = self
.client
.node_manager
.get_request(path, None, self.client.get_timeout(Api::GetBalance))
.await?;
Ok(resp.data)
}
pub async fn outputs(self, address: &str, options: OutputsOptions) -> Result<Box<[UtxoInput]>> {
let path = format!("api/v1/addresses/{}/outputs", address);
#[derive(Debug, Serialize, Deserialize)]
struct ResponseWrapper {
data: OutputsAddressResponse,
}
let resp: ResponseWrapper = self
.client
.node_manager
.get_request(
&path,
options.into_query().as_deref(),
self.client.get_timeout(Api::GetOutput),
)
.await?;
resp.data
.output_ids
.iter()
.map(|s| {
if s.len() == OUTPUT_ID_LENGTH {
let mut transaction_id = [0u8; 32];
hex::decode_to_slice(&s[..TRANSACTION_ID_LENGTH], &mut transaction_id)?;
let index = u16::from_le_bytes(
hex::decode(&s[TRANSACTION_ID_LENGTH..]).map_err(|_| Error::InvalidParameter("index"))?[..]
.try_into()
.map_err(|_| Error::InvalidParameter("index"))?,
);
Ok(UtxoInput::new(TransactionId::new(transaction_id), index)?)
} else {
Err(Error::OutputError("Invalid output length from API response"))
}
})
.collect::<Result<Box<[UtxoInput]>>>()
}
}