From 2c3b28bc847764bf035cb690a65b69df429bddd1 Mon Sep 17 00:00:00 2001 From: kexkey Date: Thu, 23 May 2019 12:26:21 -0400 Subject: [PATCH] Added addresstype when calling getnewaddress. --- doc/API.v0.md | 11 +- proxy_docker/app/script/requesthandler.sh | 3 +- proxy_docker/app/script/walletoperations.sh | 284 ++++++++++---------- 3 files changed, 158 insertions(+), 140 deletions(-) diff --git a/doc/API.v0.md b/doc/API.v0.md index fcfccda..8298218 100644 --- a/doc/API.v0.md +++ b/doc/API.v0.md @@ -554,10 +554,13 @@ Proxy response: ### Get a new Bitcoin address from spending wallet (called by application) -Calls getnewaddress RPC on the spending wallet. Used to refill the spending wallet from cold wallet (ie Trezor). +Calls getnewaddress RPC on the spending wallet. Used to refill the spending wallet from cold wallet (ie Trezor). Will derive the default address type (set in your bitcoin.conf file, p2sh-segwit if not specified) or you can supply the address type like the following examples. ```http GET http://cyphernode:8888/getnewaddress +GET http://cyphernode:8888/getnewaddress/bech32 +GET http://cyphernode:8888/getnewaddress/legacy +GET http://cyphernode:8888/getnewaddress/p2sh-segwit ``` Proxy response: @@ -568,6 +571,12 @@ Proxy response: } ``` +```json +{ + "address":"tb1ql7yvh3lmajxmaljsnsu3w8lhwczu963tvjfzpj" +} +``` + ### Spend coins from spending wallet (called by application) Calls sendtoaddress RPC on the spending wallet with supplied info. diff --git a/proxy_docker/app/script/requesthandler.sh b/proxy_docker/app/script/requesthandler.sh index 1206c0f..ceb1216 100644 --- a/proxy_docker/app/script/requesthandler.sh +++ b/proxy_docker/app/script/requesthandler.sh @@ -227,8 +227,9 @@ main() ;; getnewaddress) # curl (GET) http://192.168.111.152:8080/getnewaddress + # curl (GET) http://192.168.111.152:8080/getnewaddress/bech32 - response=$(getnewaddress) + response=$(getnewaddress $(echo "${line}" | cut -d ' ' -f2 | cut -d '/' -f3)) response_to_client "${response}" ${?} break ;; diff --git a/proxy_docker/app/script/walletoperations.sh b/proxy_docker/app/script/walletoperations.sh index fd2b57f..4a62a0d 100644 --- a/proxy_docker/app/script/walletoperations.sh +++ b/proxy_docker/app/script/walletoperations.sh @@ -4,71 +4,71 @@ . ./sendtobitcoinnode.sh spend() { - trace "Entering spend()..." + trace "Entering spend()..." - local data - local request=${1} - local address=$(echo "${request}" | jq ".address" | tr -d '"') - trace "[spend] address=${address}" - local amount=$(echo "${request}" | jq ".amount" | awk '{ printf "%.8f", $0 }') - trace "[spend] amount=${amount}" - local response - local id_inserted + local data + local request=${1} + local address=$(echo "${request}" | jq ".address" | tr -d '"') + trace "[spend] address=${address}" + local amount=$(echo "${request}" | jq ".amount" | awk '{ printf "%.8f", $0 }') + trace "[spend] amount=${amount}" + local response + local id_inserted - response=$(send_to_spender_node "{\"method\":\"sendtoaddress\",\"params\":[\"${address}\",${amount}]}") - local returncode=$? - trace_rc ${returncode} - trace "[spend] response=${response}" + response=$(send_to_spender_node "{\"method\":\"sendtoaddress\",\"params\":[\"${address}\",${amount}]}") + local returncode=$? + trace_rc ${returncode} + trace "[spend] response=${response}" - if [ "${returncode}" -eq 0 ]; then - local txid=$(echo "${response}" | jq ".result" | tr -d '"') - trace "[spend] txid=${txid}" + if [ "${returncode}" -eq 0 ]; then + local txid=$(echo "${response}" | jq ".result" | tr -d '"') + trace "[spend] txid=${txid}" - # Let's insert the txid in our little DB to manage the confirmation and tell it's not a watching address - sql "INSERT OR IGNORE INTO tx (txid) VALUES (\"${txid}\")" - trace_rc $? - id_inserted=$(sql "SELECT id FROM tx WHERE txid=\"${txid}\"") - trace_rc $? - sql "INSERT OR IGNORE INTO recipient (address, amount, tx_id) VALUES (\"${address}\", ${amount}, ${id_inserted})" - trace_rc $? + # Let's insert the txid in our little DB to manage the confirmation and tell it's not a watching address + sql "INSERT OR IGNORE INTO tx (txid) VALUES (\"${txid}\")" + trace_rc $? + id_inserted=$(sql "SELECT id FROM tx WHERE txid=\"${txid}\"") + trace_rc $? + sql "INSERT OR IGNORE INTO recipient (address, amount, tx_id) VALUES (\"${address}\", ${amount}, ${id_inserted})" + trace_rc $? - data="{\"status\":\"accepted\"" - data="${data},\"hash\":\"${txid}\"}" - else - local message=$(echo "${response}" | jq -e ".error.message") - data="{\"message\":${message}}" - fi + data="{\"status\":\"accepted\"" + data="${data},\"hash\":\"${txid}\"}" + else + local message=$(echo "${response}" | jq -e ".error.message") + data="{\"message\":${message}}" + fi - trace "[spend] responding=${data}" - echo "${data}" + trace "[spend] responding=${data}" + echo "${data}" - return ${returncode} + return ${returncode} } getbalance() { - trace "Entering getbalance()..." + trace "Entering getbalance()..." - local response - local data='{"method":"getbalance"}' - response=$(send_to_spender_node "${data}") - local returncode=$? - trace_rc ${returncode} - trace "[getbalance] response=${response}" + local response + local data='{"method":"getbalance"}' + response=$(send_to_spender_node "${data}") + local returncode=$? + trace_rc ${returncode} + trace "[getbalance] response=${response}" - if [ "${returncode}" -eq 0 ]; then - local balance=$(echo ${response} | jq ".result") - trace "[getbalance] balance=${balance}" + if [ "${returncode}" -eq 0 ]; then + local balance=$(echo ${response} | jq ".result") + trace "[getbalance] balance=${balance}" - data="{\"balance\":${balance}}" - else - trace "[getbalance] Coudn't get balance!" - data="" - fi + data="{\"balance\":${balance}}" + else + trace "[getbalance] Coudn't get balance!" + data="" + fi - trace "[getbalance] responding=${data}" - echo "${data}" + trace "[getbalance] responding=${data}" + echo "${data}" - return ${returncode} + return ${returncode} } getbalancebyxpublabel() { @@ -100,8 +100,8 @@ getbalancebyxpub() { trace "[getbalancebyxpub] event=${event}" local addresses local balance - local data - local returncode + local data + local returncode # addresses=$(./bitcoin-cli -rpcwallet=xpubwatching01.dat getaddressesbylabel upub5GtUcgGed1aGH4HKQ3vMYrsmLXwmHhS1AeX33ZvDgZiyvkGhNTvGd2TA5Lr4v239Fzjj4ZY48t6wTtXUy2yRgapf37QHgt6KWEZ6bgsCLpb | jq "keys" | tr -d '\n ') data="{\"method\":\"getaddressesbylabel\",\"params\":[${xpub}]}" @@ -112,127 +112,135 @@ getbalancebyxpub() { data="{\"method\":\"listunspent\",\"params\":[0, 9999999, \"${addresses}\"]}" trace "[getbalancebyxpub] data=${data}" - balance=$(send_to_xpub_watcher_wallet ${data} | jq "[.[].amount] | add | . * 100000000 | trunc | . / 100000000") - returncode=$? + balance=$(send_to_xpub_watcher_wallet ${data} | jq "[.[].amount] | add | . * 100000000 | trunc | . / 100000000") + returncode=$? trace_rc ${returncode} trace "[getbalancebyxpub] balance=${balance}" data="{\"event\":\"${event}\",\"xpub\":\"${xpub}\",\"balance\":${balance}}" - echo "${data}" + echo "${data}" return ${returncode} } getnewaddress() { - trace "Entering getnewaddress()..." + trace "Entering getnewaddress()..." - local response - local data='{"method":"getnewaddress"}' - response=$(send_to_spender_node "${data}") - local returncode=$? - trace_rc ${returncode} - trace "[getnewaddress] response=${response}" + local address_type=${1} + trace "[getnewaddress] address_type=${address_type}" - if [ "${returncode}" -eq 0 ]; then - local address=$(echo ${response} | jq ".result") - trace "[getnewaddress] address=${address}" + local response + local data + if [ -z "${address_type}" ]; then + data='{"method":"getnewaddress"}' + else + data="{\"method\":\"getnewaddress\",\"params\":[\"\",\"${address_type}\"]}" + fi + response=$(send_to_spender_node "${data}") + local returncode=$? + trace_rc ${returncode} + trace "[getnewaddress] response=${response}" - data="{\"address\":${address}}" - else - trace "[getnewaddress] Coudn't get a new address!" - data="" - fi + if [ "${returncode}" -eq 0 ]; then + local address=$(echo ${response} | jq ".result") + trace "[getnewaddress] address=${address}" - trace "[getnewaddress] responding=${data}" - echo "${data}" + data="{\"address\":${address}}" + else + trace "[getnewaddress] Coudn't get a new address!" + data="" + fi - return ${returncode} + trace "[getnewaddress] responding=${data}" + echo "${data}" + + return ${returncode} } addtobatching() { - trace "Entering addtobatching()..." + trace "Entering addtobatching()..." - local address=${1} - trace "[addtobatching] address=${address}" - local amount=${2} - trace "[addtobatching] amount=${amount}" + local address=${1} + trace "[addtobatching] address=${address}" + local amount=${2} + trace "[addtobatching] amount=${amount}" - sql "INSERT OR IGNORE INTO recipient (address, amount) VALUES (\"${address}\", ${amount})" - returncode=$? - trace_rc ${returncode} + sql "INSERT OR IGNORE INTO recipient (address, amount) VALUES (\"${address}\", ${amount})" + returncode=$? + trace_rc ${returncode} - return ${returncode} + return ${returncode} } batchspend() { - trace "Entering batchspend()..." + trace "Entering batchspend()..." - local data - local response - local recipientswhere - local recipientsjson - local id_inserted + local data + local response + local recipientswhere + local recipientsjson + local id_inserted - # We will batch all the addresses in DB without a TXID - local batching=$(sql 'SELECT address, amount FROM recipient WHERE tx_id IS NULL') - trace "[batchspend] batching=${batching}" + # We will batch all the addresses in DB without a TXID + local batching=$(sql 'SELECT address, amount FROM recipient WHERE tx_id IS NULL') + trace "[batchspend] batching=${batching}" - local returncode - local address - local amount - local notfirst=false - local IFS=$'\n' - for row in ${batching} - do - trace "[batchspend] row=${row}" - address=$(echo "${row}" | cut -d '|' -f1) - trace "[batchspend] address=${address}" - amount=$(echo "${row}" | cut -d '|' -f2) - trace "[batchspend] amount=${amount}" + local returncode + local address + local amount + local notfirst=false + local IFS=$'\n' + for row in ${batching} + do + trace "[batchspend] row=${row}" + address=$(echo "${row}" | cut -d '|' -f1) + trace "[batchspend] address=${address}" + amount=$(echo "${row}" | cut -d '|' -f2) + trace "[batchspend] amount=${amount}" - if ${notfirst}; then - recipientswhere="${recipientswhere}," - recipientsjson="${recipientsjson}," - else - notfirst=true - fi + if ${notfirst}; then + recipientswhere="${recipientswhere}," + recipientsjson="${recipientsjson}," + else + notfirst=true + fi - recipientswhere="${recipientswhere}\"${address}\"" - recipientsjson="${recipientsjson}\"${address}\":${amount}" - done + recipientswhere="${recipientswhere}\"${address}\"" + recipientsjson="${recipientsjson}\"${address}\":${amount}" + done - response=$(send_to_spender_node "{\"method\":\"sendmany\",\"params\":[\"\", {${recipientsjson}}]}") - returncode=$? - trace_rc ${returncode} - trace "[batchspend] response=${response}" + response=$(send_to_spender_node "{\"method\":\"sendmany\",\"params\":[\"\", {${recipientsjson}}]}") + returncode=$? + trace_rc ${returncode} + trace "[batchspend] response=${response}" - if [ "${returncode}" -eq 0 ]; then - local txid=$(echo "${response}" | jq ".result" | tr -d '"') - trace "[batchspend] txid=${txid}" + if [ "${returncode}" -eq 0 ]; then + local txid=$(echo "${response}" | jq ".result" | tr -d '"') + trace "[batchspend] txid=${txid}" - # Let's insert the txid in our little DB to manage the confirmation and tell it's not a watching address - sql "INSERT OR IGNORE INTO tx (txid) VALUES (\"${txid}\")" - returncode=$? - trace_rc ${returncode} - if [ "${returncode}" -eq 0 ]; then - id_inserted=$(sql "SELECT id FROM tx WHERE txid=\"${txid}\"") - trace "[batchspend] id_inserted: ${id_inserted}" - sql "UPDATE recipient SET tx_id=${id_inserted} WHERE address IN (${recipientswhere})" - trace_rc $? - fi + # Let's insert the txid in our little DB to manage the confirmation and tell it's not a watching address + sql "INSERT OR IGNORE INTO tx (txid) VALUES (\"${txid}\")" + returncode=$? + trace_rc ${returncode} + if [ "${returncode}" -eq 0 ]; then + id_inserted=$(sql "SELECT id FROM tx WHERE txid=\"${txid}\"") + trace "[batchspend] id_inserted: ${id_inserted}" + sql "UPDATE recipient SET tx_id=${id_inserted} WHERE address IN (${recipientswhere})" + trace_rc $? + fi - data="{\"status\":\"accepted\"" - data="${data},\"hash\":\"${txid}\"}" - else - local message=$(echo "${response}" | jq -e ".error.message") - data="{\"message\":${message}}" - fi + data="{\"status\":\"accepted\"" + data="${data},\"hash\":\"${txid}\"}" + else + local message=$(echo "${response}" | jq -e ".error.message") + data="{\"message\":${message}}" + fi - trace "[batchspend] responding=${data}" - echo "${data}" + trace "[batchspend] responding=${data}" + echo "${data}" - return ${returncode} + return ${returncode} } create_wallet() {