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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
pub mod v10;
pub mod v10_gen;
use crate::*;
pub use v10::{
TransactionDocumentV10, TransactionDocumentV10Builder, TransactionDocumentV10Stringified,
TransactionInputUnlocksV10, TransactionInputV10, TransactionOutputV10,
UnsignedTransactionDocumentV10,
};
#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
pub struct UTXOConditions {
pub origin_str: Option<String>,
pub script: WalletScriptV10,
}
impl From<WalletScriptV10> for UTXOConditions {
fn from(script: WalletScriptV10) -> Self {
UTXOConditions {
origin_str: None,
script,
}
}
}
impl UTXOConditions {
pub fn reduce(&mut self) {
if self.check() {
self.origin_str = None;
}
}
pub fn check(&self) -> bool {
if let Some(ref origin_str) = self.origin_str {
origin_str == self.script.to_string().as_str()
} else {
true
}
}
}
impl ToString for UTXOConditions {
fn to_string(&self) -> String {
if let Some(ref origin_str) = self.origin_str {
origin_str.to_string()
} else {
self.script.to_string()
}
}
}
#[derive(Debug, Error)]
pub enum TxVerifyErr {
#[error("Transaction must have at least one input")]
NoInput,
#[error("Transaction must have at least one issuer")]
NoIssuer,
#[error("Transaction must have at least one output")]
NoOutput,
#[error("Not same amount of inputs and unlocks: found {0} inputs and {1} unlocks.")]
NotSameAmountOfInputsAndUnlocks(usize, usize),
#[error("Not same sum of inputs amount and outputs amount: ({0}, {1}).")]
NotSameSumOfInputsAmountAndOutputsAmount(SourceAmount, SourceAmount),
#[error("{0}")]
Sigs(DocumentSigsErr),
#[error("Transaction too long: found {found} lines (max {max}).")]
TooManyLines { found: usize, max: usize },
#[error("Wrong currency: expected \'{expected}\' found \'{found}\'.")]
WrongCurrency { expected: String, found: String },
}
#[derive(Clone, Copy, Debug, Error)]
pub enum GenTxError {
#[error("Too Many signers or recipients.")]
TooManySignersOrRecipients,
}
pub trait UnsignedTransactionDocumentTrait<'a>: Sized {
type PubKey: PublicKey;
type SignedDoc: TransactionDocumentTrait<'a>;
fn as_text(&self) -> &str;
fn sign<S: Signator<PublicKey = Self::PubKey>>(
self,
signator: &S,
) -> Result<SignedOrUnsignedDocument<Self::SignedDoc, Self>, TransactionSignErr>;
}
pub trait TransactionDocumentTrait<'a>: Sized {
type Address;
type Input: 'a;
type Inputs: AsRef<[Self::Input]>;
type InputUnlocks: 'a;
type InputsUnlocks: AsRef<[Self::InputUnlocks]>;
type Output: 'a;
type Outputs: AsRef<[Self::Output]>;
type PubKey: PublicKey;
type UnsignedDoc: UnsignedTransactionDocumentTrait<'a>;
fn generate_simple_txs(
blockstamp: Blockstamp,
currency: String,
inputs_with_sum: (Vec<Self::Input>, SourceAmount),
issuer: Self::PubKey,
recipient: WalletScriptV10,
user_amount_and_comment: (SourceAmount, String),
cash_back_pubkey: Option<Self::PubKey>,
) -> Vec<Self::UnsignedDoc>;
fn get_inputs(&'a self) -> Self::Inputs;
fn get_inputs_unlocks(&'a self) -> Self::InputsUnlocks;
fn get_outputs(&'a self) -> Self::Outputs;
fn verify(&self, expected_currency: Option<&str>) -> Result<(), TxVerifyErr>;
}
#[derive(Debug, Error, Eq, PartialEq)]
pub enum TransactionSignErr {
#[error("The signator's public key is not in the transaction's issuers")]
InvalidSignator,
#[error("Signatures don\'t match: {0:?}")]
InvalidSignatures(HashMap<usize, SigError>),
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub enum TransactionDocument {
V10(TransactionDocumentV10),
}
#[derive(Clone, Debug, Deserialize, Hash, Serialize, PartialEq, Eq)]
pub enum TransactionDocumentStringified {
V10(TransactionDocumentV10Stringified),
}
impl ToStringObject for TransactionDocument {
type StringObject = TransactionDocumentStringified;
fn to_string_object(&self) -> TransactionDocumentStringified {
match self {
TransactionDocument::V10(tx_v10) => {
TransactionDocumentStringified::V10(tx_v10.to_string_object())
}
}
}
}
impl TransactionDocument {
pub fn compute_hash(&self) -> Hash {
match self {
TransactionDocument::V10(tx_v10) => tx_v10.compute_hash(),
}
}
pub fn get_hash_opt(&self) -> Option<Hash> {
match self {
TransactionDocument::V10(tx_v10) => tx_v10.get_hash_opt(),
}
}
pub fn get_hash(&mut self) -> Hash {
match self {
TransactionDocument::V10(tx_v10) => tx_v10.get_hash(),
}
}
pub fn reduce(&mut self) {
match self {
TransactionDocument::V10(tx_v10) => tx_v10.reduce(),
};
}
}
impl Document for TransactionDocument {
type PublicKey = PubKeyEnum;
fn version(&self) -> usize {
match self {
TransactionDocument::V10(tx_v10) => tx_v10.version(),
}
}
fn currency(&self) -> &str {
match self {
TransactionDocument::V10(tx_v10) => tx_v10.currency(),
}
}
fn blockstamp(&self) -> Blockstamp {
match self {
TransactionDocument::V10(tx_v10) => tx_v10.blockstamp(),
}
}
fn issuers(&self) -> SmallVec<[Self::PublicKey; 1]> {
match self {
TransactionDocument::V10(tx_v10) => svec![PubKeyEnum::Ed25519(tx_v10.issuers()[0])],
}
}
fn signatures(&self) -> SmallVec<[<Self::PublicKey as PublicKey>::Signature; 1]> {
match self {
TransactionDocument::V10(tx_v10) => svec![Sig::Ed25519(tx_v10.signatures()[0])],
}
}
fn as_bytes(&self) -> BeefCow<[u8]> {
match self {
TransactionDocument::V10(tx_v10) => tx_v10.as_bytes(),
}
}
}
impl CompactTextDocument for TransactionDocument {
fn as_compact_text(&self) -> String {
match self {
TransactionDocument::V10(tx_v10) => tx_v10.as_compact_text(),
}
}
}
impl TextDocument for TransactionDocument {
type CompactTextDocument_ = TransactionDocument;
fn as_text(&self) -> &str {
match self {
TransactionDocument::V10(tx_v10) => tx_v10.as_text(),
}
}
fn to_compact_document(&self) -> Cow<Self::CompactTextDocument_> {
Cow::Borrowed(self)
}
}
#[derive(Debug, Clone)]
pub enum TransactionDocumentBuilder<'a> {
V10(TransactionDocumentV10Builder<'a>),
}
impl<'a> TextDocumentBuilder for TransactionDocumentBuilder<'a> {
type Document = TransactionDocument;
type Signator = SignatorEnum;
fn build_with_text_and_sigs(
self,
text: String,
signatures: SmallVec<
[<<Self::Document as Document>::PublicKey as PublicKey>::Signature; 1],
>,
) -> TransactionDocument {
match self {
TransactionDocumentBuilder::V10(tx_v10_builder) => TransactionDocument::V10(
tx_v10_builder.build_with_text_and_sigs(
text,
signatures
.into_iter()
.filter_map(|sig| {
if let Sig::Ed25519(sig) = sig {
Some(sig)
} else {
None
}
})
.collect(),
),
),
}
}
fn generate_text(&self) -> String {
match self {
TransactionDocumentBuilder::V10(tx_v10_builder) => tx_v10_builder.generate_text(),
}
}
}
#[cfg(test)]
pub(super) mod tests {
use super::*;
use smallvec::smallvec;
use std::str::FromStr;
use unwrap::unwrap;
use v10::{TransactionInputUnlocksV10, TransactionOutputV10};
pub(super) fn tx_output_v10(amount: i64, recv: &str) -> TransactionOutputV10 {
TransactionOutputV10 {
amount: SourceAmount::with_base0(amount),
conditions: UTXOConditions::from(WalletScriptV10::single(WalletConditionV10::Sig(
unwrap!(ed25519::PublicKey::from_base58(recv)),
))),
}
}
#[test]
fn generate_real_document() {
let keypair = ed25519::KeyPairFromSeed32Generator::generate(unwrap!(
Seed32::from_base58("DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV"),
"Fail to parse Seed32"
));
let pubkey = keypair.public_key();
let signator = keypair.generate_signator();
let sig = unwrap!(ed25519::Signature::from_base64(
"cq86RugQlqAEyS8zFkB9o0PlWPSb+a6D/MEnLe8j+okyFYf/WzI6pFiBkQ9PSOVn5I0dwzVXg7Q4N1apMWeGAg==",
), "Fail to parse Signature");
let blockstamp = unwrap!(
Blockstamp::from_str(
"0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855",
),
"Fail to parse blockstamp"
);
let builder = TransactionDocumentV10Builder {
currency: "duniter_unit_test_currency",
blockstamp,
locktime: 0,
issuers: svec![pubkey],
inputs: &[TransactionInputV10 {
amount: SourceAmount::with_base0(10),
id: SourceIdV10::Ud(UdSourceIdV10 {
issuer: unwrap!(
ed25519::PublicKey::from_base58(
"DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV"
),
"Fail to parse PublicKey"
),
block_number: BlockNumber(0),
}),
}],
unlocks: &[TransactionInputUnlocksV10 {
index: 0,
unlocks: svec![WalletUnlockProofV10::Sig(0)],
}],
outputs: smallvec![tx_output_v10(
10,
"FD9wujR7KABw88RyKEGBYRLz8PA6jzVCbcBAsrBXBqSa",
)],
comment: "test",
hash: None,
};
assert!(builder
.clone()
.build_with_signature(svec![sig])
.verify_signatures()
.is_ok());
assert!(builder
.build_and_sign(vec![signator])
.verify_signatures()
.is_ok());
}
#[test]
fn compute_transaction_hash() {
let pubkey = unwrap!(
ed25519::PublicKey::from_base58("FEkbc4BfJukSWnCU6Hed6dgwwTuPFTVdgz5LpL4iHr9J"),
"Fail to parse PublicKey"
);
let sig = unwrap!(ed25519::Signature::from_base64(
"XEwKwKF8AI1gWPT7elR4IN+bW3Qn02Dk15TEgrKtY/S2qfZsNaodsLofqHLI24BBwZ5aadpC88ntmjo/UW9oDQ==",
), "Fail to parse Signature");
let blockstamp = unwrap!(
Blockstamp::from_str(
"60-00001FE00410FCD5991EDD18AA7DDF15F4C8393A64FA92A1DB1C1CA2E220128D",
),
"Fail to parse Blockstamp"
);
let builder = TransactionDocumentV10Builder {
currency: "g1",
blockstamp,
locktime: 0,
issuers: svec![pubkey],
inputs: &[TransactionInputV10 {
amount: SourceAmount::with_base0(950),
id: SourceIdV10::Utxo(UtxoIdV10 {
tx_hash: unwrap!(Hash::from_hex(
"2CF1ACD8FE8DC93EE39A1D55881C50D87C55892AE8E4DB71D4EBAB3D412AA8FD"
)),
output_index: 1,
}),
}],
unlocks: &[TransactionInputUnlocksV10::default()],
outputs: smallvec![
tx_output_v10(30, "38MEAZN68Pz1DTvT3tqgxx4yQP6snJCQhPqEFxbDk4aE"),
tx_output_v10(920, "FEkbc4BfJukSWnCU6Hed6dgwwTuPFTVdgz5LpL4iHr9J"),
],
comment: "Pour cesium merci",
hash: None,
};
let mut tx_doc = TransactionDocument::V10(builder.build_with_signature(svec![sig]));
assert!(tx_doc.verify_signatures().is_ok());
assert!(tx_doc.get_hash_opt().is_none());
assert_eq!(
tx_doc.get_hash(),
unwrap!(Hash::from_hex(
"876D2430E0B66E2CE4467866D8F923D68896CACD6AA49CDD8BDD0096B834DEF1"
))
);
}
}