Skip to content

Commit

Permalink
feat/393 - Add scripts to start local CORS proxies (anoma#394)
Browse files Browse the repository at this point in the history
* Add scripts to start proxy, update Cosmos env for consistency

* Add proxy env to extension, update README.md

* SDK & Query must also use proxy if specified
  • Loading branch information
jurevans authored Sep 19, 2023
1 parent a26bfab commit 9217849
Show file tree
Hide file tree
Showing 14 changed files with 409 additions and 54 deletions.
4 changes: 4 additions & 0 deletions apps/extension/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ yarn start
yarn start:chrome # Same as yarn start, which defaults to a Chrome target
yarn start:firefox # Run development extension in Firefox - use ./build/firefox

# If you are running `yarn dev:proxy` in the `namada-interface`, you can make use of that proxy by using:
yarn start:chrome:proxy
yarn start:firefox:proxy

# Run tests
yarn test
yarn test:watch
Expand Down
14 changes: 8 additions & 6 deletions apps/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,29 @@
"lint:ci": "yarn lint --max-warnings 0",
"start": "yarn start:chrome",
"start:chrome": "yarn clean:chrome && export NODE_ENV=development; export TARGET=chrome && webpack --watch",
"start:chrome:proxy": "export REACT_APP_PROXY=true; yarn start:chrome",
"start:firefox": "yarn clean:firefox && export NODE_ENV=development; export TARGET=firefox && webpack --watch",
"start:firefox:proxy": "export REACT_APP_PROXY=true; yarn start:firefox",
"test": "./scripts/build-node.sh && yarn jest",
"test:watch": "./scripts/build-node.sh && yarn jest --watchAll=true",
"test:ci": "jest",
"wasm:build": "./scripts/build.sh --release",
"wasm:build:dev": "./scripts/build.sh"
},
"dependencies": {
"@cosmjs/encoding": "^0.29.0",
"@dao-xyz/borsh": "^5.1.5",
"@ledgerhq/hw-transport": "^6.28.3",
"@ledgerhq/hw-transport-webhid": "^6.27.14",
"@ledgerhq/hw-transport-webusb": "^6.27.14",
"@namada/chains": "0.1.0",
"@namada/components": "0.1.0",
"@namada/crypto": "0.1.0",
"@namada/ledger-namada": "0.0.1",
"@namada/shared": "0.1.0",
"@namada/storage": "0.1.0",
"@namada/types": "0.1.0",
"@namada/utils": "0.1.0",
"@namada/ledger-namada": "0.0.1",
"@cosmjs/encoding": "^0.29.0",
"@dao-xyz/borsh": "^5.1.5",
"@ledgerhq/hw-transport": "^6.28.3",
"@ledgerhq/hw-transport-webhid": "^6.27.14",
"@ledgerhq/hw-transport-webusb": "^6.27.14",
"bignumber.js": "^9.1.1",
"buffer": "^6.0.3",
"dompurify": "^3.0.2",
Expand Down
10 changes: 6 additions & 4 deletions apps/extension/src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
IndexedDBKVStore,
MemoryKVStore,
} from "@namada/storage";
import { defaultChainId, chains } from "@namada/chains";
import { defaultChainId, chains, ProxyMappings } from "@namada/chains";
import { init as initCrypto } from "@namada/crypto/src/init";
import { init as initShared } from "@namada/shared/src/init";
import { Query, Sdk } from "@namada/shared";
Expand Down Expand Up @@ -52,7 +52,9 @@ const txStore = new MemoryKVStore(KVPrefix.Memory);

const DEFAULT_URL =
"https://d3brk13lbhxfdb.cloudfront.net/qc-testnet-5.1.025a61165acd05e";
const { REACT_APP_NAMADA_URL = DEFAULT_URL } = process.env;
const { REACT_APP_PROXY, REACT_APP_NAMADA_URL = DEFAULT_URL } = process.env;

const NamadaRpcEndpoint = REACT_APP_PROXY ? ProxyMappings["namada"] : REACT_APP_NAMADA_URL;

