Skip to content
Open
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
34 changes: 23 additions & 11 deletions deployment/docker/localsecret/faucet/faucet_server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const http = require("http");
const querystring = require("querystring");
const exec = require("child_process").exec;
const execFile = require("child_process").execFile;

const FAUCET_WALLET_NAME = process.env.FAUCET_WALLET_NAME || "a";
const FAUCET_AMOUNT = process.env.FAUCET_AMOUNT || "1000000000";
Expand All @@ -9,13 +9,16 @@ const DENOM = process.env.DENOM || "uscrt";
let faucet_address;

/**
* Execute a shell command and return it as a Promise.
* @param cmd {string}
* Execute a program directly (no shell) and return its JSON output as a Promise.
* Arguments are passed as an array so user-controlled values are never
* interpreted by a shell.
* @param cmd {string} program to run
* @param args {string[]} arguments to pass to the program
* @return {Promise<string>}
*/
function execShellCommand(cmd) {
function execShellCommand(cmd, args = []) {
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
execFile(cmd, args, (error, stdout, stderr) => {
if (error) {
console.error("error in execShellCommand", error);
reject(error);
Expand All @@ -38,10 +41,20 @@ function execShellCommand(cmd) {
* @returns result of executing the command.
*/
async function send_command(src_key_name, src_address, dest_address, amount) {
const send_message = `secretd tx bank send ${src_address} ${dest_address} ${amount}${DENOM} --from ${src_key_name} --gas-prices 0.25uscrt -y`;
console.log(`send_message: \n ${send_message}`);

const result = await execShellCommand(send_message);
// Build the argument list explicitly. `dest_address` is passed as a single,
// literal argument, so it cannot be interpreted as shell syntax.
const send_args = [
"tx", "bank", "send",
src_address,
dest_address,
`${amount}${DENOM}`,
"--from", src_key_name,
"--gas-prices", "0.25uscrt",
"-y",
];
console.log(`send_message: \n secretd ${send_args.join(" ")}`);

const result = await execShellCommand("secretd", send_args);
console.log(`Sent tokens with txhash: ${result.txhash}`);
return result.txhash;
}
Expand All @@ -57,8 +70,7 @@ async function get_address(key_name) {
return faucet_address;
}

const list_keys = "secretd keys list";
const result = await execShellCommand(list_keys);
const result = await execShellCommand("secretd", ["keys", "list"]);

for (index in result) {
const key = result[index];
Expand Down
39 changes: 25 additions & 14 deletions scripts/faucet/faucet_server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const http = require("http");
const querystring = require("querystring");
const exec = require("child_process").exec;
const execFile = require("child_process").execFile;

const FAUCET_WALLET_NAME = process.env.FAUCET_WALLET_NAME || "a";
const FAUCET_AMOUNT = process.env.FAUCET_AMOUNT || "1000000000";
Expand All @@ -9,20 +9,22 @@ const DENOM = process.env.DENOM || "uscrt";
let faucet_address;

/**
* Execute a shell command and return it as a Promise.
* @param cmd {string}
* Execute a program directly (no shell) and return its JSON output as a Promise.
* Arguments are passed as an array so user-controlled values are never
* interpreted by a shell.
* @param cmd {string} program to run
* @param args {string[]} arguments to pass to the program
* @return {Promise<string>}
*/
function execShellCommand(cmd) {
function execShellCommand(cmd, args = []) {
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
execFile(cmd, args, (error, stdout) => {
if (error) {
console.error("error in execShellCommand", error);
reject(error);
} else if (stderr) {
console.error("stderr in execShellCommand", stderr);
reject(stderr);
} else {
// stderr is ignored: the original commands suppressed it with
// "2> /dev/null", and execFile does not use a shell for redirection.
resolve(JSON.parse(stdout));
}
});
Expand All @@ -38,10 +40,20 @@ function execShellCommand(cmd) {
* @returns result of executing the command.
*/
async function send_command(src_key_name, src_address, dest_address, amount) {
const send_message = `secretd tx bank send ${src_address} ${dest_address} ${amount}${DENOM} --from ${src_key_name} --gas-prices 0.25uscrt -y 2> /dev/null`;
console.log(`send_message: \n ${send_message}`);

const result = await execShellCommand(send_message);
// Build the argument list explicitly. `dest_address` is passed as a single,
// literal argument, so it cannot be interpreted as shell syntax.
const send_args = [
"tx", "bank", "send",
src_address,
dest_address,
`${amount}${DENOM}`,
"--from", src_key_name,
"--gas-prices", "0.25uscrt",
"-y",
];
console.log(`send_message: \n secretd ${send_args.join(" ")}`);

const result = await execShellCommand("secretd", send_args);
console.log(`Sent tokens with txhash: ${result.txhash}`);
return result.txhash;
}
Expand All @@ -57,8 +69,7 @@ async function get_address(key_name) {
return faucet_address;
}

const list_keys = "secretd keys list --output json 2> /dev/null";
const result = await execShellCommand(list_keys);
const result = await execShellCommand("secretd", ["keys", "list", "--output", "json"]);

for (index in result) {
const key = result[index];
Expand Down