#!/bin/sh

#full test, requires 3 secret/public key pairs

PGPPATH=.
export PGPPATH

#8 digit keyids for the three keys

KEY1=4F56C649
KEY2=3C5C5C85
KEY3=EEF0580D

#passphrases for the secret versions of the above three keys

PASS1=cresyspass
PASS2=ignored
PASS3=ignored

#if there is no passphrase, you still need something above

if [ -s pubring.pgp -a -s secring.pgp ] ; then
    echo beginning test
else
    echo You must extract or create the local keyrings first
    exit 0
fi

##################################################
#Basic function test

echo generating protocoin
./xchequer -i 4F56C649 > cashfile.bld 2> cashfile.ubl

echo signing coin
./xchequer -p cresyspass < cashfile.bld > cashfile.sbl

echo unblinding coin
cat cashfile.sbl cashfile.ubl | ./xchequer  > cashfile.sgn

echo verifying coin
cat cashfile.sgn | ./xchequer  

echo spending test

if [ -e spent.db ]; then
    if (cat cashfile.sgn | ./xchequer -v)
        then echo unspent
        else echo ERROR-SPENT
    fi
else
    echo Database not created yet
fi
if (cat cashfile.sgn | ./xchequer -x)
    then echo unspent
    else echo ERROR-SPENT
fi
if (cat cashfile.sgn | ./xchequer -x)
    then echo ERROR-UNSPENT
    else echo spent
fi
if (cat cashfile.sgn | ./xchequer -v)
    then echo ERROR-UNSPENT
    else echo spent
fi

##################################################
#Batch Mode

echo generating protocoin
./xchequer -n 50 -i 4F56C649 > cashfile.bld 2> cashfile.ubl

echo signing coin
./xchequer -b -p cresyspass < cashfile.bld > cashfile.sbl

echo unblinding coin
cat cashfile.sbl cashfile.ubl > cashfile.tmp
./xchequer -b > cashfile.sgn < cashfile.tmp

echo verifying coin
cat cashfile.sgn | ./xchequer -b

echo spending test:

if (cat cashfile.sgn | ./xchequer -b -v) ; then 
    echo unspent ; else echo ERROR-SPENT ;fi

if (cat cashfile.sgn | ./xchequer -b -x); then 
    echo unspent ; else echo ERROR-SPENT ;fi

if (cat cashfile.sgn | ./xchequer -b -x); then 
    echo ERROR-UNSPENT ; else echo spent ;fi

if (cat cashfile.sgn | ./xchequer -b -v); then 
    echo ERROR-UNSPENT ; else echo spent ;fi

##################################################
# batch as filter
date

./xchequer -n 100 -i 4F56C649 2>&1 | tee cashfile.bld | \
./xchequer -b -p cresyspass | tee cashfile.sbl | \
./xchequer -b | tee cashfile.sgn | \
./xchequer -b

date

echo spending test:

if (cat cashfile.sgn | ./xchequer -b -v) ; then
    echo unspent ; else echo ERROR-SPENT ;fi

if (cat cashfile.sgn | ./xchequer -b -x); then
echo unspent ; else echo ERROR-SPENT ;fi

if (cat cashfile.sgn | ./xchequer -b -x); then
echo ERROR-UNSPENT ; else echo spent ;fi

if (cat cashfile.sgn | ./xchequer -b -v); then
echo ERROR-UNSPENT ; else echo spent ;fi

##################################################
#Title certs

echo generating Title
./xchequer -p cresyspass -i 4F56C649 -t "xcheq title" >cashfile.ttl

echo generating protocoin
./xchequer -T -i 4F56C649 > cashfile.bld 2> cashfile.ubl <cashfile.ttl

echo signing coin
cat cashfile.ttl cashfile.bld | ./xchequer -p cresyspass > cashfile.sbl

echo unblinding coin
cat cashfile.ttl cashfile.sbl cashfile.ubl | ./xchequer > cashfile.sgn

echo verifying coin
cat cashfile.ttl cashfile.sgn | ./xchequer

echo spending test

if [ -e spent.db ]; then
    if (cat cashfile.ttl cashfile.sgn | ./xchequer -v)
        then echo unspent
        else echo ERROR-SPENT
    fi
else
    echo Database not created yet
fi

./xchequer -T -i 4F56C649 > cashfile.bld 2> cashfile.ubl <cashfile.ttl

if (cat cashfile.ttl cashfile.sgn cashfile.bld |\
 ./xchequer -X -p cresyspass > cashfile.sbl)
    then echo exchanged
    else echo ERROR-SPENT ; exit -1
fi

if (cat cashfile.ttl cashfile.sgn | ./xchequer -x)
    then echo ERROR-UNSPENT
    else echo spent
fi
if (cat cashfile.ttl cashfile.sgn | ./xchequer -v)
    then echo ERROR-UNSPENT
    else echo spent
fi

echo unblinding coin
cat cashfile.ttl cashfile.sbl cashfile.ubl | ./xchequer > cashfile.sgn

echo verifying coin
cat cashfile.ttl cashfile.sgn | ./xchequer -T

##################################################
# encryption, signatures, everything

# encrypt only

./xchequer -i $KEY1 -r $KEY2 > cashfile.bld 2> cashfile.ubl
./xchequer -p $PASS1 < cashfile.bld > cashfile.sbl
cat cashfile.sbl cashfile.bld | ./xchequer -r $KEY2 -p $PASS2 > cashfile.sgn
cat cashfile.sgn | ./xchequer >cashfile.spt

# sign only

./xchequer -i $KEY1 -s $KEY3 -p $PASS3 > cashfile.bld 2>cashfile.ubl
./xchequer -p $PASS1 < cashfile.bld > cashfile.sbl
cat cashfile.sbl cashfile.bld | ./xchequer -s $KEY3 > cashfile.sgn
cat cashfile.sgn | ./xchequer >cashfile.spt

# both

./xchequer -i $KEY1 -r $KEY2 -s $KEY3 -p $PASS3 > cashfile.bld 2> cashfile.ubl
./xchequer -p $PASS1 < cashfile.bld > cashfile.sbl
cat cashfile.sbl cashfile.bld | \
 ./xchequer -r $KEY2 -s $KEY3 -p $PASS2 > cashfile.sgn
cat cashfile.sgn | ./xchequer >cashfile.spt

# with title

./xchequer -p cresyspass -i 4F56C649 -t "Torture Test" >cashfile.ttl

cat cashfile.ttl | ./xchequer -T -i $KEY1 -r $KEY2 -s $KEY3 \
 -p $PASS3 > cashfile.bld 2> cashfile.ubl
cat cashfile.ttl cashfile.bld | ./xchequer -p $PASS1 > cashfile.sbl
cat cashfile.ttl cashfile.sbl cashfile.bld | \
 ./xchequer -r $KEY2 -s $KEY3 -p $PASS2 > cashfile.sgn

# continue
while (cat cashfile.ttl cashfile.sgn | ./xchequer >cashfile.spt );do
cat cashfile.ttl | ./xchequer -T -i $KEY1 -r $KEY2 -s $KEY3 \
 -p $PASS3 > cashfile.bld 2> cashfile.ubl
cat cashfile.ttl cashfile.bld | ./xchequer -p $PASS1 > cashfile.sbl
cat cashfile.ttl cashfile.sbl cashfile.bld | \
 ./xchequer -r $KEY2 -s $KEY3 -p $PASS2 > cashfile.sgn
done
