Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #21 from api3dao/BEC-268/generate-gateway-key
Browse files Browse the repository at this point in the history
BEC-268 | Implement the generate-gateway-key script
  • Loading branch information
aquarat authored Apr 8, 2022
2 parents 60a9bf3 + 7ffcc0c commit 5d7b011
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
2 changes: 1 addition & 1 deletion data/documentation.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"beaconId": "0xb5087bcd2a04f5653ad8a6b39fa4fb10fabbdec124b8b489b4d8d610e6392198",
"name": "CoinGecko BTC/USD 0.1 percent deviation",
"description": "The public CoinGecko BTC/USD price ticker",
"templateUrl": "https://github.com/api3dao/operations/blob/d1106eaddfb3f8f75a88eb8b1428c0cea863b029/data/apis/api3/templates/coingecko btc_usd.json",
"templateUrl": "https://github.com/api3dao/operations/blob/f3245421d6e4e7f180026a4ce0415bc9f7902afd/data/apis/api3/templates/coingecko btc_usd.json",
"chains": ["ropsten"]
}
]
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"scripts": {
"create-boilerplate": "ts-node src/create-boilerplate.ts",
"create-config": "ts-node src/create-config.ts",
"generate-gateway-key": "ts-node src/generate-gateway-key.ts",
"build": "yarn run clean && yarn run compile",
"clean": "rimraf -rf ./dist *.tgz",
"compile": "tsc -p tsconfig.json",
Expand Down
74 changes: 74 additions & 0 deletions src/generate-gateway-key.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { randomBytes } from 'crypto';
import { PromptObject } from 'prompts';
import { OperationsRepository } from './types';
import { runAndHandleErrors } from './utils/cli';
import { promptQuestions } from './utils/prompts';
import { readOperationsRepository } from './utils/read-operations';
import { writeOperationsRepository } from './utils/write-operations';

const questions = (operationsRepository: OperationsRepository): PromptObject[] => {
return [
{
type: 'autocomplete',
name: 'name',
message: 'What is the name of the API Integration?',
choices: Object.keys(operationsRepository.apis).map((api) => ({ title: api, value: api })),
},
{
type: 'autocomplete',
name: 'deployment',
message: 'For which deployment do you want to generate the gateway keys for?',
choices: (prev) =>
Object.keys(operationsRepository.apis[prev].deployments).map((deployment) => ({
title: deployment,
value: deployment,
})),
},
];
};

const main = async () => {
const httpGatewayKey = randomBytes(48).toString('hex');
const signedHttpGatewayKey = randomBytes(48).toString('hex');

const operationsRepository = readOperationsRepository();
const response = await promptQuestions(questions(operationsRepository));

const oldSecrets = operationsRepository.apis[response.name].deployments[response.deployment].secrets.content
.trim()
.split('\n')
.filter((secret) => secret !== 'HTTP_GATEWAY_API_KEY=' && secret !== 'HTTP_SIGNED_DATA_GATEWAY_API_KEY=');

const newSecrets = {
filename: '.env',
content: [
...oldSecrets,
`HTTP_GATEWAY_API_KEY=${httpGatewayKey}`,
`HTTP_SIGNED_DATA_GATEWAY_API_KEY=${signedHttpGatewayKey}`,
].join('\n'),
};

const updatedOpsData: OperationsRepository = {
...operationsRepository,
apis: {
...operationsRepository.apis,
[response.name]: {
...operationsRepository.apis[response.name],
deployments: {
...operationsRepository.apis[response.name].deployments,
[response.deployment]: {
...operationsRepository.apis[response.name].deployments[response.deployment],
secrets: newSecrets,
},
},
},
},
};

console.log(`🔑 Generated HTTP Gateway API Key: ${httpGatewayKey}`);
console.log(`🔑 Generated HTTP Signed Data Gateway API Key: ${signedHttpGatewayKey}`);

writeOperationsRepository(updatedOpsData);
};

runAndHandleErrors(main);

0 comments on commit 5d7b011

Please sign in to comment.