Showing posts with label #python. Show all posts
Showing posts with label #python. Show all posts

Tuesday, October 20, 2015

Back to Census library examples - really sending data!




Okay, I got distracted by Burning Man prep and left this sort of hanging - but back to using the Census library.  Just for the record, I don't own 'cathuggers.com' - please don't start blasting emails to people there.  Cats are awesome, get over it.



Last entry we were querying the blockchain using the REST API, which is cool enough but it's annoying for actually doing stuff.  I'm not a super fan of Python (the 2.x to 3.x breakage is a train wreck equal to the perl Parrot nonsense if you are old enough to remember that, my opinion as always) - but it's handy to do things.  To implement using the library is pretty easy...

__author__ = 'lpreimesberger'
import censusprotocollib
import json

print "Hello world!  I'm trying to query the blockchain server on this host!"wallet_address = raw_input( "which wallet to look for? : ")
print censusprotocollib.get_wallet_by_email("localhost", wallet_address )

Running this gets:
lpreimesberger@dadbuntu:~/projects/testthelibrary$ python testwalletquery.py 
Hello world!  I'm trying to query the blockchain server on this host!
which wallet to look for? : ilovecats@cathuggers.com
[{"_id":"55c3ffb0cdd86818fb3914b8","balance":0,"version":1,"created":1436734011857,"txid":"433225d2-e843-4448-a97d-aefaf4b5d5a0","item_type":"wallet","tag":"generated by censusprotocollib","source":"1JwSSubhmg6iPtRjtyqhUYYH7bZg3Lfy1T","signature":"","block_in":"","email":"ilovecats@cathuggers.com","rsa_keys":["-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAseG7lt/JwOHyzG0hKbKQ\niDmiQ0bTY+mg6AQq8wdZx3lQF0BOWCtnYYxVL+zWr2fqMqjtsc6rC11p4VvmojBm\nGZAEMH1um+sUAzMLKellAdfRNH1XRA5Ag/treaWOlmY/2ilTfq2raSgq/Ed/kGix\nl+NynzgNiKqFVbBbCZ4rvPmkiyEmygpuLXU5MO8qOSpX1cegauByciKxcdqcBLjg\nQgj7XLO9eqGCX/ypzysXPAIgdWIUrZci8gjdJf65eQBywaQ1jLTpASV3kzY4pB4d\n3lBWZuIe5Ck65ZVQjeaPXgYVDLUFp+R2eERWAWydaKLqaSaL3NGg6XMuqoFzx0Ex\nrQIDAQAB\n-----END PUBLIC KEY-----"]}]
This built in encryption is using RSA RFC3447 - this is a deliberate choice to not use the ECDSA keys from Census so you can create and revoke without losing everything.  This also prevents easier cracking of your Census key, since there isn't any chance a lazy developer like me would implement something that would give predictable, encrypted messages that could limit the key possibilities.  RSA is probably good enough for most everything.
Doing this in code makes:
__author__ = 'lpreimesberger'
import censusprotocollib
import json

print "Hello world!  I'm trying to query the blockchain server on this host!"wallet_address = raw_input( "which wallet to look for? : ")
# a single email address can have many wallets - we'll cheat and use the first
the_wallet = json.loads(censusprotocollib.get_wallet_by_email("localhost", wallet_address ))[0]
print "Send this message on the ILOVECATS forum!"print "-------------------------------------------------------------------"print censusprotocollib.rsa_encode(the_wallet["rsa_keys"], "cats")
print "-------------------------------------------------------------------"
Running this will create a nice message for our cat loving friend:
lpreimesberger@dadbuntu:~/projects/testthelibrary$ python testwalletquery.py 
Hello world! I'm trying to query the blockchain server on this host! which wallet to look for? : ilovecats@cathuggers.com 
Send this message on the ILOVECATS forum! 
------------------------------------------------------------------- L49L0Gt2tTNTv4IuHZRyF1wmCXz7NDrXtXev9jbTeADP8Uxeqy7COw9DlKKq+vOWrjnGb9it4prPBjObi7zptEi8J4I47Qb6FI8VHSbkNk3dei4Uev3pYY2BgQcFWJW4cQfkGohgBEPSGRlC7yXcmMw0uYXeekaAOzjkP7nTYUFV7XSGP8TpJGJCEwOLQLPRF0mmMkZiFXxuGTBew2CB9zqUUD10+hCtSsP61FJSVsf6JW2h1eNSsPWK1LE9mS5sfY+oYMckLrmI4jYQMlhiiVfgUk6DbgdjI6Scc420G6MkToOgZHL/tb/nVBeQefiojJPLpv2OOm6o9lKKXq4Xmg== 
-------------------------------------------------------------------
You can post this, or if you are super interested can use smtplib to send to a mail server.  You can also sign the message using your private RSA key using Crypto or use the censusprotocol.sign() function to sign with your Census key.  If you are sending stuff outside the protocol - you should probably only sign with your RSA key though.  Again, if you send predictable stuff - it's easier to guess your private key.  Keep the Census key for protocol packet - use RSA for other stuff.

Wednesday, July 8, 2015

Census Protocol and Wallets

I started using (and fixing incidentally, if somebody wants to volunteer to help :-P ) the Census Protocol python library and it seems pretty handy so far.  I haven't pushed my latest changes to git yet - so if you're anxious just chill.  There was a bunch of stupid errors in the library as is that I just fixed.  Easiest thing to start with is the Census version of "Hello World."

The Census Protocol is a blockchain application that's made to send and record the possession of virtual goods. The software itself is planned to be open source - but it's not widely available yet since it's changing a lot still. A small cabal (sorry – I just like that word :) ) are working on it now and breaking each others changes the way that only nerds can – but hopefully it should be pushed to the public repo soon.

To verify the local node is running and mostly works - asking its version is the simplest.  This is through /api/v1/version or just using the version call in the library.


__author__ = 'lpreimesberger'
import censusprotocollib
import json

print "Hello world!  I'm trying to talk to the blockchain server on this host!"
print json.dumps( censusprotocollib.version("localhost"), sort_keys=True, indent=4, separators=(',', ': '))
print "Tada!"

Running this gives the expected output on my local system:
lpreimesberger@dadbuntu:~/projects/testthelibrary$ python testversion.py  

Hello world!  I'm trying to talk to the blockchain server on this host!
"{\"result\":\"ok\",\"vendor\":\"census foundation baseline implementation\",\"version\":{\"core\":\"0.01\",\"hard_fork\":1}}"
Tada!



Good start, right? Well, okay - that's pretty useless. You could do that in a web browser after installing the base packages.

Everything in Census is based around the wallets - if a node doesn't think there's a valid wallet existing in the blockchain (or at least its local coinbase) it will reject any item transfer to that address. For local submissions – it's rejected outright. If they are sent from other nodes they remain eternally in the graveyard until the consensus says they are good or the node sees a valid wallet create with a valid signature pass.

Next post will be trying to query the local database (again, baby steps) – and then using the library to generate a new wallet and try to do something useful. Since the wallets contain valid ECDSA and RSA public keys – we can probably make a ghetto PGP application pretty quickly.