sdk_common/src/network.rs

212 lines
5.4 KiB
Rust

use anyhow::Result;
use rand::{thread_rng, RngCore};
use serde::{Deserialize, Serialize};
use sp_client::bitcoin::hex::DisplayHex;
use sp_client::bitcoin::OutPoint;
use tsify::Tsify;
use crate::error::AnkError;
use crate::pcd::{Pcd, PcdCommitments, Roles};
use crate::serialization::{OutPointMemberMap, OutPointProcessMap};
use crate::signature::Proof;
#[derive(Debug, Serialize, Deserialize, Tsify)]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub enum AnkFlag {
NewTx,
Faucet,
Cipher,
Commit,
Handshake,
Sync,
Unknown,
}
impl From<&str> for AnkFlag {
fn from(value: &str) -> Self {
match value {
"NewTx" => Self::NewTx,
"Faucet" => Self::Faucet,
"Cipher" => Self::Cipher,
"Commit" => Self::Commit,
"Handshake" => Self::Handshake,
"Sync" => Self::Sync,
_ => Self::Unknown,
}
}
}
impl From<String> for AnkFlag {
fn from(value: String) -> Self {
(&value[..]).into()
}
}
impl AnkFlag {
pub fn new_from_byte(byte: u8) -> Self {
match byte {
0 => Self::NewTx,
1 => Self::Faucet,
2 => Self::Cipher,
3 => Self::Commit,
4 => Self::Handshake,
5 => Self::Sync,
_ => Self::Unknown,
}
}
pub fn as_str(&self) -> &str {
match self {
Self::NewTx => "NewTx",
Self::Faucet => "Faucet",
Self::Cipher => "Cipher",
Self::Commit => "Commit",
Self::Handshake => "Handshake",
Self::Sync => "Sync",
Self::Unknown => "Unknown",
}
}
}
/// Message sent to the server to commit some state in a transaction
/// Client must first send a commit message with empty validation_tokens
/// Relay will ignore a commit message for an update he's not aware of that also bears validation_tokens
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Tsify)]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct CommitMessage {
pub process_id: OutPoint,
pub pcd_commitment: PcdCommitments, // map of field <=> hash of the clear value
pub roles: Roles,
pub public_data: Pcd,
pub validation_tokens: Vec<Proof>,
pub error: Option<AnkError>,
}
impl CommitMessage {
/// Create a new commitment message for a creation/update transaction
pub fn new(
process_id: OutPoint,
pcd_commitment: PcdCommitments,
roles: Roles,
public_data: Pcd,
validation_tokens: Vec<Proof>
) -> Self {
Self {
process_id,
pcd_commitment,
roles,
public_data,
validation_tokens,
error: None,
}
}
/// Add validation tokens for a pending commitment
pub fn add_validation_tokens(&mut self, validation_tokens: Vec<Proof>) {
self.validation_tokens.extend_from_slice(&validation_tokens);
self.validation_tokens.sort_unstable();
self.validation_tokens.dedup();
}
/// Remove all validation tokens
/// This should rarely be useful, and we'd rather let caller copy all he needs, sorts on its own
/// and clear here
pub fn clear_validation_tokens(&mut self) {
self.validation_tokens = vec![];
}
}
#[derive(Debug, Serialize, Deserialize, Tsify)]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct FaucetMessage {
pub sp_address: String,
pub commitment: String,
pub error: Option<AnkError>,
}
impl FaucetMessage {
pub fn new(sp_address: String) -> Self {
let mut buf = [0u8; 32];
thread_rng().fill_bytes(&mut buf);
Self {
sp_address,
commitment: buf.to_lower_hex_string(),
error: None,
}
}
pub fn to_string(&self) -> String {
serde_json::to_string(self).unwrap()
}
}
#[derive(Debug, PartialEq, Serialize, Deserialize, Tsify)]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct NewTxMessage {
pub transaction: String,
pub tweak_data: Option<String>,
pub error: Option<AnkError>,
}
impl NewTxMessage {
pub fn new(transaction: String, tweak_data: Option<String>) -> Self {
Self {
transaction,
tweak_data,
error: None,
}
}
pub fn to_string(&self) -> String {
serde_json::to_string(self).unwrap()
}
}
#[derive(Debug, Serialize, Deserialize, Tsify)]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct HandshakeMessage {
pub sp_address: String,
pub peers_list: OutPointMemberMap,
pub processes_list: OutPointProcessMap,
pub chain_tip: u32,
}
impl HandshakeMessage {
pub fn new(sp_address: String, peers_list: OutPointMemberMap, processes_list: OutPointProcessMap, chain_tip: u32) -> Self {
Self {
sp_address,
peers_list,
processes_list,
chain_tip,
}
}
pub fn to_string(&self) -> String {
serde_json::to_string(self).unwrap()
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Envelope {
pub flag: AnkFlag,
pub content: String,
}
impl Envelope {
pub fn new(flag: AnkFlag, raw: &str) -> Self {
Self {
flag,
content: raw.into(),
}
}
pub fn from_string(json: &str) -> Result<Self> {
let res: Self = serde_json::from_str(json)?;
Ok(res)
}
pub fn to_string(&self) -> String {
serde_json::to_string(self).unwrap()
}
}