Python Instawallet API


SUBMITTED BY: Guest

DATE: Oct. 11, 2012, 6:32 a.m.

FORMAT: Python

SIZE: 2.4 kB

HITS: 1498

  1. #!/usr/bin/env python
  2. # Pynstawallet 2: Python binding to Instawallet API
  3. # by Multiversum
  4. # Improvements:
  5. # - ability to create new wallet from a passphrase
  6. # - ability to get balance in BTC, not only Satoshis
  7. from requests import get, post
  8. from hashlib import sha256
  9. from base64 import b64encode
  10. instaweb_base = r'https://www.instawallet.org'
  11. instaapi_base = instaweb_base + r'/api/v1'
  12. def btcaddress(walletid):
  13. ''' Get Bitcoin address associated with private wallet ID '''
  14. resp = get(instaapi_base+'/w/'+walletid+'/address').json
  15. if resp:
  16. return resp['address']
  17. else:
  18. return None
  19. def passtoid(passphrase):
  20. ''' Convert (hash) your passphrase or password to the private Instawallet ID '''
  21. preid = b64encode(sha256(passphrase).digest()).strip('/=\\+')
  22. return filter(lambda x: x not in '/=\\+', preid)
  23. def new_wallet(passphrase=None):
  24. ''' Create a new wallet (automatically or from passphrase) and return its private wallet ID and public bitcoin address '''
  25. if passphrase:
  26. walletid = passtoid(passphrase)
  27. resp = get(instaweb_base+'/w/'+walletid)
  28. return walletid, btcaddress(walletid)
  29. else:
  30. resp = post(instaapi_base+'/new_wallet').json
  31. if resp:
  32. walletid = resp['wallet_id']
  33. return walletid, btcaddress(walletid)
  34. else:
  35. return None
  36. def balance(walletid):
  37. ''' Get your Instawallet balance in Satoshis by private wallet ID '''
  38. resp = get(instaapi_base+'/w/'+walletid+'/balance').json
  39. if resp:
  40. return resp['balance']
  41. else:
  42. return None
  43. def btcbalance(walletid):
  44. ''' Get your Instawallet balance in Bitcoins by private wallet ID '''
  45. satbalance = balance(walletid)
  46. if satbalance is not None:
  47. return satbalance/float(100000000.)
  48. else:
  49. return None
  50. def pay(walletid, dest_addr, amount):
  51. ''' Initiate a payment from wallet ID to destination Bitcoin address.
  52. The payment amount is in Satoshis.
  53. The function returns operation result code and message. '''
  54. params = { 'address' : dest_addr, 'amount' : amount }
  55. resp = post(instaapi_base+'/w/'+walletid+'/payment', data=params).json
  56. if resp:
  57. return resp['message_code'], resp['message']
  58. else:
  59. return -1, 'Unknown error'

comments powered by Disqus