#!/bin/sh # # # # . ./db/config.sh . ./sendtobitcoinnode.sh . ./callbacks_job.sh . ./watchrequest.sh . ./unwatchrequest.sh . ./getactivewatches.sh . ./confirmation.sh . ./blockchainrpc.sh . ./responsetoclient.sh . ./trace.sh . ./manage_missed_conf.sh . ./walletoperations.sh . ./bitcoin.sh . ./call_lightningd.sh . ./ots.sh . ./newblock.sh . ./batching.sh main() { trace "Entering main()..." 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}" 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 -rd '' -n ${content_length} line line=$(echo "${line}" | jq -c) trace "[main] line=${line}" fi case "${cmd}" in helloworld) # GET http://192.168.111.152:8080/helloworld response_to_client "Hello, world!" 0 break ;; installation_info) # GET http://192.168.111.152:8080/info if [ -f "$DB_PATH/info.json" ]; then response=$(cat "$DB_PATH/info.json") else response='{ "error": "missing installation data" }' fi response_to_client "${response}" ${?} break ;; 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"} # BODY {"address":"2N8DcqzfkYi8CkYzvNNS5amoq3SbAcQNXKp","confirmedCallbackURL":"192.168.111.233:1111/callback1conf","eventMessage":"eyJib3VuY2VfYWRkcmVzcyI6IjJNdkEzeHIzOHIxNXRRZWhGblBKMVhBdXJDUFR2ZTZOamNGIiwibmJfY29uZiI6MH0K"} # BODY {"address":"2N8DcqzfkYi8CkYzvNNS5amoq3SbAcQNXKp","confirmedCallbackURL":"192.168.111.233:1111/callback1conf","eventMessage":"eyJib3VuY2VfYWRkcmVzcyI6IjJNdkEzeHIzOHIxNXRRZWhGblBKMVhBdXJDUFR2ZTZOamNGIiwibmJfY29uZiI6MH0K","label":"myLabel"} response=$(watchrequest "${line}") response_to_client "${response}" ${?} break ;; unwatch) # curl (GET) 192.168.111.152:8080/unwatch/2N8DcqzfkYi8CkYzvNNS5amoq3SbAcQNXKp # or # POST http://192.168.111.152:8080/unwatch # BODY {"address":"2N8DcqzfkYi8CkYzvNNS5amoq3SbAcQNXKp","unconfirmedCallbackURL":"192.168.111.233:1111/callback0conf","confirmedCallbackURL":"192.168.111.233:1111/callback1conf"} # or # BODY {"id":3124} # args: # - address: string, required # - unconfirmedCallbackURL: string, optional # - confirmedCallbackURL: string, optional # or # - id: the id returned by the watch local address="null" local unconfirmedCallbackURL="null" local confirmedCallbackURL="null" local watchid="null" # Let's make it work even for a GET request (equivalent to a POST with empty json object body) if [ "$http_method" = "POST" ]; then address=$(echo "${line}" | jq -r ".address") unconfirmedCallbackURL=$(echo "${line}" | jq ".unconfirmedCallbackURL") confirmedCallbackURL=$(echo "${line}" | jq ".confirmedCallbackURL") watchid=$(echo "${line}" | jq ".id") else address=$(echo "${line}" | cut -d ' ' -f2 | cut -d '/' -f3) fi response=$(unwatchrequest "${watchid}" "${address}" "${unconfirmedCallbackURL}" "${confirmedCallbackURL}") response_to_client "${response}" ${?} break ;; watchxpub) # POST http://192.168.111.152:8080/watchxpub # BODY {"label":"4421","pub32":"tpubD6NzVbkrYhZ4YR3QK2tyfMMvBghAvqtNaNK1LTyDWcRHLcMUm3ZN2cGm5BS3MhCRCeCkXQkTXXjiJgqxpqXK7PeUSp86DTTgkLpcjMtpKWk","path":"0/n","nstart":0,"unconfirmedCallbackURL":"192.168.111.233:1111/callback0conf","confirmedCallbackURL":"192.168.111.233:1111/callback1conf"} # curl -H "Content-Type: application/json" -d '{"label":"2219","pub32":"upub5GtUcgGed1aGH4HKQ3vMYrsmLXwmHhS1AeX33ZvDgZiyvkGhNTvGd2TA5Lr4v239Fzjj4ZY48t6wTtXUy2yRgapf37QHgt6KWEZ6bgsCLpb","path":"0/1/n","nstart":55,"unconfirmedCallbackURL":"192.168.111.233:1111/callback0conf","confirmedCallbackURL":"192.168.111.233:1111/callback1conf"}' proxy:8888/watchxpub response=$(watchpub32request "${line}") response_to_client "${response}" ${?} break ;; unwatchxpubbyxpub) # GET http://192.168.111.152:8080/unwatchxpubbyxpub/tpubD6NzVbkrYhZ4YR3QK2tyfMMvBghAvqtNaNK1LTyDWcRHLcMUm3ZN2cGm5BS3MhCRCeCkXQkTXXjiJgqxpqXK7PeUSp86DTTgkLpcjMtpKWk response=$(unwatchpub32request "${line}") response_to_client "${response}" ${?} break ;; unwatchxpubbylabel) # GET http://192.168.111.152:8080/unwatchxpubbylabel/4421 response=$(unwatchpub32labelrequest "${line}") response_to_client "${response}" ${?} break ;; getactivewatchesbyxpub) # GET http://192.168.111.152:8080/getactivewatchesbyxpub/tpubD6NzVbkrYhZ4YR3QK2tyfMMvBghAvqtNaNK1LTyDWcRHLcMUm3ZN2cGm5BS3MhCRCeCkXQkTXXjiJgqxpqXK7PeUSp86DTTgkLpcjMtpKWk response=$(getactivewatchesbyxpub "$(echo "${line}" | cut -d ' ' -f2 | cut -d '/' -f3)") response_to_client "${response}" ${?} break ;; getactivewatchesbylabel) # GET http://192.168.111.152:8080/getactivewatchesbylabel/4421 response=$(getactivewatchesbylabel "$(echo "${line}" | cut -d ' ' -f2 | cut -d '/' -f3)") response_to_client "${response}" ${?} break ;; getactivexpubwatches) # GET http://192.168.111.152:8080/getactivexpubwatches response=$(getactivexpubwatches) response_to_client "${response}" ${?} break ;; watchtxid) # POST http://192.168.111.152:8080/watchtxid # BODY {"txid":"b081ca7724386f549cf0c16f71db6affeb52ff7a0d9b606fb2e5c43faffd3387","confirmedCallbackURL":"192.168.111.233:1111/callback1conf","xconfCallbackURL":"192.168.111.233:1111/callbackXconf","nbxconf":6} # curl -H "Content-Type: application/json" -d '{"txid":"b081ca7724386f549cf0c16f71db6affeb52ff7a0d9b606fb2e5c43faffd3387","confirmedCallbackURL":"192.168.111.233:1111/callback1conf","xconfCallbackURL":"192.168.111.233:1111/callbackXconf","nbxconf":6}' proxy:8888/watchtxid response=$(watchtxidrequest "${line}") response_to_client "${response}" ${?} break ;; unwatchtxid) # POST http://192.168.111.152:8080/unwatchtxid # BODY {"txid":"b081ca7724386f549cf0c16f71db6affeb52ff7a0d9b606fb2e5c43faffd3387","unconfirmedCallbackURL":"192.168.111.233:1111/callback0conf","confirmedCallbackURL":"192.168.111.233:1111/callback1conf"} # or # BODY {"id":3124} # args: # - txid: string, required # - unconfirmedCallbackURL: string, optional # - confirmedCallbackURL: string, optional # or # - id: the id returned by watchtxid local txid=$(echo "${line}" | jq -r ".txid") local unconfirmedCallbackURL=$(echo "${line}" | jq ".unconfirmedCallbackURL") local confirmedCallbackURL=$(echo "${line}" | jq ".confirmedCallbackURL") local watchid=$(echo "${line}" | jq ".id") response=$(unwatchtxidrequest "${watchid}" "${txid}" "${unconfirmedCallbackURL}" "${confirmedCallbackURL}") response_to_client "${response}" ${?} break ;; getactivewatches) # curl (GET) 192.168.111.152:8080/getactivewatches response=$(getactivewatches) response_to_client "${response}" ${?} break ;; get_txns_by_watchlabel) # curl (GET) 192.168.111.152:8080/get_txns_by_watchlabel/