#!/usr/pkg/bin/bash holder=grondilu content() { echo -e "Content-type: text/${1:-html}"; echo ; } title() { echo "
ERROR: $@
" ; back ; } urldecode() { echo -e "$(gsed 'y/+/ /; s/%/\\x/g')" | dos2unix ; } maintenance() { content html echo "This site is being maintained. Please come back later.
" exit } . /arpa/gm/g/grondilu/bin/base58.sh csv="${0//.cgi/.csv}" archive="${0//.cgi/.tar}" bc_key="https://github.com/grondilu/bc_key" bc_sign="https://github.com/grondilu/bitcoin-bash-tools" #content html #echo "This page has been closed due to technical difficulties on this webserver. It might come back later, but not any time soon :(
" #echo "You may have a look at the source code here though. It should work on an other webserver.
" #exit if [[ "$REQUEST_METHOD" = "POST" ]] && [[ "$CONTENT_LENGTH" -gt 0 ]] then read -n $CONTENT_LENGTH POST_DATA <&0 content html declare -A post_data while IFS='=' read -r -d '&' key value do [[ $key ]] && post_data[$key]="$(urldecode <<< $value)" done <<< "$POST_DATA&" transaction="${post_data[tx]}" pubkey="$(gsed -n '/BEGIN /,/END /p' <<<"${post_data[signature]}")" signature="$(gsed '/BEGIN /,/END /d' <<<"${post_data[signature]}")" if ! openssl dgst -sha256 \ -verify <(echo "$pubkey") -signature <(echo "$signature") <(echo "$transaction") 2>&1 then error "Wrong signature" elif from="$(pubkey | publicKeyToAddress)" echo "Good signature from $from.
" echo "" parse() { gsed -r "s/^$1:\s+//p" <<<"$transaction" ; } id="$(parse id)" grep -q "^$id," "$csv" then error "This transaction has already been done: $id" elif to="$(parse to)" ! checkBitcoinAddress "$to" then error "Destination address is not a valid bitcoin address" elif quantity="$(parse quantity)" [[ ! "$quantity" =~ ^[1-9][0-9]*$ ]] then error "Submitted quantity is not a valid integer amount." elif company="$(parse company)" owned="$(awk -F, "\$4 == \"$company\" { if(\$3==\"$from\") s+=\$5 ; if(\$2==\"$from\") s-=\$5 } END { print s }" "$csv" )" [[ "$quantity" > "$owned" ]] then error "Address '$from' is not associated to enough shares of company '$company'." elif [[ "${post_data[confirm]}" = "yes" ]] then echo $id,$from,$to,$company,$quantity >> $csv transaction > tmp/"$id" signature > tmp/"$id".sha256 pubkey > tmp/"$from".pem tar uf "$archive" tmp/{"$id"{,.sha256},"$from".pem} rm tmp/{"$id"{,.sha256},"$from".pem} echo "Your transaction has been executed. You can find it here.
" back else echo "This address is associated to $owned share(s) of the company '$company'.
"
echo "After execution, there will be $((owned - quantity)) share(s) left.
Unknow asset
" back ;; esac elif [[ -n "$to" ]] then content html if ! checkBitcoinAddress "$to" then echo "$to is not a bitcoin address (checkBitcoinAddress returned $?)
" back else echo -n " $(back)ID | FROM | TO | WHAT | QUANTITY |
$id | $from | $to | $what | $quantity |