Skip to content

Commit

Permalink
Merge pull request #37 from entropyxyz/add-aux-data
Browse files Browse the repository at this point in the history
Add extra data to program state
  • Loading branch information
jakehemmerle authored Oct 31, 2023
2 parents b2fdf04 + 0e20cdc commit 596a546
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
8 changes: 5 additions & 3 deletions examples/barebones/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl Program for BarebonesProgram {
/// This is the only function required by the program runtime. `signature_request` is the preimage of the curve element to be
/// signed, eg. RLP-serialized Ethereum transaction request, raw x86_64 executable, etc.
fn evaluate(signature_request: InitialState) -> Result<(), Error> {
let data: vec::Vec<u8> = signature_request.data;
let data: vec::Vec<u8> = signature_request.preimage;

// our constraint just checks that the length of the signature request is greater than 10
if data.len() < 10 {
Expand All @@ -40,7 +40,8 @@ mod tests {
#[test]
fn test_should_sign() {
let signature_request = InitialState {
data: "some_data_longer_than_10_bytes".to_string().into_bytes(),
preimage: "some_data_longer_than_10_bytes".to_string().into_bytes(),
extra: None
};

assert!(BarebonesProgram::evaluate(signature_request).is_ok());
Expand All @@ -50,7 +51,8 @@ mod tests {
fn test_should_error() {
// data being checked is under 10 bytes in length
let signature_request = InitialState {
data: "under10".to_string().into_bytes(),
preimage: "under10".to_string().into_bytes(),
extra: None
};

assert!(BarebonesProgram::evaluate(signature_request).is_err());
Expand Down
8 changes: 5 additions & 3 deletions examples/basic-transaction/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Program for BasicTransaction {
fn evaluate(state: InitialState) -> Result<(), CoreError> {
// parse the raw tx into some type
let parsed_tx =
<Evm as Architecture>::TransactionRequest::try_parse(state.data.as_slice())?;
<Evm as Architecture>::TransactionRequest::try_parse(state.preimage.as_slice())?;

// construct a whitelist ACL
// TODO can we just use Address instead of AddressRaw?
Expand Down Expand Up @@ -54,7 +54,8 @@ mod tests {
fn test_evaluate() {
let signature_request = InitialState {
// `data` is an RLP serialized ETH transaction with the recipient set to `0x772b9a9e8aa1c9db861c6611a82d251db4fac990`
data: "0xef01808094772b9a9e8aa1c9db861c6611a82d251db4fac990019243726561746564204f6e20456e74726f7079018080".to_string().into_bytes(),
preimage: "0xef01808094772b9a9e8aa1c9db861c6611a82d251db4fac990019243726561746564204f6e20456e74726f7079018080".to_string().into_bytes(),
extra: None
};

assert!(BasicTransaction::evaluate(signature_request).is_ok());
Expand All @@ -64,7 +65,8 @@ mod tests {
fn test_start_fail() {
let signature_request = InitialState {
// `data` is the same as previous test, but recipient address ends in `1` instead of `0`, so it should fail
data: "0xef01808094772b9a9e8aa1c9db861c6611a82d251db4fac991019243726561746564204f6e20456e74726f7079018080".to_string().into_bytes(),
preimage: "0xef01808094772b9a9e8aa1c9db861c6611a82d251db4fac991019243726561746564204f6e20456e74726f7079018080".to_string().into_bytes(),
extra: None
};

assert!(BasicTransaction::evaluate(signature_request).is_err());
Expand Down
13 changes: 9 additions & 4 deletions examples/siwe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct Siwe;

impl Program for Siwe {
fn evaluate(signature_request: InitialState) -> Result<(), Error> {
let string_message = String::from_utf8(signature_request.data)
let string_message = String::from_utf8(signature_request.preimage)
.map_err(|err| Error::Evaluation(err.to_string()))?;
let siwe_message = string_message
.parse::<Message>()
Expand All @@ -43,12 +43,14 @@ export_program!(Siwe);
// write a test that calls evaluate and passes it the proper parameters
#[cfg(test)]
mod tests {
use alloc::vec;

use super::*;

#[test]
fn test_should_sign() {
let signature_request = InitialState {
data: "localhost wants you to sign in with your Ethereum account:
preimage: "localhost wants you to sign in with your Ethereum account:
0x6Ee9894c677EFa1c56392e5E7533DE76004C8D94
This is a test statement.
Expand All @@ -60,6 +62,7 @@ Nonce: oNCEHm5jzQU2WvuBB
Issued At: 2022-01-28T23:28:16.013Z"
.to_string()
.into_bytes(),
extra: None
};

assert!(Siwe::evaluate(signature_request).is_ok());
Expand All @@ -68,7 +71,7 @@ Issued At: 2022-01-28T23:28:16.013Z"
#[test]
fn test_bad_siwe_message() {
let signature_request = InitialState {
data: "localhost does not want you to sign in with your Ethereum account:
preimage: "localhost does not want you to sign in with your Ethereum account:
0x6Ee9894c677EFa1c56392e5E7533DE76004C8D94
This is a test statement.
Expand All @@ -80,6 +83,7 @@ Nonce: oNCEHm5jzQU2WvuBB
Issued At: 2022-01-28T23:28:16.013Z"
.to_string()
.into_bytes(),
extra: None
};

assert!(Siwe::evaluate(signature_request).is_err());
Expand All @@ -88,7 +92,7 @@ Issued At: 2022-01-28T23:28:16.013Z"
#[test]
fn test_bad_domain() {
let signature_request = InitialState {
data: "google.com does not want you to sign in with your Ethereum account:
preimage: "google.com does not want you to sign in with your Ethereum account:
0x6Ee9894c677EFa1c56392e5E7533DE76004C8D94
This is a test statement.
Expand All @@ -100,6 +104,7 @@ Nonce: oNCEHm5jzQU2WvuBB
Issued At: 2022-01-28T23:28:16.013Z"
.to_string()
.into_bytes(),
extra: None
};

assert!(Siwe::evaluate(signature_request).is_err());
Expand Down
6 changes: 4 additions & 2 deletions wit/application.wit
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ world program {
export evaluate: func(config: initial-state) -> result<_, error>

record initial-state {
/// The preimage of the user's data under program evaulation (eg. RLP-encoded ETH transaction request).
data: list<u8>
/// Preimage of the user's data under program evaulation (eg. RLP-encoded ETH transaction request).
preimage: list<u8>,
/// Auxiliary data required for program evaluation, that won't be signed (eg. zero-knowledge proof, third party signature)
extra: option<list<u8>>
}
}

0 comments on commit 596a546

Please sign in to comment.