-
Notifications
You must be signed in to change notification settings - Fork 3
/
ui.py
72 lines (65 loc) · 2.22 KB
/
ui.py
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
70
71
72
from flask import Flask, render_template, request, redirect, url_for
from flask_qrcode import QRcode
from wallet import Wallet
from bedrock.tx import Tx
from bedrock.script import Script
from io import BytesIO
import json
from rpc import WalletRPC
app = Flask(__name__)
QRcode(app)
@app.route("/create-transaction", methods=['GET', 'POST'])
def create_transaction():
wallet = Wallet.open()
if request.method == 'GET':
if wallet.tx_data is not None:
msg = json.dumps(wallet.tx_data)
else:
msg = None
else:
wallet = Wallet.open()
wallet.start_tx(
request.form['recipient'], int(request.form['satoshis']), 300
)
msg = json.dumps(wallet.tx_data)
return render_template('create-transaction.html', msg=msg)
@app.route("/scan-xpub", methods=['GET', 'POST'])
def scan_xpub():
if request.method == 'GET':
return render_template('scan-xpub.html')
else:
print(request.json)
Wallet.create(request.json['xpub'])
return 'ok', 200
@app.route("/address")
def address():
wallet = Wallet.open()
return wallet.consume_address(), 200
@app.route("/add-signature", methods=['GET', 'POST'])
def add_signature():
if request.method == 'GET':
return render_template('add-signature.html')
else:
wallet = Wallet.open()
script_sig = Script.parse(BytesIO(bytes.fromhex(request.json['signature'])))
tx = Tx.parse(BytesIO(bytes.fromhex(wallet.tx_data['tx'])))
for i, tx_in in enumerate(tx.tx_ins):
if tx_in.script_sig.cmds == []:
tx.tx_ins[i].script_sig = script_sig
print('filled in script sig')
break
# FIXME: for some reason script_sig is None ...
if all([tx_in.script_sig.cmds != [] for tx_in in tx.tx_ins]):
rpc = WalletRPC('bitboy')
print(tx.serialize().hex())
rpc.broadcast(tx.serialize().hex())
print('broadcasted')
# wallet.tx_data = None
# FIXME
return tx.id()
wallet.save()
return 'ok'
if __name__ == '__main__':
import logging
logging.basicConfig(level=logging.DEBUG)
app.run(debug=True, port=9999)