示例#1
0
def 查钱包金额(私钥集合):
    for 私钥 in 私钥集合:
        print("私钥:%s"%私钥)
        my_key=Key(私钥)
        print("余额:%s"%my_key.get_balance('btc'))
示例#2
0
from bit import Key

my_key = Key()
print(my_key.get_balance('usd'))
示例#3
0
class MainPage(tk.Frame):  
    def __init__(self, parent, controller): 
        tk.Frame.__init__(self, parent)
        
        self.parent = parent
        self.configure(background='white')
        self.currency = 'btc'
        self.trans_url = "https://blockstream.info/testnet/api/tx/"
        
        addressLabel = tk.Label(self, text = "Address: dfsu3fdu434", bg = "white", fg = "black", font = LARGEFONT)
        addressLabel.grid(row = 0, column = 0, columnspan = 2, sticky = W, padx = 10, pady = 10, ipadx = 5, ipady = 5)
        self.coinLabel = tk.Label(self, text = "Coins: 1 btc", bg = "white", fg = "black", font = LARGEFONT)
        self.coinLabel.grid(row = 1, column = 0, columnspan = 2, sticky = W, padx = 10, pady = 10, ipadx = 5, ipady = 5)
        self.sendAddressInput = tk.Entry(self, bg='#ebf3ff', font = LARGEFONT)
        self.sendAddressInput.grid(row = 2, column = 0, sticky = W+E, padx = 10, pady = 10, ipadx = 5, ipady = 5)
        self.sendCoinsInput = tk.Entry(self, bg='#ebf3ff', font = LARGEFONT)
        self.sendCoinsInput.grid(row = 3, column = 0, sticky = W, padx = 10, pady = 10, ipadx = 5, ipady = 5)
        sendButton = tk.Button(self, text = 'Send',
                                fg = 'white',
                                bg = '#8c9fbd',
                                activeforeground = 'white',
                                activebackground = '#b8c2d1',
                                borderwidth = 2,
                                relief = FLAT,
                                overrelief = RAISED,
                                font = LARGEFONT,
                                command = lambda : self.asyncSendCoins())
        sendButton.grid(row = 4, column = 0, sticky = W, padx = 10, pady = 10, ipadx = 5, ipady = 5)
        copyButton = tk.Button(self, text = 'Copy PrivKey to Clipboard',
                                fg = 'white',
                                bg = '#de9ec0',
                                activeforeground = 'white',
                                activebackground = '#de9ec0',
                                borderwidth = 2,
                                relief = FLAT,
                                overrelief = RAISED,
                                font = LARGEFONT,
                                command = lambda : self.copyKey())
        copyButton.grid(row = 4, column = 1, sticky = W, padx = 10, pady = 10, ipadx = 5, ipady = 5)
        self.changeCurrencyInput = tk.Entry(self, bg='#ebf3ff', font = LARGEFONT)
        self.changeCurrencyInput.grid(row = 5, column = 0, sticky = W, padx = 10, pady = 10, ipadx = 5, ipady = 5)
        changeCurrencyButton = tk.Button(self, text = 'Change currency',
                                fg = 'white',
                                bg = '#8c9fbd',
                                activeforeground = 'white',
                                activebackground = '#b8c2d1',
                                borderwidth = 2,
                                relief = FLAT,
                                overrelief = RAISED,
                                font = LARGEFONT,
                                command = lambda : self.asyncChangeCurrency())
        changeCurrencyButton.grid(row = 5, column = 1, padx = 10, pady = 10, ipadx = 5, ipady = 5)
        
        self.transactions = Listbox(self)
        self.transactions.grid(row = 0, column = 2, rowspan = 3, columnspan = 4, sticky = 'nsew')
        self.transactions.bind('<<ListboxSelect>>', self.asynconselect)
        self.transactionsVar = StringVar()
        self.transactionsVar.set(150 * " ")
        self.transactionsLabel = tk.Message(self, textvariable = self.transactionsVar, bg = "white", fg = "black", width = 300)
        self.transactionsLabel.grid(row = 3, column = 2, rowspan = 3, sticky = W+E, columnspan = 4, padx = 10, pady = 10, ipadx = 5, ipady = 5)
        
        self.parent.after(3000, self.asyncUpdateInterface)
        
        if (controller.config.get('istestnet') == 1):
            self.trans_url = "https://blockstream.info/testnet/api/tx/"
            if (controller.config.get('private_key') != None):
                self.key = PrivateKeyTestnet(controller.config.get('private_key'))
            else:
                self.key = PrivateKeyTestnet()
        else:
            self.trans_url = "https://blockstream.info/api/tx/"
            if (controller.config.get('private_key') != None):
                self.key = Key(controller.config.get('private_key'))
            else:
                self.key = Key()
        
        addressLabel.config(text = "Address: " + self.key.address)
        self.coinLabel.config(text = "Coins: " + self.key.get_balance(self.currency) + " " + self.currency)
            
        transactions_list = self.key.get_transactions()
        for trans in transactions_list:
            print(trans)
            self.transactions.insert(END, trans)
        print(self.trans_url)
        for x in range(6):
            Grid.columnconfigure(self, x, weight=1)

        for y in range(5):
            Grid.rowconfigure(self, y, weight=1)
    
    def asyncChangeCurrency(self):
        x = threading.Thread(target = self.changeCurrency)
        x.start()
        
    def changeCurrency(self):
        cur_values = dict(SUPPORTED_CURRENCIES).keys()
        us_input = self.changeCurrencyInput.get()
        self.changeCurrencyInput.delete(0, 'end')
        if (us_input in cur_values):
            self.currency = us_input
            coins = self.key.get_balance(self.currency)
            self.coinLabel.config(text = "Coins: " + coins + " " + self.currency)
    
    def asynconselect(self,evt):
        x = threading.Thread(target = self.onselect, args = (evt,))
        x.start()
    
    def onselect(self, evt):
        # Note here that Tkinter passes an event object to onselect()
        w = evt.widget
        index = 0
        try:
            index = int(w.curselection()[0])
        except IndexError:
            print('Unselected item in list of transactions')
            return
        value = w.get(index)
        cb.copy(value)
        print('You selected item %d: "%s"' % (index, value))
        try:
            response = requests.get(self.trans_url + value)
            print(response.json())
            json_resp = response.json()
            in_address = json_resp['vin'][0]['prevout']['scriptpubkey_address']
            out_address = json_resp['vout'][0]['scriptpubkey_address']
            fee = json_resp['fee']
            money = json_resp['vout'][0]['value']
            block_time = json_resp['status']['block_time']
            block_time = datetime.datetime.fromtimestamp(block_time).strftime('%Y-%m-%d %H:%M:%S')
            self.transactionsVar.set("Transaction: " + str(value) +
                            "\nFrom: " + str(in_address) + 
                            "\nTo: " + str(out_address) +
                            "\nValue: " + str(money) + " satoshi" +
                            "\nFee: " + str(fee) + " satoshi" + 
                            "\nTime: " + block_time)
        except BaseException:
            messagebox.showerror(title = 'Error', message = 'Maybe problems with connection or you don\'t have transactions')
    
    def asyncSendCoins(self):
        x = threading.Thread(target = self.sendCoins)
        x.start()
    
    def sendCoins(self):
        us_address = self.sendAddressInput.get()
        us_coins = self.sendCoinsInput.get()
        self.sendAddressInput.delete(0, 'end')
        self.sendCoinsInput.delete(0, 'end')
        try:
            if (us_address == '' or us_coins == ''):
                return
            us_coins = float(us_coins)
        except ValueError:
            print('invalid transformation')
            return
        
        try:
            # Send coins and update labels
            outputs = [
                (us_address, us_coins, self.currency)
            ]
            print(self.key.send(outputs))
            self.updateInterface()
            messagebox.showinfo(title = 'Transacation', message = 'Successed')
        except BaseException:
            messagebox.showerror(title = 'Error', message = 'Maybe, you don\'t have internet connection or use wrong key or you don\'t have enough money')
            
    def asyncUpdateInterface(self):
        x = threading.Thread(target = self.cycleUpdateInterface, daemon = True)
        x.start()
    
    def cycleUpdateInterface(self):
        self.updateInterface()
        self.parent.after(3000, self.asyncUpdateInterface)
            
    def updateInterface(self):
        self.coinLabel.config(text = "Coins: " + self.key.get_balance(self.currency) + " " + self.currency)
        new_transactions = self.key.get_transactions()
        trans_values = self.transactions.get(0, 'end')
        for trans in new_transactions:
            if trans not in trans_values:
                self.transactions.insert(0, trans)

    def copyKey(self):
        cb.copy(self.key.to_wif())