More comments, cleaned code

This commit is contained in:
kexkey
2019-05-20 15:04:28 -04:00
committed by kexkey
parent ac2b031bf0
commit 73d7cdf8b2
7 changed files with 19 additions and 20 deletions

View File

@@ -12,6 +12,8 @@ main() {
local response local response
local response_topic local response_topic
# Messages should have this form:
# {"response-topic":"response/5541","cmd":"web","url":"2557df870b9a:1111/callback1conf","body":"eyJpZCI6IjUxIiwiYWRkc...dCI6MTUxNzYwMH0K"}
while read msg; do while read msg; do
trace "[main] New msg just arrived!" trace "[main] New msg just arrived!"
trace "[main] msg=${msg}" trace "[main] msg=${msg}"

View File

@@ -13,7 +13,6 @@ publish_response() {
trace "[publish_response] response_topic=${response_topic}" trace "[publish_response] response_topic=${response_topic}"
trace "[publish_response] returncode=${returncode}" trace "[publish_response] returncode=${returncode}"
# response=$(echo "${response}" | base64 | tr -d '\n')
trace "[publish_response] mosquitto_pub -h broker -t \"${response_topic}\" -m \"${response}\"" trace "[publish_response] mosquitto_pub -h broker -t \"${response_topic}\" -m \"${response}\""
mosquitto_pub -h broker -t "${response_topic}" -m "${response}" mosquitto_pub -h broker -t "${response_topic}" -m "${response}"
returncode=$? returncode=$?

View File

@@ -20,6 +20,7 @@ web() {
# jq -e will have a return code of 1 if the supplied tag is null. # jq -e will have a return code of 1 if the supplied tag is null.
if [ "$?" -eq "0" ]; then if [ "$?" -eq "0" ]; then
# body tag not null, so it's a POST # body tag not null, so it's a POST
# The body field has been based-64 to avoid dealing with escaping special chars
body=$(echo "${body}" | base64 -d) body=$(echo "${body}" | base64 -d)
trace "[web] body=${body}" trace "[web] body=${body}"
else else
@@ -31,16 +32,7 @@ web() {
returncode=$? returncode=$?
trace_rc ${returncode} trace_rc ${returncode}
if [ "${returncode}" -eq "0" ]; then
# {"result":"success", "response":"<html></html>"}
result="success"
else
# {"result":"error", "response":"<html></html>"}
result="error"
fi
echo "${response}" echo "${response}"
# echo "{\"result\":\"${result}\",\"http_code\":\"${http_code}\"}"
return ${returncode} return ${returncode}
} }
@@ -55,8 +47,8 @@ curl_it() {
local rnd=$(dd if=/dev/urandom bs=5 count=1 | xxd -pc 5) local rnd=$(dd if=/dev/urandom bs=5 count=1 | xxd -pc 5)
if [ -n "${data}" ]; then if [ -n "${data}" ]; then
trace "[curl_it] curl -o webresponse-${rnd} -w \"%{http_code}\" -H \"Content-Type: application/json\" -H \"X-Forwarded-Proto: https\" -d ${data} ${url}" trace "[curl_it] curl -o webresponse-${rnd} -w \"%{http_code}\" -H \"Content-Type: application/json\" -H \"X-Forwarded-Proto: https\" -d \"${data}\" ${url}"
rc=$(curl -o webresponse-${rnd} -w "%{http_code}" -H "Content-Type: application/json" -H "X-Forwarded-Proto: https" -d ${data} ${url}) rc=$(curl -o webresponse-${rnd} -w "%{http_code}" -H "Content-Type: application/json" -H "X-Forwarded-Proto: https" -d "${data}" ${url})
returncode=$? returncode=$?
else else
trace "[curl_it] curl -o webresponse-$$ -w \"%{http_code}\" ${url}" trace "[curl_it] curl -o webresponse-$$ -w \"%{http_code}\" ${url}"

View File

@@ -13,8 +13,8 @@ WORKDIR ${HOME}
COPY app/data/* ./ COPY app/data/* ./
COPY app/script/* ./ COPY app/script/* ./
COPY --from=cyphernode/clightning:v0.7.0-test /usr/local/bin/lightning-cli ./ COPY --from=cyphernode/clightning:v0.7.0-test /usr/local/bin/lightning-cli ./
COPY --from=eclipse-mosquitto /usr/bin/mosquitto_rr /usr/bin/mosquitto_sub /usr/bin/mosquitto_pub /usr/bin/ COPY --from=eclipse-mosquitto:1.6 /usr/bin/mosquitto_rr /usr/bin/mosquitto_sub /usr/bin/mosquitto_pub /usr/bin/
COPY --from=eclipse-mosquitto /usr/lib/libmosquitto* /usr/lib/ COPY --from=eclipse-mosquitto:1.6 /usr/lib/libmosquitto* /usr/lib/
RUN chmod +x startproxy.sh requesthandler.sh lightning-cli sqlmigrate*.sh waitanyinvoice.sh tests* \ RUN chmod +x startproxy.sh requesthandler.sh lightning-cli sqlmigrate*.sh waitanyinvoice.sh tests* \
&& chmod o+w . \ && chmod o+w . \

View File

@@ -150,6 +150,8 @@ confirmation() {
do do
watching_id=$(echo "${row}" | cut -d '|' -f1) watching_id=$(echo "${row}" | cut -d '|' -f1)
address=$(echo "${row}" | cut -d '|' -f2) address=$(echo "${row}" | cut -d '|' -f2)
# In the case of us spending to a watched address, the address appears twice in the details,
# once on the spend side (negative amount) and once on the receiving side (positive amount)
tx_vout_n=$(echo "${tx_details}" | jq ".result.details | map(select(.address==\"${address}\"))[0] | .vout") tx_vout_n=$(echo "${tx_details}" | jq ".result.details | map(select(.address==\"${address}\"))[0] | .vout")
tx_vout_amount=$(echo "${tx_details}" | jq ".result.details | map(select(.address==\"${address}\"))[0] | .amount | fabs" | awk '{ printf "%.8f", $0 }') tx_vout_amount=$(echo "${tx_details}" | jq ".result.details | map(select(.address==\"${address}\"))[0] | .amount | fabs" | awk '{ printf "%.8f", $0 }')
sql "INSERT OR IGNORE INTO watching_tx (watching_id, tx_id, vout, amount) VALUES (${watching_id}, ${id_inserted}, ${tx_vout_n}, ${tx_vout_amount})" sql "INSERT OR IGNORE INTO watching_tx (watching_id, tx_id, vout, amount) VALUES (${watching_id}, ${id_inserted}, ${tx_vout_n}, ${tx_vout_amount})"
@@ -177,6 +179,7 @@ confirmation() {
) 201>./.confirmation.lock ) 201>./.confirmation.lock
# There's a lock in callbacks, let's get out of the confirmation lock before entering another one
do_callbacks do_callbacks
echo '{"result":"confirmed"}' echo '{"result":"confirmed"}'

View File

@@ -14,11 +14,14 @@ notify_web() {
local response local response
local http_code local http_code
# We use the pid as the response-topic, so there's no conflict in responses.
trace "[notify_web] mosquitto_rr -h broker -W 5 -t notifier -e \"response/$$\" -m \"{\"response-topic\":\"response/$$\",\"cmd\":\"web\",\"url\":\"${url}\",\"body\":\"${body}\"}\"" trace "[notify_web] mosquitto_rr -h broker -W 5 -t notifier -e \"response/$$\" -m \"{\"response-topic\":\"response/$$\",\"cmd\":\"web\",\"url\":\"${url}\",\"body\":\"${body}\"}\""
response=$(mosquitto_rr -h broker -W 5 -t notifier -e "response/$$" -m "{\"response-topic\":\"response/$$\",\"cmd\":\"web\",\"url\":\"${url}\",\"body\":\"${body}\"}") response=$(mosquitto_rr -h broker -W 5 -t notifier -e "response/$$" -m "{\"response-topic\":\"response/$$\",\"cmd\":\"web\",\"url\":\"${url}\",\"body\":\"${body}\"}")
returncode=$? returncode=$?
trace_rc ${returncode} trace_rc ${returncode}
# The response looks like this: {"curl_code":0,"http_code":200,"body":"..."} where the body
# is the base64(response body) but we don't need the response content here.
trace "[notify_web] response=${response}" trace "[notify_web] response=${response}"
http_code=$(echo "${response}" | jq ".http_code" | tr -d '"') http_code=$(echo "${response}" | jq ".http_code" | tr -d '"')
trace "[notify_web] http_code=${http_code}" trace "[notify_web] http_code=${http_code}"

View File

@@ -26,13 +26,14 @@ spend() {
local txid=$(echo "${response}" | jq ".result" | tr -d '"') local txid=$(echo "${response}" | jq ".result" | tr -d '"')
trace "[spend] txid=${txid}" trace "[spend] txid=${txid}"
# Let's get transaction details on the spending wallet so that we have fee information
tx_details=$(get_transaction ${txid} "spender") tx_details=$(get_transaction ${txid} "spender")
tx_raw_details=$(get_rawtransaction ${txid}) tx_raw_details=$(get_rawtransaction ${txid})
# Amounts and fees are negative when spending so we absolute those fields
local tx_hash=$(echo "${tx_raw_details}" | jq '.result.hash') local tx_hash=$(echo "${tx_raw_details}" | jq '.result.hash')
local tx_ts_firstseen=$(echo "${tx_details}" | jq '.result.timereceived') local tx_ts_firstseen=$(echo "${tx_details}" | jq '.result.timereceived')
local tx_amount=$(echo "${tx_details}" | jq '.result.amount | fabs' | awk '{ printf "%.8f", $0 }') local tx_amount=$(echo "${tx_details}" | jq '.result.amount | fabs' | awk '{ printf "%.8f", $0 }')
local tx_size=$(echo "${tx_raw_details}" | jq '.result.size') local tx_size=$(echo "${tx_raw_details}" | jq '.result.size')
local tx_vsize=$(echo "${tx_raw_details}" | jq '.result.vsize') local tx_vsize=$(echo "${tx_raw_details}" | jq '.result.vsize')
local tx_replaceable=$(echo "${tx_details}" | jq '.result."bip125-replaceable"') local tx_replaceable=$(echo "${tx_details}" | jq '.result."bip125-replaceable"')
@@ -40,8 +41,7 @@ spend() {
local fees=$(echo "${tx_details}" | jq '.result.fee | fabs' | awk '{ printf "%.8f", $0 }') local fees=$(echo "${tx_details}" | jq '.result.fee | fabs' | awk '{ printf "%.8f", $0 }')
local rawtx=$(echo "${tx_details}" | jq '.result.hex') local rawtx=$(echo "${tx_details}" | jq '.result.hex')
# Let's insert the txid in our little DB to manage the confirmation and tell it's not a watching address # Let's insert the txid in our little DB -- then we'll already have it when receiving confirmation
#sql "INSERT OR IGNORE INTO tx (txid) VALUES (\"${txid}\")"
sql "INSERT OR IGNORE INTO tx (txid, hash, confirmations, timereceived, fee, size, vsize, is_replaceable, raw_tx) VALUES (\"${txid}\", ${tx_hash}, 0, ${tx_ts_firstseen}, ${fees}, ${tx_size}, ${tx_vsize}, ${tx_replaceable}, ${rawtx})" sql "INSERT OR IGNORE INTO tx (txid, hash, confirmations, timereceived, fee, size, vsize, is_replaceable, raw_tx) VALUES (\"${txid}\", ${tx_hash}, 0, ${tx_ts_firstseen}, ${fees}, ${tx_size}, ${tx_vsize}, ${tx_replaceable}, ${rawtx})"
trace_rc $? trace_rc $?
id_inserted=$(sql "SELECT id FROM tx WHERE txid=\"${txid}\"") id_inserted=$(sql "SELECT id FROM tx WHERE txid=\"${txid}\"")
@@ -230,13 +230,14 @@ batchspend() {
local txid=$(echo "${response}" | jq ".result" | tr -d '"') local txid=$(echo "${response}" | jq ".result" | tr -d '"')
trace "[batchspend] txid=${txid}" trace "[batchspend] txid=${txid}"
# Let's get transaction details on the spending wallet so that we have fee information
tx_details=$(get_transaction ${txid} "spender") tx_details=$(get_transaction ${txid} "spender")
tx_raw_details=$(get_rawtransaction ${txid}) tx_raw_details=$(get_rawtransaction ${txid})
# Amounts and fees are negative when spending so we absolute those fields
local tx_hash=$(echo "${tx_raw_details}" | jq '.result.hash') local tx_hash=$(echo "${tx_raw_details}" | jq '.result.hash')
local tx_ts_firstseen=$(echo "${tx_details}" | jq '.result.timereceived') local tx_ts_firstseen=$(echo "${tx_details}" | jq '.result.timereceived')
local tx_amount=$(echo "${tx_details}" | jq '.result.amount | fabs' | awk '{ printf "%.8f", $0 }') local tx_amount=$(echo "${tx_details}" | jq '.result.amount | fabs' | awk '{ printf "%.8f", $0 }')
local tx_size=$(echo "${tx_raw_details}" | jq '.result.size') local tx_size=$(echo "${tx_raw_details}" | jq '.result.size')
local tx_vsize=$(echo "${tx_raw_details}" | jq '.result.vsize') local tx_vsize=$(echo "${tx_raw_details}" | jq '.result.vsize')
local tx_replaceable=$(echo "${tx_details}" | jq '.result."bip125-replaceable"') local tx_replaceable=$(echo "${tx_details}" | jq '.result."bip125-replaceable"')
@@ -244,8 +245,7 @@ batchspend() {
local fees=$(echo "${tx_details}" | jq '.result.fee | fabs' | awk '{ printf "%.8f", $0 }') local fees=$(echo "${tx_details}" | jq '.result.fee | fabs' | awk '{ printf "%.8f", $0 }')
local rawtx=$(echo "${tx_details}" | jq '.result.hex') local rawtx=$(echo "${tx_details}" | jq '.result.hex')
# Let's insert the txid in our little DB to manage the confirmation and tell it's not a watching address # Let's insert the txid in our little DB -- then we'll already have it when receiving confirmation
#sql "INSERT OR IGNORE INTO tx (txid) VALUES (\"${txid}\")"
sql "INSERT OR IGNORE INTO tx (txid, hash, confirmations, timereceived, fee, size, vsize, is_replaceable, raw_tx) VALUES (\"${txid}\", ${tx_hash}, 0, ${tx_ts_firstseen}, ${fees}, ${tx_size}, ${tx_vsize}, ${tx_replaceable}, ${rawtx})" sql "INSERT OR IGNORE INTO tx (txid, hash, confirmations, timereceived, fee, size, vsize, is_replaceable, raw_tx) VALUES (\"${txid}\", ${tx_hash}, 0, ${tx_ts_firstseen}, ${fees}, ${tx_size}, ${tx_vsize}, ${tx_replaceable}, ${rawtx})"
returncode=$? returncode=$?
trace_rc ${returncode} trace_rc ${returncode}