Skip to content

Commit

Permalink
feat: for Keplr support (and presumably other wallets) we need CORS
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Jun 15, 2021
1 parent 7b6024d commit 96d4ee3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
14 changes: 14 additions & 0 deletions packages/agoric-cli/lib/chain-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@ export const DEFAULT_API_PORT = 1317;
// Rewrite the app.toml.
export function finishCosmosApp({
appToml,
enableCors,
exportMetrics,
portNum = `${DEFAULT_RPC_PORT}`,
}) {
const rpcPort = Number(portNum);
const app = TOML.parse(appToml);

if (enableCors) {
app.api['enabled-unsafe-cors'] = true;
}

// Offset the GRPC listener from our rpc port.
app.grpc.address = `0.0.0.0:${rpcPort +
DEFAULT_GRPC_PORT -
Expand All @@ -56,6 +61,7 @@ export function finishCosmosApp({
// Rewrite the config.toml.
export function finishTendermintConfig({
configToml,
enableCors,
exportMetrics,
portNum = `${DEFAULT_RPC_PORT}`,
persistentPeers = '',
Expand All @@ -80,6 +86,14 @@ export function finishTendermintConfig({
config.rpc.laddr = `tcp://0.0.0.0:${rpcPort}`;
config.rpc.max_body_bytes = 15 * 10 ** 6;

if (
enableCors &&
(!config.rpc.cors_allowed_origins ||
config.rpc.cors_allowed_origins.length === 0)
) {
config.rpc.cors_allowed_origins = ['*'];
}

if (exportMetrics) {
const promPort = rpcPort - DEFAULT_RPC_PORT + DEFAULT_PROM_PORT;
config.instrumentation.prometheus = true;
Expand Down
5 changes: 5 additions & 0 deletions packages/agoric-cli/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ const main = async (progname, rawArgs, powers) => {
program
.command('set-defaults <program> <config-dir>')
.description('update the configuration files for <program> in <config-dir>')
.option(
'--enable-cors',
'open RPC and API endpoints to all cross-origin requests',
false,
)
.option(
'--export-metrics',
'open ports to export Prometheus metrics',
Expand Down
4 changes: 3 additions & 1 deletion packages/agoric-cli/lib/set-defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default async function setDefaultsMain(progname, rawArgs, powers, opts) {
X`<prog> must currently be 'ag-chain-cosmos'`,
);

const { exportMetrics } = opts;
const { exportMetrics, enableCors } = opts;

let appFile;
let configFile;
Expand Down Expand Up @@ -51,6 +51,7 @@ export default async function setDefaultsMain(progname, rawArgs, powers, opts) {

const newAppToml = finishCosmosApp({
appToml,
enableCors,
exportMetrics,
});
await create(appFile, newAppToml);
Expand All @@ -63,6 +64,7 @@ export default async function setDefaultsMain(progname, rawArgs, powers, opts) {

const newConfigToml = finishTendermintConfig({
configToml,
enableCors,
persistentPeers,
seeds,
unconditionalPeerIds,
Expand Down
14 changes: 10 additions & 4 deletions packages/deployment/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,18 +409,24 @@ show-config display the client connection parameters
const allIds = await needBacktick(
`${shellEscape(progname)} show-all-ids`,
);
const faucetAddress = await needBacktick(
`${shellEscape(progname)} show-faucet-address`,
);
const hasPeers = dsts.find(dst => dst.startsWith('peer'));
await Promise.all(
dsts.map(async (dst, i) => {
// Update the config.toml and genesis.json.
const corsFlags = [];
if (hasPeers && dst.startsWith('peer')) {
// Some "peers", and this is one of them.
corsFlags.push('--enable-cors');
} else if (!hasPeers && dst.startsWith('validator')) {
// No "peers", and this is a validator.
corsFlags.push('--enable-cors');
}
await needDoRun([
agoricCli,
`set-defaults`,
`ag-chain-cosmos`,
`--bootstrap-address=${faucetAddress.trimRight()}`,
`--persistent-peers=${peers}`,
...corsFlags,
`--seeds=${seeds}`,
`--unconditional-peer-ids=${allIds}`,
...importFlags,
Expand Down

0 comments on commit 96d4ee3

Please sign in to comment.