Commit ffbcaf84 authored by Éloïs's avatar Éloïs

[feat] create example for each document type

parent cc8cf545
Pipeline #3709 failed with stages
in 59 seconds
use *;
pub fn example_doc(doc_type: DocumentType) -> String {
match doc_type {
DocumentType::Any => "".to_owned(),
DocumentType::TxV10 => "Version: 10
Type: Transaction
Currency: g1
Blockstamp: 107702-0000017CDBE974DC9A46B89EE7DC2BEE4017C43A005359E0853026C21FB6A084
Locktime: 0
Issuers:
Do6Y6nQ2KTo5fB4MXbSwabXVmXHxYRB9UUAaTPKn1XqC
Inputs:
1002:0:D:Do6Y6nQ2KTo5fB4MXbSwabXVmXHxYRB9UUAaTPKn1XqC:104937
1002:0:D:Do6Y6nQ2KTo5fB4MXbSwabXVmXHxYRB9UUAaTPKn1XqC:105214
Unlocks:
0:SIG(0)
1:SIG(0)
Outputs:
2004:0:SIG(DTgQ97AuJ8UgVXcxmNtULAs8Fg1kKC1Wr9SAS96Br9NG)
Comment: c est pour 2 mois d adhesion ressourcerie
lnpuFsIymgz7qhKF/GsZ3n3W8ZauAAfWmT4W0iJQBLKJK2GFkesLWeMj/+GBfjD6kdkjreg9M6VfkwIZH+hCCQ==".to_owned(),
DocumentType::IdentityV10 => "Version: 10
Type: Identity
Currency: duniter_unit_test_currency
Issuer: DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV
UniqueID: tic
Timestamp: 0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855
1eubHHbuNfilHMM0G2bI30iZzebQ2cQ1PC7uPAw08FGMMmQCRerlF/3pc4sAcsnexsxBseA/3lY03KlONqJBAg==".to_owned(),
DocumentType::MembershipV10 => "Version: 10
Type: Membership
Currency: duniter_unit_test_currency
Issuer: DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV
Block: 0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855
Membership: IN
UserID: tic
CertTS: 0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855
s2hUbokkibTAWGEwErw6hyXSWlWFQ2UWs2PWx8d/kkElAyuuWaQq4Tsonuweh1xn4AC1TVWt4yMR3WrDdkhnAw==".to_owned(),
DocumentType::CertificationV10 => "Version: 10
Type: Certification
Currency: g1-test
Issuer: 5B8iMAzq1dNmFe3ZxFTBQkqhq4fsztg1gZvxHXCk1XYH
IdtyIssuer: mMPioknj2MQCX9KyKykdw8qMRxYR2w1u3UpdiEJHgXg
IdtyUniqueID: mmpio
IdtyTimestamp: 7543-000044410C5370DE8DBA911A358F318096B7A269CFC2BB93272E397CC513EA0A
IdtySignature: SmSweUD4lEMwiZfY8ux9maBjrQQDkC85oMNsin6oSQCPdXG8sFCZ4FisUaWqKsfOlZVb/HNa+TKzD2t0Yte+DA==
CertTimestamp: 167884-0001DFCA28002A8C96575E53B8CEF8317453A7B0BA255542CCF0EC8AB5E99038
wqZxPEGxLrHGv8VdEIfUGvUcf+tDdNTMXjLzVRCQ4UhlhDRahOMjfcbP7byNYr5OfIl83S1MBxF7VJgu8YasCA==".to_owned(),
DocumentType::RevocationV10 => "Version: 10
Type: Revocation
Currency: g1
Issuer: DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV
IdtyUniqueID: tic
IdtyTimestamp: 0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855
IdtySignature: 1eubHHbuNfilHMM0G2bI30iZzebQ2cQ1PC7uPAw08FGMMmQCRerlF/3pc4sAcsnexsxBseA/3lY03KlONqJBAg==
XXOgI++6qpY9O31ml/FcfbXCE6aixIrgkT5jL7kBle3YOMr+8wrp7Rt+z9hDVjrNfYX2gpeJsuMNfG4T/fzVDQ==".to_owned(),
DocumentType::PeerV11 => "11:g1:0:7iMV3b6j2hSj5WtrfchfvxivS9swN3opDgxudeHq64fb:50-000005B1CEB4EC5245EF7E33101A330A1C9A358EC45A25FC13F78BB58C9E7370
WS2P V2 S 7 g1.durs.ifee.fr 443 ws2p
WS2P V2 S 7 84.16.72.210 443 ws2p
EQ2D5almq2RNUi3XZNtTpjo9nWtJF0PzsCW7ROAzCQKiEtpI7/fW8Z23GJ2a/SIxfYSzlq/cZqksE4EoVe1rAw==".to_owned(),
DocumentType::HeadV3 => "3:g1:0:0:0:0:0:7iMV3b6j2hSj5WtrfchfvxivS9swN3opDgxudeHq64fb:50-000005B1CEB4EC5245EF7E33101A330A1C9A358EC45A25FC13F78BB58C9E7370:durs:0.1.0-a0.1
vwlxpkCbv83qYSiClYA/GD35hs0AsZBnqv7uoE8hqlarT2c6jVRKhjp8JBqmRI7Se4IDwC2owk0mF4CglvyACQ==
2".to_owned(),
}
}
......@@ -6,10 +6,12 @@ extern crate serde_derive;
extern crate wasm_bindgen;
pub mod crypto;
pub mod examples;
pub mod parsers;
mod utils;
use cfg_if::cfg_if;
use wasm_bindgen::prelude::*;
cfg_if! {
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
......@@ -20,3 +22,16 @@ cfg_if! {
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
}
}
#[wasm_bindgen]
#[derive(Debug, Copy, Clone)]
pub enum DocumentType {
Any,
TxV10,
IdentityV10,
MembershipV10,
CertificationV10,
RevocationV10,
PeerV11,
HeadV3,
}
......@@ -13,19 +13,7 @@ use durs_network_documents::network_peer::*;
use durs_network_documents::*;
use std::ops::Deref;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
#[derive(Debug, Copy, Clone)]
pub enum DocumentType {
Any,
TxV10,
IdentityV10,
MembershipV10,
CertificationV10,
RevocationV10,
PeerV11,
HeadV3,
}
use *;
#[wasm_bindgen]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
......@@ -89,7 +77,7 @@ pub fn parse_doc_and_verify(source: &str, doc_type: DocumentType) -> DocCheckRes
_ => unimplemented!(),
},
Document_::NetworkDocument(network_document) => match network_document {
NetworkDocument::Peer(peer) => match peer {
NetworkDocument::Peer(peer_box) => match peer_box.deref() {
PeerCard::V11(peer_v11) => {
if let Ok(_) = peer_v11.verify() {
true
......@@ -128,12 +116,24 @@ pub fn parse_doc_into_json(doc: &str, doc_type: DocumentType) -> String {
match parse_doc(doc, doc_type) {
Ok(document) => match document {
Document_::DUBPDocument(blockchain_document) => match blockchain_document {
DUBPDocument::V10(doc_v10) => V10Document::to_json_string_pretty(&doc_v10.deref())
.unwrap_or("Fail to jsonifie document.".to_owned()),
DUBPDocument::V10(doc_v10_box) => {
let doc_v10 = doc_v10_box.deref();
if let V10Document::Transaction(ref tx_v10_box) = doc_v10 {
let mut tx_v10 = tx_v10_box.deref().clone();
tx_v10.compute_hash();
V10Document::to_json_string_pretty(&V10Document::Transaction(Box::new(
tx_v10,
)))
.unwrap_or("Fail to jsonifie document.".to_owned())
} else {
V10Document::to_json_string_pretty(&doc_v10)
.unwrap_or("Fail to jsonifie document.".to_owned())
}
}
_ => unimplemented!(),
},
Document_::NetworkDocument(network_document) => match network_document {
NetworkDocument::Peer(peer) => match peer {
NetworkDocument::Peer(peer_box) => match peer_box.deref() {
PeerCard::V11(peer_v11) => PeerCardV11::to_json_string_pretty(&peer_v11)
.unwrap_or("Fail to jsonifie document.".to_owned()),
_ => unimplemented!(),
......@@ -180,9 +180,9 @@ fn parse_doc(doc: &str, doc_type: DocumentType) -> Result<Document_, TextDocumen
DocumentType::RevocationV10 => Ok(Document_::DUBPDocument(DUBPDocument::V10(Box::new(
V10Document::Revocation(Box::new(RevocationDocumentParser::parse(doc)?)),
)))),
DocumentType::PeerV11 => Ok(Document_::NetworkDocument(NetworkDocument::Peer(
DocumentType::PeerV11 => Ok(Document_::NetworkDocument(NetworkDocument::Peer(Box::new(
PeerCard::V11(PeerCardV11::parse(doc)?),
))),
)))),
DocumentType::HeadV3 => Ok(Document_::NetworkDocument(NetworkDocument::Head(
NetworkHead::V3(Box::new(NetworkHeadV3::parse(doc)?)),
))),
......
......@@ -8,7 +8,9 @@ extern crate pretty_assertions;
extern crate wasm_bindgen_test;
use dup_tools_wasm::crypto::*;
use dup_tools_wasm::examples::*;
use dup_tools_wasm::parsers::*;
use dup_tools_wasm::*;
use wasm_bindgen_test::*;
wasm_bindgen_test_configure!(run_in_browser);
......@@ -27,38 +29,99 @@ fn test_generate_ed25519_keypair() {
}
#[wasm_bindgen_test]
fn test_parse_idty_v10() {
let doc = "Version: 10
Type: Identity
Currency: duniter_unit_test_currency
Issuer: DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV
UniqueID: tic
Timestamp: 0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855
1eubHHbuNfilHMM0G2bI30iZzebQ2cQ1PC7uPAw08FGMMmQCRerlF/3pc4sAcsnexsxBseA/3lY03KlONqJBAg==";
fn test_parse_and_verify_example_idty_v10() {
let doc = example_doc(DocumentType::IdentityV10);
assert_eq!(
parse_doc_into_json(doc, DocumentType::Any),
parse_doc_into_json(&doc, DocumentType::Any),
"{\n \"Identity\": {\n \"currency\": \"duniter_unit_test_currency\",\n \"username\": \"tic\",\n \"blockstamp\": \"0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855\",\n \"issuer\": \"DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV\",\n \"signature\": \"1eubHHbuNfilHMM0G2bI30iZzebQ2cQ1PC7uPAw08FGMMmQCRerlF/3pc4sAcsnexsxBseA/3lY03KlONqJBAg==\"\n }\n}".to_owned()
);
assert_eq!(
parse_doc_and_verify(doc, DocumentType::Any),
parse_doc_and_verify(&doc, DocumentType::Any),
DocCheckResult::ValidSig,
);
}
#[wasm_bindgen_test]
fn test_parse_peer_v11() {
let doc = "11:g1:0:7iMV3b6j2hSj5WtrfchfvxivS9swN3opDgxudeHq64fb:50-000005B1CEB4EC5245EF7E33101A330A1C9A358EC45A25FC13F78BB58C9E7370
WS2P V2 S 7 g1.durs.ifee.fr 443 ws2p
WS2P V2 S 7 84.16.72.210 443 ws2p
EQ2D5almq2RNUi3XZNtTpjo9nWtJF0PzsCW7ROAzCQKiEtpI7/fW8Z23GJ2a/SIxfYSzlq/cZqksE4EoVe1rAw==";
fn test_parse_and_verify_example_membership_v10() {
let doc = example_doc(DocumentType::MembershipV10);
assert_eq!(
parse_doc_into_json(doc, DocumentType::Any),
parse_doc_into_json(&doc, DocumentType::Any),
"{\n \"Membership\": {\n \"currency\": \"duniter_unit_test_currency\",\n \"issuer\": \"DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV\",\n \"blockstamp\": \"0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855\",\n \"membership\": \"IN\",\n \"username\": \"tic\",\n \"identity_blockstamp\": \"0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855\",\n \"signature\": \"s2hUbokkibTAWGEwErw6hyXSWlWFQ2UWs2PWx8d/kkElAyuuWaQq4Tsonuweh1xn4AC1TVWt4yMR3WrDdkhnAw==\"\n }\n}".to_owned()
);
assert_eq!(
parse_doc_and_verify(&doc, DocumentType::Any),
DocCheckResult::ValidSig,
);
}
#[wasm_bindgen_test]
fn test_parse_and_verify_example_cert_v10() {
let doc = example_doc(DocumentType::CertificationV10);
assert_eq!(
parse_doc_into_json(&doc, DocumentType::Any),
"{\n \"Certification\": {\n \"currency\": \"g1-test\",\n \"issuer\": \"5B8iMAzq1dNmFe3ZxFTBQkqhq4fsztg1gZvxHXCk1XYH\",\n \"target\": \"mMPioknj2MQCX9KyKykdw8qMRxYR2w1u3UpdiEJHgXg\",\n \"identity_username\": \"mmpio\",\n \"identity_blockstamp\": \"7543-000044410C5370DE8DBA911A358F318096B7A269CFC2BB93272E397CC513EA0A\",\n \"identity_sig\": \"SmSweUD4lEMwiZfY8ux9maBjrQQDkC85oMNsin6oSQCPdXG8sFCZ4FisUaWqKsfOlZVb/HNa+TKzD2t0Yte+DA==\",\n \"blockstamp\": \"167884-0001DFCA28002A8C96575E53B8CEF8317453A7B0BA255542CCF0EC8AB5E99038\",\n \"signature\": \"wqZxPEGxLrHGv8VdEIfUGvUcf+tDdNTMXjLzVRCQ4UhlhDRahOMjfcbP7byNYr5OfIl83S1MBxF7VJgu8YasCA==\"\n }\n}".to_owned()
);
assert_eq!(
parse_doc_and_verify(&doc, DocumentType::Any),
DocCheckResult::ValidSig,
);
}
#[wasm_bindgen_test]
fn test_parse_and_verify_example_revoc_v10() {
let doc = example_doc(DocumentType::RevocationV10);
assert_eq!(
parse_doc_into_json(&doc, DocumentType::Any),
"{\n \"Revocation\": {\n \"currency\": \"g1\",\n \"issuer\": \"DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV\",\n \"identity_username\": \"tic\",\n \"identity_blockstamp\": \"0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855\",\n \"identity_sig\": \"1eubHHbuNfilHMM0G2bI30iZzebQ2cQ1PC7uPAw08FGMMmQCRerlF/3pc4sAcsnexsxBseA/3lY03KlONqJBAg==\",\n \"signature\": \"XXOgI++6qpY9O31ml/FcfbXCE6aixIrgkT5jL7kBle3YOMr+8wrp7Rt+z9hDVjrNfYX2gpeJsuMNfG4T/fzVDQ==\"\n }\n}".to_owned()
);
assert_eq!(
parse_doc_and_verify(&doc, DocumentType::Any),
DocCheckResult::ValidSig,
);
}
#[wasm_bindgen_test]
fn test_parse_and_verify_example_tx_v10() {
let doc = example_doc(DocumentType::TxV10);
assert_eq!(
parse_doc_into_json(&doc, DocumentType::Any),
"{\n \"Transaction\": {\n \"currency\": \"g1\",\n \"blockstamp\": \"107702-0000017CDBE974DC9A46B89EE7DC2BEE4017C43A005359E0853026C21FB6A084\",\n \"locktime\": 0,\n \"issuers\": [\n \"Do6Y6nQ2KTo5fB4MXbSwabXVmXHxYRB9UUAaTPKn1XqC\"\n ],\n \"inputs\": [\n \"1002:0:D:Do6Y6nQ2KTo5fB4MXbSwabXVmXHxYRB9UUAaTPKn1XqC:104937\",\n \"1002:0:D:Do6Y6nQ2KTo5fB4MXbSwabXVmXHxYRB9UUAaTPKn1XqC:105214\"\n ],\n \"unlocks\": [\n \"0:SIG(0)\",\n \"1:SIG(0)\"\n ],\n \"outputs\": [\n \"2004:0:SIG(DTgQ97AuJ8UgVXcxmNtULAs8Fg1kKC1Wr9SAS96Br9NG)\"\n ],\n \"comment\": \"c est pour 2 mois d adhesion ressourcerie\",\n \"signatures\": [\n \"lnpuFsIymgz7qhKF/GsZ3n3W8ZauAAfWmT4W0iJQBLKJK2GFkesLWeMj/+GBfjD6kdkjreg9M6VfkwIZH+hCCQ==\"\n ],\n \"hash\": \"FB10D45F0146D3461FD25D921D686A350717E5D61024CC0DF61F3F6B0CADF9E0\"\n }\n}".to_owned()
);
assert_eq!(
parse_doc_and_verify(&doc, DocumentType::Any),
DocCheckResult::ValidSig,
);
}
#[wasm_bindgen_test]
fn test_parse_and_verify_example_peer_v11() {
let doc = example_doc(DocumentType::PeerV11);
assert_eq!(
parse_doc_into_json(&doc, DocumentType::Any),
"{\n \"currency_name\": \"g1\",\n \"issuer\": \"7iMV3b6j2hSj5WtrfchfvxivS9swN3opDgxudeHq64fb\",\n \"node_id\": \"0\",\n \"blockstamp\": \"50-000005B1CEB4EC5245EF7E33101A330A1C9A358EC45A25FC13F78BB58C9E7370\",\n \"endpoints\": [\n \"WS2P V2 S 7 g1.durs.ifee.fr 443 ws2p\",\n \"WS2P V2 S 7 84.16.72.210 443 ws2p\"\n ],\n \"sig\": \"EQ2D5almq2RNUi3XZNtTpjo9nWtJF0PzsCW7ROAzCQKiEtpI7/fW8Z23GJ2a/SIxfYSzlq/cZqksE4EoVe1rAw==\"\n}".to_owned()
);
assert_eq!(
parse_doc_and_verify(doc, DocumentType::Any),
parse_doc_and_verify(&doc, DocumentType::Any),
DocCheckResult::ValidSig,
);
}
#[wasm_bindgen_test]
fn test_parse_and_verify_example_head_v3() {
let doc = example_doc(DocumentType::HeadV3);
assert_eq!(
parse_doc_into_json(&doc, DocumentType::Any),
"{\n \"body\": \"3:g1:0:0:0:0:0:7iMV3b6j2hSj5WtrfchfvxivS9swN3opDgxudeHq64fb:50-000005B1CEB4EC5245EF7E33101A330A1C9A358EC45A25FC13F78BB58C9E7370:durs:0.1.0-a0.1\",\n \"signature\": \"vwlxpkCbv83qYSiClYA/GD35hs0AsZBnqv7uoE8hqlarT2c6jVRKhjp8JBqmRI7Se4IDwC2owk0mF4CglvyACQ==\",\n \"step\": 2\n}".to_owned()
);
assert_eq!(
parse_doc_and_verify(&doc, DocumentType::Any),
DocCheckResult::ValidSig,
);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment