Ronnie Jaimungal
Ronnie Jaimungal

Reputation: 39

How to send Ether from Address with private key and password?

I'm trying to use PHP and Cron to send Ether from an existing account using web3 or something similar, any tips on how I can accomplish this?

I have JSON in an SQL table {"address":"0x22b853A8Df90BF9A7e21445C16FBbEaa43e0c307","password":"inu8p","privateKey":"0xb9cabca09e5cbdfb2c953d574bce5b9d837a91ffe1f3817fc6a01da63bf5ff56"}

The problem is the JavaScript doesn't get executed with CRON. Is there any library in Node where I can send a POST request via an API or something?

Upvotes: 2

Views: 3813

Answers (2)

YinYang16
YinYang16

Reputation: 471

You want to send a raw Ethereum transaction in PHP, so in your case I'd recommend php-eth-raw-tx package.

The usage is pretty simple:

$tx = new \EthereumRawTx\Transaction(
\BitWasp\Buffertools\Buffer::hex('d44d259015b61a5fe5027221239d840d92583adb'),
\BitWasp\Buffertools\Buffer::int(1 * 10**18),
);

$raw = $tx->getRaw(\BitWasp\Buffertools\Buffer::hex('b9cabca09e5cbdfb2c953d574bce5b9d837a91ffe1f3817fc6a01da63bf5ff56'));

In this example you send 1.0 ETH from address 0x22b853A8Df90BF9A7e21445C16FBbEaa43e0c307 (identified by a private key 0xb9cabca09e5cbdfb2c953d574bce5b9d837a91ffe1f3817fc6a01da63bf5ff56) to address 0xd44d259015b61a5fe5027221239d840d92583adb.

All you need to do is to create a PHP file and add it to your crontab.

See more examples here: https://github.com/Domraider/php-eth-raw-tx/tree/master/examples

Upvotes: 1

Nick
Nick

Reputation: 5198

You can use geth or other client's RPC server. You can send a request to the RPC server via web3, or even node's http module, but Web3 is easier.

You can then schedule this with a node cron module, like this one: https://www.npmjs.com/package/cron

You will want to ensure whatever RPC server you use is setup securely, as there are active scanning and attacks against them.

A partial example:

//Send some eth every hour
new CronJob('* * * 1 0 0', () => {
  //The following arguments are simplified...
  web3.eth.sendTransaction({from: "0xc0ffee", to: "0xdeadbeef...", value: "1.0"}, () => {
    console.log('transaction sent');
  });
}, null, true, 'America/Los_Angeles');

Upvotes: 1

Related Questions