#!/usr/pkg/bin/bash holder=grondilu content() { echo -e "Content-type: text/${1:-html}"; echo ; } title() { echo "${holder^}'s stock exchange" ; } image() { echo "" ; } space() { echo ""{,,,} ; } back() { echo "

back to main page

" ; } error() { 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.

" echo "

This the transaction order :

And this is the signature :

Press confirmation button to confirm this transaction (this is your last chance to retract !):
" back fi elif [[ -z "$QUERY_STRING" ]] then content html title image bitcoin-factory-small.png height=175 echo "

${holder^}'s exchange

" space echo " " elif eval "${QUERY_STRING//&/ }" [[ -n "$show_source" ]] then content plain cat "$0" elif [[ -n "$asset" ]] then case "${asset,,}" in list) content html echo "" back ;; drdgold|ebay|testshare) content plain tar xOf "$archive" "$asset.asc" ;; *) content html echo "

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 "
You are about to make the following transfer:

Please make a signed sha256 digest of this text and put it here, along with the public keysee example:

(you will be asked for confirmation in the next screen)
$(back) " back fi elif [[ -n "$id" ]] then content plain tar xOf "$archive" "tmp/$id" tar xOf "$archive" "tmp/$id.sha256" | xxd -p tar xOf "$archive" "tmp/$(grep "^$id," "$csv" |cut -d, -f2).pem" elif [[ "$tx_log" = html ]] then content html echo " " ( IFS=, while read id from to what quantity do echo "" done < $csv ) echo "
ID FROM TO WHAT QUANTITY
$id $from $to $what $quantity
" back elif [[ "$tx_log" = csv ]] then content "csv\nContent-disposition: attachment; filename=$csv" cat "$csv" elif [[ "$add_asset" = true ]] then content html error "This feature still has to be implemented. Sorry." echo "
Enter a bitcoin address:
Choose a number of shares:
Enter the Asset description :

" back else content plain echo "unknown query string : $QUERY_STRING" echo fi