Skip to content

Commit

Permalink
Merge branch 'feat/v1' of https://github.com/zkemail/zk-email-wallet
Browse files Browse the repository at this point in the history
…into feat/v1
  • Loading branch information
SoraSuegami committed Oct 23, 2023
2 parents 9df5869 + eadf900 commit a8f4b58
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 9 deletions.
17 changes: 17 additions & 0 deletions packages/contracts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,20 @@ Copy the addresses from log:
NFT Extension deployed at: 0xb7F8bD28719aA118EcD8D01293acEe0E804b2EE6
Uniswap Extension deployed at: 0x6CE6893f06A438A85686DC1104688ad3b032de05
```

#### Deploy ECDSAOwnedDKIMRegistry
Set the `SIGNER` to the address of the Ethereum wallet who will be setting the DKIM public key for a domain.

```
PRIVATE_KEY="" \
SIGNER=0x2f6e79a6e1a982a49ca248b70b02f76e921af400 \
forge script script/DeployECDSAOwnedDKIMRegistry.s.sol:Deploy \
-vvvv \
--rpc-url https://ethereum-sepolia.publicnode.com \
--chain-id 11155111 \
--broadcast \
--etherscan-api-key "S7RWR1ENYB73HZY7WTV3EER7U8CQNBBTAJ" \
--verify
```

Copy the address from log `ECDSAOwnedDKIMRegistry deployed at: 0xB50a02E2Da524feC1209542985b2ae2917aF7265`
30 changes: 30 additions & 0 deletions packages/contracts/script/DeployECDSAOwnedDKIMRegistry.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import "forge-std/Script.sol";
import "forge-std/console.sol";
import "../src/utils/ECDSAOwnedDKIMRegistry.sol";

contract Deploy is Script {
function run() external {
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
if (deployerPrivateKey == 0) {
console.log("PRIVATE_KEY env var not set");
return;
}

address signer = vm.envAddress("SIGNER");
if (signer == address(0)) {
console.log("SIGNER env var not set");
return;
}

uint signValidityDuration = 1 hours;

vm.startBroadcast(deployerPrivateKey);
ECDSAOwnedDKIMRegistry dkim = new ECDSAOwnedDKIMRegistry(signer, signValidityDuration);
vm.stopBroadcast();

console.log("ECDSAOwnedDKIMRegistry deployed at: %s", address(dkim));
}
}
2 changes: 1 addition & 1 deletion packages/contracts/src/utils/ECDSAOwnedDKIMRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ contract ECDSAOwnedDKIMRegistry is IDKIMRegistry {
DKIMRegistry public dkimRegistry;
address public signer;

constructor(address _signer, uint _signValidityDuration) {
constructor(address _signer, uint256 _signValidityDuration) {
dkimRegistry = new DKIMRegistry();
signer = _signer;
signValidityDuration = _signValidityDuration;
Expand Down
46 changes: 44 additions & 2 deletions packages/contracts/test/EmailWalletCore.cmd.extension.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ contract ExtensionCommandTest is EmailWalletCoreTestHelper {
NFTExtension nftExtension;
DummyNFT dummyNFT;
string[][] public nftExtTemplates = new string[][](1);
string[][] public mockExtTemplates = new string[][](9);
string[][] public mockExtTemplates = new string[][](10);

function setUp() public override {
super.setUp();
Expand All @@ -36,7 +36,7 @@ contract ExtensionCommandTest is EmailWalletCoreTestHelper {
mockExtTemplates[5] = ["Test", "Request Token Twice", "{tokenAmount}"];
mockExtTemplates[6] = ["Test", "Deposit Token", "{tokenAmount}"];
mockExtTemplates[7] = ["Test", "Execute on", "{address}"];
// A dummy template to test the subject matchers that are not above
// Dummy templates to test the subject matchers that are not above
// mockExtension has wont do anything with this template
mockExtTemplates[8] = [
"Test",
Expand All @@ -51,6 +51,12 @@ contract ExtensionCommandTest is EmailWalletCoreTestHelper {
"then send to",
"{address}"
];
mockExtTemplates[9] = [
"Test",
"to",
"{recipient}",
"now"
];
extensionHandler.publishExtension("mockExtension", address(mockExtension), mockExtTemplates, 0.1 ether);

EmailOp memory emailOp = _getBaseEmailOp();
Expand Down Expand Up @@ -146,6 +152,42 @@ contract ExtensionCommandTest is EmailWalletCoreTestHelper {
vm.stopPrank();
}

function test_SubjectWithEmailInBetween() public {
bytes memory subject = new bytes(22);
subject[0] = "T";
subject[1] = "e";
subject[2] = "s";
subject[3] = "t";
subject[4] = " ";
subject[5] = "t";
subject[6] = "o";
subject[7] = " ";

// Assume emailAddr is 10 chars
for(uint i = 8; i < 18; i++) {
subject[i] = 0x0;
}

subject[18] = " ";
subject[19] = "n";
subject[20] = "o";
subject[21] = "w";

EmailOp memory emailOp = _getBaseEmailOp();
emailOp.command = "Test";
// If the subject email is in between, then the padding will be equal to email addr length
emailOp.maskedSubject = string(subject);
emailOp.extensionParams.subjectTemplateIndex = 9;
emailOp.extensionParams.subjectParams = new bytes[](0);
emailOp.hasEmailRecipient = true;
emailOp.recipientEmailAddrCommit = bytes32(uint256(32333));
emailOp.numRecipientEmailAddrBytes = 10;

vm.startPrank(relayer);
core.handleEmailOp(emailOp); // We only need to verify subjects match (i.e dont revert)
vm.stopPrank();
}

function test_RevertIf_CommandIsInvalid() public {
EmailOp memory emailOp = _getBaseEmailOp();
emailOp.command = "INVALID ";
Expand Down
1 change: 0 additions & 1 deletion packages/prover/coordinator.py

This file was deleted.

31 changes: 28 additions & 3 deletions packages/relayer/src/chain.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::*;

use ethers::prelude::*;

#[derive(Default)]
pub(crate) struct WalletParams {
pub(crate) token_name: String,
Expand Down Expand Up @@ -46,13 +44,40 @@ pub(crate) struct AccountCreation {
}

#[derive(Default)]
pub(crate) struct AccountInitialization {}
pub(crate) struct AccountInitialization {
pub(crate) email_addr_pointer: [u8; 32],
pub(crate) email_domain: String,
pub(crate) email_timestamp: U256,
pub(crate) email_nullifier: [u8; 32],
pub(crate) proof: Bytes,
}

pub(crate) async fn call_handle_email_op(email_op: EmailOp) -> Result<String> {
let provider = Provider::<Http>::try_from(CHAIN_RPC_PROVIDER.get().unwrap())?;

let wallet: LocalWallet = PRIVATE_KEY.get().unwrap().parse()?;
let client = Arc::new(SignerMiddleware::new(
provider,
wallet.with_chain_id(*CHAIN_ID.get().unwrap()),
));

let contract_address: Address = CORE_CONTRACT_ADDRESS.get().unwrap().parse()?;

let abi_source = "./packages/contracts/artifacts/EmailWalletCore.sol/EmailWalletCore.json";

// client.send_transaction(tx, block)

todo!()
}

pub(crate) async fn call_account_creation_op(data: AccountCreation) -> Result<String> {
let provider = Provider::<Http>::try_from(CHAIN_RPC_PROVIDER.get().unwrap())?;

let wallet: LocalWallet = PRIVATE_KEY.get().unwrap().parse()?;
let client = SignerMiddleware::new(provider, wallet.with_chain_id(*CHAIN_ID.get().unwrap()));

// client.se

todo!()
}

Expand Down
8 changes: 8 additions & 0 deletions packages/relayer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ pub struct RelayerConfig {
pub(crate) web_server_address: String,
pub(crate) circuits_dir_path: PathBuf,
pub(crate) prover_address: String,
pub(crate) chain_rpc_provider: String,
pub(crate) chain_id: u32,
pub(crate) private_key: String,
pub(crate) core_contract_address: String,
}

impl RelayerConfig {
Expand Down Expand Up @@ -57,6 +61,10 @@ impl RelayerConfig {
web_server_address: env::var(WEB_SERVER_ADDRESS_KEY).unwrap(),
circuits_dir_path: env::var(CIRCUITS_DIR_PATH_KEY).unwrap().into(),
prover_address: env::var(PROVER_ADDRESS_KEY).unwrap(),
chain_rpc_provider: env::var(CHAIN_RPC_PROVIDER_KEY).unwrap(),
chain_id: env::var(CHAIN_ID_KEY).unwrap().parse().unwrap(),
private_key: env::var(PRIVATE_KEY_KEY).unwrap(),
core_contract_address: env::var(CORE_CONTRACT_ADDRESS_KEY).unwrap(),
}
}
}
Expand Down
31 changes: 29 additions & 2 deletions packages/relayer/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,27 @@ pub(crate) async fn handle_email(
generate_proof(&input, "generateSendProof", PROVER_ADDRESS.get().unwrap())
.await?;

let data = AccountInitialization::default();
let res = call_account_initialization_op(data).await?;
let relayer_rand = hex2field(RELAYER_RAND.get().unwrap())?;
let email_addr_pointer = PaddedEmailAddr::from_email_addr(&from_address)
.to_pointer(&RelayerRand(relayer_rand))?;
let email_addr_pointer = Fr::to_bytes(&email_addr_pointer);

let data = AccountInitialization {
email_addr_pointer,
email_domain: get_email_domain(&from_address)?,
email_timestamp: U256::from(parsed_email.get_timestamp()?),
email_nullifier: Fr::to_bytes(&email_nullifier(&parsed_email.signature)?),
proof: Bytes::from(proof.into_bytes()),
};
let result = call_account_initialization_op(data).await?;

tx.send(EmailMessage {
subject: "Your Account was initialized".to_string(),
body: result,
to: from_address,
message_id: None,
})
.unwrap();
}
}
} else {
Expand Down Expand Up @@ -108,6 +127,14 @@ pub(crate) async fn handle_email(
};

let result = call_handle_email_op(email_op).await?;

tx.send(EmailMessage {
subject: "Transaction were completed".to_string(),
body: result,
to: from_address,
message_id: None,
})
.unwrap();
}

Ok(())
Expand Down
11 changes: 11 additions & 0 deletions packages/relayer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,28 @@ use std::sync::{Arc, OnceLock};

use anyhow::{anyhow, bail, Result};
use email_wallet_utils::{converters::*, cryptos::*, parse_email::ParsedEmail, Fr};
use ethers::prelude::*;

static CIRCUITS_DIR_PATH: OnceLock<PathBuf> = OnceLock::new();
static WEB_SERVER_ADDRESS: OnceLock<String> = OnceLock::new();
static RELAYER_RAND: OnceLock<String> = OnceLock::new();
static PROVER_ADDRESS: OnceLock<String> = OnceLock::new();
static PRIVATE_KEY: OnceLock<String> = OnceLock::new();
static CHAIN_ID: OnceLock<u32> = OnceLock::new();
static CHAIN_RPC_PROVIDER: OnceLock<String> = OnceLock::new();
static CORE_CONTRACT_ADDRESS: OnceLock<String> = OnceLock::new();

pub async fn run(config: RelayerConfig) -> Result<()> {
CIRCUITS_DIR_PATH.set(config.circuits_dir_path).unwrap();
WEB_SERVER_ADDRESS.set(config.web_server_address).unwrap();
RELAYER_RAND.set(config.relayer_randomness).unwrap();
PROVER_ADDRESS.set(config.prover_address).unwrap();
PRIVATE_KEY.set(config.private_key).unwrap();
CHAIN_ID.set(config.chain_id).unwrap();
CHAIN_RPC_PROVIDER.set(config.chain_rpc_provider).unwrap();
CORE_CONTRACT_ADDRESS
.set(config.core_contract_address)
.unwrap();

let (tx_handler, mut rx_handler) = tokio::sync::mpsc::unbounded_channel();
let (tx_sender, mut rx_sender) = tokio::sync::mpsc::unbounded_channel::<EmailMessage>();
Expand Down
1 change: 1 addition & 0 deletions packages/relayer/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use relayer::*;

use anyhow::Result;
use ethers::prelude::*;

#[tokio::main]
async fn main() -> Result<()> {
Expand Down
4 changes: 4 additions & 0 deletions packages/relayer/src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ pub const RELAYER_RANDOMNESS_KEY: &str = "RELAYER_RANDOMNESS";
pub const WEB_SERVER_ADDRESS_KEY: &str = "WEB_SERVER_ADDRESS";
pub const CIRCUITS_DIR_PATH_KEY: &str = "CIRCUITS_DIR_PATH";
pub const PROVER_ADDRESS_KEY: &str = "PROVER_ADDRESS";
pub const CHAIN_RPC_PROVIDER_KEY: &str = "CHAIN_RPC_PROVIDER";
pub const PRIVATE_KEY_KEY: &str = "PRIVATE_KEY";
pub const CHAIN_ID_KEY: &str = "CHAIN_ID";
pub const CORE_CONTRACT_ADDRESS_KEY: &str = "CORE_CONTRACT_ADDRESS";

// Error strings
pub const WRONG_AUTH_METHOD: &str = "Not supported auth type";
Expand Down

0 comments on commit a8f4b58

Please sign in to comment.