aboutsummaryrefslogtreecommitdiff
path: root/bin/pack_z_block
blob: 71aa4b789f4f38e0f2c1d4742b022317345b14fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/bin/bash
# This file describe the structure of the ArchingKaos messages in their basis.
#
# As previously thought, we exchange one IPFS hash through whatever means we can.
# 
# GPG is mentioned as a signing algorithm for encryption, decryption and signing.
# Let's say we have a file named as `example`
#
# We can extend this with calling the encoder and further send the transaction
#

#FINGERPRINT="CHANGE THIS TO YOUR DEFAULT FINGERPRINT"
# We acquire the GPG fingerprint by email address
# The following example finds kaos@kaos.kaos' GPG fingerprint like this
# FINGERPRINT="$(gpg2 --list-keys | grep kaos@kaos.kaos -1 | head -n1 | awk '{print $1}')"

PROGRAM="$(basename $0)"

# Logging patch
logit(){
	logthis "<$PROGRAM>" "$1" "$2"
}

# Below, the usage information
usage(){
	echo ""
	echo "Usage:"
	echo "$PROGRAM <action> <data_file>"
	echo ""
	echo "	Creates and publishes a ZBLOCK based on ACTION and DATA file."
	echo ""
	echo "		action		An action for the render to grasp"
	echo "		data_file	A data file that according to the action is valid"
	echo ""
	echo "#TODO:"
	echo "implement flags to change the inputs so we can manufacture"
	echo "zblocks directly from IPFS hashes refering to blocks."
}


main(){
    # We check firstly if the encapsulated value of the "ipfs" key has already
    # appeared in the zchain.
    TO_CHECK="$(cat $MESSAGE | jq | grep ipfs | awk '{print $2}' | sed -e 's/"//g;s/,//g')"
    enter | jq | grep ipfs | awk '{print $2}' | sed -e 's/"//g;s/,//g' | sort | uniq > tempisalreadythere
    while IFS="" read -r p || [ -n "$p" ]
    do
        if [ "$p" == "$TO_CHECK" ]
        then
            logit "[ERROR]" "Value $TO_CHECK already mentioned on the zchain"
            exit 1
        fi
    done < tempisalreadythere
    rm tempisalreadythere
 
	logit "[INFO]" "We are doing" $ACTION "with content" $MESSAGE
	# We add it to IPFS
	MESSAGE_HASH=$(ipfs add -q $MESSAGE)

	# We create a detached and armor signature of it
	MESSAGE_SIGN_FILE=$MESSAGE".asc"
	gpg2 --detach-sign --sign-with $FINGERPRINT --armor --output $MESSAGE_SIGN_FILE $MESSAGE

	# We add the signature to IPFS
	MESSAGE_SIGNATURE=$(ipfs add -q $MESSAGE_SIGN_FILE)

	# We will be using our public key also to put it in the block later
	KEY="gpg.pub"
	gpg2 --armour --output $KEY --export $FINGERPRINT
	GPG_PUB_KEY=$(ipfs add -q $KEY)

	# Acquire last block of information, to chain this one with previous posted
	PREVIOUS=$(ipfs files stat /zlatest | head -n 1)

	# We create a block of json like this:
	cat > block <<EOF
{
    "timestamp":"$(date -u +%s)",
    "action":"$ACTION",
    "data":"$MESSAGE_HASH",
    "detach":"$MESSAGE_SIGNATURE",
    "gpg":"$GPG_PUB_KEY",
    "previous":"$PREVIOUS"
}
EOF
	BLOCK="block"
	BLOCK_SIG=$BLOCK".asc"
	# We have a block now, so we sign it
	gpg2 --detach-sign --sign-with $FINGERPRINT --armor --output $BLOCK_SIG $BLOCK

	# We now add the signature to IPFS
	BLOCK_SIGNATURE=$(ipfs add -q $BLOCK_SIG)

	# We also add the block!
	BLOCK=$(ipfs add -q $BLOCK)

	# So we now do the think almost again
	cat > zblock << EOF
{
    "block":"$BLOCK",
    "block_signature":"$BLOCK_SIGNATURE"
}
EOF
	ZBL="zblock"
	# and we add it on IPFS
	ZBLOCK=$(ipfs add -q $ZBL)
	echo $ZBLOCK
}

if [ ! -z $2 ];
then
	PWD="$(pwd)"
	MESSAGE="$PWD/$2"
	ACTION="$1"
	if [ -f "$MESSAGE" ]; then
		main
	else
		logit "[ERROR]" "File does not exist. Aborting..."
		exit 1
	fi

	# cat $PWD/zblock | json_pp
	# Optional or extending with
	# python send_as_ak_tx $ZBLOCK
	# or for "offline" use
	echo $ZBLOCK > $ZLATEST
	ipfs name publish --key=zchain $ZBLOCK > /dev/null 2>&1
    if [ "$?" -ne 0 ]
    then
        logit "[ERROR]" "Failed publishing ZBLOCK: $ZBLOCK"
        exit 1
    fi
	ipfs files ls /zarchive > /dev/null 2>&1
    if [ "$?" -ne 0 ]
    then
        logit "[WARNING]" "/zarchive does not exist"
        ipfs files mkdir /zarchive > /dev/null 2>&1
        if [ "$?" -ne 0 ]
        then
            logit "[ERROR]" "Could not create /zarchive directory. Aborting."
            exit 1
        fi
    fi
	ipfs files cp /zlatest /zarchive/$(date -u +%s)-$(ipfs files stat /zlatest | head -n 1) > /dev/null 2>&1
    if [ "$?" -ne 0 ]
    then
        logit "[ERROR]" "Could not back up previous /zlatest"
        exit 1
    fi
	ipfs files rm /zlatest > /dev/null 2>&1
    if [ "$?" -ne 0 ]
    then
        logit "[ERROR]" "Could not remove previous /zlatest"
        exit 1
    fi
	ipfs files cp /ipfs/$ZBLOCK /zlatest > /dev/null 2>&1
    if [ "$?" -ne 0 ]
    then
        logit "[ERROR]" "Could not copy $ZBLOCK to /zlatest"
        exit 1
    fi
else
	usage
	exit 0
fi