Skip to content

Commit 7986548

Browse files
committed
feat: for Keplr support (and presumably other wallets) we need CORS
1 parent cf45d93 commit 7986548

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

packages/agoric-cli/lib/chain-config.js

+14
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,17 @@ export const DEFAULT_API_PORT = 1317;
2525
// Rewrite the app.toml.
2626
export function finishCosmosApp({
2727
appToml,
28+
enableCors,
2829
exportMetrics,
2930
portNum = `${DEFAULT_RPC_PORT}`,
3031
}) {
3132
const rpcPort = Number(portNum);
3233
const app = TOML.parse(appToml);
3334

35+
if (enableCors) {
36+
app.api['enabled-unsafe-cors'] = true;
37+
}
38+
3439
// Offset the GRPC listener from our rpc port.
3540
app.grpc.address = `0.0.0.0:${rpcPort +
3641
DEFAULT_GRPC_PORT -
@@ -56,6 +61,7 @@ export function finishCosmosApp({
5661
// Rewrite the config.toml.
5762
export function finishTendermintConfig({
5863
configToml,
64+
enableCors,
5965
exportMetrics,
6066
portNum = `${DEFAULT_RPC_PORT}`,
6167
persistentPeers = '',
@@ -80,6 +86,14 @@ export function finishTendermintConfig({
8086
config.rpc.laddr = `tcp://0.0.0.0:${rpcPort}`;
8187
config.rpc.max_body_bytes = 15 * 10 ** 6;
8288

89+
if (
90+
enableCors &&
91+
(!config.rpc.cors_allowed_origins ||
92+
config.rpc.cors_allowed_origins.length === 0)
93+
) {
94+
config.rpc.cors_allowed_origins = ['*'];
95+
}
96+
8397
if (exportMetrics) {
8498
const promPort = rpcPort - DEFAULT_RPC_PORT + DEFAULT_PROM_PORT;
8599
config.instrumentation.prometheus = true;

packages/agoric-cli/lib/main.js

+5
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ const main = async (progname, rawArgs, powers) => {
111111
program
112112
.command('set-defaults <program> <config-dir>')
113113
.description('update the configuration files for <program> in <config-dir>')
114+
.option(
115+
'--enable-cors',
116+
'open RPC and API endpoints to all cross-origin requests',
117+
false,
118+
)
114119
.option(
115120
'--export-metrics',
116121
'open ports to export Prometheus metrics',

packages/agoric-cli/lib/set-defaults.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default async function setDefaultsMain(progname, rawArgs, powers, opts) {
1717
X`<prog> must currently be 'ag-chain-cosmos'`,
1818
);
1919

20-
const { exportMetrics } = opts;
20+
const { exportMetrics, enableCors } = opts;
2121

2222
let appFile;
2323
let configFile;
@@ -51,6 +51,7 @@ export default async function setDefaultsMain(progname, rawArgs, powers, opts) {
5151

5252
const newAppToml = finishCosmosApp({
5353
appToml,
54+
enableCors,
5455
exportMetrics,
5556
});
5657
await create(appFile, newAppToml);
@@ -63,6 +64,7 @@ export default async function setDefaultsMain(progname, rawArgs, powers, opts) {
6364

6465
const newConfigToml = finishTendermintConfig({
6566
configToml,
67+
enableCors,
6668
persistentPeers,
6769
seeds,
6870
unconditionalPeerIds,

packages/deployment/src/main.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -409,18 +409,24 @@ show-config display the client connection parameters
409409
const allIds = await needBacktick(
410410
`${shellEscape(progname)} show-all-ids`,
411411
);
412-
const faucetAddress = await needBacktick(
413-
`${shellEscape(progname)} show-faucet-address`,
414-
);
412+
const hasPeers = dsts.find(dst => dst.startsWith('peer'));
415413
await Promise.all(
416414
dsts.map(async (dst, i) => {
417415
// Update the config.toml and genesis.json.
416+
const corsFlags = [];
417+
if (hasPeers && dst.startsWith('peer')) {
418+
// Some "peers", and this is one of them.
419+
corsFlags.push('--enable-cors');
420+
} else if (!hasPeers && dst.startsWith('validator')) {
421+
// No "peers", and this is a validator.
422+
corsFlags.push('--enable-cors');
423+
}
418424
await needDoRun([
419425
agoricCli,
420426
`set-defaults`,
421427
`ag-chain-cosmos`,
422-
`--bootstrap-address=${faucetAddress.trimRight()}`,
423428
`--persistent-peers=${peers}`,
429+
...corsFlags,
424430
`--seeds=${seeds}`,
425431
`--unconditional-peer-ids=${allIds}`,
426432
...importFlags,

0 commit comments

Comments
 (0)