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
use chrono::prelude::*;
use reqwest::r#async::{Client, Response};
use reqwest::Error;
use tokio::prelude::*;
use iota_conversion::Trinary;
use iota_model::*;
use iota_pow::{PearlDiver, PowOptions};
use iota_validation::input_validator;
use crate::Result;
use super::responses::AttachToTangleResponse;
use std::convert::TryInto;
lazy_static! {
pub static ref MAX_TIMESTAMP_VALUE: i64 = (3_i64.pow(27) - 1) / 2;
}
#[derive(Clone, Debug)]
pub struct AttachOptions<'a, 'b> {
pub threads: usize,
pub trunk_transaction: &'a str,
pub branch_transaction: &'b str,
pub min_weight_magnitude: usize,
pub trytes: Vec<String>,
}
impl<'a, 'b> Default for AttachOptions<'a, 'b> {
fn default() -> Self {
AttachOptions {
threads: num_cpus::get(),
trunk_transaction: "",
branch_transaction: "",
min_weight_magnitude: 14,
trytes: vec![],
}
}
}
pub fn attach_to_tangle(
client: &Client,
uri: &str,
options: AttachOptions,
) -> impl Future<Item = Response, Error = Error> {
let body = json!({
"command": "attachToTangle",
"trunkTransaction": options.trunk_transaction,
"branchTransaction": options.branch_transaction,
"minWeightMagnitude": options.min_weight_magnitude,
"trytes": options.trytes,
});
client
.post(uri)
.header("ContentType", "application/json")
.header("X-IOTA-API-Version", "1")
.body(body.to_string())
.send()
}
pub fn attach_to_tangle_local(options: AttachOptions) -> Result<AttachToTangleResponse> {
ensure!(
input_validator::is_hash(&options.trunk_transaction),
"Provided trunk transaction is not valid: {:?}",
options.trunk_transaction
);
ensure!(
input_validator::is_hash(&options.branch_transaction),
"Provided branch transaction is not valid: {:?}",
options.branch_transaction
);
ensure!(
input_validator::is_array_of_trytes(&options.trytes),
"Provided trytes are not valid: {:?}",
options.trytes
);
let mut result_trytes: Vec<String> = Vec::with_capacity(options.trytes.len());
let mut previous_transaction = String::new();
for i in 0..options.trytes.len() {
let mut tx: Transaction = options.trytes[i].parse()?;
tx.trunk_transaction = if previous_transaction.is_empty() {
options.trunk_transaction.into()
} else {
previous_transaction.clone()
};
tx.branch_transaction = if previous_transaction.is_empty() {
options.branch_transaction
} else {
options.trunk_transaction
}
.into();
if tx.tag.is_empty() || tx.tag == "9".repeat(27) {
tx.tag = tx.obsolete_tag.clone();
}
tx.attachment_timestamp = Utc::now().timestamp_millis();
tx.attachment_timestamp_lower_bound = 0;
tx.attachment_timestamp_upper_bound = *MAX_TIMESTAMP_VALUE;
let tx_trytes: String = tx.try_into()?;
let tx_trits = tx_trytes.trits();
let result_trits = PearlDiver::default().search(
tx_trits,
PowOptions {
min_weight_magnitude: options.min_weight_magnitude,
..PowOptions::default()
},
)?;
result_trytes.push(result_trits.trytes()?);
previous_transaction = result_trytes[i].parse::<Transaction>()?.hash.into();
}
result_trytes.reverse();
Ok(AttachToTangleResponse::new(
None,
None,
None,
Some(result_trytes),
))
}