use ::microledger::error::Error;
use ::microledger::microledger::MicroLedger;
use clap::{App, Arg};
use keri::{
prefix::{BasicPrefix},
};
use microledger::{
block::Block,
controlling_identifier::ControllingIdentifier,
seal_bundle::{BlockAttachment, SealBundle, SealData},
signature::Signature,
};
use std::io::{self, Read};
fn main() -> Result<(), Error> {
let mut serialized_microledger = String::new();
let mut stdin = io::stdin();
stdin.read_to_string(&mut serialized_microledger).unwrap();
let microledger: MicroLedger = if serialized_microledger.len() == 0 {
MicroLedger::new()
} else {
serde_json::from_str(&serialized_microledger).unwrap_or(MicroLedger::new())
};
let matches = App::new("Microledger example")
.version("1.0")
.subcommand(
App::new("next")
.about("Generate next block with given payload and controlling identifiers")
.arg(
Arg::new("embeddedAttachement")
.short('e')
.long("embeddedAttachement")
.takes_value(true)
.multiple_occurrences(true)
.value_name("STRING")
.help("Add embedded attachement"),
)
.arg(
Arg::new("controller")
.short('c')
.long("controller")
.takes_value(true)
.value_name("VEC")
.help("Set controlling identifier"),
),
)
.subcommand(
App::new("anchor")
.about("Add block to microledger")
.arg(
Arg::new("block")
.short('b')
.long("anchor")
.takes_value(true)
.value_name("BLOCK")
.help("Block to be added"),
)
.arg(
Arg::new("signatures")
.short('s')
.long("signatures")
.takes_value(true)
.value_name("SIGNATURE")
.help("Attach signature to block"),
)
.arg(
Arg::new("attachment")
.long("attachment")
.takes_value(true)
.value_name("STRING")
.help("Attach seals to block"),
),
)
.get_matches();
if let Some(ref matches) = matches.subcommand_matches("next") {
let controlling_id: BasicPrefix = if let Some(c) = matches.value_of("controller") {
Ok(c.parse()?)
} else {
Err(microledger::error::Error::BlockError(
"missing ids".to_string(),
))
}?;
let seal_bundle = if let Some(i) = matches.values_of("embeddedAttachement") {
i.fold(SealBundle::new(), |acc, data| {
acc.attach(SealData::AttachedData(data.to_string()))
})
} else {
SealBundle::default()
};
let block = microledger.pre_anchor_block(
vec![ControllingIdentifier::Basic(controlling_id)],
&seal_bundle,
);
println!("{}", serde_json::to_string(&block).unwrap());
println!(
"{}",
serde_json::to_string(&seal_bundle.get_attachement()).unwrap()
);
}
if let Some(ref matches) = matches.subcommand_matches("anchor") {
let block = matches
.value_of("block")
.ok_or(Error::MicroError("Missing block argument".into()))?;
let block: Block = serde_json::from_str(&block).unwrap();
if let Some(signature) = matches.value_of("signatures") {
let s = Signature::SelfSigning(signature.parse()?);
let seal_bundle = if let Some(attachment) = matches.value_of("attachment") {
let seals: BlockAttachment = serde_json::from_str(attachment)
.map_err(|e| Error::MicroError(e.to_string()))?;
seals.to_seal_bundle()
} else {
SealBundle::new()
};
let signed_block = block.to_signed_block(vec![(s)], &seal_bundle);
let m = microledger.anchor(signed_block)?;
println!("{}", serde_json::to_string(&m).unwrap());
} else {
}
}
Ok(())
}