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,27 @@
FROM node:11.1-alpine
RUN apk add --update --no-cache \
git \
jq \
su-exec \
&& yarn global add javascript-opentimestamps
WORKDIR /script
COPY script/otsclient.sh /script/otsclient.sh
COPY script/requesthandler.sh /script/requesthandler.sh
COPY script/responsetoclient.sh /script/responsetoclient.sh
COPY script/startotsclient.sh /script/startotsclient.sh
COPY script/trace.sh /script/trace.sh
RUN chmod +x /script/startotsclient.sh /script/requesthandler.sh
ENTRYPOINT ["su-exec"]
# docker build -t otsclient-js .
# docker run -it --rm --name otsclient -v /home/debian/otsfiles:/otsfiles otsclient-js `id -u cyphernode`:`id -g cyphernode` ash
# ots-cli.js stamp -d 1ddfb769eb0b8876bc570e25580e6a53afcf973362ee1ee4b54a807da2e5eed7
# ots-cli.js verify -d 1ddfb769eb0b8876bc570e25580e6a53afcf973362ee1ee4b54a807da2e5eed7 1ddfb769eb0b8876bc570e25580e6a53afcf973362ee1ee4b54a807da2e5eed7.ots
# ots-cli.js info 1ddfb769eb0b8876bc570e25580e6a53afcf973362ee1ee4b54a807da2e5eed7.ots
# ots-cli.js upgrade 1ddfb769eb0b8876bc570e25580e6a53afcf973362ee1ee4b54a807da2e5eed7.ots

View File

@@ -0,0 +1,24 @@
# Build image
```shell
docker build -t otsclientimg .
```
# OTS files directory...
```shell
mkdir -p ~/otsfiles
sudo chown -R cyphernode:debian ~/otsfiles ; sudo chmod g+ws ~/otsfiles
sudo find ~/otsfiles -type d -exec chmod 2775 {} \; ; sudo find ~/otsfiles -type f -exec chmod g+rw {} \;
```
# Usefull examples
See https://github.com/opentimestamps/javascript-opentimestamps
List SegWit addresses for path 0/24-30 for a pub32:
```shell
curl -H "Content-Type: application/json" -d '{"pub32":"tpubD6NzVbkrYhZ4YR3QK2tyfMMvBghAvqtNaNK1LTyDWcRHLcMUm3ZN2cGm5BS3MhCRCeCkXQkTXXjiJgqxpqXK7PeUSp86DTTgkLpcjMtpKWk","path":"0/25-30"}' http://localhost:7777/derive
curl -H "Content-Type: application/json" -d '{"pub32":"vpub5SLqN2bLY4WeZF3kL4VqiWF1itbf3A6oRrq9aPf16AZMVWYCuN9TxpAZwCzVgW94TNzZPNc9XAHD4As6pdnExBtCDGYRmNJrcJ4eV9hNqcv","path":"0/25-30"}' http://localhost:7777/derive
```

View File

@@ -0,0 +1,2 @@
TRACING=1
OTSCLIENT_LISTENING_PORT=6666

View File

@@ -0,0 +1,83 @@
#!/bin/sh
. ./trace.sh
stamp()
{
trace "Entering stamp()..."
local hash=${1}
trace "[stamp] hash=${hash}"
local result
local returncode
local data
trace "[stamp] ots-cli.js stamp -d ${hash}"
result=$(cd /otsfiles && ots-cli.js stamp -d ${hash} 2>&1)
returncode=$?
trace_rc ${returncode}
trace "[stamp] result=${result}"
# The timestamp proof '1ddfb769eb0b8876bc570e25580e6a53afcf973362ee1ee4b54a807da2e5eed7.ots' has been created!
data="{\"method\":\"stamp\",\"hash\":\"${hash}\",\"result\":\""
trace "[stamp] grepping..."
echo "${result}" | grep "has been created!" > /dev/null
returncode=$?
trace_rc ${returncode}
if [ "${returncode}" -eq "0" ]; then
# String found
data="${data}success\"}"
else
# String nor found
data="${data}error\",\"error\":\"${result}\"}"
fi
trace "[stamp] data=${data}"
echo "${data}"
return ${returncode}
}
upgrade()
{
trace "Entering upgrade()..."
local hash=${1}
trace "[upgrade] hash=${hash}"
local result
local returncode
trace "[upgrade] ots-cli.js upgrade ${hash}.ots"
result=$(cd /otsfiles && ots-cli.js upgrade ${hash}.ots 2>&1)
returncode=$?
trace_rc ${returncode}
trace "[upgrade] result=${result}"
# Success! Timestamp complete
# Failed! Timestamp not complete
data="{\"method\":\"upgrade\",\"hash\":\"${hash}\",\"result\":\""
trace "[upgrade] grepping..."
echo "${result}" | grep "Success!" > /dev/null
returncode=$?
trace_rc ${returncode}
if [ "${returncode}" -eq "0" ]; then
data="${data}success\"}"
else
data="${data}error\",\"error\":\"${result}\"}"
fi
trace "[upgrade] data=${data}"
echo "${data}"
return ${returncode}
}

View File

@@ -0,0 +1,88 @@
#!/bin/sh
#
#
#
#
. ./otsclient.sh
. ./responsetoclient.sh
. ./trace.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 -n ${content_length} line
trace "[main] line=${line}"
fi
case "${cmd}" in
stamp)
# GET http://192.168.111.152:8080/stamp/1ddfb769eb0b8876bc570e25580e6a53afcf973362ee1ee4b54a807da2e5eed7
response=$(stamp $(echo "${line}" | cut -d ' ' -f2 | cut -d '/' -f3))
response_to_client "${response}" ${?}
break
;;
upgrade)
# GET http://192.168.111.152:8080/upgrade/1ddfb769eb0b8876bc570e25580e6a53afcf973362ee1ee4b54a807da2e5eed7
response=$(upgrade $(echo "${line}" | cut -d ' ' -f2 | cut -d '/' -f3))
response_to_client "${response}" ${?}
break
;;
esac
break
fi
done
trace "[main] exiting"
return 0
}
export TRACING
main
exit $?

View File

@@ -0,0 +1,21 @@
#!/bin/sh
. ./trace.sh
response_to_client()
{
trace "Entering response_to_client()..."
local response=${1}
local returncode=${2}
([ -z "${returncode}" ] || [ "${returncode}" -eq "0" ]) && echo -ne "HTTP/1.1 200 OK\r\n"
[ -n "${returncode}" ] && [ "${returncode}" -ne "0" ] && echo -ne "HTTP/1.1 400 Bad Request\r\n"
echo -e "Content-Type: application/json\r\nContent-Length: ${#response}\r\n\r\n${response}"
# 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

@@ -0,0 +1,6 @@
#!/bin/sh
export TRACING
export OTSCLIENT_LISTENING_PORT
nc -vlkp${OTSCLIENT_LISTENING_PORT} -e ./requesthandler.sh

View File

@@ -0,0 +1,15 @@
#!/bin/sh
trace()
{
if [ -n "${TRACING}" ]; then
echo "$(date -Is) ${1}" 1>&2
fi
}
trace_rc()
{
if [ -n "${TRACING}" ]; then
echo "$(date -Is) Last return code: ${1}" 1>&2
fi
}