aboutsummaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
Diffstat (limited to 'bin')
-rwxr-xr-xbin/enter85
-rwxr-xr-xbin/get-latest2
-rwxr-xr-xbin/ipfs-starter2
-rwxr-xr-xbin/json2bash11
-rwxr-xr-xbin/news94
-rwxr-xr-xbin/pack_z_block113
-rwxr-xr-xbin/zchain-reset27
7 files changed, 334 insertions, 0 deletions
diff --git a/bin/enter b/bin/enter
new file mode 100755
index 0000000..6865797
--- /dev/null
+++ b/bin/enter
@@ -0,0 +1,85 @@
+#!/bin/bash
+# By default we enter from the latest block
+# We can alter this by changing this value
+entrance="$(cat $ZLATEST)"
+
+# Start of tests
+#entrance="QmW5WVXCJfhb4peHG6cbEdamC24vZzMX2Vz386vpENh38U"
+#entrance="QmNjQq7GkuXGF8kFT1z2Mv3i4JhY7sBXVUmHDiR1zkQjoE"
+#entrance="QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH"
+# End of tests
+
+# We assign the IPFS CIDv0 of an empty file as this is used
+# as our GENESIS block, hence the "seed" that the tree grows
+# from.
+seed="$(cat $ZGENESIS)"
+
+# We assume that we found the entrance inside a block, hence
+# ZBLOCK is labeled as previous
+zblock="$entrance"
+
+# The loop
+# We break the loop from inside the loop
+while true
+do
+ # Check if $zblock exists as variable
+ if [ ! -v $zblock ]
+ then
+ # Check if it is not our seed cause if it is we skip this part
+ if [ "$zblock" != "$seed" ]
+ then
+ # Announce to stdout which ZBLOCK is being read at the moment
+ echo "Examining $zblock"
+
+ # We create files named after each ZBLOCK IPFS CID for later
+ # reference. Files are empty.
+ touch $ZBLOCKDIR/$zblock
+
+ # We concatenate the zblock's contents, pipe them through filter
+ # json2bash and output them to tmp-file
+ ipfs cat $zblock | json2bash > tmp-zblock
+
+ # Supposingly you are on a safe environment and you only have
+ # access to your chain, I would consider mild secure to source
+ # the files into your bash.
+ # File an issue/pull request if you think it can be done better!!
+ source tmp-zblock
+
+ # Same as above applies to BLOCK and DATA subparts of each ZBLOCK
+ # BLOCKS
+ ipfs cat $block | json2bash > tmp-block
+ source tmp-block
+ touch $BLOCKDIR/$block
+
+ # DATA (but we don't source it's stuff)
+ # Only print to stdout
+ ipfs cat $data
+ touch $DATADIR/$data
+
+ # Now, since we sourced the BLOCK to our terminal, we can search
+ # for $previous variable. In case we don't find one, we exit with
+ # code 3
+ if [ -v $previous ]
+ then
+ echo "Block $block has no previous zblock"
+ exit 3
+
+ # Otherwise, we inform of the sequence
+ else
+ echo "$zblock after $previous"
+ zblock=$previous
+ fi
+
+ # Now check if it is equal to the seed
+ # which apparently means we reached the seed.
+ elif [ "$zblock" == "$seed" ]
+ then
+ echo "$zblock is GENESIS block"
+ exit 0
+ fi
+ # And finally, if nothing is there exit with error
+ else
+ echo "Check not passed... No previous IPFS CID"
+ exit 1
+ fi
+done
diff --git a/bin/get-latest b/bin/get-latest
new file mode 100755
index 0000000..9138c1a
--- /dev/null
+++ b/bin/get-latest
@@ -0,0 +1,2 @@
+#!/bin/bash
+ipfs files stat /zlatest | head -n 1
diff --git a/bin/ipfs-starter b/bin/ipfs-starter
new file mode 100755
index 0000000..5cab1c3
--- /dev/null
+++ b/bin/ipfs-starter
@@ -0,0 +1,2 @@
+#!/bin/bash
+screen -dmS ipfs-daemon ipfs daemon
diff --git a/bin/json2bash b/bin/json2bash
new file mode 100755
index 0000000..347ca93
--- /dev/null
+++ b/bin/json2bash
@@ -0,0 +1,11 @@
+#!/bin/bash
+#
+# json2bash
+#
+# Kaotisk Hund <kaotisk/kaotiskhund>
+#
+# Strips " , { } from input and replaces ":" with =
+#
+# Usage:
+# ipfs cat <hash> | json2bash
+sed -e 's/{//g' | sed -e 's/}//g' | sed -e 's/":"/=/g' | sed -e 's/"//g' | sed -e 's/,//g' | sed -e 's/ //g'
diff --git a/bin/news b/bin/news
new file mode 100755
index 0000000..910f1cb
--- /dev/null
+++ b/bin/news
@@ -0,0 +1,94 @@
+#!/bin/bash
+ZNEWSDIR="$WORKDIR/news"
+echo $ZNEWSDIR
+if [ ! -d $ZNEWSDIR ]; then
+ mkdir $ZNEWSDIR
+ cd $ZNEWSDIR
+ git init
+ echo "News repository" > README
+ echo "Qmetc" >> README
+ git add README
+ git commit -m "Initiated news repository"
+ echo znewsdir created along with git repo
+else
+ echo znewsdir found
+fi
+create(){
+ export NEWS_FILE="$(date +%Y%m%d_%H%M%S)"
+ vi $NEWS_FILE
+ echo "Renaming..."
+ TITLE="$(head -n 1 $NEWS_FILE)"
+ TO_FILE=$NEWS_FILE-$(echo $TITLE | tr '[:upper:]' '[:lower:]' | sed -e 's/ /\_/g' )
+ IPFS_FILE=$(ipfs add -q $NEWS_FILE)
+ mv $NEWS_FILE $ZNEWSDIR/$TO_FILE
+ cd $ZNEWSDIR
+ sed -e 's,Qm.*,'"$IPFS_FILE"',g' README
+ add $TO_FILE
+ echo "Adding to git repo..."
+ git add $TO_FILE README
+ git commit -m "Added $TO_FILE with $(head -n 1 $ZNEWSDIR/$TO_FILE)"
+}
+index(){
+ FILES="$(ls -1 $ZNEWSDIR)"
+ i=0
+ for FILE in $FILES
+ do
+ DATE=$(echo $FILE | cut -d - -f 1 | awk '{print $1}')
+ TITLE=$(head -n 1 $ZNEWSDIR/$FILE)
+ echo $i \| $DATE \| $TITLE
+ let i+=1
+ done
+}
+title(){
+ echo ak-news-cli
+ echo "--------------"
+}
+import(){
+ echo "#TODO"
+ echo $1
+}
+add(){
+ if [ -f $1 ]; then
+ FILE="$1"
+ echo "Adding news from " $FILE
+ DATETIME=$(echo $FILE | cut -d - -f 1 | awk '{print $1}')
+ TITLE=$(head -n 1 $FILE)
+ FILE_IPFS_HASH=$(ipfs add -q $FILE)
+ FILE_SIGN_FILE=$FILE".asc"
+ gpg2 --detach-sign --sign-with $FINGERPRINT --armor --output $FILE_SIGN_FILE $FILE
+ FILE_SIGNATURE=$(ipfs add -q $FILE_SIGN_FILE)
+ cat > data <<EOF
+{
+ "datetime":"$DATETIME",
+ "title":"$TITLE",
+ "filename":"$FILE",
+ "ipfs":"$FILE_IPFS_HASH",
+ "detach":"$FILE_SIGNATURE"
+}
+EOF
+ else echo "File $FILE doesn't exist";
+ fi
+ pack_z_block "news/add" data
+}
+usage(){
+ echo "#TODO"
+ echo "All you need to know is that there are two options available:"
+ echo "help Prints this help message"
+ echo "index Prints an indexed table of your news files"
+ echo "import <file> #TODO"
+ echo "add <file> Creates a data file from the news file you point to"
+ echo "create Vim is going to pop up, you will write and save your"
+ echo " newsletter and it's going to be saved"
+}
+title
+if [ ! -z $1 ]; then
+ case $1 in
+ help) usage;exit;;
+ index) index;break;;
+ import) import $2; break;;
+ add) add $2; break;;
+ create) create; exit;;
+ * ) usage;;
+ esac
+else usage
+fi
diff --git a/bin/pack_z_block b/bin/pack_z_block
new file mode 100755
index 0000000..da5e22b
--- /dev/null
+++ b/bin/pack_z_block
@@ -0,0 +1,113 @@
+#!/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}')"
+
+# Below, the usage information
+usage(){
+ echo "$0 - action data_file"
+ 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"
+}
+
+main(){
+
+ echo "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
+{
+ "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
+}
+
+title(){
+ echo "AK zblock creator"
+ echo "================="
+}
+
+title
+if [ ! -z $2 ];
+then
+ PWD="$(pwd)"
+ MESSAGE="$PWD/$2"
+ ACTION="$1"
+ if [ -f "$MESSAGE" ]; then
+ main
+ else
+ echo "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
+ ipfs files mkdir /zarchive
+ ipfs files cp /zlatest /zarchive/$(date -u +%s)-$(ipfs files stat /zlatest | head -n 1)
+ ipfs files rm /zlatest
+ ipfs files cp /ipfs/$ZBLOCK /zlatest
+else
+ usage
+ exit 0
+fi
+
diff --git a/bin/zchain-reset b/bin/zchain-reset
new file mode 100755
index 0000000..2a289c0
--- /dev/null
+++ b/bin/zchain-reset
@@ -0,0 +1,27 @@
+#!/bin/bash
+title(){
+ echo "Zchain reset"
+ echo "------------"
+}
+usage(){
+ echo "Usage: $0 reset"
+ exit 0
+}
+reset (){
+ cp $ZGENESIS $ZLATEST
+ ipfs name publish --key=zchain /ipfs/$ZLATEST
+ ipfs files mkdir /zarchive
+ ipfs files cp /zlatest /zarchive/$(date -u +%s)-$(ipfs files stat /zlatest | head -n 1)
+ ipfs files rm /zlatest
+ ipfs files cp /ipfs/$(cat $ZLATEST) /zlatest
+ echo "Probably reset"
+ exit 0
+}
+if [ ! -z $1 ]; then
+ case $1 in
+ reset) reset; exit;;
+ * ) usage;;
+ esac
+else usage
+fi
+