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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use super::input::{Input, UnsignedInput};
#[cfg(feature = "json")]
use super::json;
use super::DataInput;
use super::{
super::{digest32::blake2b256_hash, ergo_box::ErgoBoxCandidate},
Transaction, TxId,
};
#[cfg(feature = "json")]
use crate::chain::transaction::ErgoBox;
#[cfg(feature = "json")]
use crate::chain::transaction::TransactionFromJsonError;
use crate::serialization::SigmaSerializable;
use crate::sigma_protocol::prover::{ProofBytes, ProverResult};
#[cfg(feature = "json")]
use core::convert::TryFrom;
#[cfg(feature = "json")]
use serde::{Deserialize, Serialize};
#[cfg_attr(feature = "json", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "json",
serde(
try_from = "json::transaction::UnsignedTransactionJson",
into = "json::transaction::UnsignedTransactionJson"
)
)]
#[derive(PartialEq, Debug, Clone)]
pub struct UnsignedTransaction {
tx_id: TxId,
pub inputs: Vec<UnsignedInput>,
pub data_inputs: Vec<DataInput>,
pub output_candidates: Vec<ErgoBoxCandidate>,
}
impl UnsignedTransaction {
pub fn new(
inputs: Vec<UnsignedInput>,
data_inputs: Vec<DataInput>,
output_candidates: Vec<ErgoBoxCandidate>,
) -> UnsignedTransaction {
let tx_to_sign = UnsignedTransaction {
tx_id: TxId::zero(),
inputs,
data_inputs,
output_candidates,
};
let tx_id = tx_to_sign.calc_tx_id();
UnsignedTransaction {
tx_id,
..tx_to_sign
}
}
fn calc_tx_id(&self) -> TxId {
let bytes = self.bytes_to_sign();
TxId(blake2b256_hash(&bytes))
}
pub fn id(&self) -> TxId {
self.tx_id.clone()
}
pub fn bytes_to_sign(&self) -> Vec<u8> {
let empty_proofs_input = self
.inputs
.iter()
.map(|ui| Input {
box_id: ui.box_id.clone(),
spending_proof: ProverResult {
proof: ProofBytes::Empty,
extension: ui.extension.clone(),
},
})
.collect();
let tx = Transaction::new(
empty_proofs_input,
self.data_inputs.clone(),
self.output_candidates.clone(),
);
tx.sigma_serialize_bytes()
}
}
#[cfg(feature = "json")]
impl TryFrom<json::transaction::UnsignedTransactionJson> for UnsignedTransaction {
type Error = TransactionFromJsonError;
fn try_from(
tx_json: json::transaction::UnsignedTransactionJson,
) -> std::result::Result<Self, Self::Error> {
let output_candidates = tx_json.outputs.iter().map(|o| o.clone().into()).collect();
let tx_to_sign = UnsignedTransaction {
tx_id: TxId::zero(),
inputs: tx_json.inputs,
data_inputs: tx_json.data_inputs,
output_candidates,
};
let tx_id = tx_to_sign.calc_tx_id();
let tx = UnsignedTransaction {
tx_id,
..tx_to_sign
};
if tx.tx_id == tx_json.tx_id {
Ok(tx)
} else {
Err(TransactionFromJsonError::InvalidTxId)
}
}
}
#[cfg(feature = "json")]
impl From<UnsignedTransaction> for json::transaction::UnsignedTransactionJson {
fn from(t: UnsignedTransaction) -> Self {
json::transaction::UnsignedTransactionJson {
tx_id: t.tx_id.clone(),
inputs: t.inputs.clone(),
data_inputs: t.data_inputs.clone(),
outputs: t
.output_candidates
.iter()
.enumerate()
.map(|(idx, c)| ErgoBox::from_box_candidate(c, t.tx_id.clone(), idx as u16))
.collect(),
}
}
}
#[cfg(test)]
pub mod tests {
use super::*;
use proptest::prelude::*;
use proptest::{arbitrary::Arbitrary, collection::vec};
impl Arbitrary for UnsignedTransaction {
type Parameters = ();
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
(
vec(any::<UnsignedInput>(), 1..10),
vec(any::<DataInput>(), 0..10),
vec(any::<ErgoBoxCandidate>(), 1..10),
)
.prop_map(|(inputs, data_inputs, outputs)| Self::new(inputs, data_inputs, outputs))
.boxed()
}
type Strategy = BoxedStrategy<Self>;
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(16))]
#[test]
fn test_unsigned_tx_bytes_to_sign(v in any::<UnsignedTransaction>()) {
prop_assert!(!v.bytes_to_sign().is_empty());
}
}
}