0

Is there a CLI command where I can pass in an ethereum private key and send some coins to another address? I'm trying to build out a reoccurring payment system where a user can send 1 eth to another address every week. But I'm looking for a wallet via the command line that can pass in a private key to do the send out. What are the best options?

1 Answer 1

1

You can npm install [email protected], and then try this NodeJS script:

const Web3 = require("web3");

const NODE_ADDRESS = process.argv[2];
const DEST_ADDRESS = process.argv[3];
const WEI_AMOUNT   = process.argv[4];
const PRIVATE_KEY  = process.argv[5];

async function scan() {
    return await new Promise(function(resolve, reject) {
        process.stdin.resume();
        process.stdin.once("data", function(data) {
            process.stdin.pause();
            resolve(data.toString().trim());
        });
    });
}

async function getGasPrice(web3) {
    while (true) {
        const nodeGasPrice = await web3.eth.getGasPrice();
        process.stdout.write(`Enter gas-price or leave empty to use ${nodeGasPrice}: `);
        const userGasPrice = await scan();
        if (/^\d+$/.test(userGasPrice))
            return userGasPrice;
        if (userGasPrice == "")
            return nodeGasPrice;
        console.log("Illegal gas-price");
    }
}

async function getTransactionReceipt(web3) {
    while (true) {
        process.stdout.write("Enter transaction-hash or leave empty to retry: ");
        const hash = await scan();
        if (/^0x([0-9A-Fa-f]{64})$/.test(hash)) {
            const receipt = await web3.eth.getTransactionReceipt(hash);
            if (receipt)
                return receipt;
            console.log("Invalid transaction-hash");
        }
        else if (hash) {
            console.log("Illegal transaction-hash");
        }
        else {
            return null;
        }
    }
}

async function send(web3, to, value, privateKey) {
    while (true) {
        try {
            const options = {
                to      : to,
                value   : value,
                gas     : 21000,
                gasPrice: await getGasPrice(web3)
            };
            const signed  = await web3.eth.accounts.signTransaction(options, privateKey);
            const receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction);
            return receipt;
        }
        catch (error) {
            console.log(error.message);
            const receipt = await getTransactionReceipt(web3);
            if (receipt)
                return receipt;
        }
    }
}

async function run() {
    const web3 = new Web3(NODE_ADDRESS);
    await send(web3, DEST_ADDRESS, WEI_AMOUNT, PRIVATE_KEY);
    if (web3.currentProvider.constructor.name == "WebsocketProvider")
        web3.currentProvider.connection.close();
}

run();
2
  • what is an example setting for: process.argv ? where can i find a node address? and what do i put for wei amount? i'm guessing dest_address is where the coins are going and private key is the eth private key? thanks!!! Dec 6, 2019 at 19:04
  • @Patoshiパトシ: Node Address (1st input) example: https://mainnet.infura.io. Dest Address (2nd input): The account that you want to transfer to. Wei Amount (3rd input): How much wei you want to transfer. Private Key (4th input): The private key of the account that you want to transfer from. Dec 6, 2019 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.