Skip to content

Commit e88b2e9

Browse files
authored
feat: Add to_addr field in API (#48)
* feat: add to_addr * test: replace code test * fix tests change apis package.json change apis package.json
1 parent 29d589e commit e88b2e9

File tree

15 files changed

+556
-14
lines changed

15 files changed

+556
-14
lines changed

.cargo/config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
paths = ["./"]
2+
3+
[target.x86_64-unknown-linux-gnu]
4+
rustflags = ["-Clink-arg=-Wl,--allow-multiple-definition"]

.github/workflows/test.yaml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ jobs:
1212
uses: actions/setup-node@v3
1313
with:
1414
node-version: 18
15-
- name: "Setup Rust"
16-
uses: ATiltedTree/setup-rust@v1
15+
- name: Setup Rust
16+
uses: actions-rs/toolchain@v1
1717
with:
18-
rust-version: nightly
19-
components: clippy
18+
toolchain: stable
19+
override: true
20+
components: rustfmt, clippy
2021
- name: Download circom (Linux)
2122
run: wget https://github.com/iden3/circom/releases/latest/download/circom-linux-amd64 -O /usr/local/bin/circom && chmod +x /usr/local/bin/circom
2223
- name: Install yarn

packages/apis/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ itertools = "0.10.3"
2323
thiserror = "1.0.40"
2424
serde_json = "1.0.95"
2525

26+
2627
[dependencies.neon]
2728
version = "0.10"
2829
default-features = false

packages/apis/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"build": "cargo-cp-artifact -nc index.node -- cargo build --message-format=json-render-diagnostics",
1818
"build-debug": "npm run build --",
1919
"build-release": "npm run build -- --release",
20-
"install": "node-pre-gyp install --update-binary --fallback-to-build=false || npm run build-release",
20+
"install": "npm run build-debug",
21+
"install-release": "node-pre-gyp install --update-binary --fallback-to-build=false || npm run build-release",
2122
"test": "cargo test",
2223
"package": "node-pre-gyp package",
2324
"upload-binary": "npm run package && node-pre-gyp-github publish"
@@ -35,4 +36,4 @@
3536
"package_name": "apis-{node_abi}-{platform}-{arch}.tar.gz",
3637
"module_path": "./"
3738
}
38-
}
39+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"parts": [
3+
{
4+
"is_public": false,
5+
"regex_def": "((\r\n)|^)to:"
6+
},
7+
{
8+
"is_public": false,
9+
"regex_def": "([^\r\n]+<)?"
10+
},
11+
{
12+
"is_public": true,
13+
"regex_def": "(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|0|1|2|3|4|5|6|7|8|9|!|#|$|%|&|'|\\*|\\+|-|/|=|\\?|^|_|`|{|\\||}|~|\\.)+@(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|0|1|2|3|4|5|6|7|8|9|_|\\.|-)+"
14+
},
15+
{
16+
"is_public": false,
17+
"regex_def": ">?\r\n"
18+
}
19+
]
20+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"parts": [
3+
{
4+
"is_public": false,
5+
"regex_def": "((\r\n)|^)to:"
6+
},
7+
{
8+
"is_public": true,
9+
"regex_def": "[^\r\n]+"
10+
},
11+
{
12+
"is_public": false,
13+
"regex_def": "\r\n"
14+
}
15+
]
16+
}

packages/apis/src/extract_substrs.rs

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
use zk_regex_compiler::DecomposedRegexConfig;
21
use fancy_regex::Regex;
3-
42
use neon::prelude::*;
53
use serde_json;
64
use thiserror::Error;
5+
use zk_regex_compiler::DecomposedRegexConfig;
76

87
/// Error definitions of the compiler.
98
#[derive(Error, Debug)]
@@ -102,6 +101,17 @@ pub fn extract_from_addr_idxes(
102101
extract_substr_idxes(input_str, &regex_config)
103102
}
104103

