Skip to content

Commit

Permalink
feat: fetch and load masp params
Browse files Browse the repository at this point in the history
  • Loading branch information
jurevans committed Sep 24, 2024
1 parent 6bd1e33 commit edba58b
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 47 deletions.
2 changes: 1 addition & 1 deletion apps/namadillo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"dev": "vite",
"preview": "vite preview",
"dev:local": "NODE_ENV=development NAMADA_INTERFACE_LOCAL=\"true\" yarn dev",
"dev:proxy": "NAMADA_INTERFACE_PROXY=true && ./scripts/start-proxies.sh && yarn dev:local",
"dev:proxy": "VITE_PROXY=true && ./scripts/start-proxies.sh && yarn dev:local",
"build": "NODE_ENV=production && yarn wasm:build && vite build",
"build:only": "NODE_ENV=production && vite build",
"lint": "eslint src --ext .ts,.tsx",
Expand Down
7 changes: 7 additions & 0 deletions apps/namadillo/scripts/proxies.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"alias": "MASP Params",
"url": "https://github.com/anoma/masp-mpc/releases/download/namada-trusted-setup/",
"proxyPort": 8010
}
]
30 changes: 2 additions & 28 deletions apps/namadillo/scripts/startProxies.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,8 @@
const { exec } = require("child_process");
require("dotenv").config();

const {
NAMADA_INTERFACE_NAMADA_ALIAS = "Namada",
NAMADA_INTERFACE_NAMADA_URL,
NAMADA_INTERFACE_COSMOS_ALIAS = "Cosmos",
NAMADA_INTERFACE_COSMOS_URL,
NAMADA_INTERFACE_ETH_ALIAS = "Ethereum",
NAMADA_INTERFACE_ETH_URL,
} = process.env;
const proxiesJson = require("./proxies.json");

const proxyConfigs = [
{
alias: NAMADA_INTERFACE_NAMADA_ALIAS,
url: NAMADA_INTERFACE_NAMADA_URL,
proxyPort: 8010,
},
{
alias: NAMADA_INTERFACE_COSMOS_ALIAS,
url: NAMADA_INTERFACE_COSMOS_URL,
proxyPort: 8011,
},
{
alias: NAMADA_INTERFACE_ETH_ALIAS,
url: NAMADA_INTERFACE_ETH_URL,
proxyPort: 8012,
},
];

proxyConfigs.forEach(({ alias, url, proxyPort }) => {
proxiesJson.forEach(({ alias, url, proxyPort }) => {
if (url) {
console.log(`Starting local-cors-proxy for ${alias}`);
console.log(`-> ${url} proxied to http://localhost:${proxyPort}/proxy\n`);
Expand Down
20 changes: 19 additions & 1 deletion apps/namadillo/src/hooks/useSdk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ import {
useEffect,
useState,
} from "react";
import Proxies from "../../scripts/proxies.json";

export const SdkContext = createContext<Sdk | undefined>(undefined);

const { VITE_PROXY: isProxied } = import.meta.env;

const paramsUrl =
isProxied ? `http://localhost:${Proxies[0].proxyPort}/proxy/` : undefined;

const initializeSdk = async (): Promise<Sdk> => {
const { cryptoMemory } = await initSdk();
const store = getDefaultStore();
Expand Down Expand Up @@ -47,7 +53,19 @@ export const SdkProvider: FunctionComponent<PropsWithChildren> = ({

useEffect(() => {
if (nativeToken.data) {
getSdkInstance().then((sdk) => setSdk(sdk));
getSdkInstance().then((sdk) => {
setSdk(sdk);
const { masp } = sdk;
masp.hasMaspParams().then((hasMaspParams) => {
if (!hasMaspParams) {
return masp.loadMaspParams("").catch((e) => console.error(`${e}`));
}
masp
.fetchAndStoreMaspParams(paramsUrl)
.then(() => masp.loadMaspParams(""))
.catch((e) => console.error(`${e}`));
});
});
}
}, [nativeToken.data]);

Expand Down
5 changes: 3 additions & 2 deletions packages/sdk/src/masp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ export class Masp {
/**
* Fetch MASP parameters and store them in SDK
* @async
* @param [url] - optional URL to override the default
* @returns void
*/
async fetchAndStoreMaspParams(): Promise<void> {
return await SdkWasm.fetch_and_store_masp_params();
async fetchAndStoreMaspParams(url?: string): Promise<void> {
return await SdkWasm.fetch_and_store_masp_params(url);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/shared/lib/src/sdk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ impl Sdk {
Ok(js_sys::Boolean::from(has.as_bool().unwrap()).into())
}

pub async fn fetch_and_store_masp_params() -> Result<(), JsValue> {
fetch_and_store_masp_params().await?;
pub async fn fetch_and_store_masp_params(url: Option<String>) -> Result<(), JsValue> {
fetch_and_store_masp_params(url).await?;
Ok(())
}

Expand Down Expand Up @@ -495,5 +495,5 @@ extern "C" {
#[wasm_bindgen(catch, js_name = "hasMaspParams")]
async fn has_masp_params() -> Result<JsValue, JsValue>;
#[wasm_bindgen(catch, js_name = "fetchAndStoreMaspParams")]
async fn fetch_and_store_masp_params() -> Result<JsValue, JsValue>;
async fn fetch_and_store_masp_params(url: Option<String>) -> Result<JsValue, JsValue>;
}
28 changes: 16 additions & 12 deletions packages/shared/lib/src/sdk/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ export async function hasMaspParams(): Promise<boolean> {
);
}

export async function fetchAndStoreMaspParams(): Promise<[void, void, void]> {
export async function fetchAndStoreMaspParams(
url?: string
): Promise<[void, void, void]> {
return Promise.all([
fetchAndStore("masp-spend.params"),
fetchAndStore("masp-output.params"),
fetchAndStore("masp-convert.params"),
fetchAndStore("masp-spend.params", url),
fetchAndStore("masp-output.params", url),
fetchAndStore("masp-convert.params", url),
]);
}

Expand All @@ -24,17 +26,19 @@ export async function getMaspParams(): Promise<[unknown, unknown, unknown]> {
]);
}

export async function fetchAndStore(params: string): Promise<void> {
const data = await fetchParams(params);
export async function fetchAndStore(
params: string,
url?: string
): Promise<void> {
const data = await fetchParams(params, url);
await set(params, data);
}

export async function fetchParams(params: string): Promise<Uint8Array> {
const path =
process.env.NAMADA_INTERFACE_MASP_PARAMS_PATH ||
"https://github.com/anoma/masp-mpc/releases/download/namada-trusted-setup/";

return fetch(`${path}${params}`)
export async function fetchParams(
params: string,
url: string = "https://github.com/anoma/masp-mpc/releases/download/namada-trusted-setup/"
): Promise<Uint8Array> {
return fetch(`${url}${params}`)
.then((response) => response.arrayBuffer())
.then((ab) => new Uint8Array(ab));
}
Expand Down

0 comments on commit edba58b

Please sign in to comment.