Example #1
0
 def redeemprivatekey(self, key, address_from, address_to):
     if type(address_to) == str or type(address_to) == unicode:
         address_to = ((address_to, None),)
     if address_from != privkey2address(key):
         return None
     unspent_transactions = self.bitcoind_api.listunspent(1, 9999999, [address_from])
     tot_amount = sum([Decimal(x['amount']) for x in unspent_transactions])
     tot_fee = len(unspent_transactions) * settings.BITCOIN_PRIVKEY_FEE
     tot_spend = tot_fee
     if tot_amount > tot_spend:
         final_arr = {}
         for addr in address_to:
             if addr[1] and addr[1]<0:
                 raise Exception("No negative spend values allowed")
             if addr[1] and tot_amount > addr[1] + tot_spend:
                 final_arr[addr[0]] = decimal_float(addr[1])
                 tot_spend += addr[1]
             elif not addr[1] and tot_amount > tot_spend:
                 final_arr[addr[0]] = decimal_float((tot_amount - tot_spend))
                 break
             else:
                 return None  # raise Exception("Invalid amount parameters")
         # print final_arr
         # print unspent_transactions
         spend_transactions = [{"txid": ut['txid'], "vout": ut['vout']} for ut in unspent_transactions]
         spend_transactions_sign = [{"txid": ut['txid'], "vout": ut['vout'], "scriptPubKey": ut['scriptPubKey']} for ut in unspent_transactions]
         raw_transaction = self.bitcoind_api.createrawtransaction(spend_transactions, final_arr)
         raw_transaction_signed = self.bitcoind_api.signrawtransaction(raw_transaction, spend_transactions_sign, [key])
         # print raw_transaction, raw_transaction_signed
         return self.bitcoind_api.sendrawtransaction(raw_transaction_signed['hex'])
     else:
         return None
Example #2
0
 def importprivatekey(self, key):
     # import private key functionality here later
     # NOTE: only
     label = "import"
     address_from = privkey2address(key)
     if not address_from or not address_from.startswith("1"):
         print address_from
         return None
     # print address_from
     try:
         self.bitcoind_api.importprivkey(key, label)
     except jsonrpc.JSONRPCException:
         pass
     unspent_transactions = self.bitcoind_api.listunspent(1, 9999999, [address_from])
     return (address_from, quantitize_bitcoin(Decimal(sum([Decimal(x['amount']) for x in unspent_transactions]))))
Example #3
0
 def redeemprivatekey(self, key, address_from, address_to):
     if type(address_to) == str or type(address_to) == unicode:
         address_to = ((address_to, None), )
     if address_from != privkey2address(key):
         return None
     unspent_transactions = self.bitcoind_api.listunspent(
         1, 9999999, [address_from])
     tot_amount = sum([Decimal(x['amount']) for x in unspent_transactions])
     tot_fee = len(unspent_transactions) * settings.BITCOIN_PRIVKEY_FEE
     tot_spend = tot_fee
     if tot_amount > tot_spend:
         final_arr = {}
         for addr in address_to:
             if addr[1] and addr[1] < 0:
                 raise Exception("No negative spend values allowed")
             if addr[1] and tot_amount > addr[1] + tot_spend:
                 final_arr[addr[0]] = decimal_float(addr[1])
                 tot_spend += addr[1]
             elif not addr[1] and tot_amount > tot_spend:
                 final_arr[addr[0]] = decimal_float(
                     (tot_amount - tot_spend))
                 break
             else:
                 return None  # raise Exception("Invalid amount parameters")
         # print final_arr
         # print unspent_transactions
         spend_transactions = [{
             "txid": ut['txid'],
             "vout": ut['vout']
         } for ut in unspent_transactions]
         spend_transactions_sign = [{
             "txid": ut['txid'],
             "vout": ut['vout'],
             "scriptPubKey": ut['scriptPubKey']
         } for ut in unspent_transactions]
         raw_transaction = self.bitcoind_api.createrawtransaction(
             spend_transactions, final_arr)
         raw_transaction_signed = self.bitcoind_api.signrawtransaction(
             raw_transaction, spend_transactions_sign, [key])
         # print raw_transaction, raw_transaction_signed
         return self.bitcoind_api.sendrawtransaction(
             raw_transaction_signed['hex'])
     else:
         return None
Example #4
0
 def importprivatekey(self, key):
     # import private key functionality here later
     # NOTE: only
     label = "import"
     address_from = privkey2address(key)
     if not address_from or not address_from.startswith("1"):
         print address_from
         return None
     # print address_from
     try:
         self.bitcoind_api.importprivkey(key, label)
     except jsonrpc.JSONRPCException:
         pass
     unspent_transactions = self.bitcoind_api.listunspent(
         1, 9999999, [address_from])
     return (address_from,
             quantitize_bitcoin(
                 Decimal(
                     sum([
                         Decimal(x['amount']) for x in unspent_transactions
                     ]))))