Skip to content

Commit

Permalink
fix: shielded context for nodejs feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszjasiuk committed Sep 19, 2024
1 parent 6326da1 commit ebb576d
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 16 deletions.
11 changes: 10 additions & 1 deletion apps/extension/src/background/sdk/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const {
defaultTokenAddress = "tnam1qxgfw7myv4dh0qna4hq0xdg6lx77fzl7dcem8h7e",
} = process.env;

// Extension does not care about the MASP indexer - this will map to None in Rust
const MASP_INDEXER_URL = "";

export class SdkService {
private constructor(
private rpc: string,
Expand All @@ -27,6 +30,12 @@ export class SdkService {
}

getSdk(): Sdk {
return getSdk(this.cryptoMemory, this.rpc, "NOT USED DB NAME", this.token);
return getSdk(
this.cryptoMemory,
this.rpc,
MASP_INDEXER_URL,
"NOT USED DB NAME",
this.token
);
}
}
4 changes: 2 additions & 2 deletions packages/sdk/src/tests/signing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe("Signing", () => {
validSignature.signature
);

expect(result).toBe(null);
expect(result).toBeUndefined();
});

it("should throw error when validating an invalid signature", () => {
Expand All @@ -40,6 +40,6 @@ describe("Signing", () => {
invalidSignature.signature
);

expect(verify).toThrowError();
expect(verify).toThrow();
});
});
29 changes: 27 additions & 2 deletions packages/shared/lib/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 24 additions & 10 deletions packages/shared/lib/src/sdk/masp/masp_node.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use async_trait::async_trait;
use namada_sdk::{
borsh::{BorshDeserialize, BorshSerialize},
masp::{ContextSyncStatus, DispatcherCache, ShieldedContext, ShieldedUtils},
masp::{ContextSyncStatus, DispatcherCache, ShieldedUtils},
masp_proofs::prover::LocalTxProver,
ShieldedWallet,
};
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};

Expand All @@ -21,6 +22,8 @@ const FILE_NAME: &str = "shielded.dat";
const TMP_FILE_NAME: &str = "shielded.tmp";
const SPECULATIVE_FILE_NAME: &str = "speculative_shielded.dat";
const SPECULATIVE_TMP_FILE_NAME: &str = "speculative_shielded.tmp";
const CACHE_FILE_NAME: &str = "shielded_sync.cache";
const CACHE_FILE_TMP_PREFIX: &str = "shielded_sync.cache.tmp";

/// Mostly copied from the Namada CLI

Expand All @@ -32,7 +35,7 @@ pub struct NodeShieldedUtils {
}

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

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

let utils = Self { context_dir };

ShieldedContext {
ShieldedWallet {
utils,
sync_status,
..Default::default()
Expand Down Expand Up @@ -85,7 +88,7 @@ impl ShieldedUtils for NodeShieldedUtils {

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

*ctx = ShieldedContext {
*ctx = ShieldedWallet {
utils: ctx.utils.clone(),
..ShieldedContext::<U>::deserialize(&mut &bytes[..])?
..ShieldedWallet::<U>::deserialize(&mut &bytes[..])?
};
Ok(())
}

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

/// Save a cache of data as part of shielded sync if that
/// process gets interrupted.
async fn cache_save(&self, _cache: &DispatcherCache) -> std::io::Result<()> {
// TODO:
todo!()
async fn cache_save(&self, cache: &DispatcherCache) -> std::io::Result<()> {
let tmp_path = path_buf_to_js_value(self.context_dir.join(CACHE_FILE_TMP_PREFIX));
{
let mut bytes = Vec::new();
cache.serialize(&mut bytes).expect("cannot serialize cache");
let uint8_array = js_sys::Uint8Array::from(&bytes[..]);

write_file_sync(tmp_path.clone(), uint8_array.into()).unwrap();
}

let new_path = path_buf_to_js_value(self.context_dir.join(CACHE_FILE_NAME));
renameSync(tmp_path, new_path).unwrap();

Ok(())
}

/// Load a cache of data as part of shielded sync if that
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/lib/src/sdk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl Sdk {
let context_dir = context_dir.as_string().unwrap();

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

Ok(())
}
Expand Down

0 comments on commit ebb576d

Please sign in to comment.