Skip to content

Commit 5828d30

Browse files
committed
fix: shielded context for nodejs feature
1 parent 6326da1 commit 5828d30

File tree

4 files changed

+62
-14
lines changed

4 files changed

+62
-14
lines changed

apps/extension/src/background/sdk/service.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ const {
88
defaultTokenAddress = "tnam1qxgfw7myv4dh0qna4hq0xdg6lx77fzl7dcem8h7e",
99
} = process.env;
1010

11+
// Extension does not care about the MASP indexer - this will map to None in Rust
12+
const MASP_INDEXER_URL = "";
13+
1114
export class SdkService {
1215
private constructor(
1316
private rpc: string,
@@ -27,6 +30,12 @@ export class SdkService {
2730
}
2831

2932
getSdk(): Sdk {
30-
return getSdk(this.cryptoMemory, this.rpc, "NOT USED DB NAME", this.token);
33+
return getSdk(
34+
this.cryptoMemory,
35+
this.rpc,
36+
MASP_INDEXER_URL,
37+
"NOT USED DB NAME",
38+
this.token
39+
);
3140
}
3241
}

packages/shared/lib/Cargo.lock

Lines changed: 27 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/shared/lib/src/sdk/masp/masp_node.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use async_trait::async_trait;
22
use namada_sdk::{
33
borsh::{BorshDeserialize, BorshSerialize},
4-
masp::{ContextSyncStatus, DispatcherCache, ShieldedContext, ShieldedUtils},
4+
masp::{ContextSyncStatus, DispatcherCache, ShieldedUtils},
55
masp_proofs::prover::LocalTxProver,
6+
ShieldedWallet,
67
};
78
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
89

@@ -21,6 +22,8 @@ const FILE_NAME: &str = "shielded.dat";
2122
const TMP_FILE_NAME: &str = "shielded.tmp";
2223
const SPECULATIVE_FILE_NAME: &str = "speculative_shielded.dat";
2324
const SPECULATIVE_TMP_FILE_NAME: &str = "speculative_shielded.tmp";
25+
const CACHE_FILE_NAME: &str = "shielded_sync.cache";
26+
const CACHE_FILE_TMP_PREFIX: &str = "shielded_sync.cache.tmp";
2427

2528
/// Mostly copied from the Namada CLI
2629
@@ -32,7 +35,7 @@ pub struct NodeShieldedUtils {
3235
}
3336

3437
impl NodeShieldedUtils {
35-
pub async fn new(context_dir: &str) -> ShieldedContext<Self> {
38+
pub async fn new(context_dir: &str) -> ShieldedWallet<Self> {
3639
let context_dir = PathBuf::from(context_dir);
3740

3841
let spend_path = context_dir.join(SPEND_NAME);
@@ -56,7 +59,7 @@ impl NodeShieldedUtils {
5659

5760
let utils = Self { context_dir };
5861

59-
ShieldedContext {
62+
ShieldedWallet {
6063
utils,
6164
sync_status,
6265
..Default::default()
@@ -85,7 +88,7 @@ impl ShieldedUtils for NodeShieldedUtils {
8588

8689
async fn load<U: ShieldedUtils>(
8790
&self,
88-
ctx: &mut ShieldedContext<U>,
91+
ctx: &mut ShieldedWallet<U>,
8992
force_confirmed: bool,
9093
) -> std::io::Result<()> {
9194
let file_name = if force_confirmed {
@@ -101,14 +104,14 @@ impl ShieldedUtils for NodeShieldedUtils {
101104
//TODO: change to_bytes to sth more descripive, add "from_bytes"
102105
let bytes = to_bytes(read_file_sync(path).unwrap().into());
103106

104-
*ctx = ShieldedContext {
107+
*ctx = ShieldedWallet {
105108
utils: ctx.utils.clone(),
106-
..ShieldedContext::<U>::deserialize(&mut &bytes[..])?
109+
..ShieldedWallet::<U>::deserialize(&mut &bytes[..])?
107110
};
108111
Ok(())
109112
}
110113

111-
async fn save<U: ShieldedUtils>(&self, ctx: &ShieldedContext<U>) -> std::io::Result<()> {
114+
async fn save<U: ShieldedUtils>(&self, ctx: &ShieldedWallet<U>) -> std::io::Result<()> {
112115
let (tmp_file_name, file_name) = match ctx.sync_status {
113116
ContextSyncStatus::Confirmed => (TMP_FILE_NAME, FILE_NAME),
114117
ContextSyncStatus::Speculative => (SPECULATIVE_TMP_FILE_NAME, SPECULATIVE_FILE_NAME),
@@ -139,9 +142,20 @@ impl ShieldedUtils for NodeShieldedUtils {
139142

140143
/// Save a cache of data as part of shielded sync if that
141144
/// process gets interrupted.
142-
async fn cache_save(&self, _cache: &DispatcherCache) -> std::io::Result<()> {
143-
// TODO:
144-
todo!()
145+
async fn cache_save(&self, cache: &DispatcherCache) -> std::io::Result<()> {
146+
let tmp_path = path_buf_to_js_value(self.context_dir.join(CACHE_FILE_TMP_PREFIX));
147+
{
148+
let mut bytes = Vec::new();
149+
cache.serialize(&mut bytes).expect("cannot serialize cache");
150+
let uint8_array = js_sys::Uint8Array::from(&bytes[..]);
151+
152+
write_file_sync(tmp_path.clone(), uint8_array.into()).unwrap();
153+
}
154+
155+
let new_path = path_buf_to_js_value(self.context_dir.join(CACHE_FILE_NAME));
156+
renameSync(tmp_path, new_path).unwrap();
157+
158+
Ok(())
145159
}
146160

147161
/// Load a cache of data as part of shielded sync if that

packages/shared/lib/src/sdk/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl Sdk {
102102
let context_dir = context_dir.as_string().unwrap();
103103

104104
let mut shielded = self.namada.shielded_mut().await;
105-
*shielded = masp::JSShieldedUtils::new(&context_dir).await;
105+
*shielded = ShieldedContext::new(masp::JSShieldedUtils::new(&context_dir).await);
106106

107107
Ok(())
108108
}

0 commit comments

Comments
 (0)