<s> import os <EOL> import json <EOL> import jsonrpc <EOL> import sys <EOL> import urllib <EOL> import urllib2 <EOL> import random <EOL> import hashlib <EOL> import base64 <EOL> from decimal import Decimal <EOL> import decimal <EOL> import warnings <EOL> from django . core . cache import cache <EOL> from django . db import transaction <EOL> from django_bitcoin import settings <EOL> from django_bitcoin import currency <EOL> from pywallet import privkey2address <EOL> def quantitize_bitcoin ( d ) : <EOL> return d . quantize ( Decimal ( "<STR_LIT>" ) ) <EOL> def decimal_float ( d ) : <EOL> return float ( d . quantize ( Decimal ( "<STR_LIT>" ) ) ) <EOL> class BitcoindConnection ( object ) : <EOL> def __init__ ( self , connection_string , main_account_name ) : <EOL> self . bitcoind_api = jsonrpc . ServiceProxy ( connection_string ) <EOL> self . account_name = main_account_name <EOL> def total_received ( self , address , minconf = settings . BITCOIN_MINIMUM_CONFIRMATIONS ) : <EOL> if settings . BITCOIN_TRANSACTION_CACHING : <EOL> cache_key = address + "<STR_LIT:_>" + str ( minconf ) <EOL> cached = cache . get ( cache_key ) <EOL> if cached != None : <EOL> return cached <EOL> cached = decimal . Decimal ( <EOL> self . bitcoind_api . getreceivedbyaddress ( address , minconf ) ) <EOL> cache . set ( cache_key , cached , <NUM_LIT:5> ) <EOL> return cached <EOL> return decimal . Decimal ( <EOL> self . bitcoind_api . getreceivedbyaddress ( address , minconf ) ) <EOL> def send ( self , address , amount , * args , ** kwargs ) : <EOL> return self . bitcoind_api . sendtoaddress ( address , float ( amount ) , * args , ** kwargs ) <EOL> def sendmany ( self , address_amount_dict , * args , ** kwargs ) : <EOL> return self . bitcoind_api . sendmany ( self . account_name , address_amount_dict , * args , ** kwargs ) <EOL> def create_address ( self , for_account = None , * args , ** kwargs ) : <EOL> return self . bitcoind_api . getnewaddress ( <EOL> for_account or self . account_name , * args , ** kwargs ) <EOL> def gettransaction ( self , txid , * args , ** kwargs ) : <EOL> return self . bitcoind_api . gettransaction ( txid , * args , ** kwargs ) <EOL> def importprivatekey ( self , key ) : <EOL> label = "<STR_LIT>" <EOL> address_from = privkey2address ( key ) <EOL> if not address_from or not address_from . startswith ( "<STR_LIT:1>" ) : <EOL> print address_from <EOL> return None <EOL> try : <EOL> self . bitcoind_api . importprivkey ( key , label ) <EOL> except jsonrpc . JSONRPCException : <EOL> pass <EOL> unspent_transactions = self . bitcoind_api . listunspent ( <NUM_LIT:1> , <NUM_LIT> , [ address_from ] ) <EOL> return ( address_from , quantitize_bitcoin ( Decimal ( sum ( [ Decimal ( x [ '<STR_LIT>' ] ) for x in unspent_transactions ] ) ) ) ) <EOL> def redeemprivatekey ( self , key , address_from , address_to ) : <EOL> if type ( address_to ) == str or type ( address_to ) == unicode : <EOL> address_to = ( ( address_to , None ) , ) <EOL> if address_from != privkey2address ( key ) : <EOL> return None <EOL> unspent_transactions = self . bitcoind_api . listunspent ( <NUM_LIT:1> , <NUM_LIT> , [ address_from ] ) <EOL> tot_amount = sum ( [ Decimal ( x [ '<STR_LIT>' ] ) for x in unspent_transactions ] ) <EOL> tot_fee = len ( unspent_transactions ) * settings . BITCOIN_PRIVKEY_FEE <EOL> tot_spend = tot_fee <EOL> if tot_amount > tot_spend : <EOL> final_arr = { } <EOL> for addr in address_to : <EOL> if addr [ <NUM_LIT:1> ] and addr [ <NUM_LIT:1> ] < <NUM_LIT:0> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if addr [ <NUM_LIT:1> ] and tot_amount > addr [ <NUM_LIT:1> ] + tot_spend : <EOL> final_arr [ addr [ <NUM_LIT:0> ] ] = decimal_float ( addr [ <NUM_LIT:1> ] ) <EOL> tot_spend += addr [ <NUM_LIT:1> ] <EOL> elif not addr [ <NUM_LIT:1> ] and tot_amount > tot_spend : <EOL> final_arr [ addr [ <NUM_LIT:0> ] ] = decimal_float ( ( tot_amount - tot_spend ) ) <EOL> break <EOL> else : <EOL> return None <EOL> spend_transactions = [ { "<STR_LIT>" : ut [ '<STR_LIT>' ] , "<STR_LIT>" : ut [ '<STR_LIT>' ] } for ut in unspent_transactions ] <EOL> spend_transactions_sign = [ { "<STR_LIT>" : ut [ '<STR_LIT>' ] , "<STR_LIT>" : ut [ '<STR_LIT>' ] , "<STR_LIT>" : ut [ '<STR_LIT>' ] } for ut in unspent_transactions ] <EOL> raw_transaction = self . bitcoind_api . createrawtransaction ( spend_transactions , final_arr ) <EOL> raw_transaction_signed = self . bitcoind_api . signrawtransaction ( raw_transaction , spend_transactions_sign , [ key ] ) <EOL> return self . bitcoind_api . sendrawtransaction ( raw_transaction_signed [ '<STR_LIT>' ] ) <EOL> else : <EOL> return None <EOL> bitcoind = BitcoindConnection ( settings . BITCOIND_CONNECTION_STRING , <EOL> settings . MAIN_ACCOUNT ) <EOL> def bitcoin_getnewaddress ( account_name = None ) : <EOL> warnings . warn ( "<STR_LIT>" , <EOL> DeprecationWarning ) <EOL> return bitcoind . create_address ( account_name = account_name ) <EOL> def bitcoin_getbalance ( address , minconf = <NUM_LIT:1> ) : <EOL> warnings . warn ( "<STR_LIT>" , <EOL> DeprecationWarning ) <EOL> return bitcoind . total_received ( address , minconf ) <EOL> def bitcoin_getreceived ( address , minconf = settings . BITCOIN_MINIMUM_CONFIRMATIONS ) : <EOL> warnings . warn ( "<STR_LIT>" , <EOL> DeprecationWarning ) <EOL> return bitcoind . total_received ( address , minconf ) <EOL> def bitcoin_sendtoaddress ( address , amount ) : <EOL> warnings . warn ( "<STR_LIT>" , <EOL> DeprecationWarning ) <EOL> return bitcoind . send ( address , amount ) <EOL> def bitcoinprice_usd ( ) : <EOL> """<STR_LIT>""" <EOL> warnings . warn ( "<STR_LIT>" , <EOL> DeprecationWarning ) <EOL> return { "<STR_LIT>" : currency . exchange . get_rate ( "<STR_LIT>" ) } <EOL> def bitcoinprice_eur ( ) : <EOL> warnings . warn ( "<STR_LIT>" , <EOL> DeprecationWarning ) <EOL> return { "<STR_LIT>" : currency . exchange . get_rate ( "<STR_LIT>" ) } <EOL> def bitcoinprice ( currency ) : <EOL> warnings . warn ( "<STR_LIT>" , <EOL> DeprecationWarning ) <EOL> return currency . exchange . get_rate ( currency ) <EOL> def generateuniquehash ( length = <NUM_LIT> , extradata = '<STR_LIT>' ) : <EOL> r = str ( os . urandom ( <NUM_LIT:64> ) ) <EOL> m = hashlib . sha256 ( ) <EOL> m . update ( r + str ( extradata ) ) <EOL> key = m . digest ( ) <EOL> key = base64 . urlsafe_b64encode ( key ) <EOL> return key [ : min ( length , <NUM_LIT> ) ] <EOL> import string <EOL> ALPHABET = string . ascii_uppercase + string . ascii_lowercase + string . digits + '<STR_LIT>' <EOL> ALPHABET_REVERSE = dict ( ( c , i ) for ( i , c ) in enumerate ( ALPHABET ) ) <EOL> BASE = len ( ALPHABET ) <EOL> SIGN_CHARACTER = '<STR_LIT:%>' <EOL> def int2base64 ( n ) : <EOL> if n < <NUM_LIT:0> : <EOL> return SIGN_CHARACTER + num_encode ( - n ) <EOL> s = [ ] <EOL> while True : <EOL> n , r = divmod ( n , BASE ) <EOL> s . append ( ALPHABET [ r ] ) <EOL> if n == <NUM_LIT:0> : break <EOL> return '<STR_LIT>' . join ( reversed ( s ) ) <EOL> def base642int ( s ) : <EOL> if s [ <NUM_LIT:0> ] == SIGN_CHARACTER : <EOL> return - num_decode ( s [ <NUM_LIT:1> : ] ) <EOL> n = <NUM_LIT:0> <EOL> for c in s : <EOL> n = n * BASE + ALPHABET_REVERSE [ c ] <EOL> return n </s>
