Take an optional pairingid as sender instead of Member
This commit is contained in:
parent
dab1a4dd2c
commit
e205229e92
65
src/prd.rs
65
src/prd.rs
@ -11,7 +11,8 @@ use sp_client::silentpayments::SilentPaymentAddress;
|
|||||||
use sp_client::SpClient;
|
use sp_client::SpClient;
|
||||||
use tsify::Tsify;
|
use tsify::Tsify;
|
||||||
|
|
||||||
use crate::pcd::{Member, Pcd, PcdCommitments, Roles};
|
use crate::pcd::{Pcd, PcdCommitments, Roles};
|
||||||
|
use crate::serialization::OutPointMemberMap;
|
||||||
use crate::signature::{AnkHash, AnkMessageHash, Proof};
|
use crate::signature::{AnkHash, AnkMessageHash, Proof};
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize, Tsify)]
|
#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize, Tsify)]
|
||||||
@ -58,7 +59,7 @@ impl AnkPrdHash {
|
|||||||
pub struct Prd {
|
pub struct Prd {
|
||||||
pub prd_type: PrdType,
|
pub prd_type: PrdType,
|
||||||
pub process_id: OutPoint,
|
pub process_id: OutPoint,
|
||||||
pub sender: Member,
|
pub sender: Option<OutPoint>,
|
||||||
pub keys: BTreeMap<String, [u8; 32]>, // key is a key in pcd, value is the key to decrypt it
|
pub keys: BTreeMap<String, [u8; 32]>, // key is a key in pcd, value is the key to decrypt it
|
||||||
pub pcd_commitments: PcdCommitments,
|
pub pcd_commitments: PcdCommitments,
|
||||||
pub validation_tokens: Vec<Proof>,
|
pub validation_tokens: Vec<Proof>,
|
||||||
@ -74,7 +75,7 @@ impl Prd {
|
|||||||
/// If validation_tokens contains a valid proof signed by ourselves of empty prd,
|
/// If validation_tokens contains a valid proof signed by ourselves of empty prd,
|
||||||
/// we confirm the secret if necessary and don't return anything
|
/// we confirm the secret if necessary and don't return anything
|
||||||
pub fn new_connect(
|
pub fn new_connect(
|
||||||
sender: Member,
|
sender: Option<OutPoint>,
|
||||||
secret_hash: AnkMessageHash,
|
secret_hash: AnkMessageHash,
|
||||||
previous_proof: Option<Proof>,
|
previous_proof: Option<Proof>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
@ -98,7 +99,7 @@ impl Prd {
|
|||||||
|
|
||||||
pub fn new_update(
|
pub fn new_update(
|
||||||
process_id: OutPoint,
|
process_id: OutPoint,
|
||||||
sender: Member,
|
sender: OutPoint,
|
||||||
roles: Roles,
|
roles: Roles,
|
||||||
public_data: Pcd,
|
public_data: Pcd,
|
||||||
keys: BTreeMap<String, [u8; 32]>,
|
keys: BTreeMap<String, [u8; 32]>,
|
||||||
@ -107,7 +108,7 @@ impl Prd {
|
|||||||
Self {
|
Self {
|
||||||
prd_type: PrdType::Update,
|
prd_type: PrdType::Update,
|
||||||
process_id,
|
process_id,
|
||||||
sender,
|
sender: Some(sender),
|
||||||
validation_tokens: vec![],
|
validation_tokens: vec![],
|
||||||
keys,
|
keys,
|
||||||
pcd_commitments,
|
pcd_commitments,
|
||||||
@ -120,14 +121,14 @@ impl Prd {
|
|||||||
|
|
||||||
pub fn new_response(
|
pub fn new_response(
|
||||||
process_id: OutPoint,
|
process_id: OutPoint,
|
||||||
sender: Member,
|
sender: OutPoint,
|
||||||
validation_tokens: Vec<Proof>,
|
validation_tokens: Vec<Proof>,
|
||||||
pcd_commitments: PcdCommitments,
|
pcd_commitments: PcdCommitments,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
prd_type: PrdType::Response,
|
prd_type: PrdType::Response,
|
||||||
process_id,
|
process_id,
|
||||||
sender,
|
sender: Some(sender),
|
||||||
validation_tokens,
|
validation_tokens,
|
||||||
pcd_commitments,
|
pcd_commitments,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
@ -136,29 +137,29 @@ impl Prd {
|
|||||||
|
|
||||||
pub fn new_confirm(
|
pub fn new_confirm(
|
||||||
process_id: OutPoint,
|
process_id: OutPoint,
|
||||||
sender: Member,
|
sender: OutPoint,
|
||||||
pcd_commitments: PcdCommitments,
|
pcd_commitments: PcdCommitments,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
prd_type: PrdType::Confirm,
|
prd_type: PrdType::Confirm,
|
||||||
process_id,
|
process_id,
|
||||||
pcd_commitments,
|
pcd_commitments,
|
||||||
sender,
|
sender: Some(sender),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_request(process_id: OutPoint, sender: Member, state_ids: Vec<[u8; 32]>) -> Self {
|
pub fn new_request(process_id: OutPoint, sender: OutPoint, state_ids: Vec<[u8; 32]>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
prd_type: PrdType::Request,
|
prd_type: PrdType::Request,
|
||||||
process_id,
|
process_id,
|
||||||
sender,
|
sender: Some(sender),
|
||||||
payload: serde_json::to_string(&state_ids).unwrap(),
|
payload: serde_json::to_string(&state_ids).unwrap(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn extract_from_message(plain: &[u8], local_address: SilentPaymentAddress) -> Result<Self> {
|
pub fn extract_from_message(plain: &[u8], local_address: SilentPaymentAddress, members: &OutPointMemberMap) -> Result<Self> {
|
||||||
let prd: Prd = serde_json::from_slice(plain)?;
|
let prd: Prd = serde_json::from_slice(plain)?;
|
||||||
|
|
||||||
// check that the proof is consistent
|
// check that the proof is consistent
|
||||||
@ -171,24 +172,30 @@ impl Prd {
|
|||||||
"Proof signed by ourselves, we are parsing our own message",
|
"Proof signed by ourselves, we are parsing our own message",
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
// take the spending keys in sender
|
|
||||||
let addresses = prd.sender.get_addresses();
|
|
||||||
let mut spend_keys: Vec<PublicKey> = vec![];
|
|
||||||
for address in addresses {
|
|
||||||
spend_keys.push(<SilentPaymentAddress>::try_from(address)?.get_spend_key());
|
|
||||||
}
|
|
||||||
// The key in proof must be one of the sender keys
|
|
||||||
let mut known_key = false;
|
|
||||||
for key in spend_keys {
|
|
||||||
if key == proof_key {
|
|
||||||
known_key = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !known_key {
|
|
||||||
return Err(anyhow::Error::msg("Proof signed with an unknown key"));
|
|
||||||
}
|
|
||||||
proof.verify()?;
|
proof.verify()?;
|
||||||
|
|
||||||
|
if let Some(sender) = prd.sender {
|
||||||
|
// take the spending keys in sender
|
||||||
|
let addresses = members.0.get(&sender).ok_or(anyhow::Error::msg("Unknown sender"))?.get_addresses();
|
||||||
|
let mut spend_keys: Vec<PublicKey> = vec![];
|
||||||
|
for address in addresses {
|
||||||
|
spend_keys.push(<SilentPaymentAddress>::try_from(address)?.get_spend_key());
|
||||||
|
}
|
||||||
|
// The key in proof must be one of the sender keys
|
||||||
|
let mut known_key = false;
|
||||||
|
for key in spend_keys {
|
||||||
|
if key == proof_key {
|
||||||
|
known_key = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !known_key {
|
||||||
|
log::warn!("Proof signed with a key that doesn't match declared sender");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log::warn!("No declared sender");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
log::warn!("No proof for prd with process_id {}", prd.process_id);
|
log::warn!("No proof for prd with process_id {}", prd.process_id);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user