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.

No comments:

Post a Comment