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
use crate::{Api, Client, Error, Result};
use bee_message::prelude::{TransactionId, UtxoInput};
use bee_rest_api::types::{
body::SuccessBody,
responses::{BalanceAddressResponse, OutputsAddressResponse},
};
use std::convert::TryInto;
const OUTPUT_ID_LENGTH: usize = 68;
const TRANSACTION_ID_LENGTH: usize = 64;
#[derive(Clone, Debug, Serialize, Deserialize)]
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, Serialize, Deserialize)]
pub struct OutputsOptions {
#[serde(rename = "includeSpent")]
pub include_spent: bool,
#[serde(rename = "outputType")]
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);
let resp: SuccessBody<BalanceAddressResponse> = 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);
let resp: SuccessBody<OutputsAddressResponse> = 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]>>>()
}
pub async fn outputs_response(self, address: &str, options: OutputsOptions) -> Result<OutputsAddressResponse> {
let path = format!("api/v1/addresses/{}/outputs", address);
let resp: SuccessBody<OutputsAddressResponse> = self
.client
.node_manager
.get_request(
&path,
options.into_query().as_deref(),
self.client.get_timeout(Api::GetOutput),
)
.await?;
Ok(resp.data)
}
}