forked from V8ST8K/BTC-Wallet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwallet.py
More file actions
70 lines (50 loc) · 1.52 KB
/
wallet.py
File metadata and controls
70 lines (50 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from bitcoin import *
import requests
class Wallet():
def __init__(self, bot, uid):
wallet = bot.user_get(uid, "wallet")
if not wallet:
wallet = self._create_wallet(uid)
bot.user_set(uid, "wallet", wallet)
self.address = wallet["address"]
self.public = wallet["public"]
self.private = wallet["private"]
self.comission = bot.const["comission"]
def _create_wallet(self, uid):
key = random_key()
private = sha256(key)
public = privtopub(private)
address = pubtoaddr(public)
wallet = {"private": private,
"public": public,
"address": address}
return wallet
def get_currency(self):
res = requests.get("https://blockchain.info/ticker").json()
currency = res["RUB"]["buy"]
return currency
def get_balance(self):
res = requests.get("https://blockchain.info/balance?active=%s" % self.address).json()
balance = res[self.address]["final_balance"]
return balance
def send_money(self, value, address):
balance = self.get_balance()
if balance - (value + self.comission)<0:
return -1
try:
h = bci_unspent(self.address)
except Exception as ex:
return -1
if int(balance - (value + self.comission)) == 0:
outs = [{'value': int(value), 'address': address}]
else:
outs = [{'value': int(value), 'address': address},
{'value': int(balance - (value + self.comission)), 'address': self.address}]
try:
tx = mktx(h, outs)
for i in range(len(h)):
tx = sign(tx, i, self.private)
bci_pushtx(tx)
except Exception as ex:
return -2
return 0