Skip to content

Commit e7126b2

Browse files
Account for external records when computing commitments in SnapshotQuery
1 parent bce4d9f commit e7126b2

File tree

8 files changed

+98
-26
lines changed

8 files changed

+98
-26
lines changed

wasm/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

wasm/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@provablehq/wasm",
33
"version": "0.9.8",
4-
"type": "module",
4+
"type":"module",
55
"description": "SnarkVM WASM binaries with javascript bindings",
66
"collaborators": [
77
"The Provable Team"

wasm/src/programs/snapshot_query.rs

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl SnapshotQuery {
113113
.enumerate()
114114
.filter_map(|(index, js_value)| {
115115
if let Some(s) = js_value.as_string() {
116-
// Detect if the string contains a nonce to indicate it's a plaintext record.
116+
// Detect if the string contains a nonce to indicate the input is a plaintext record.
117117
if !s.contains("_nonce") {
118118
return None;
119119
};
@@ -125,13 +125,15 @@ impl SnapshotQuery {
125125
let program_id = program.id();
126126
let function = program.get_function(function_id).ok()?;
127127
let input = function.inputs().get_index(index)?;
128-
let record_name = match input.value_type() {
129-
&ValueTypeNative::Record(record_name) => record_name,
130-
_ => return None,
131-
};
132-
133-
// Compute the commitment.
134-
record.to_commitment(program_id, &record_name, &record_view_key).ok()
128+
match input.value_type() {
129+
&ValueTypeNative::Record(record_name) => {
130+
record.to_commitment(program_id, &record_name, &record_view_key).ok()
131+
}
132+
&ValueTypeNative::ExternalRecord(locator) => {
133+
record.to_commitment(locator.program_id(), locator.resource(), &record_view_key).ok()
134+
}
135+
_ => None,
136+
}
135137
} else {
136138
None
137139
}
@@ -246,7 +248,12 @@ impl QueryTrait<CurrentNetwork> for SnapshotQuery {
246248

247249
mod tests {
248250
use super::*;
249-
use crate::{test::PROVABLE_API, utilities::rest::get_network};
251+
use crate::{
252+
test::{PROVABLE_API, TOKEN_REGISTRY_RECORD_OWNER_VIEW_KEY, TOKEN_REGISTRY_RECORD_V1},
253+
types::native::ProgramIDNative,
254+
utilities::{rest::get_network, test::programs::EXTERNAL_RECORDS_DEMO},
255+
};
256+
use snarkvm_synthesizer_program::Program;
250257
use wasm_bindgen_test::*;
251258

252259
#[wasm_bindgen_test]
@@ -274,4 +281,25 @@ mod tests {
274281
assert!(height > 10_000_000);
275282
}
276283
}
284+
285+
#[wasm_bindgen_test]
286+
fn test_correct_commitment_computation() {
287+
let record = RecordPlaintextNative::from_str(TOKEN_REGISTRY_RECORD_V1).unwrap();
288+
let view_key = ViewKeyNative::from_str(TOKEN_REGISTRY_RECORD_OWNER_VIEW_KEY).unwrap();
289+
let program_id = ProgramIDNative::from_str("token_registry.aleo").unwrap();
290+
let record_name = IdentifierNative::from_str("Token").unwrap();
291+
let rvk = (*record.nonce() * *view_key).to_x_coordinate();
292+
let commitment = record.to_commitment(&program_id, &record_name, &rvk).unwrap();
293+
let program = Program::from_str(EXTERNAL_RECORDS_DEMO).unwrap();
294+
let function_id = IdentifierNative::from_str("deposit_private_token").unwrap();
295+
296+
let input_1 = JsValue::from_str("aleo1s3ws5tra87fjycnjrwsjcrnw2qxr8jfqqdugnf0xzqqw29q9m5pqem2u4t");
297+
let input_2 = JsValue::from_str("5u128");
298+
let input_3 = JsValue::from_str(TOKEN_REGISTRY_RECORD_V1);
299+
let inputs = vec![input_1, input_2, input_3];
300+
301+
let commitments =
302+
SnapshotQuery::collect_commitments_from_inputs(&program, &function_id, &view_key, &inputs).unwrap();
303+
assert_eq!(commitments[0], commitment);
304+
}
277305
}

wasm/src/record/record_plaintext.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,10 @@ mod tests {
334334

335335
use crate::{
336336
types::native::{PrivateKeyNative, ViewKeyNative},
337-
utilities::test::{CREDITS_RECORD_V1, CREDITS_SENDER_CIPHERTEXT, CREDITS_SENDER_PLAINTEXT, get_env},
337+
utilities::test::get_env,
338338
};
339339

340+
use crate::utilities::test::record::{CREDITS_RECORD_V1, CREDITS_SENDER_CIPHERTEXT, CREDITS_SENDER_PLAINTEXT};
340341
use wasm_bindgen_test::*;
341342

342343
const CREDITS_RECORD: &str = r"{

wasm/src/utilities/encrypt.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,16 +298,15 @@ impl EncryptionToolkit {
298298
#[cfg(test)]
299299
mod tests {
300300
use super::*;
301-
302301
use crate::{
303-
test::{
302+
test::get_env,
303+
types::native::{PrivateKeyNative, ViewKeyNative},
304+
utilities::test::record::{
304305
CREDITS_RECORD_V1,
305306
CREDITS_RECORD_VIEW_KEY,
306307
CREDITS_SENDER_CIPHERTEXT,
307308
CREDITS_SENDER_PLAINTEXT,
308-
get_env,
309309
},
310-
types::native::{PrivateKeyNative, ViewKeyNative},
311310
};
312311
use std::str::FromStr;
313312
use wasm_bindgen_test::wasm_bindgen_test;

wasm/src/utilities/test/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,8 @@ pub use plaintext::*;
2323
pub mod programs;
2424
pub use programs::*;
2525

26+
pub mod record;
27+
pub use record::*;
28+
2629
pub mod uri;
2730
pub use uri::*;

wasm/src/utilities/test/programs.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ use crate::{array, object};
1818

1919
use js_sys::{Array, Object};
2020

21-
/// V1 credits.aleo record.
22-
pub const CREDITS_RECORD_V1: &str = "{ owner: aleo12a4wll9ax6w5355jph0dr5wt2vla5sss2t4cnch0tc3vzh643v8qcfvc7a.private, microcredits: 1000000u64.private, _nonce: 3634848344765318974603121890869676775499130077229666060613233255327643175219group.public, _version: 1u8.public }";
21+
pub const EXTERNAL_RECORDS_DEMO: &str = r#"
22+
import token_registry.aleo;
23+
program external_record_test.aleo;
2324
24-
/// Record view key for the V1 credits.aleo record.
25-
pub const CREDITS_RECORD_VIEW_KEY: &str =
26-
"5237002936265850807349726649400053591020997883662246784632368923777787639801field";
27-
28-
/// Sender ciphertext of the credits.aleo record.
29-
pub const CREDITS_SENDER_CIPHERTEXT: &str =
30-
"1182590395568997043375432557467567048762179115999922880321493200728848194550field";
25+
function deposit_private_token:
26+
input r0 as address.private;
27+
input r1 as u128.private;
28+
input r2 as token_registry.aleo/Token.record;
29+
output r2 as token_registry.aleo/Token.record;
3130
32-
/// Sender plaintext of the credits.aleo record.
33-
pub const CREDITS_SENDER_PLAINTEXT: &str = "aleo1j92w9mhqznj2hvufad796y8suykjppk7f6n6xmncmktfm95vggzqx4sjlh";
31+
constructor:
32+
assert.eq program_owner aleo1rhgdu77hgyqd3xjj8ucu3jj9r2krwz6mnzyd80gncr5fxcwlh5rsvzp9px;
33+
"#;
3434

3535
pub const HELLO_PROGRAM: &str = r#"program hello.aleo;
3636
function main:

wasm/src/utilities/test/record.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (C) 2019-2025 Provable Inc.
2+
// This file is part of the Provable SDK library.
3+
4+
// The Provable SDK library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// The Provable SDK library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with the Provable SDK library. If not, see <https://www.gnu.org/licenses/>.
16+
17+
/// V1 credits.aleo record.
18+
pub const CREDITS_RECORD_V1: &str = "{ owner: aleo12a4wll9ax6w5355jph0dr5wt2vla5sss2t4cnch0tc3vzh643v8qcfvc7a.private, microcredits: 1000000u64.private, _nonce: 3634848344765318974603121890869676775499130077229666060613233255327643175219group.public, _version: 1u8.public }";
19+
/// Record view key for the V1 credits.aleo record.
20+
pub const CREDITS_RECORD_VIEW_KEY: &str =
21+
"5237002936265850807349726649400053591020997883662246784632368923777787639801field";
22+
/// Sender ciphertext of the credits.aleo record.
23+
pub const CREDITS_SENDER_CIPHERTEXT: &str =
24+
"1182590395568997043375432557467567048762179115999922880321493200728848194550field";
25+
/// Sender plaintext of the credits.aleo record.
26+
pub const CREDITS_SENDER_PLAINTEXT: &str = "aleo1j92w9mhqznj2hvufad796y8suykjppk7f6n6xmncmktfm95vggzqx4sjlh";
27+
28+
/// Token registry record view_key.
29+
pub const TOKEN_REGISTRY_RECORD_OWNER_VIEW_KEY: &str = "AViewKey1pTzjTxeAYuDpACpz2k72xQoVXvfY4bJHrjeAQp6Ywe5g";
30+
31+
/// Token registry test vector.
32+
pub const TOKEN_REGISTRY_RECORD_V1: &str = r#"{
33+
owner: aleo1s3ws5tra87fjycnjrwsjcrnw2qxr8jfqqdugnf0xzqqw29q9m5pqem2u4t.private,
34+
amount: 1000u128.private,
35+
token_id: 1751493913335802797273486270793650302076377624243810059080883537084141842600field.private,
36+
external_authorization_required: false.private,
37+
authorized_until: 0u32.private,
38+
_nonce: 353510505137682717871934563523691055502582931368477380633253282125012046603group.public,
39+
_version: 1u8.public
40+
}"#;

0 commit comments

Comments
 (0)