const messenger = new ExtensionMessenger();
const router = new ExtensionRouter(
Expand All @@ -68,8 +70,8 @@ const init = new Promise<void>(async (resolve) => {
const { memory: cryptoMemory } = await initCrypto();

await initShared();
const sdk = new Sdk(REACT_APP_NAMADA_URL);
const query = new Query(REACT_APP_NAMADA_URL);
const sdk = new Sdk(NamadaRpcEndpoint);
const query = new Query(NamadaRpcEndpoint);

const sdkData: Record<string, string> | undefined = await sdkStore.get(
SDK_KEY
Expand Down
4 changes: 2 additions & 2 deletions apps/namada-interface/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ REACT_APP_NAMADA_FAUCET_LIMIT=1000
# COSMOS
REACT_APP_COSMOS_ALIAS=Cosmos Testnet
REACT_APP_COSMOS_CHAIN_ID=cosmoshub-4
REACT_APP_COSMOS_CHAIN_URL=https://rpc.sentry-01.theta-testnet.polypore.xyz
REACT_APP_COSMOS_URL=https://rpc.sentry-01.theta-testnet.polypore.xyz

# OSMOSIS
REACT_APP_OSMOSIS_ALIAS=Osmosis Testnet
Expand All @@ -21,7 +21,7 @@ REACT_APP_OSMOSIS_URL=http://127.0.0.1:54321
# ETH
REACT_APP_ETH_ALIAS=Eth Testnet
REACT_APP_ETH_CHAIN_ID="0x7A69"
REACT_APP_COSMOS_URL=http://127.0.0.1:12345
REACT_APP_ETH_URL=http://127.0.0.1:12345

# ETH smart contracts
CONTRACT_ADDR_ETH_BRIDGE="0xCf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9"
3 changes: 3 additions & 0 deletions apps/namada-interface/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ yarn dev
# ledger and a testnet):
yarn dev:local

# If you are running chains locally, it is recommended that you instead proxy RPC requests:
yarn dev:proxy

# Build production release:
yarn build

Expand Down
3 changes: 3 additions & 0 deletions apps/namada-interface/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@
},
"scripts": {
"start": "webpack-dev-server",
"start:proxy": "node ./scripts/startProxies.js",
"dev": "export NODE_ENV=development && yarn start",
"dev:local": "export NODE_ENV=development; export REACT_APP_LOCAL=\"true\" && yarn dev",
"dev:proxy": "export REACT_APP_PROXY=true; ./scripts/start-proxies.sh; yarn dev:local",
"build": "export NODE_ENV=production && yarn wasm:build && webpack",
"lint": "eslint src --ext .ts,.tsx",
"lint:fix": "yarn lint -- --fix",
Expand Down Expand Up @@ -89,6 +91,7 @@
"html-webpack-plugin": "^5.5.0",
"jest": "^29.4.1",
"jest-fetch-mock": "^3.0.3",
"local-cors-proxy": "^1.1.0",
"prettier": "^2.5.1",
"redux-mock-store": "^1.5.4",
"style-loader": "^3.3.1",
Expand Down
6 changes: 6 additions & 0 deletions apps/namada-interface/scripts/start-proxies.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

SCRIPT_DIR=$(cd "$(dirname "$0")"; pwd -P)

node $SCRIPT_DIR/startProxies.js&

44 changes: 44 additions & 0 deletions apps/namada-interface/scripts/startProxies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const { exec } = require('child_process');
require('dotenv').config();

const {
REACT_APP_NAMADA_ALIAS = "Namada",
REACT_APP_NAMADA_URL,
REACT_APP_COSMOS_ALIAS = "Cosmos",
REACT_APP_COSMOS_URL,
REACT_APP_ETH_ALIAS = "Ethereum",
REACT_APP_ETH_URL,
} = process.env;

const proxyConfigs = [
{
alias: REACT_APP_NAMADA_ALIAS,
url: REACT_APP_NAMADA_URL,
proxyPort: 8010,
},
{
alias: REACT_APP_COSMOS_ALIAS,
url: REACT_APP_COSMOS_URL,
proxyPort: 8011,
},
{
alias: REACT_APP_ETH_ALIAS,
url: REACT_APP_ETH_URL,
proxyPort: 8012,
}
];

proxyConfigs.forEach(({ alias, url, proxyPort }) => {
if (url) {
console.log(`Starting local-cors-proxy for ${alias}`)
console.log(`-> ${url} proxied to http://127.0.0.1:${proxyPort}/proxy\n`);

exec(`lcp --proxyUrl ${url} --port ${proxyPort}`, (error, stdout, stderr) => {
console.log(stdout);
console.log(stderr);
if (error !== null) {
console.log(`exec error: ${error}`)
}
})
}
})
6 changes: 4 additions & 2 deletions packages/chains/src/chains/cosmos.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { BridgeType, Chain, Extensions } from "@namada/types";
import { ProxyMappings } from "../types";

const DEFAULT_ALIAS = "Cosmos Hub";
const DEFAULT_CHAIN_ID = "cosmoshub-4";
const DEFAULT_RPC = "https://api.cosmos.network/";

const {
REACT_APP_PROXY: isProxied,
REACT_APP_COSMOS_ALIAS: alias = DEFAULT_ALIAS,
REACT_APP_COSMOS_CHAIN_ID: chainId = DEFAULT_CHAIN_ID,
REACT_APP_COSMOS_CHAIN_URL: rpc = DEFAULT_RPC,
REACT_APP_COSMOS_URL: rpc = DEFAULT_RPC,
} = process.env;

const cosmos: Chain = {
Expand All @@ -17,7 +19,7 @@ const cosmos: Chain = {
coinType: 118,
},
bridgeType: [BridgeType.IBC],
rpc,
rpc: isProxied ? ProxyMappings["cosmos"] : rpc,
chainId,
currency: {
token: "uatom",
Expand Down
6 changes: 4 additions & 2 deletions packages/chains/src/chains/ethereum.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { BridgeType, Chain, Extensions } from "@namada/types";
import { ProxyMappings } from "../types";

const DEFAULT_ALIAS = "Goerli Testnet";
const DEFAULT_CHAIN_ID = "0x5";
const DEFAULT_RPC = "https://eth-goerli.g.alchemy.com/v2/";

const {
REACT_APP_PROXY: isProxied,
REACT_APP_ETH_ALIAS: alias = DEFAULT_ALIAS,
REACT_APP_ETH_CHAIN_ID: chainId = DEFAULT_CHAIN_ID,
REACT_APP_ETH_CHAIN_URL: rpc = DEFAULT_RPC,
REACT_APP_ETH_URL: rpc = DEFAULT_RPC,
} = process.env;

const ethereum: Chain = {
Expand All @@ -17,7 +19,7 @@ const ethereum: Chain = {
coinType: 1,
},
bridgeType: [BridgeType.Ethereum],
rpc,
rpc: isProxied ? ProxyMappings["ethereum"] : rpc,
chainId,
currency: {
token: "Ethereum",
Expand Down
4 changes: 3 additions & 1 deletion packages/chains/src/chains/namada.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BridgeType, Chain, Extensions } from "@namada/types";
import { ProxyMappings } from "../types";

const DEFAULT_ALIAS = "Namada Testnet";
const DEFAULT_CHAIN_ID = "qc-testnet-5.1.025a61165acd05e";
Expand All @@ -7,6 +8,7 @@ const DEFAULT_RPC =
const DEFAULT_BECH32_PREFIX = "atest";

const {
REACT_APP_PROXY: isProxied,
REACT_APP_NAMADA_ALIAS: alias = DEFAULT_ALIAS,
REACT_APP_NAMADA_CHAIN_ID: chainId = DEFAULT_CHAIN_ID,
REACT_APP_NAMADA_URL: rpc = DEFAULT_RPC,
Expand All @@ -21,7 +23,7 @@ const namada: Chain = {
coinType: 877,
},
bridgeType: [BridgeType.IBC, BridgeType.Ethereum],
rpc,
rpc: isProxied ? ProxyMappings["namada"] : rpc,
chainId,
currency: {
token: "Nam",
Expand Down
2 changes: 2 additions & 0 deletions packages/chains/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ export const chains = {
[namada.chainId]: namada,
[ethereum.chainId]: ethereum,
};

export * from "./types";
10 changes: 10 additions & 0 deletions packages/chains/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const getProxyURL = (port: number): string => `http://127.0.0.1:${port}/proxy`;

type ProxiedChains = "namada" | "cosmos" | "ethereum";

// Define unique Proxy URLs for each chain:
export const ProxyMappings: Record<ProxiedChains, string> = {
namada: getProxyURL(8010),
cosmos: getProxyURL(8011),
ethereum: getProxyURL(8012),
}
Loading

0 comments on commit 9217849

Please sign in to comment.