Skip to content

Commit

Permalink
fix: forgot verify
Browse files Browse the repository at this point in the history
  • Loading branch information
gluax committed Jun 7, 2024
1 parent 649fdbd commit 9b78a95
Showing 1 changed file with 58 additions and 23 deletions.
81 changes: 58 additions & 23 deletions common/src/msgs/data_requests/execute/commit_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,26 @@ pub struct Execute {
trait SignSelf {
const METHOD_NAME: &'static str;

fn public_key(&self) -> &[u8];
fn proof(&self) -> &[u8];
fn set_proof(&mut self, proof: Vec<u8>);

// maybe needs to be an option if the struct has no fields
fn fields(&self) -> impl IntoIterator<Item = &[u8]>;

fn sign(&mut self, signing_key: &[u8], chain_id: &str, contract_addr: &str, seq: u128) -> Result<()> {
fn hash(&self, chain_id: &str, contract_addr: &str, seq: u128) -> Hash {
let seq = seq.to_be_bytes();
let msg = std::iter::once(Self::METHOD_NAME.as_bytes()).chain(self.fields().into_iter().chain([
chain_id.as_bytes(),
contract_addr.as_bytes(),
&seq,
]));

let msg_hash = hash(msg);
hash(msg)
}

fn sign(&mut self, signing_key: &[u8], chain_id: &str, contract_addr: &str, seq: u128) -> Result<()> {
let msg_hash = self.hash(chain_id, contract_addr, seq);

let vrf = Secp256k1Sha256::default();
let proof = vrf.prove(signing_key, &msg_hash)?;
Expand All @@ -38,6 +44,10 @@ trait SignSelf {

Ok(())
}

fn verify(&self, chain_id: &str, contract_addr: &str, seq: u128) -> Result<()> {
verify_proof(self.public_key(), self.proof(), self.hash(chain_id, contract_addr, seq))
}
}

// Option 1: Implement a trait for the structs
Expand All @@ -51,6 +61,14 @@ impl SignSelf for Execute {
fn fields(&self) -> impl IntoIterator<Item = &[u8]> {
[self.dr_id.as_slice(), self.commitment.as_slice()]
}

fn public_key(&self) -> &[u8] {
&self.public_key
}

fn proof(&self) -> &[u8] {
&self.proof
}
}

// Option 2: Builder pattern
Expand Down Expand Up @@ -92,39 +110,56 @@ impl ExecuteBuilder {
}
}

// Option 3: long new function
// Option 3: plain functions
impl Execute {
#[allow(clippy::too_many_arguments)]
pub fn new(
signing_key: &[u8],
dr_id: Hash,
commitment: Hash,
public_key: PublicKey,
height: u64,
chain_id: &str,
contract_addr: &str,
seq: u128,
) -> Result<Self> {
let msg_hash = hash([
pub fn new(dr_id: Hash, commitment: Hash, public_key: PublicKey) -> Result<Self> {
Ok(Execute {
dr_id,
commitment,
public_key,
proof: vec![],
})
}

pub fn hash(&self, height: u64, chain_id: &str, contract_addr: &str, seq: u128) -> Hash {
hash([
"commit_data_result".as_bytes(),
&dr_id,
&self.dr_id,
// this one does expect a height... but I think this is wrong and we should remove that??
&height.to_be_bytes(),
&commitment,
&self.commitment,
chain_id.as_bytes(),
contract_addr.as_bytes(),
&seq.to_be_bytes(),
]);
])
}

pub fn prove(
&mut self,
signing_key: &[u8],
height: u64,
chain_id: &str,
contract_addr: &str,
seq: u128,
) -> Result<()> {
let msg_hash = self.hash(height, chain_id, contract_addr, seq);

// We should lazy static or something to avoid creating a new instance every time
let vrf = Secp256k1Sha256::default();
let proof = vrf.prove(signing_key, &msg_hash)?;

Ok(Execute {
dr_id,
commitment,
public_key,
proof: vrf.prove(signing_key, &msg_hash)?,
})
self.proof = proof;

Ok(())
}

pub fn verify(&self, height: u64, chain_id: &str, contract_addr: &str, seq: u128) -> Result<()> {
verify_proof(
&self.public_key,
&self.proof,
self.hash(height, chain_id, contract_addr, seq),
)
}
}

Expand Down

0 comments on commit 9b78a95

Please sign in to comment.