Skip to content

Instantly share code, notes, and snippets.

@lsparrish
Last active December 19, 2015 18:19
Show Gist options
  • Save lsparrish/5997760 to your computer and use it in GitHub Desktop.
Save lsparrish/5997760 to your computer and use it in GitHub Desktop.
Onecredit -- The hacker's currency!
# Onecredit -- The hacker's currency!
'''
1c is an experiment in digital economy, loosely inspired by
bitcoin. The code is simpler, and the terminology is geekier
(credits, tokens, units etc). We also have some new features
planned.
* One "Coin" which divides as many times as you want.
* Units represent savings-style investment.
* They can be traded via sendactions.
* Credit tokens, which are generated in response to demand.
* They are consumed if the supply becomes plentiful.
* Useful for spending and denomenating contracts and debt.
* Individual forks encouraged
* May serve as cryptostocks, personal currencies, game money.
* Friend to friend blockchain sharing protects from 51% attack
First objective, we create a lightweight script that implements
the basic functions and is dead simple to understand.
Since crypto is hard, we won't be doing any of it ourselves. We
trust PyCrypto for that.
'''
'''
keypair
To use a crypto system like this, you need a public key and a
private key. The public key can be known to anyone. It is based
on the private key. The private key is a random number known
only to yourself.
'''
from Crypto import Random
from Crypto.PublicKey import RSA
def keypair():
r=Random.new().read
k=RSA.generate((1024),r)
return k, k.publickey().exportKey()
# some starter values for testing purposes
# prikey, pubkey = keypair()
'''
sha256hash
This function is used to convert a string of information, which
might be orderly, into a string of random-looking bits. We use
the hex digest because it is easier than raw bits.
'''
from Crypto.Hash import SHA256
def sha256hash(text):
return SHA256.new(text).hexdigest()
'''
address
Addresses identify you to the network. They are based on the
hash of your public key. We just use the first 16 characters,
because this is plenty for our purposes.
'''
def address(pubkey):
return sha256hash(pubkey)[:16]
'''
addresslist
Address list is a list of addresses and the public key they
correspond to. The first item is genesis address, which has no
public key.
'''
addresslist=[('genesis','')]
def newaddress(addresslist,pubkey):
addresslist.append((address(pubkey), pubkey))
'''
sendaction (kinda like a transaction)
Send Actions contain:
| amount of coin | division level
| sending address | receiving address
| signature of sending address
Sendactions are fundamental, and the system needs to support
billions of them eventually. They must be optimized heavily
in the final implementation.
For now, we use a list structure with some tuples. For example:
genesis = [(1, 0), ('sending','receiving'),'signature']
'''
# genesis sendaction adds 1 coin, division level 0.
genesis = [(1, 0), ('',''),'']
from Crypto.Hash import SHA256
def sendaction(amount, division, sending, receiving, prikey):
text='' + str(amount) + str(division) + str(sending) + str(receiving)
# one of my installs of python can't sign anything but a hash digest.
texthash=SHA256.SHA256Hash.new(text).digest()
(signature,)=prikey.sign(texthash,'')
return [(amount, division), (sending, receiving), str(signature)[:16]]
'''
datachain
The datachain is a list of sendactions. It begins with the
genesis sendaction, and each additional sendaction moves amounts
to new addresses. Each is labeled with the receiving address.
'''
datachain={'genesis':genesis}
def addsendaction(datachain, sendaction):
datachain.update({sendaction[1][1] : sendaction})
'''
Problem statement:
Need to make sure coins sent total out to 1.
make sure that every thing divides by 2^n
(does divisibility add too much complexity at this point?)
Example:
amount | division
a | 1 | 0
b | 1024 | 10
f(a) == f(b)
def f(x):
d = pow(2,x[1])
return x[0] / d
'''
# coinamount is a tuple containing a unit value and a division level
def divisionlevel(coinamount):
d = pow(2,coinamount[1])
return coinamount[0] / d
'''
Problem statement:
Need to make sure sendactions are signed by sending identity.
1. given sending address, get sending public key
2. given signature text, create sha256 hash
3. given sending public key and hash, verify sendaction
(to be implemented)
'''
'''
test
move from genesis to new addresses
'''
def test(datachain):
prikey, pubkey = keypair()
newaddress(addresslist,pubkey)
sending='genesis'
receiving=address(pubkey)
amount=2
division=1
l=sendaction(amount, division, sending, receiving, prikey)
addsendaction(datachain,l)
test(datachain);test(datachain)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment