indy-vdr 0.3.0

A library for interacting with Hyperledger Indy Node, a distributed ledger for self-sovereign identity (https://www.hyperledger.org/use/hyperledger-indy).
Documentation
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
use std::collections::HashMap;
use std::convert::TryFrom;
use std::fs::File;
use std::io::BufReader;
use std::iter::IntoIterator;
use std::path::Path;

use serde_json::{self, Deserializer, Value as SJsonValue};

use super::types::{
    NodeTransaction, NodeTransactionV0, NodeTransactionV1, ProtocolVersion, VerifierInfo,
    VerifierKey, Verifiers,
};
use crate::common::error::prelude::*;
use crate::common::merkle_tree::MerkleTree;
use crate::utils::{
    base58,
    keys::{EncodedVerKey, KeyEncoding, KeyType},
};

pub type NodeTransactionMap = HashMap<String, NodeTransactionV1>;

/// A collection of pool genesis transactions.
#[derive(Clone, PartialEq, Eq)]
pub struct PoolTransactions {
    inner: Vec<Vec<u8>>, // stored in msgpack format
}

impl PoolTransactions {
    /// Create a new blank set of transactions.
    fn new(inner: Vec<Vec<u8>>) -> Self {
        Self { inner }
    }

    /// Load JSON pool transactions from a string.
    pub fn from_json(txns: &str) -> VdrResult<Self> {
        let stream = Deserializer::from_str(txns).into_iter::<SJsonValue>();
        let txns = _json_iter_to_msgpack(stream)?;
        if txns.is_empty() {
            Err(input_err("No genesis transactions found"))
        } else {
            Ok(Self::new(txns))
        }
    }

    /// Load JSON pool transactions from a file.
    pub fn from_json_file<P>(file_name: P) -> VdrResult<Self>
    where
        P: AsRef<Path> + std::fmt::Debug,
    {
        let f = File::open(&file_name).map_err(|err| {
            err_msg(
                VdrErrorKind::FileSystem(err),
                format!("Can't open genesis transactions file: {:?}", file_name),
            )
        })?;
        let reader = BufReader::new(&f);
        let stream = Deserializer::from_reader(reader).into_iter::<SJsonValue>();
        let txns = _json_iter_to_msgpack(stream)?;
        if txns.is_empty() {
            Err(input_err("No genesis transactions found"))
        } else {
            Ok(Self::new(txns))
        }
    }

    /// Load pool transactions from a sequence of JSON strings.
    pub fn from_json_transactions<T>(txns: T) -> VdrResult<Self>
    where
        T: IntoIterator,
        T::Item: AsRef<str>,
    {
        let mut pt = Self { inner: vec![] };
        pt.extend_from_json(txns)?;
        Ok(pt)
    }

    /// Load pool transactions from a sequence of msgpack-encoded byte strings.
    pub fn from_transactions<T>(txns: T) -> Self
    where
        T: IntoIterator,
        T::Item: AsRef<[u8]>,
    {
        Self::new(txns.into_iter().map(|t| t.as_ref().to_vec()).collect())
    }

    /// Extend the pool transactions with a set of msgpack-encoded byte strings.
    pub fn extend<T>(&mut self, txns: T)
    where
        T: IntoIterator<Item = Vec<u8>>,
    {
        self.inner.extend(txns)
    }

    /// Extend the pool transactions with a set of JSON strings.
    pub fn extend_from_json<'a, T>(&mut self, txns: T) -> VdrResult<()>
    where
        T: IntoIterator,
        T::Item: AsRef<str>,
    {
        for txn in txns {
            let txn = serde_json::from_str::<SJsonValue>(txn.as_ref())
                .with_input_err("Error deserializing transaction as JSON")?;
            self.inner.push(_json_to_msgpack(&txn)?);
        }
        Ok(())
    }

    /// Derive a `MerkleTree` instance from the set of pool transactions.
    pub fn merkle_tree(&self) -> VdrResult<MerkleTree> {
        Ok(MerkleTree::from_vec(self.inner.clone())?)
    }

    /// Convert the set of pool transactions into a `MerkleTree` instance.
    pub fn into_merkle_tree(self) -> VdrResult<MerkleTree> {
        Ok(MerkleTree::from_vec(self.inner)?)
    }

    /// Iterate the set of transactions as a sequence of msgpack-encoded byte strings.
    pub fn iter(&self) -> impl Iterator<Item = &Vec<u8>> {
        self.inner.iter()
    }

    /// Get a sequence of JSON strings representing the pool transactions.
    pub fn encode_json(&self) -> VdrResult<Vec<String>> {
        Ok(self
            .json_values()?
            .into_iter()
            .map(|v| v.to_string())
            .collect())
    }

    /// Get a sequence of `serde_json::Value` instances representing the pool transactions.
    pub fn json_values(&self) -> VdrResult<Vec<SJsonValue>> {
        self.inner.iter().try_fold(vec![], |mut vec, txn| {
            let value = rmp_serde::decode::from_slice(txn)
                .with_input_err("Genesis transaction cannot be decoded")?;
            vec.push(value);
            Ok(vec)
        })
    }

    /// Get the number of pool transactions.
    pub fn len(&self) -> usize {
        self.inner.len()
    }
}

impl std::fmt::Debug for PoolTransactions {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "PoolTransactions(len={})", self.len())
    }
}

impl std::fmt::Display for PoolTransactions {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let vec_json = unwrap_or_return!(self.encode_json(), Err(std::fmt::Error {}));
        let txns = SJsonValue::from(vec_json);
        write!(f, "{}", txns)
    }
}

impl From<Vec<Vec<u8>>> for PoolTransactions {
    fn from(txns: Vec<Vec<u8>>) -> Self {
        Self::new(txns)
    }
}

impl From<&MerkleTree> for PoolTransactions {
    fn from(merkle_tree: &MerkleTree) -> Self {
        Self::from_transactions(merkle_tree)
    }
}

impl TryFrom<&[String]> for PoolTransactions {
    type Error = VdrError;

    fn try_from(txns: &[String]) -> VdrResult<Self> {
        PoolTransactions::from_json_transactions(txns)
    }
}

fn _json_to_msgpack(txn: &SJsonValue) -> VdrResult<Vec<u8>> {
    if let Some(txn) = txn.as_object() {
        let mp_txn = rmp_serde::encode::to_vec_named(txn)
            .with_input_err("Can't encode genesis txn as msgpack")?;
        Ok(mp_txn)
    } else {
        Err(input_err("Unexpected value, not a JSON object"))
    }
}

fn _json_iter_to_msgpack<T>(mut iter: T) -> VdrResult<Vec<Vec<u8>>>
where
    T: Iterator<Item = Result<SJsonValue, serde_json::Error>>,
{
    iter.try_fold(vec![], |mut vec, txn| match txn {
        Ok(txn) => {
            let mp_txn = _json_to_msgpack(&txn)?;
            vec.push(mp_txn);
            Ok(vec)
        }
        Err(err) => Err(input_err(format!(
            "Error parsing genesis transactions: {}",
            err
        ))),
    })
}

pub fn build_node_transaction_map<T>(
    txns: T,
    protocol_version: ProtocolVersion,
) -> VdrResult<NodeTransactionMap>
where
    T: IntoIterator,
    T::Item: AsRef<[u8]>,
{
    txns.into_iter().try_fold(HashMap::new(), |mut map, txn| {
        let mut node_txn = _decode_transaction(txn.as_ref(), protocol_version)?;
        let dest = node_txn.txn.data.dest.clone();
        let exist: Option<&mut NodeTransactionV1> = map.get_mut(&dest);
        if exist.is_some() {
            exist.unwrap().update(&mut node_txn)?;
        } else {
            map.insert(dest, node_txn);
        }
        Ok(map)
    })
}

pub fn build_verifiers(txn_map: NodeTransactionMap) -> VdrResult<Verifiers> {
    Ok(txn_map
        .into_iter()
        .map(|(public_key, txn)| {
            let node_alias = txn.txn.data.data.alias.clone();

            if txn.txn.data.data.services.is_none()
                || !txn
                    .txn
                    .data
                    .data
                    .services
                    .as_ref()
                    .unwrap()
                    .contains(&"VALIDATOR".to_string())
            {
                return Err(input_err(format!(
                    "Node '{}' is not a validator",
                    node_alias
                )));
            }

            let verkey = EncodedVerKey::new(
                &public_key,
                Some(KeyType::ED25519),
                Some(KeyEncoding::BASE58),
            )
            .decode()
            .map_input_err(|| {
                format!(
                    "Node '{}' has invalid field 'dest': failed parsing base58",
                    node_alias
                )
            })?;

            let enc_key = verkey
                .key_exchange()
                .map_input_err(|| {
                    format!(
                        "Node '{}' has invalid field 'dest': key not accepted",
                        node_alias
                    )
                })?
                .key_bytes()
                .to_vec();

            let client_addr = match (&txn.txn.data.data.client_ip, &txn.txn.data.data.client_port) {
                (&Some(ref client_ip), &Some(ref client_port)) => {
                    format!("tcp://{}:{}", client_ip, client_port)
                }
                _ => {
                    return Err(input_err(format!(
                        "Node '{}' has no client address",
                        node_alias
                    )))
                }
            };

            let node_addr = match (&txn.txn.data.data.node_ip, &txn.txn.data.data.node_port) {
                (&Some(ref node_ip), &Some(ref node_port)) => {
                    format!("tcp://{}:{}", node_ip, node_port)
                }
                _ => {
                    return Err(input_err(format!(
                        "Node '{}' has no node address",
                        node_alias
                    )))
                }
            };

            let bls_key =
                match txn.txn.data.data.blskey {
                    Some(ref blskey) => {
                        let key = base58::decode(blskey.as_str()).map_input_err(|| {
                            format!("Node '{}': invalid base58 in field blskey", node_alias)
                        })?;
                        Some(VerifierKey::from_bytes(&key).map_input_err(|| {
                            format!("Node '{}': invalid field blskey", node_alias)
                        })?)
                    }
                    None => None,
                };

            let info = VerifierInfo {
                client_addr,
                node_addr,
                bls_key,
                public_key,
                enc_key,
            };

            Ok((node_alias, info))
        })
        .fold(HashMap::new(), |mut map, res| {
            match res {
                Err(err) => {
                    info!("Skipped: {}", err);
                }
                Ok((alias, info)) => {
                    map.insert(alias, info);
                }
            }
            map
        })
        .into())
}

