Add CommitMessage

This commit is contained in:
Sosthene 2024-10-03 16:20:14 +02:00
parent bb82a0b760
commit 8d4238f531

View File

@ -4,14 +4,16 @@ use anyhow::{Error, Result};
use js_sys::Date; use js_sys::Date;
use rand::{thread_rng, RngCore}; use rand::{thread_rng, RngCore};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Value; use serde_json::{Map, Value};
use sp_client::bitcoin::consensus::serialize;
use sp_client::bitcoin::hex::{DisplayHex, FromHex}; use sp_client::bitcoin::hex::{DisplayHex, FromHex};
use sp_client::bitcoin::OutPoint; use sp_client::bitcoin::{OutPoint, Transaction};
use tsify::Tsify; use tsify::Tsify;
use crate::crypto::AAD; use crate::crypto::AAD;
use crate::error::AnkError; use crate::error::AnkError;
use crate::pcd::Member; use crate::pcd::Member;
use crate::signature::Proof;
#[derive(Debug, Serialize, Deserialize, Tsify)] #[derive(Debug, Serialize, Deserialize, Tsify)]
#[tsify(into_wasm_abi, from_wasm_abi)] #[tsify(into_wasm_abi, from_wasm_abi)]
@ -19,6 +21,7 @@ pub enum AnkFlag {
NewTx, NewTx,
Faucet, Faucet,
Cipher, Cipher,
Commit,
Unknown, Unknown,
} }
@ -28,6 +31,7 @@ impl From<&str> for AnkFlag {
"NewTx" => Self::NewTx, "NewTx" => Self::NewTx,
"Faucet" => Self::Faucet, "Faucet" => Self::Faucet,
"Cipher" => Self::Cipher, "Cipher" => Self::Cipher,
"Commit" => Self::Commit,
_ => Self::Unknown, _ => Self::Unknown,
} }
} }
@ -45,6 +49,7 @@ impl AnkFlag {
0 => Self::NewTx, 0 => Self::NewTx,
1 => Self::Faucet, 1 => Self::Faucet,
2 => Self::Cipher, 2 => Self::Cipher,
3 => Self::Commit,
_ => Self::Unknown, _ => Self::Unknown,
} }
} }
@ -53,12 +58,59 @@ impl AnkFlag {
match self { match self {
Self::NewTx => "NewTx", Self::NewTx => "NewTx",
Self::Faucet => "Faucet", Self::Faucet => "Faucet",
Self::Cipher => "Cipher", Self::Cipher => "Cipher",
Self::Commit => "Commit",
Self::Unknown => "Unknown", 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 init_tx: String, // Can be tx or txid of the first transaction of the chain, which is maybe not ideal
pub encrypted_pcd: Map<String, Value>,
pub keys: Map<String, Value>,
pub validation_tokens: Vec<Proof>,
pub error: Option<AnkError>,
}
impl CommitMessage {
/// Create a new commitment message for the first transaction of the chain
/// init_tx must be the hex string of the transaction
/// validation_tokens must be empty
pub fn new_first_commitment(transaction: Transaction, encrypted_pcd: Map<String, Value>, keys: Map<String, Value>) -> Self {
Self {
init_tx: serialize(&transaction).to_lower_hex_string(),
encrypted_pcd,
keys,
validation_tokens: vec![],
error: None,
}
}
/// Create a new commitment message for an update transaction
/// init_tx must be the hex string of the txid of the first commitment transaction
/// validation_tokens must be empty
pub fn new_update_commitment(init_tx: OutPoint, encrypted_pcd: Map<String, Value>, keys: Map<String, Value>) -> Self {
Self {
init_tx: init_tx.to_string(),
encrypted_pcd,
keys,
validation_tokens: vec![],
error: None,
}
}
/// Set the validation tokens for a pending commitment
pub fn set_validation_tokens(&mut self, validation_tokens: Vec<Proof>) {
self.validation_tokens = validation_tokens;
}
}
#[derive(Debug, Serialize, Deserialize, Tsify)] #[derive(Debug, Serialize, Deserialize, Tsify)]
#[tsify(into_wasm_abi, from_wasm_abi)] #[tsify(into_wasm_abi, from_wasm_abi)]
pub struct FaucetMessage { pub struct FaucetMessage {