Merge branch 'init-message' into dev
This commit is contained in:
commit
09b0f7a794
@ -1,20 +1,17 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use aes_gcm::aead::{Aead, Payload};
|
use anyhow::Result;
|
||||||
use aes_gcm::{Aes256Gcm, KeyInit};
|
|
||||||
use anyhow::{Error, Result};
|
|
||||||
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::{Map, Value};
|
use serde_json::Value;
|
||||||
use sp_client::bitcoin::consensus::serialize;
|
use sp_client::bitcoin::consensus::serialize;
|
||||||
use sp_client::bitcoin::hex::{DisplayHex, FromHex};
|
use sp_client::bitcoin::hex::DisplayHex;
|
||||||
use sp_client::bitcoin::{OutPoint, Transaction};
|
use sp_client::bitcoin::{OutPoint, Transaction};
|
||||||
use tsify::Tsify;
|
use tsify::Tsify;
|
||||||
|
|
||||||
use crate::crypto::AAD;
|
|
||||||
use crate::error::AnkError;
|
use crate::error::AnkError;
|
||||||
use crate::pcd::{Member, RoleDefinition};
|
use crate::pcd::{Member, RoleDefinition};
|
||||||
|
use crate::process::Process;
|
||||||
use crate::signature::Proof;
|
use crate::signature::Proof;
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Tsify)]
|
#[derive(Debug, Serialize, Deserialize, Tsify)]
|
||||||
@ -24,7 +21,7 @@ pub enum AnkFlag {
|
|||||||
Faucet,
|
Faucet,
|
||||||
Cipher,
|
Cipher,
|
||||||
Commit,
|
Commit,
|
||||||
Init,
|
Handshake,
|
||||||
Unknown,
|
Unknown,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,7 +32,7 @@ impl From<&str> for AnkFlag {
|
|||||||
"Faucet" => Self::Faucet,
|
"Faucet" => Self::Faucet,
|
||||||
"Cipher" => Self::Cipher,
|
"Cipher" => Self::Cipher,
|
||||||
"Commit" => Self::Commit,
|
"Commit" => Self::Commit,
|
||||||
"Init" => Self::Init,
|
"Handshake" => Self::Handshake,
|
||||||
_ => Self::Unknown,
|
_ => Self::Unknown,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -54,7 +51,7 @@ impl AnkFlag {
|
|||||||
1 => Self::Faucet,
|
1 => Self::Faucet,
|
||||||
2 => Self::Cipher,
|
2 => Self::Cipher,
|
||||||
3 => Self::Commit,
|
3 => Self::Commit,
|
||||||
4 => Self::Init,
|
4 => Self::Handshake,
|
||||||
_ => Self::Unknown,
|
_ => Self::Unknown,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -65,7 +62,7 @@ impl AnkFlag {
|
|||||||
Self::Faucet => "Faucet",
|
Self::Faucet => "Faucet",
|
||||||
Self::Cipher => "Cipher",
|
Self::Cipher => "Cipher",
|
||||||
Self::Commit => "Commit",
|
Self::Commit => "Commit",
|
||||||
Self::Init => "Init",
|
Self::Handshake => "Handshake",
|
||||||
Self::Unknown => "Unknown",
|
Self::Unknown => "Unknown",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -171,6 +168,28 @@ impl NewTxMessage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Serialize, Deserialize, Tsify)]
|
||||||
|
#[tsify(into_wasm_abi, from_wasm_abi)]
|
||||||
|
pub struct HandshakeMessage {
|
||||||
|
pub sp_address: String,
|
||||||
|
pub peers_list: HashMap<Member, OutPoint>,
|
||||||
|
pub processes_list: HashMap<OutPoint, Process>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HandshakeMessage {
|
||||||
|
pub fn new(sp_address: String, peers_list: HashMap<Member, OutPoint>, processes_list: HashMap<OutPoint, Process>) -> Self {
|
||||||
|
Self {
|
||||||
|
sp_address,
|
||||||
|
peers_list,
|
||||||
|
processes_list,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_string(&self) -> String {
|
||||||
|
serde_json::to_string(self).unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct Envelope {
|
pub struct Envelope {
|
||||||
pub flag: AnkFlag,
|
pub flag: AnkFlag,
|
||||||
|
25
src/pcd.rs
25
src/pcd.rs
@ -1,6 +1,7 @@
|
|||||||
use anyhow::{Error, Result};
|
use anyhow::{Error, Result};
|
||||||
use rs_merkle::{algorithms::Sha256, MerkleTree};
|
use rs_merkle::{algorithms::Sha256, MerkleTree};
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
|
use std::hash::{Hash as StdHash, Hasher};
|
||||||
|
|
||||||
use aes_gcm::{
|
use aes_gcm::{
|
||||||
aead::{Aead, Payload},
|
aead::{Aead, Payload},
|
||||||
@ -22,12 +23,34 @@ use crate::{
|
|||||||
signature::{AnkHash, AnkValidationNoHash, AnkValidationYesHash, Proof},
|
signature::{AnkHash, AnkValidationNoHash, AnkValidationYesHash, Proof},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Tsify)]
|
#[derive(Debug, Default, Clone, Serialize, Deserialize, Tsify)]
|
||||||
#[tsify(into_wasm_abi, from_wasm_abi)]
|
#[tsify(into_wasm_abi, from_wasm_abi)]
|
||||||
pub struct Member {
|
pub struct Member {
|
||||||
sp_addresses: Vec<String>,
|
sp_addresses: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl PartialEq for Member {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
let self_set: HashSet<_> = self.sp_addresses.iter().collect();
|
||||||
|
let other_set: HashSet<_> = other.sp_addresses.iter().collect();
|
||||||
|
self_set == other_set
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Eq for Member {}
|
||||||
|
|
||||||
|
impl StdHash for Member {
|
||||||
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||||
|
// Convert to a set to ensure order independence
|
||||||
|
let set: HashSet<_> = self.sp_addresses.iter().collect();
|
||||||
|
let mut unique_items: Vec<_> = set.into_iter().collect();
|
||||||
|
unique_items.sort_unstable(); // Sort to ensure consistent hashing
|
||||||
|
for item in unique_items {
|
||||||
|
item.hash(state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Member {
|
impl Member {
|
||||||
pub fn new(sp_addresses: Vec<SilentPaymentAddress>) -> Result<Self> {
|
pub fn new(sp_addresses: Vec<SilentPaymentAddress>) -> Result<Self> {
|
||||||
if sp_addresses.is_empty() {
|
if sp_addresses.is_empty() {
|
||||||
|
21
src/prd.rs
21
src/prd.rs
@ -27,6 +27,7 @@ pub enum PrdType {
|
|||||||
Response, // Validate (or disagree) with a prd update
|
Response, // Validate (or disagree) with a prd update
|
||||||
Confirm, // Confirm we received an update
|
Confirm, // Confirm we received an update
|
||||||
TxProposal, // Send a psbt asking for recipient signature
|
TxProposal, // Send a psbt asking for recipient signature
|
||||||
|
Request // asks for the prd update for some state,
|
||||||
}
|
}
|
||||||
|
|
||||||
sha256t_hash_newtype! {
|
sha256t_hash_newtype! {
|
||||||
@ -51,7 +52,7 @@ impl AnkPrdHash {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Tsify)]
|
#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize, Tsify)]
|
||||||
#[tsify(into_wasm_abi, from_wasm_abi)]
|
#[tsify(into_wasm_abi, from_wasm_abi)]
|
||||||
#[allow(non_camel_case_types)]
|
#[allow(non_camel_case_types)]
|
||||||
pub struct Prd {
|
pub struct Prd {
|
||||||
@ -86,7 +87,7 @@ impl Prd {
|
|||||||
|
|
||||||
pub fn new_update(
|
pub fn new_update(
|
||||||
root_commitment: OutPoint,
|
root_commitment: OutPoint,
|
||||||
sender: String, // Should take Member as argument
|
sender: Member,
|
||||||
roles: HashMap<String, RoleDefinition>,
|
roles: HashMap<String, RoleDefinition>,
|
||||||
keys: Map<String, Value>,
|
keys: Map<String, Value>,
|
||||||
pcd_commitments: Value,
|
pcd_commitments: Value,
|
||||||
@ -94,7 +95,7 @@ impl Prd {
|
|||||||
Self {
|
Self {
|
||||||
prd_type: PrdType::Update,
|
prd_type: PrdType::Update,
|
||||||
root_commitment: root_commitment.to_string(),
|
root_commitment: root_commitment.to_string(),
|
||||||
sender,
|
sender: serde_json::to_string(&sender).unwrap(),
|
||||||
validation_tokens: vec![],
|
validation_tokens: vec![],
|
||||||
keys,
|
keys,
|
||||||
pcd_commitments,
|
pcd_commitments,
|
||||||
@ -105,14 +106,14 @@ impl Prd {
|
|||||||
|
|
||||||
pub fn new_response(
|
pub fn new_response(
|
||||||
root_commitment: OutPoint,
|
root_commitment: OutPoint,
|
||||||
sender: String,
|
sender: Member,
|
||||||
validation_tokens: Vec<Proof>,
|
validation_tokens: Vec<Proof>,
|
||||||
pcd_commitments: Value,
|
pcd_commitments: Value,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
prd_type: PrdType::Response,
|
prd_type: PrdType::Response,
|
||||||
root_commitment: root_commitment.to_string(),
|
root_commitment: root_commitment.to_string(),
|
||||||
sender,
|
sender: serde_json::to_string(&sender).unwrap(),
|
||||||
validation_tokens: validation_tokens,
|
validation_tokens: validation_tokens,
|
||||||
keys: Map::new(),
|
keys: Map::new(),
|
||||||
pcd_commitments,
|
pcd_commitments,
|
||||||
@ -138,6 +139,16 @@ impl Prd {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn new_request(root_commitment: OutPoint, sender: Member, state_ids: Vec<[u8; 32]>) -> Self {
|
||||||
|
Self {
|
||||||
|
prd_type: PrdType::Request,
|
||||||
|
root_commitment: root_commitment.to_string(),
|
||||||
|
sender: serde_json::to_string(&sender).unwrap(),
|
||||||
|
payload: serde_json::to_string(&state_ids).unwrap(),
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn extract_from_message(plain: &[u8], local_address: SilentPaymentAddress) -> Result<Self> {
|
pub fn extract_from_message(plain: &[u8], local_address: SilentPaymentAddress) -> Result<Self> {
|
||||||
let prd: Prd = serde_json::from_slice(plain)?;
|
let prd: Prd = serde_json::from_slice(plain)?;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user