fn _decode_transaction(
    gen_txn: &[u8],
    protocol_version: ProtocolVersion,
) -> VdrResult<NodeTransactionV1> {
    let gen_txn: NodeTransaction = rmp_serde::decode::from_slice(gen_txn)
        .with_input_err("Genesis transaction cannot be decoded")?;

    match gen_txn {
        NodeTransaction::NodeTransactionV0(txn) => {
            if protocol_version != 1 {
                Err(err_msg(VdrErrorKind::Incompatible,
                            format!("Libindy PROTOCOL_VERSION is {} but Pool Genesis Transactions are of version {}.\
                                           Call indy_set_protocol_version(1) to set correct PROTOCOL_VERSION",
                                    protocol_version, NodeTransactionV0::VERSION)))
            } else {
                Ok(NodeTransactionV1::from(txn))
            }
        }
        NodeTransaction::NodeTransactionV1(txn) => {
            if protocol_version != 2 {
                Err(err_msg(VdrErrorKind::Incompatible,
                            format!("Libindy PROTOCOL_VERSION is {} but Pool Genesis Transactions are of version {}.\
                                           Call indy_set_protocol_version(2) to set correct PROTOCOL_VERSION",
                                    protocol_version, NodeTransactionV1::VERSION)))
            } else {
                Ok(txn)
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    mod pool_transactions_tests {
        use super::*;
        use indy_test_utils::genesis::GenesisTransactions;

        pub fn _transactions() -> Vec<String> {
            GenesisTransactions::new(Some(4)).transactions.clone()
        }

        pub fn _merkle_tree() -> MerkleTree {
            PoolTransactions::from_json_transactions(&_transactions())
                .unwrap()
                .merkle_tree()
                .unwrap()
        }

        #[test]
        fn test_pool_transactions_from_transactions_json_works() {
            let transaction = GenesisTransactions::new(None);

            let transactions: PoolTransactions =
                PoolTransactions::from_json_transactions(&transaction.transactions).unwrap();

            assert_eq!(
                transactions.encode_json().unwrap(),
                GenesisTransactions::default_transactions()
            )
        }

        #[test]
        fn test_pool_transactions_from_file_works() {
            let mut transaction = GenesisTransactions::new(None);
            let file = transaction.store_to_file();

            let transactions: PoolTransactions = PoolTransactions::from_json_file(&file).unwrap();

            assert_eq!(
                transactions.encode_json().unwrap(),
                GenesisTransactions::default_transactions()
            )
        }

        #[test]
        fn test_pool_transactions_from_file_for_unknown_file() {
            let file = {
                let mut transaction = GenesisTransactions::new(None);
                let file = transaction.store_to_file();

                file.clone()
            };

            let _err = PoolTransactions::from_json_file(&file).unwrap_err();
        }

        #[test]
        fn test_pool_transactions_from_file_for_invalid_transactions() {
            let mut txns = GenesisTransactions::from_transactions(&[r#"{invalid}"#]);
            let _err = PoolTransactions::from_json_file(txns.store_to_file()).unwrap_err();
        }

        #[test]
        fn test_merkle_tree_from_transactions_works() {
            let merkle_tree = _merkle_tree();

            assert_eq!(merkle_tree.count(), 4, "test restored MT size");
            assert_eq!(
                merkle_tree.root_hash_hex(),
                "ef25b5d33e511d2b8e3fbf267cc4496a77cf522976d5ac158878f787190d9a97",
                "test restored MT root hash"
            );
        }
    }

    mod build_node_transaction_map_tests {
        use super::*;
        use crate::pool::genesis::tests::pool_transactions_tests::{_merkle_tree, _transactions};

        pub const NODE1_OLD: &str = r#"{"data":{"alias":"Node1","client_ip":"192.168.1.35","client_port":9702,"node_ip":"192.168.1.35","node_port":9701,"services":["VALIDATOR"]},"dest":"Gw6pDLhcBcoQesN72qfotTgFa7cbuqZpkX3Xo6pLhPhv","identifier":"FYmoFw55GeQH7SRFa37dkx1d2dZ3zUF8ckg7wmL7ofN4","txnId":"fea82e10e894419fe2bea7d96296a6d46f50f93f9eeda954ec461b2ed2950b62","type":"0"}"#;
        pub const NODE2_OLD: &str = r#"{"data":{"alias":"Node2","client_ip":"192.168.1.35","client_port":9704,"node_ip":"192.168.1.35","node_port":9703,"services":["VALIDATOR"]},"dest":"8ECVSk179mjsjKRLWiQtssMLgp6EPhWXtaYyStWPSGAb","identifier":"8QhFxKxyaFsJy4CyxeYX34dFH8oWqyBv1P4HLQCsoeLy","txnId":"1ac8aece2a18ced660fef8694b61aac3af08ba875ce3026a160acbc3a3af35fc","type":"0"}"#;

        #[test]
        fn test_build_node_transaction_map_works_for_node_1_4_and_protocol_version_1_4() {
            let txn_map =
                build_node_transaction_map(_merkle_tree(), ProtocolVersion::Node1_4).unwrap();

            assert_eq!(4, txn_map.len());
            assert!(txn_map.contains_key("Gw6pDLhcBcoQesN72qfotTgFa7cbuqZpkX3Xo6pLhPhv"));
            assert!(txn_map.contains_key("8ECVSk179mjsjKRLWiQtssMLgp6EPhWXtaYyStWPSGAb"));

            let node1: NodeTransactionV1 = serde_json::from_str(&_transactions()[0]).unwrap();
            let node2: NodeTransactionV1 = serde_json::from_str(&_transactions()[1]).unwrap();

            assert_eq!(
                txn_map["Gw6pDLhcBcoQesN72qfotTgFa7cbuqZpkX3Xo6pLhPhv"],
                node1
            );
            assert_eq!(
                txn_map["8ECVSk179mjsjKRLWiQtssMLgp6EPhWXtaYyStWPSGAb"],
                node2
            );
        }

        #[test]
        fn test_build_node_transaction_map_works_for_node_1_4_and_protocol_version_1_3() {
            let _err =
                build_node_transaction_map(_merkle_tree(), ProtocolVersion::Node1_3).unwrap_err();
        }

        #[test]
        fn test_build_node_transaction_map_works_for_node_1_3_and_protocol_version_1_3() {
            let merkle_tree = PoolTransactions::from_json_transactions(vec![NODE1_OLD, NODE2_OLD])
                .unwrap()
                .merkle_tree()
                .unwrap();

            let txn_map =
                build_node_transaction_map(merkle_tree, ProtocolVersion::Node1_3).unwrap();

            assert_eq!(2, txn_map.len());
            assert!(txn_map.contains_key("Gw6pDLhcBcoQesN72qfotTgFa7cbuqZpkX3Xo6pLhPhv"));
            assert!(txn_map.contains_key("8ECVSk179mjsjKRLWiQtssMLgp6EPhWXtaYyStWPSGAb"));

            let node1: NodeTransactionV1 = NodeTransactionV1::from(
                serde_json::from_str::<NodeTransactionV0>(NODE1_OLD).unwrap(),
            );
            let node2: NodeTransactionV1 = NodeTransactionV1::from(
                serde_json::from_str::<NodeTransactionV0>(NODE2_OLD).unwrap(),
            );

            assert_eq!(
                txn_map["Gw6pDLhcBcoQesN72qfotTgFa7cbuqZpkX3Xo6pLhPhv"],
                node1
            );
            assert_eq!(
                txn_map["8ECVSk179mjsjKRLWiQtssMLgp6EPhWXtaYyStWPSGAb"],
                node2
            );
        }

        #[test]
        fn test_build_node_transaction_map_works_for_node_1_3_and_protocol_version_1_4() {
            let merkle_tree = PoolTransactions::from_json_transactions(vec![NODE1_OLD, NODE2_OLD])
                .unwrap()
                .merkle_tree()
                .unwrap();

            let _err =
                build_node_transaction_map(merkle_tree, ProtocolVersion::Node1_4).unwrap_err();
        }
    }
}