A Rust blockchain library that provides the building blocks for creating a full-fledged blockchain application or platform, allowing you to focus on the higher-level features of your application without worrying about the low-level details of block validation
, data serialization
, block building and mining
, and cryptographic operations
.
Record Trait ``` fn main() { use blockify::{data::MetaData, record::Record}; use serde::{Deserialize, Serialize};
struct Vote { session: i32, choice: i32, }
// Generate a new keypair let keypair = blockify::generateed25519key_pair();
// Clone the public key let pubkey = keypair.clone().intopublic_key();
// Create a new Vote
instance
let my_record = Vote {
session: 0,
choice: 2,
};
// calculate the hash of myrecord let myrecordhash = blockify::hash(&myrecord);
// sign myrecord with the AuthKeyPair instance and obtain a digital signature let signature = myrecord.sign(&keypair).unwrap();
// verify the authencity of the digital signature assert!(myrecord.verify(&signature, &pubkey).is_ok());
// record the myvote (convert it into a SignedRecord instance) let signedrecord = my_record.record(keypair, MetaData::empty()).unwrap();
// Compare the signature of my_record
with that inside the SignedRecord
instance
asserteq!(&signature, signedrecord.signature());
// Compare the public key used to sign myrecord with that inside the SignedRecord
instance.
asserteq!(&pubkey, signedrecord.signer());
// Compare the hash of myrecord with that inside the SignedRecord
instance.
asserteq!(&myrecordhash, signed_record.hash());
// Verify the signature within the SignedRecord
instance.
assert!(signedrecord.verify().isok());
}
```
use serde::{Deserialize, Serialize};
struct Vote { data: String, }
impl Vote { fn new(data: &str) -> Self { Vote { data: data.into() } } }
fn main() {
let chainurl = "target2/main/sqlite/";
std::fs::createdirall(chainurl).expect("could not create chainurl");
let datas1 = vec!["abcd", "efgh", "ijkl"];
let datas2 = vec!["mnop", "qrst", "uvwx"];
let keypair = blockify::generateed25519keypair();
let records1 = datas1
.intoiter()
.map(|w| Vote::new(w).record(keypair.clone(), MetaData::empty()))
.filter(|r| r.isok())
.map(|v| v.unwrap())
.collect::
let mut builder1 = UnchainedInstance::new(MetaData::empty(), 0);
let mut builder2 = UnchainedInstance::new(MetaData::empty(), 1);
for record in records1 {
builder1.push(record);
}
for record in records2 {
builder2.push(record);
}
let mut chain = SqliteChain::new(chain_url).expect("sqlite connection cannot be established");
let instance1 = chain.append(&builder1).expect("builder1 append erred");
let instance2 = chain.append(&builder2).expect("builder2 append erred");
let block1 = chain
.block_at(instance1.position())
.expect("couldn't retrieve block1");
let block2 = chain
.block_at(instance2.position())
.expect("couldn't retrieve block2");
assert!(block1.validate(&instance1).is_ok());
assert!(block2.validate(&instance2).is_ok());
let records_from_block1 = block1
.records()
.expect("couldn't retrieve records from block1");
assert_eq!(builder1.records().as_slice(), &*records_from_block1);
let records_from_block2 = block2
.records()
.expect("couldn't retrieve records from block2");
assert_eq!(builder2.records().as_slice(), &*records_from_block2);
}
```
cargo add blockify
All forms of contributions are gladly welcome.
MIT