Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: for Keplr support (and presumably other wallets) we need CORS #3324

Merged
merged 1 commit into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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