OTS Stamping support, first pass

This commit is contained in:
kexkey
2018-11-12 12:01:01 -05:00
committed by kexkey
parent 557163f3c4
commit 830e16c12d
22 changed files with 827 additions and 264 deletions

View File

@@ -0,0 +1,203 @@
#!/bin/sh
. ./trace.sh
serve_ots_stamp()
{
trace "Entering serve_ots_stamp()..."
local request=${1}
local hash=$(echo "${request}" | jq ".hash" | tr -d '"')
trace "[serve_ots_stamp] hash=${hash}"
local callbackUrl=$(echo "${request}" | jq ".callbackUrl" | tr -d '"')
trace "[serve_ots_stamp] callbackUrl=${callbackUrl}"
local result
local returncode
local errorstring
# Already requested?
local requested
requested=$(sql "SELECT requested FROM stamp WHERE hash='${hash}'")
if [ -n "${requested}" ]; then
# Hash exists in DB...
trace "[serve_ots_stamp] Hash already exists in DB."
if [ "${requested}" -eq "1" ]; then
# Stamp already requested
trace "[serve_ots_stamp] Stamp already requested"
errorstring="Duplicate stamping request, hash already exists in DB and been OTS requested"
returncode=1
else
errorstring=$(request_ots_stamp "${hash}")
returncode=$?
fi
else
sql "INSERT OR IGNORE INTO stamp (hash, callbackUrl) VALUES (\"${hash}\", \"${callbackUrl}\")"
returncode=$?
trace_rc ${returncode}
if [ "${returncode}" -eq "0" ]; then
errorstring=$(request_ots_stamp "${hash}")
returncode=$?
trace_rc ${returncode}
else
trace "[serve_ots_stamp] Stamp request could not be inserted in DB"
errorstring="Stamp request could not be inserted in DB, please retry later"
returncode=1
fi
fi
if [ "${returncode}" -eq "0" ]; then
result="{\"method\":\"ots_stamp\",\"hash\":\"${hash}\",\"result\":\"success\""
else
result="{\"method\":\"ots_stamp\",\"hash\":\"${hash}\",\"result\":\"error\",\"error\":\"${errorstring}\""
fi
trace "[serve_ots_stamp] result=${result}"
# Output response to stdout before exiting with return code
echo "${result}"
return ${returncode}
}
request_ots_stamp()
{
# Request the OTS server to stamp
local hash=${1}
local returncode
local result
local errorstring
trace "[request_ots_stamp] Stamping..."
result=$(curl -s ${OTSCLIENT_CONTAINER}/stamp/${hash})
returncode=$?
trace_rc ${returncode}
trace "[request_ots_stamp] Stamping result=${result}"
if [ "${returncode}" -eq 0 ]; then
# jq -e will have a return code of 1 if the supplied tag is null.
errorstring=$(echo "${result}" | tr '\r\n' ' ' | jq -e ".error" | tr -d '"')
if [ "$?" -eq "0" ]; then
# Error tag not null, so there's an error
# If the error message is "Already exists"
trace "[request_ots_stamp] grepping 'already exists'..."
echo "${result}" | grep "already exists" > /dev/null
returncode=$?
trace_rc ${returncode}
if [ "${returncode}" -eq "0" ]; then
# "already exists" found, let's try updating DB again
trace "[request_ots_stamp] was already requested to the OTS server... let's update the DB, looks like it didn't work on first try"
sql "UPDATE stamp SET requested=1 WHERE hash='${hash}'"
errorstring="Duplicate stamping request, hash already exists in DB and been OTS requested"
returncode=1
else
# If OTS CLIENT responded with an error, it is not down, it just can't stamp it. ABORT.
trace "[request_ots_stamp] Stamping error: ${errorstring}"
sql "DELETE FROM stamp WHERE hash='${hash}'"
returncode=1
fi
else
trace "[request_ots_stamp] Stamping request sent successfully!"
sql "UPDATE stamp SET requested=1 WHERE hash='${hash}'"
errorstring=""
returncode=0
fi
else
trace "[request_ots_stamp] Stamping error, will retry later: ${errorstring}"
errorstring=""
returncode=0
fi
echo "${errorstring}"
return ${returncode}
}
serve_ots_backoffice()
{
# What we want to do here:
# ========================
# Re-request the unrequested calls to ots_stamp
# Upgrade requested calls to ots_stamp that have not been called back yet
# Call back newly upgraded stamps
trace "Entering serve_ots_backoffice()..."
local result
local returncode
# Let's fetch all the incomplete stamping request
local callbacks=$(sql 'SELECT hash, callbackUrl, requested, upgraded FROM stamp WHERE NOT calledback')
trace "[serve_ots_backoffice] callbacks=${callbacks}"
local url
local hash
local requested
local upgraded
local IFS=$'\n'
for row in ${callbacks}
do
trace "[serve_ots_backoffice] row=${row}"
hash=$(echo "${row}" | cut -d '|' -f1)
trace "[serve_ots_backoffice] hash=${hash}"
requested=$(echo "${row}" | cut -d '|' -f3)
trace "[serve_ots_backoffice] requested=${requested}"
upgraded=$(echo "${row}" | cut -d '|' -f4)
trace "[serve_ots_backoffice] upgraded=${upgraded}"
if [ "${requested}" -ne "1" ]; then
# Re-request the unrequested calls to ots_stamp
request_ots_stamp "${hash}"
returncode=$?
else
if [ "${upgraded}" -ne "1" ]; then
# Upgrade requested calls to ots_stamp that have not been called back yet
trace "[serve_ots_backoffice] curl -s ${OTSCLIENT_CONTAINER}/upgrade/${hash}"
result=$(curl -s ${OTSCLIENT_CONTAINER}/upgrade/${hash})
returncode=$?
trace_rc ${returncode}
trace "[serve_ots_backoffice] result=${result}"
if [ "${returncode}" -eq 0 ]; then
trace "[serve_ots_backoffice] just upgraded!"
sql "UPDATE stamp SET upgraded=1 WHERE hash=\"${hash}\""
trace_rc $?
upgraded=1
fi
fi
if [ "${upgraded}" -eq "1" ]; then
trace "[serve_ots_backoffice] upgraded! Let's send the OTS file to callback..."
url=$(echo "${row}" | cut -d '|' -f2)
trace "[serve_ots_backoffice] url=${url}"
# Call back newly upgraded stamps
curl -H "X-Forwarded-Proto: https" ${url}
returncode=$?
trace_rc ${returncode}
if [ "${returncode}" -eq "0" ]; then
sql "UPDATE stamp SET calledback=1 WHERE hash=\"${hash}\""
trace_rc $?
fi
fi
fi
done
}
serve_ots_getfile()
{
trace "Entering serve_ots_getfile()..."
local hash=${1}
trace "[serve_ots_getfile] hash=${hash}"
file_response_to_client "/otsfiles/" "${hash}.ots"
returncode=$?
trace_rc ${returncode}
return ${returncode}
}

View File

@@ -17,216 +17,238 @@
. ./walletoperations.sh
. ./bitcoin.sh
. ./call_lightningd.sh
. ./ots.sh
main()
{
trace "Entering main()..."
trace "Entering main()..."
local step=0
local cmd
local http_method
local line
local content_length
local response
local returncode
local step=0
local cmd
local http_method
local line
local content_length
local response
local returncode
while read line; do
line=$(echo "${line}" | tr -d '\r\n')
trace "[main] line=${line}"
while read line; do
line=$(echo "${line}" | tr -d '\r\n')
trace "[main] line=${line}"
if [ "${cmd}" = "" ]; then
# First line!
# Looking for something like:
# GET /cmd/params HTTP/1.1
# POST / HTTP/1.1
cmd=$(echo "${line}" | cut -d '/' -f2 | cut -d ' ' -f1)
trace "[main] cmd=${cmd}"
http_method=$(echo "${line}" | cut -d ' ' -f1)
trace "[main] http_method=${http_method}"
if [ "${http_method}" = "GET" ]; then
step=1
fi
fi
if [ "${line}" = "" ]; then
trace "[main] empty line"
if [ ${step} -eq 1 ]; then
trace "[main] body part finished, disconnecting"
break
else
trace "[main] headers part finished, body incoming"
step=1
fi
fi
# line=content-length: 406
case "${line}" in *[cC][oO][nN][tT][eE][nN][tT]-[lL][eE][nN][gG][tT][hH]*)
content_length=$(echo ${line} | cut -d ':' -f2)
trace "[main] content_length=${content_length}";
;;
esac
if [ ${step} -eq 1 ]; then
trace "[main] step=${step}"
if [ "${http_method}" = "POST" ]; then
read -n ${content_length} line
trace "[main] line=${line}"
fi
case "${cmd}" in
watch)
# POST http://192.168.111.152:8080/watch
# BODY {"address":"2N8DcqzfkYi8CkYzvNNS5amoq3SbAcQNXKp","unconfirmedCallbackURL":"192.168.111.233:1111/callback0conf","confirmedCallbackURL":"192.168.111.233:1111/callback1conf"}
if [ "${cmd}" = "" ]; then
# First line!
# Looking for something like:
# GET /cmd/params HTTP/1.1
# POST / HTTP/1.1
cmd=$(echo "${line}" | cut -d '/' -f2 | cut -d ' ' -f1)
trace "[main] cmd=${cmd}"
http_method=$(echo "${line}" | cut -d ' ' -f1)
trace "[main] http_method=${http_method}"
if [ "${http_method}" = "GET" ]; then
step=1
fi
fi
if [ "${line}" = "" ]; then
trace "[main] empty line"
if [ ${step} -eq 1 ]; then
trace "[main] body part finished, disconnecting"
break
else
trace "[main] headers part finished, body incoming"
step=1
fi
fi
# line=content-length: 406
case "${line}" in *[cC][oO][nN][tT][eE][nN][tT]-[lL][eE][nN][gG][tT][hH]*)
content_length=$(echo ${line} | cut -d ':' -f2)
trace "[main] content_length=${content_length}";
;;
esac
if [ ${step} -eq 1 ]; then
trace "[main] step=${step}"
if [ "${http_method}" = "POST" ]; then
read -n ${content_length} line
trace "[main] line=${line}"
fi
case "${cmd}" in
watch)
# POST http://192.168.111.152:8080/watch
# BODY {"address":"2N8DcqzfkYi8CkYzvNNS5amoq3SbAcQNXKp","unconfirmedCallbackURL":"192.168.111.233:1111/callback0conf","confirmedCallbackURL":"192.168.111.233:1111/callback1conf"}
response=$(watchrequest "${line}")
response_to_client "${response}" ${?}
break
;;
unwatch)
# curl (GET) 192.168.111.152:8080/unwatch/2N8DcqzfkYi8CkYzvNNS5amoq3SbAcQNXKp
response=$(watchrequest "${line}")
response_to_client "${response}" ${?}
break
;;
unwatch)
# curl (GET) 192.168.111.152:8080/unwatch/2N8DcqzfkYi8CkYzvNNS5amoq3SbAcQNXKp
response=$(unwatchrequest "${line}")
response_to_client "${response}" ${?}
break
;;
getactivewatches)
# curl (GET) 192.168.111.152:8080/getactivewatches
response=$(unwatchrequest "${line}")
response_to_client "${response}" ${?}
break
;;
getactivewatches)
# curl (GET) 192.168.111.152:8080/getactivewatches
response=$(getactivewatches)
response_to_client "${response}" ${?}
break
;;
conf)
# curl (GET) 192.168.111.152:8080/conf/b081ca7724386f549cf0c16f71db6affeb52ff7a0d9b606fb2e5c43faffd3387
response=$(getactivewatches)
response_to_client "${response}" ${?}
break
;;
conf)
# curl (GET) 192.168.111.152:8080/conf/b081ca7724386f549cf0c16f71db6affeb52ff7a0d9b606fb2e5c43faffd3387
response=$(confirmation_request "${line}")
response_to_client "${response}" ${?}
break
;;
getbestblockhash)
# curl (GET) http://192.168.111.152:8080/getbestblockhash
response=$(confirmation_request "${line}")
response_to_client "${response}" ${?}
break
;;
getbestblockhash)
# curl (GET) http://192.168.111.152:8080/getbestblockhash
response=$(get_best_block_hash)
response_to_client "${response}" ${?}
break
;;
getblockinfo)
# curl (GET) http://192.168.111.152:8080/getblockinfo/000000006f82a384c208ecfa04d05beea02d420f3f398ddda5c7f900de5718ea
response=$(get_best_block_hash)
response_to_client "${response}" ${?}
break
;;
getblockinfo)
# curl (GET) http://192.168.111.152:8080/getblockinfo/000000006f82a384c208ecfa04d05beea02d420f3f398ddda5c7f900de5718ea
response=$(get_block_info $(echo "${line}" | cut -d ' ' -f2 | cut -d '/' -f3))
response_to_client "${response}" ${?}
break
;;
gettransaction)
# curl (GET) http://192.168.111.152:8080/gettransaction/af867c86000da76df7ddb1054b273ca9e034e8c89d049b5b2795f9f590f67648
response=$(get_block_info $(echo "${line}" | cut -d ' ' -f2 | cut -d '/' -f3))
response_to_client "${response}" ${?}
break
;;
gettransaction)
# curl (GET) http://192.168.111.152:8080/gettransaction/af867c86000da76df7ddb1054b273ca9e034e8c89d049b5b2795f9f590f67648
response=$(get_rawtransaction $(echo "${line}" | cut -d ' ' -f2 | cut -d '/' -f3))
response_to_client "${response}" ${?}
break
;;
getbestblockinfo)
# curl (GET) http://192.168.111.152:8080/getbestblockinfo
response=$(get_rawtransaction $(echo "${line}" | cut -d ' ' -f2 | cut -d '/' -f3))
response_to_client "${response}" ${?}
break
;;
getbestblockinfo)
# curl (GET) http://192.168.111.152:8080/getbestblockinfo
response=$(get_best_block_info)
response_to_client "${response}" ${?}
break
;;
executecallbacks)
# curl (GET) http://192.168.111.152:8080/executecallbacks
response=$(get_best_block_info)
response_to_client "${response}" ${?}
break
;;
executecallbacks)
# curl (GET) http://192.168.111.152:8080/executecallbacks
manage_not_imported
manage_missed_conf
response=$(do_callbacks)
response_to_client "${response}" ${?}
break
;;
getbalance)
# curl (GET) http://192.168.111.152:8080/getbalance
manage_not_imported
manage_missed_conf
response=$(do_callbacks)
response_to_client "${response}" ${?}
break
;;
getbalance)
# curl (GET) http://192.168.111.152:8080/getbalance
response=$(getbalance)
response_to_client "${response}" ${?}
break
;;
getnewaddress)
# curl (GET) http://192.168.111.152:8080/getnewaddress
response=$(getbalance)
response_to_client "${response}" ${?}
break
;;
getnewaddress)
# curl (GET) http://192.168.111.152:8080/getnewaddress
response=$(getnewaddress)
response_to_client "${response}" ${?}
break
;;
spend)
# POST http://192.168.111.152:8080/spend
# BODY {"address":"2N8DcqzfkYi8CkYzvNNS5amoq3SbAcQNXKp","amount":0.00233}
response=$(getnewaddress)
response_to_client "${response}" ${?}
break
;;
spend)
# POST http://192.168.111.152:8080/spend
# BODY {"address":"2N8DcqzfkYi8CkYzvNNS5amoq3SbAcQNXKp","amount":0.00233}
response=$(spend "${line}")
response_to_client "${response}" ${?}
break
;;
addtobatch)
# POST http://192.168.111.152:8080/addtobatch
# BODY {"address":"2N8DcqzfkYi8CkYzvNNS5amoq3SbAcQNXKp","amount":0.00233}
response=$(spend "${line}")
response_to_client "${response}" ${?}
break
;;
addtobatch)
# POST http://192.168.111.152:8080/addtobatch
# BODY {"address":"2N8DcqzfkYi8CkYzvNNS5amoq3SbAcQNXKp","amount":0.00233}
response=$(addtobatching $(echo "${line}" | jq ".address" | tr -d '"') $(echo "${line}" | jq ".amount"))
response_to_client "${response}" ${?}
break
;;
batchspend)
# GET http://192.168.111.152:8080/batchspend
response=$(addtobatching $(echo "${line}" | jq ".address" | tr -d '"') $(echo "${line}" | jq ".amount"))
response_to_client "${response}" ${?}
break
;;
batchspend)
# GET http://192.168.111.152:8080/batchspend
response=$(batchspend "${line}")
response_to_client "${response}" ${?}
break
;;
deriveindex)
# curl GET http://192.168.111.152:8080/deriveindex/25-30
# curl GET http://192.168.111.152:8080/deriveindex/34
response=$(batchspend "${line}")
response_to_client "${response}" ${?}
break
;;
deriveindex)
# curl GET http://192.168.111.152:8080/deriveindex/25-30
# curl GET http://192.168.111.152:8080/deriveindex/34
response=$(deriveindex $(echo "${line}" | cut -d ' ' -f2 | cut -d '/' -f3))
response_to_client "${response}" ${?}
break
;;
derivepubpath)
# POST http://192.168.111.152:8080/derivepubpath
# BODY {"pub32":"tpubD6NzVbkrYhZ4YR3QK2tyfMMvBghAvqtNaNK1LTyDWcRHLcMUm3ZN2cGm5BS3MhCRCeCkXQkTXXjiJgqxpqXK7PeUSp86DTTgkLpcjMtpKWk","path":"0/25-30"}
# BODY {"pub32":"upub5GtUcgGed1aGH4HKQ3vMYrsmLXwmHhS1AeX33ZvDgZiyvkGhNTvGd2TA5Lr4v239Fzjj4ZY48t6wTtXUy2yRgapf37QHgt6KWEZ6bgsCLpb","path":"0/25-30"}
# BODY {"pub32":"vpub5SLqN2bLY4WeZF3kL4VqiWF1itbf3A6oRrq9aPf16AZMVWYCuN9TxpAZwCzVgW94TNzZPNc9XAHD4As6pdnExBtCDGYRmNJrcJ4eV9hNqcv","path":"0/25-30"}
response=$(deriveindex $(echo "${line}" | cut -d ' ' -f2 | cut -d '/' -f3))
response_to_client "${response}" ${?}
break
;;
derivepubpath)
# POST http://192.168.111.152:8080/derivepubpath
# BODY {"pub32":"tpubD6NzVbkrYhZ4YR3QK2tyfMMvBghAvqtNaNK1LTyDWcRHLcMUm3ZN2cGm5BS3MhCRCeCkXQkTXXjiJgqxpqXK7PeUSp86DTTgkLpcjMtpKWk","path":"0/25-30"}
# BODY {"pub32":"upub5GtUcgGed1aGH4HKQ3vMYrsmLXwmHhS1AeX33ZvDgZiyvkGhNTvGd2TA5Lr4v239Fzjj4ZY48t6wTtXUy2yRgapf37QHgt6KWEZ6bgsCLpb","path":"0/25-30"}
# BODY {"pub32":"vpub5SLqN2bLY4WeZF3kL4VqiWF1itbf3A6oRrq9aPf16AZMVWYCuN9TxpAZwCzVgW94TNzZPNc9XAHD4As6pdnExBtCDGYRmNJrcJ4eV9hNqcv","path":"0/25-30"}
response=$(send_to_pycoin "${line}")
response_to_client "${response}" ${?}
break
;;
ln_getinfo)
# GET http://192.168.111.152:8080/ln_getinfo
response=$(send_to_pycoin "${line}")
response_to_client "${response}" ${?}
break
;;
ln_getinfo)
# GET http://192.168.111.152:8080/ln_getinfo
response=$(ln_getinfo)
response_to_client "${response}" ${?}
break
;;
ln_create_invoice)
# POST http://192.168.111.152:8080/ln_create_invoice
# BODY {"msatoshi":"10000","label":"koNCcrSvhX3dmyFhW","description":"Bylls order #10649","expiry":"900"}
response=$(ln_getinfo)
response_to_client "${response}" ${?}
break
;;
ln_create_invoice)
# POST http://192.168.111.152:8080/ln_create_invoice
# BODY {"msatoshi":"10000","label":"koNCcrSvhX3dmyFhW","description":"Bylls order #10649","expiry":"900"}
response=$(ln_create_invoice "${line}")
response_to_client "${response}" ${?}
break
;;
ln_pay)
# POST http://192.168.111.152:8080/ln_pay
# BODY {"bolt11":"lntb1pdca82tpp5gv8mn5jqlj6xztpnt4r472zcyrwf3y2c3cvm4uzg2gqcnj90f83qdp2gf5hgcm0d9hzqnm4w3kx2apqdaexgetjyq3nwvpcxgcqp2g3d86wwdfvyxcz7kce7d3n26d2rw3wf5tzpm2m5fl2z3mm8msa3xk8nv2y32gmzlhwjved980mcmkgq83u9wafq9n4w28amnmwzujgqpmapcr3","expected_msatoshi":"10000","expected_description":"Bitcoin Outlet order #7082"}
response=$(ln_create_invoice "${line}")
response_to_client "${response}" ${?}
break
;;
ln_pay)
# POST http://192.168.111.152:8080/ln_pay
# BODY {"bolt11":"lntb1pdca82tpp5gv8mn5jqlj6xztpnt4r472zcyrwf3y2c3cvm4uzg2gqcnj90f83qdp2gf5hgcm0d9hzqnm4w3kx2apqdaexgetjyq3nwvpcxgcqp2g3d86wwdfvyxcz7kce7d3n26d2rw3wf5tzpm2m5fl2z3mm8msa3xk8nv2y32gmzlhwjved980mcmkgq83u9wafq9n4w28amnmwzujgqpmapcr3","expected_msatoshi":"10000","expected_description":"Bitcoin Outlet order #7082"}
response=$(ln_pay "${line}")
response_to_client "${response}" ${?}
break
;;
ln_newaddr)
# GET http://192.168.111.152:8080/ln_newaddr
response=$(ln_pay "${line}")
response_to_client "${response}" ${?}
break
;;
ln_newaddr)
# GET http://192.168.111.152:8080/ln_newaddr
response=$(ln_newaddr)
response_to_client "${response}" ${?}
break
;;
esac
break
fi
done
trace "[main] exiting"
return 0
response=$(ln_newaddr)
response_to_client "${response}" ${?}
break
;;
ots_stamp)
# POST http://192.168.111.152:8080/ots_stamp
# BODY {"hash":"1ddfb769eb0b8876bc570e25580e6a53afcf973362ee1ee4b54a807da2e5eed7","callbackUrl":"192.168.111.233:1111/callbackUrl"}
response=$(serve_ots_stamp "${line}")
response_to_client "${response}" ${?}
break
;;
ots_backoffice)
# curl (GET) http://192.168.111.152:8080/ots_upgradeandcallback
response=$(serve_ots_backoffice)
response_to_client "${response}" ${?}
break
;;
ots_getfile)
# curl (GET) http://192.168.111.152:8080/ots_getfile/1ddfb769eb0b8876bc570e25580e6a53afcf973362ee1ee4b54a807da2e5eed7
serve_ots_getfile $(echo "${line}" | cut -d ' ' -f2 | cut -d '/' -f3)
break
;;
esac
break
fi
done
trace "[main] exiting"
return 0
}
export NODE_RPC_URL=$BTC_NODE_RPC_URL

