Commit 6022cc6
2014-01-23 01:23:24
Changed files (4)
.gitcoin.py.swp
Binary file
.my-miner.swp
Binary file
gitcoin.py
@@ -0,0 +1,31 @@
+#!/usr/bin/python
+from __future__ import print_function
+import sys
+import time
+#from random import random
+from hashlib import sha1
+
+def githash(data):
+ s = sha1()
+ s.update("commit %u\0" % len(data))
+ s.update(data)
+ return s.hexdigest()
+
+if __name__ == "__main__":
+ counter = 0
+ body = sys.stdin.read()
+ difficulty = open("./difficulty.txt","r").read().split()[0]
+ while True:
+ start = time.time()
+ #print(counter%1000, file=sys.stderr)
+ #nonce = sha1(str(random())).hexdigest()
+ counter += 1
+ commit = body+str(counter)
+ #if githash(body+str(counter)) < difficulty:
+ print(commit)
+ print("bodyhash "+githash(body), file=sys.stderr)
+ print("githash "+githash(commit), file=sys.stderr)
+ break
+ end = time.time()
+ print("%g Kh/s ... %d " % (1/(end-start)/1024, counter), end='', file=sys.stderr)
+
my-miner
@@ -0,0 +1,97 @@
+#!/bin/bash
+
+set -eu
+
+if [ "$#" != 2 ]; then
+ echo >&2 "Usage: $0 <clone_url> <public_username>
+
+A VERY SLOW mining implementation. This should give you an idea of
+where to start, but it probably won't successfully mine you any
+Gitcoins.
+
+Arguments:
+
+<clone_url> is the string you'd pass to git clone (i.e.
+ something of the form username@hostname:path)
+
+<public_username> is the public username provided to you in
+ the CTF web interface."
+ exit 1
+fi
+
+export clone_spec=$1
+export public_username=$2
+
+prepare_index() {
+ perl -i -pe 's/($ENV{public_username}: )(\d+)/$1 . ($2+1)/e' LEDGER.txt
+ grep -q "$public_username" LEDGER.txt || echo "$public_username: 1" >> LEDGER.txt
+
+ git add LEDGER.txt
+}
+
+solve() {
+ # Brute force until you find something that's lexicographically
+ # small than $difficulty.
+ difficulty=$(cat difficulty.txt)
+
+ # Create a Git tree object reflecting our current working
+ # directory
+ tree=$(git write-tree)
+ parent=$(git rev-parse HEAD)
+ timestamp=$(date +%s)
+ body="tree $tree
+parent $parent
+author CTF user <me@example.com> $timestamp +0000
+committer CTF user <me@example.com> $timestamp +0000
+
+Give me a Gitcoin
+
+"
+
+ # See http://git-scm.com/book/en/Git-Internals-Git-Objects for
+ # details on Git objects.
+ #time sha1=$(echo "${body}${counter}" | git hash-object -t commit --stdin)
+ #sha1=$(git hash-object -t commit --stdin <<< "${body}${counter}")
+ coin=$(python ../miner/gitcoin.py <<< "$body")
+ #git hash-object -t commit --stdin -w <<< "$coin" > /dev/null
+ sha1=$(git hash-object -t commit --stdin <<< "$coin")
+ echo "SHA1 $sha1"
+ (printf "commit %s\0" $(wc -c <<< "$body"); cat <<<"$body") | shasum
+ (printf "commit %s\0" $(wc -c <<< "$coin"); cat <<<"$coin") | shasum
+ #git reset --hard "$sha1"
+ break
+}
+
+reset() {
+ git fetch origin master >/dev/null 2>/dev/null
+ git reset --hard origin/master >/dev/null
+ git stash >/dev/null
+ git pull
+ echo "Current difficulty:"
+ cat difficulty.txt
+}
+
+# Set up repo
+local_path=./${clone_spec##*:}
+
+if [ -d "$local_path" ]; then
+ echo "Using existing repository at $local_path"
+ cd "$local_path"
+else
+ echo "Cloning repository to $local_path"
+ git clone "$clone_spec" "$local_path"
+ cd "$local_path"
+fi
+
+while true; do
+ reset
+ prepare_index
+ solve
+ if git push origin master; then
+ echo "Success :)"
+ break
+ else
+ echo "Starting over :("
+ reset
+ fi
+done