62 lines
1.5 KiB
Rust
62 lines
1.5 KiB
Rust
use std::path::PathBuf;
|
|
|
|
use bft_crdt_derive::add_crdt_fields;
|
|
|
|
use bft_json_crdt::{
|
|
json_crdt::{BaseCrdt, CrdtNode, IntoCrdtNode},
|
|
keypair::{Ed25519KeyPair, KeyPair},
|
|
list_crdt::ListCrdt,
|
|
};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
use serde_json::{json, Value};
|
|
|
|
use crate::keys;
|
|
|
|
#[add_crdt_fields]
|
|
#[derive(Clone, CrdtNode, Serialize, Deserialize)]
|
|
pub(crate) struct CrdtList {
|
|
pub(crate) list: ListCrdt<Transaction>, // switch to Transaction as soon as char is working
|
|
}
|
|
|
|
/// A fake Transaction struct we can use as a simulated payload
|
|
#[add_crdt_fields]
|
|
#[derive(Clone, CrdtNode, Serialize, Deserialize)]
|
|
pub(crate) struct Transaction {
|
|
from: String,
|
|
to: String,
|
|
amount: f64,
|
|
}
|
|
|
|
pub(crate) fn new(side_dir: PathBuf) -> (BaseCrdt<CrdtList>, Ed25519KeyPair) {
|
|
let keys = keys::load_from_file(side_dir);
|
|
let bft_crdt = BaseCrdt::<CrdtList>::new(&keys);
|
|
println!("Author is {}", keys.public().to_string());
|
|
(bft_crdt, keys)
|
|
}
|
|
|
|
pub(crate) fn send(
|
|
bft_crdt: &mut BaseCrdt<CrdtList>,
|
|
keys: &Ed25519KeyPair,
|
|
) -> bft_json_crdt::json_crdt::SignedOp {
|
|
// generate a placeholder transaction
|
|
let transaction = generate_transaction(keys.public().to_string());
|
|
|
|
// next job is to keep adding to this guy
|
|
let next = bft_crdt.doc.list.ops.len();
|
|
let signed_op = bft_crdt
|
|
.doc
|
|
.list
|
|
.insert_idx(next - 1, transaction.clone())
|
|
.sign(&keys);
|
|
signed_op
|
|
}
|
|
|
|
fn generate_transaction(pubkey: String) -> Value {
|
|
json!({
|
|
"from": pubkey,
|
|
"to": "Bob",
|
|
"amount": 1
|
|
})
|
|
}
|