104+
pub fn extract_to_all_idxes(input_str: &str) -> Result<Vec<(usize, usize)>, ExtractSubstrssError> {
105+
let regex_config = serde_json::from_str(include_str!("./decomposed_defs/to_all.json")).unwrap();
106+
extract_substr_idxes(input_str, &regex_config)
107+
}
108+
109+
pub fn extract_to_addr_idxes(input_str: &str) -> Result<Vec<(usize, usize)>, ExtractSubstrssError> {
110+
let regex_config =
111+
serde_json::from_str(include_str!("./decomposed_defs/to_addr.json")).unwrap();
112+
extract_substr_idxes(input_str, &regex_config)
113+
}
114+
105115
pub fn extract_subject_all_idxes(
106116
input_str: &str,
107117
) -> Result<Vec<(usize, usize)>, ExtractSubstrssError> {
@@ -247,6 +257,49 @@ pub fn extract_from_addr_idxes_node(mut cx: FunctionContext) -> JsResult<JsArray
247257
Ok(js_array)
248258
}
249259

260+
pub fn extract_to_all_idxes_node(mut cx: FunctionContext) -> JsResult<JsArray> {
261+
let input_str = cx.argument::<JsString>(0)?.value(&mut cx);
262+
263+
let substr_idxes = match extract_to_all_idxes(&input_str) {
264+
Ok(substr_idxes) => substr_idxes,
265+
Err(e) => return cx.throw_error(e.to_string()),
266+
};
267+
268+
let js_array = JsArray::new(&mut cx, substr_idxes.len() as u32);
269+
270+
for (i, (start_idx, end_idx)) in substr_idxes.iter().enumerate() {
271+
let start_end_array = JsArray::new(&mut cx, 2u32);
272+
273+
let start_idx = cx.number(*start_idx as f64);
274+
start_end_array.set(&mut cx, 0, start_idx)?;
275+
276+
let end_idx = cx.number(*end_idx as f64);
277+
start_end_array.set(&mut cx, 1, end_idx)?;
278+
279+
js_array.set(&mut cx, i as u32, start_end_array)?;
280+
}
281+
282+
Ok(js_array)
283+
}
284+
285+
pub fn extract_to_addr_idxes_node(mut cx: FunctionContext) -> JsResult<JsArray> {
286+
let input_str = cx.argument::<JsString>(0)?.value(&mut cx);
287+
let substr_idxes = match extract_to_addr_idxes(&input_str) {
288+
Ok(substr_idxes) => substr_idxes,
289+
Err(e) => return cx.throw_error(e.to_string()),
290+
};
291+
let js_array = JsArray::new(&mut cx, substr_idxes.len() as u32);
292+
for (i, (start_idx, end_idx)) in substr_idxes.iter().enumerate() {
293+
let start_end_array = JsArray::new(&mut cx, 2u32);
294+
let start_idx = cx.number(*start_idx as f64);
295+
start_end_array.set(&mut cx, 0, start_idx)?;
296+
let end_idx = cx.number(*end_idx as f64);
297+
start_end_array.set(&mut cx, 1, end_idx)?;
298+
js_array.set(&mut cx, i as u32, start_end_array)?;
299+
}
300+
Ok(js_array)
301+
}
302+
250303
pub fn extract_subject_all_idxes_node(mut cx: FunctionContext) -> JsResult<JsArray> {
251304
let input_str = cx.argument::<JsString>(0)?.value(&mut cx);
252305
let substr_idxes = match extract_subject_all_idxes(&input_str) {
@@ -369,13 +422,13 @@ mod test {
369422
}
370423

371424
#[test]
372-
fn test_code_in_subject_valid() {
425+
fn test_code_in_email_address_valid() {
373426
let code_regex = DecomposedRegexConfig {
374427
// max_byte_size: 1024,
375428
parts: vec![
376429
RegexPartConfig {
377430
is_public: false,
378-
regex_def: "CODE:0x".to_string(),
431+
regex_def: "ACCOUNTKEY.0x".to_string(),
379432
// max_size: 7,
380433
// solidity: None
381434
},
@@ -387,9 +440,9 @@ mod test {
387440
},
388441
],
389442
};
390-
let input_str = "subject:Email Wallet CODE:0x123abc";
443+
let input_str = "[email protected]";
391444
let idxes = extract_substr_idxes(input_str, &code_regex).unwrap();
392-
assert_eq!(idxes, vec![(28, 34)]);
445+
assert_eq!(idxes, vec![(21, 27)]);
393446
}
394447

395448
#[test]

packages/apis/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> {
1717
)?;
1818
cx.export_function("extractFromAllIdxes", extract_from_all_idxes_node)?;
1919
cx.export_function("extractFromAddrIdxes", extract_from_addr_idxes_node)?;
20+
cx.export_function("extractToAllIdxes", extract_to_all_idxes_node)?;
21+
cx.export_function("extractToAddrIdxes", extract_to_addr_idxes_node)?;
2022
cx.export_function("extractSubjectAllIdxes", extract_subject_all_idxes_node)?;
2123
cx.export_function("extractBodyHashIdxes", extract_body_hash_idxes_node)?;
2224
cx.export_function("extractTimestampIdxes", extract_timestamp_idxes_node)?;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"parts": [
3+
{
4+
"is_public": false,
5+
"regex_def": "((\r\n)|^)to:"
6+
},
7+
{
8+
"is_public": false,
9+
"regex_def": "([^\r\n]+<)?"
10+
},
11+
{
12+
"is_public": true,
13+
"regex_def": "(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|0|1|2|3|4|5|6|7|8|9|!|#|$|%|&|'|\\*|\\+|-|/|=|\\?|^|_|`|{|\\||}|~|\\.)+@(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|0|1|2|3|4|5|6|7|8|9|_|\\.|-)+"
14+
},
15+
{
16+
"is_public": false,
17+
"regex_def": ">?\r\n"
18+
}
19+
]
20+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
pragma circom 2.1.5;
2+
3+
include "@zk-email/zk-regex-circom/circuits/regex_helpers.circom";
4+
include "@zk-email/zk-regex-circom/circuits/common/to_all_regex.circom";
5+
include "@zk-email/zk-regex-circom/circuits/common/email_addr_regex.circom";
6+
include "@zk-email/zk-regex-circom/circuits/common/email_addr_with_name_regex.circom";
7+
8+
9+
template ToAddrRegex(msg_bytes) {
10+
signal input msg[msg_bytes];
11+
signal output out;
12+
signal output reveal0[msg_bytes];
13+
14+
signal toOut;
15+
signal toReveal[msg_bytes];
16+
(toOut, toReveal) <== ToAllRegex(msg_bytes)(msg);
17+
toOut === 1;
18+
19+
signal emailNameOut;
20+
signal emailNameReveal[msg_bytes];
21+
(emailNameOut, emailNameReveal) <== EmailAddrWithNameRegex(msg_bytes)(toReveal);
22+
23+
signal emailAddrOut;
24+
signal emailAddrReveal[msg_bytes];
25+
(emailAddrOut, emailAddrReveal) <== EmailAddrRegex(msg_bytes)(toReveal);
26+
27+
out <== MultiOR(2)([emailNameOut, emailAddrOut]);
28+
for(var i=0; i<msg_bytes; i++) {
29+
reveal0[i] <== emailNameOut * (emailNameReveal[i] - emailAddrReveal[i]) + emailAddrReveal[i];
30+
}
31+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"parts": [
3+
{
4+
"is_public": false,
5+
"regex_def": "((\r\n)|^)to:"
6+
},
7+
{
8+
"is_public": true,
9+
"regex_def": "[^\r\n]+"
10+
},
11+
{
12+
"is_public": false,
13+
"regex_def": "\r\n"
14+
}
15+
]
16+
}

0 commit comments

Comments
 (0)