How to easily perform same action on all wallets stored on daemon...

in #cryptocurrency4 years ago

New cryptocurrency users are not usually familiar with requirement that wallets need to be optimized regularly to avoid failed transactions due to size constrains.

I made up nice shell script that I run using cron on my Linux server:

#!/bin/sh curl -s -X POST http://127.0.0.1:33777/json_rpc -H 'Content-Type: application/json-rpc' -d '{"jsonrpc": "2.0", "method": "getAddresses", "password": "this_is_not_my_real_password", "params": {}, "id": "1"}' | jq '.result.addresses | .[]' | xargs -I ADDR curl -s -X POST http://127.0.0.1:33777/json_rpc -H 'Content-Type: application/json-rpc' -d '{"jsonrpc": "2.0", "method": "sendFusionTransaction", "password": "this_is_not_my_real_password", "params": {"addresses": ["ADDR"], "destinationAddress": "ADDR", "threshold" : 100000000, "anonymity": 0}, "id": "1"}'

What it essentially does is first fetch all addresses (subwallets) stored on wallet daemon (for my webwallet) using curl and then feed it through JSON parser (jq) and then use xargs to execute curl once with each address returned by "jq" (replacing "ADDR" with the actual address).

I replaced my real password with placeholder "this_is_not_my_real_password" as I don't want anyone to try to hack my wallet daemons even though access is restricted to loopback interface and not network interface that is open to internet or my hosting company.

".result.addresses" is the JSON leaf that contains the list of addresses to use, ".[]" will print just the addresses, one per line. Even though each of the addresses is quoted in jq output, the quoting is removed when xargs does the text replace, so the quotes need to be explicitly added around the text to replace.

"curl -s" will suppress any progress output so it can be cleanly used in scripts that redirect standard error stream (stderr) to "sendmail" program and send non-empty output to server administrator. Standard output stream (stdout) is piped to standard input stream (stdin) of next command in same line using "|".

"-I ADDR" means replace every occurrence of "ADDR" with current line of input to "xargs".

In case of error, output of "jq" command will be empty so "xargs" does nothing.

"33777" needs to be replaced with correct port number of the wallet daemon, JSON methods and result field names are examples that work with some coins based on Bytecoin 2 API.