View File

@@ -18,4 +18,23 @@ response_to_client()
sleep 0.2s
}
file_response_to_client()
{
trace "Entering bin_response_to_client()..."
local path=${1}
local filename=${2}
local pathfile="${path}${filename}"
local returncode
[ -r "${pathfile}" ] \
&& echo -ne "HTTP/1.1 200 OK\r\nContent-Disposition: inline; filename=\"${filename}\"\r\nContent-Length: $(stat -c'%s' ${pathfile})\r\n\r\n" \
&& cat ${pathfile}
[ ! -r "${pathfile}" ] && echo -ne "HTTP/1.1 404 Not Found\r\n"
# Small delay needed for the data to be processed correctly by peer
sleep 0.2s
}
case "${0}" in *responsetoclient.sh) response_to_client $@;; esac

View File

@@ -7,69 +7,68 @@
watchrequest()
{
trace "Entering watchrequest()..."
trace "Entering watchrequest()..."
local returncode
local request=${1}
local address=$(echo "${request}" | jq ".address" | tr -d '"')
local cb0conf_url=$(echo "${request}" | jq ".unconfirmedCallbackURL" | tr -d '"')
local cb1conf_url=$(echo "${request}" | jq ".confirmedCallbackURL" | tr -d '"')
local imported
local inserted
local id_inserted
local result
trace "[watchrequest] Watch request on address (${address}), cb 0-conf (${cb0conf_url}), cb 1-conf (${cb1conf_url})"
local returncode
local request=${1}
local address=$(echo "${request}" | jq ".address" | tr -d '"')
local cb0conf_url=$(echo "${request}" | jq ".unconfirmedCallbackURL" | tr -d '"')
local cb1conf_url=$(echo "${request}" | jq ".confirmedCallbackURL" | tr -d '"')
local imported
local inserted
local id_inserted
local result
trace "[watchrequest] Watch request on address (${address}), cb 0-conf (${cb0conf_url}), cb 1-conf (${cb1conf_url})"
result=$(importaddress_rpc "${address}")
returncode=$?
trace_rc ${returncode}
if [ "${returncode}" -eq 0 ]; then
imported=1
else
imported=0
fi
result=$(importaddress_rpc "${address}")
returncode=$?
trace_rc ${returncode}
if [ "${returncode}" -eq 0 ]; then
imported=1
else
imported=0
fi
# sql "INSERT OR IGNORE INTO watching (address, watching, callback0conf, callback1conf, imported) VALUES (\"${address}\", 1, \"${cb0conf_url}\", \"${cb1conf_url}\", ${imported})"
sql "INSERT OR IGNORE INTO watching (address, watching, callback0conf, callback1conf, imported) VALUES (\"${address}\", 1, \"${cb0conf_url}\", \"${cb1conf_url}\", ${imported})"
returncode=$?
trace_rc ${returncode}
if [ "${returncode}" -eq 0 ]; then
inserted=1
id_inserted=$(sql "SELECT id FROM watching WHERE address='${address}'")
trace "[watchrequest] id_inserted: ${id_inserted}"
else
inserted=0
fi
sql "INSERT OR IGNORE INTO watching (address, watching, callback0conf, callback1conf, imported) VALUES (\"${address}\", 1, \"${cb0conf_url}\", \"${cb1conf_url}\", ${imported})"
returncode=$?
trace_rc ${returncode}
if [ "${returncode}" -eq 0 ]; then
inserted=1
id_inserted=$(sql "SELECT id FROM watching WHERE address='${address}'")
trace "[watchrequest] id_inserted: ${id_inserted}"
else
inserted=0
fi
local fees2blocks
local fees6blocks
local fees36blocks
local fees144blocks
fees2blocks=$(getestimatesmartfee 2)
trace_rc $?
fees6blocks=$(getestimatesmartfee 6)
trace_rc $?
fees36blocks=$(getestimatesmartfee 36)
trace_rc $?
fees144blocks=$(getestimatesmartfee 144)
trace_rc $?
local fees2blocks
local fees6blocks
local fees36blocks
local fees144blocks
fees2blocks=$(getestimatesmartfee 2)
trace_rc $?
fees6blocks=$(getestimatesmartfee 6)
trace_rc $?
fees36blocks=$(getestimatesmartfee 36)
trace_rc $?
fees144blocks=$(getestimatesmartfee 144)
trace_rc $?
local data="{\"id\":\"${id_inserted}\",
\"event\":\"watch\",
\"imported\":\"${imported}\",
\"inserted\":\"${inserted}\",
\"address\":\"${address}\",
\"unconfirmedCallbackURL\":\"${cb0conf_url}\",
\"confirmedCallbackURL\":\"${cb1conf_url}\",
\"estimatesmartfee2blocks\":\"${fees2blocks}\",
\"estimatesmartfee6blocks\":\"${fees6blocks}\",
\"estimatesmartfee36blocks\":\"${fees36blocks}\",
\"estimatesmartfee144blocks\":\"${fees144blocks}\"}"
trace "[watchrequest] responding=${data}"
local data="{\"id\":\"${id_inserted}\",
\"event\":\"watch\",
\"imported\":\"${imported}\",
\"inserted\":\"${inserted}\",
\"address\":\"${address}\",
\"unconfirmedCallbackURL\":\"${cb0conf_url}\",
\"confirmedCallbackURL\":\"${cb1conf_url}\",
\"estimatesmartfee2blocks\":\"${fees2blocks}\",
\"estimatesmartfee6blocks\":\"${fees6blocks}\",
\"estimatesmartfee36blocks\":\"${fees36blocks}\",
\"estimatesmartfee144blocks\":\"${fees144blocks}\"}"
trace "[watchrequest] responding=${data}"
echo "${data}"
echo "${data}"
return ${returncode}
return ${returncode}
}
case "${0}" in *watchrequest.sh) watchrequest $@;; esac