You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1004 lines
30 KiB
1004 lines
30 KiB
13 years ago
|
#!/usr/bin/env python
|
||
|
#
|
||
|
# Electrum - lightweight Bitcoin client
|
||
|
# Copyright (C) 2011 thomasv@gitorious
|
||
|
#
|
||
|
# This program is free software: you can redistribute it and/or modify
|
||
|
# it under the terms of the GNU General Public License as published by
|
||
|
# the Free Software Foundation, either version 3 of the License, or
|
||
|
# (at your option) any later version.
|
||
|
#
|
||
|
# This program is distributed in the hope that it will be useful,
|
||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
# GNU General Public License for more details.
|
||
|
#
|
||
|
# You should have received a copy of the GNU General Public License
|
||
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
|
||
|
|
||
|
|
||
13 years ago
|
|
||
13 years ago
|
import android
|
||
13 years ago
|
|
||
12 years ago
|
from electrum import SimpleConfig, Interface, WalletSynchronizer, Wallet, format_satoshis, mnemonic_encode, mnemonic_decode, is_valid
|
||
12 years ago
|
from electrum import util
|
||
13 years ago
|
from decimal import Decimal
|
||
13 years ago
|
import datetime, re
|
||
13 years ago
|
|
||
|
|
||
13 years ago
|
|
||
13 years ago
|
def modal_dialog(title, msg = None):
|
||
13 years ago
|
droid.dialogCreateAlert(title,msg)
|
||
|
droid.dialogSetPositiveButtonText('OK')
|
||
|
droid.dialogShow()
|
||
|
droid.dialogGetResponse()
|
||
|
droid.dialogDismiss()
|
||
13 years ago
|
|
||
13 years ago
|
def modal_input(title, msg, value = None, etype=None):
|
||
|
droid.dialogCreateInput(title, msg, value, etype)
|
||
13 years ago
|
droid.dialogSetPositiveButtonText('OK')
|
||
|
droid.dialogSetNegativeButtonText('Cancel')
|
||
|
droid.dialogShow()
|
||
12 years ago
|
response = droid.dialogGetResponse()
|
||
|
result = response.result
|
||
|
if result is None:
|
||
|
print "modal input: result is none"
|
||
|
return False
|
||
13 years ago
|
droid.dialogDismiss()
|
||
12 years ago
|
if result.get('which') == 'positive':
|
||
|
return result.get('value')
|
||
13 years ago
|
|
||
13 years ago
|
def modal_question(q, msg, pos_text = 'OK', neg_text = 'Cancel'):
|
||
|
droid.dialogCreateAlert(q, msg)
|
||
|
droid.dialogSetPositiveButtonText(pos_text)
|
||
|
droid.dialogSetNegativeButtonText(neg_text)
|
||
13 years ago
|
droid.dialogShow()
|
||
12 years ago
|
response = droid.dialogGetResponse()
|
||
|
result = response.result
|
||
|
if result is None:
|
||
|
print "modal question: result is none"
|
||
|
return False
|
||
13 years ago
|
droid.dialogDismiss()
|
||
12 years ago
|
return result.get('which') == 'positive'
|
||
13 years ago
|
|
||
|
def edit_label(addr):
|
||
|
v = modal_input('Edit label',None,wallet.labels.get(addr))
|
||
13 years ago
|
if v is not None:
|
||
|
if v:
|
||
|
wallet.labels[addr] = v
|
||
|
else:
|
||
|
if addr in wallet.labels.keys():
|
||
|
wallet.labels.pop(addr)
|
||
13 years ago
|
wallet.update_tx_history()
|
||
|
wallet.save()
|
||
13 years ago
|
droid.fullSetProperty("labelTextView", "text", v)
|
||
13 years ago
|
|
||
13 years ago
|
def select_from_contacts():
|
||
|
title = 'Contacts:'
|
||
|
droid.dialogCreateAlert(title)
|
||
13 years ago
|
l = []
|
||
|
for i in range(len(wallet.addressbook)):
|
||
|
addr = wallet.addressbook[i]
|
||
|
label = wallet.labels.get(addr,addr)
|
||
|
l.append( label )
|
||
|
droid.dialogSetItems(l)
|
||
13 years ago
|
droid.dialogSetPositiveButtonText('New contact')
|
||
13 years ago
|
droid.dialogShow()
|
||
13 years ago
|
response = droid.dialogGetResponse().result
|
||
13 years ago
|
droid.dialogDismiss()
|
||
13 years ago
|
|
||
|
if response.get('which') == 'positive':
|
||
|
return 'newcontact'
|
||
|
|
||
|
result = response.get('item')
|
||
|
print result
|
||
13 years ago
|
if result is not None:
|
||
|
addr = wallet.addressbook[result]
|
||
|
return addr
|
||
13 years ago
|
|
||
|
|
||
13 years ago
|
def select_from_addresses():
|
||
13 years ago
|
droid.dialogCreateAlert("Addresses:")
|
||
|
l = []
|
||
12 years ago
|
addresses = wallet.addresses()
|
||
|
for i in range(len(addresses)):
|
||
|
addr = addresses[i]
|
||
13 years ago
|
label = wallet.labels.get(addr,addr)
|
||
|
l.append( label )
|
||
13 years ago
|
droid.dialogSetItems(l)
|
||
|
droid.dialogShow()
|
||
13 years ago
|
response = droid.dialogGetResponse()
|
||
|
result = response.result.get('item')
|
||
13 years ago
|
droid.dialogDismiss()
|
||
13 years ago
|
if result is not None:
|
||
12 years ago
|
addr = addresses[result]
|
||
13 years ago
|
return addr
|
||
13 years ago
|
|
||
|
|
||
13 years ago
|
def protocol_name(p):
|
||
12 years ago
|
if p == 't': return 'TCP'
|
||
|
if p == 'h': return 'HTTP'
|
||
|
if p == 's': return 'TCP/SSL'
|
||
|
if p == 'g': return 'HTTPS'
|
||
|
|
||
13 years ago
|
|
||
13 years ago
|
def protocol_dialog(host, protocol, z):
|
||
13 years ago
|
droid.dialogCreateAlert('Protocol',host)
|
||
13 years ago
|
if z:
|
||
|
protocols = z.keys()
|
||
|
else:
|
||
12 years ago
|
protocols = 'thsg'
|
||
13 years ago
|
l = []
|
||
13 years ago
|
current = protocols.index(protocol)
|
||
13 years ago
|
for p in protocols:
|
||
13 years ago
|
l.append(protocol_name(p))
|
||
13 years ago
|
droid.dialogSetSingleChoiceItems(l, current)
|
||
13 years ago
|
droid.dialogSetPositiveButtonText('OK')
|
||
|
droid.dialogSetNegativeButtonText('Cancel')
|
||
|
droid.dialogShow()
|
||
|
response = droid.dialogGetResponse().result
|
||
12 years ago
|
selected_item = droid.dialogGetSelectedItems().result
|
||
|
droid.dialogDismiss()
|
||
|
|
||
13 years ago
|
if not response: return
|
||
12 years ago
|
if not selected_item: return
|
||
13 years ago
|
if response.get('which') == 'positive':
|
||
12 years ago
|
p = protocols[selected_item[0]]
|
||
13 years ago
|
port = z[p]
|
||
|
return host + ':' + port + ':' + p
|
||
|
|
||
13 years ago
|
|
||
13 years ago
|
|
||
13 years ago
|
|
||
13 years ago
|
def make_layout(s, scrollable = False):
|
||
13 years ago
|
content = """
|
||
13 years ago
|
|
||
|
<LinearLayout
|
||
|
android:id="@+id/zz"
|
||
13 years ago
|
android:layout_width="match_parent"
|
||
13 years ago
|
android:layout_height="wrap_content"
|
||
|
android:background="#ff222222">
|
||
|
|
||
|
<TextView
|
||
|
android:id="@+id/textElectrum"
|
||
|
android:text="Electrum"
|
||
|
android:textSize="7pt"
|
||
|
android:textColor="#ff4444ff"
|
||
|
android:gravity="left"
|
||
|
android:layout_height="wrap_content"
|
||
|
android:layout_width="match_parent"
|
||
|
/>
|
||
|
</LinearLayout>
|
||
13 years ago
|
|
||
13 years ago
|
%s """%s
|
||
|
|
||
|
if scrollable:
|
||
|
content = """
|
||
|
<ScrollView
|
||
|
android:id="@+id/scrollview"
|
||
|
android:layout_width="match_parent"
|
||
|
android:layout_height="match_parent" >
|
||
|
|
||
|
<LinearLayout
|
||
|
android:orientation="vertical"
|
||
|
android:layout_width="match_parent"
|
||
|
android:layout_height="wrap_content" >
|
||
|
|
||
|
%s
|
||
|
|
||
|
</LinearLayout>
|
||
|
</ScrollView>
|
||
|
"""%content
|
||
|
|
||
|
|
||
13 years ago
|
return """<?xml version="1.0" encoding="utf-8"?>
|
||
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||
|
android:id="@+id/background"
|
||
|
android:orientation="vertical"
|
||
|
android:layout_width="match_parent"
|
||
|
android:layout_height="match_parent"
|
||
|
android:background="#ff000022">
|
||
|
|
||
13 years ago
|
%s
|
||
|
</LinearLayout>"""%content
|
||
13 years ago
|
|
||
|
|
||
13 years ago
|
|
||
13 years ago
|
|
||
13 years ago
|
def main_layout():
|
||
|
return make_layout("""
|
||
13 years ago
|
<TextView android:id="@+id/balanceTextView"
|
||
13 years ago
|
android:layout_width="match_parent"
|
||
13 years ago
|
android:text=""
|
||
13 years ago
|
android:textColor="#ffffffff"
|
||
13 years ago
|
android:textAppearance="?android:attr/textAppearanceLarge"
|
||
13 years ago
|
android:padding="7dip"
|
||
|
android:textSize="8pt"
|
||
13 years ago
|
android:gravity="center_vertical|center_horizontal|left">
|
||
13 years ago
|
</TextView>
|
||
|
|
||
13 years ago
|
<TextView android:id="@+id/historyTextView"
|
||
13 years ago
|
android:layout_width="match_parent"
|
||
13 years ago
|
android:layout_height="wrap_content"
|
||
13 years ago
|
android:text="Recent transactions"
|
||
13 years ago
|
android:textAppearance="?android:attr/textAppearanceLarge"
|
||
13 years ago
|
android:gravity="center_vertical|center_horizontal|center">
|
||
13 years ago
|
</TextView>
|
||
|
|
||
13 years ago
|
%s """%get_history_layout(15),True)
|
||
13 years ago
|
|
||
|
|
||
13 years ago
|
|
||
13 years ago
|
def qr_layout(addr):
|
||
|
return make_layout("""
|
||
|
|
||
|
<TextView android:id="@+id/addrTextView"
|
||
|
android:layout_width="match_parent"
|
||
13 years ago
|
android:layout_height="50"
|
||
13 years ago
|
android:text="%s"
|
||
|
android:textAppearance="?android:attr/textAppearanceLarge"
|
||
13 years ago
|
android:gravity="center_vertical|center_horizontal|center">
|
||
13 years ago
|
</TextView>
|
||
13 years ago
|
|
||
|
<ImageView
|
||
|
android:id="@+id/qrView"
|
||
|
android:gravity="center"
|
||
|
android:layout_width="match_parent"
|
||
|
android:layout_height="350"
|
||
|
android:antialias="false"
|
||
|
android:src="file:///sdcard/sl4a/qrcode.bmp" />
|
||
|
|
||
13 years ago
|
<TextView android:id="@+id/labelTextView"
|
||
|
android:layout_width="match_parent"
|
||
|
android:layout_height="50"
|
||
|
android:text="%s"
|
||
|
android:textAppearance="?android:attr/textAppearanceLarge"
|
||
|
android:gravity="center_vertical|center_horizontal|center">
|
||
|
</TextView>
|
||
|
|
||
13 years ago
|
"""%(addr,wallet.labels.get(addr,'')), True)
|
||
13 years ago
|
|
||
13 years ago
|
payto_layout = make_layout("""
|
||
13 years ago
|
|
||
13 years ago
|
<TextView android:id="@+id/recipientTextView"
|
||
|
android:layout_width="match_parent"
|
||
|
android:layout_height="wrap_content"
|
||
|
android:text="Pay to:"
|
||
|
android:textAppearance="?android:attr/textAppearanceLarge"
|
||
|
android:gravity="left">
|
||
|
</TextView>
|
||
|
|
||
13 years ago
|
|
||
13 years ago
|
<EditText android:id="@+id/recipient"
|
||
|
android:layout_width="match_parent"
|
||
|
android:layout_height="wrap_content"
|
||
13 years ago
|
android:tag="Tag Me" android:inputType="text">
|
||
13 years ago
|
</EditText>
|
||
|
|
||
13 years ago
|
<LinearLayout android:id="@+id/linearLayout1"
|
||
|
android:layout_width="match_parent"
|
||
|
android:layout_height="wrap_content">
|
||
|
<Button android:id="@+id/buttonQR" android:layout_width="wrap_content"
|
||
13 years ago
|
android:layout_height="wrap_content" android:text="From QR code"></Button>
|
||
13 years ago
|
<Button android:id="@+id/buttonContacts" android:layout_width="wrap_content"
|
||
13 years ago
|
android:layout_height="wrap_content" android:text="From Contacts"></Button>
|
||
13 years ago
|
</LinearLayout>
|
||
|
|
||
|
|
||
13 years ago
|
<TextView android:id="@+id/labelTextView"
|
||
|
android:layout_width="match_parent"
|
||
|
android:layout_height="wrap_content"
|
||
|
android:text="Description:"
|
||
|
android:textAppearance="?android:attr/textAppearanceLarge"
|
||
|
android:gravity="left">
|
||
|
</TextView>
|
||
|
|
||
|
<EditText android:id="@+id/label"
|
||
|
android:layout_width="match_parent"
|
||
|
android:layout_height="wrap_content"
|
||
13 years ago
|
android:tag="Tag Me" android:inputType="text">
|
||
13 years ago
|
</EditText>
|
||
|
|
||
|
<TextView android:id="@+id/amountLabelTextView"
|
||
|
android:layout_width="match_parent"
|
||
|
android:layout_height="wrap_content"
|
||
|
android:text="Amount:"
|
||
|
android:textAppearance="?android:attr/textAppearanceLarge"
|
||
|
android:gravity="left">
|
||
|
</TextView>
|
||
|
|
||
|
<EditText android:id="@+id/amount"
|
||
|
android:layout_width="match_parent"
|
||
|
android:layout_height="wrap_content"
|
||
13 years ago
|
android:tag="Tag Me" android:inputType="numberDecimal">
|
||
13 years ago
|
</EditText>
|
||
|
|
||
|
<LinearLayout android:layout_width="match_parent"
|
||
|
android:layout_height="wrap_content" android:id="@+id/linearLayout1">
|
||
|
<Button android:id="@+id/buttonPay" android:layout_width="wrap_content"
|
||
|
android:layout_height="wrap_content" android:text="Send"></Button>
|
||
13 years ago
|
</LinearLayout>""",False)
|
||
13 years ago
|
|
||
|
|
||
13 years ago
|
|
||
13 years ago
|
settings_layout = make_layout(""" <ListView
|
||
|
android:id="@+id/myListView"
|
||
|
android:layout_width="match_parent"
|
||
|
android:layout_height="wrap_content" />""")
|
||
13 years ago
|
|
||
|
|
||
|
|
||
13 years ago
|
def get_history_values(n):
|
||
|
values = []
|
||
|
h = wallet.get_tx_history()
|
||
13 years ago
|
length = min(n, len(h))
|
||
|
for i in range(length):
|
||
12 years ago
|
tx_hash, conf, is_mine, value, fee, balance, timestamp = h[-i-1]
|
||
13 years ago
|
try:
|
||
12 years ago
|
dt = datetime.datetime.fromtimestamp( timestamp )
|
||
13 years ago
|
if dt.date() == dt.today().date():
|
||
|
time_str = str( dt.time() )
|
||
|
else:
|
||
|
time_str = str( dt.date() )
|
||
|
except:
|
||
|
time_str = 'pending'
|
||
|
|
||
12 years ago
|
conf_str = 'v' if conf else 'o'
|
||
|
label, is_default_label = wallet.get_label(tx_hash)
|
||
|
values.append((conf_str, ' ' + time_str, ' ' + format_satoshis(value,True), ' ' + label ))
|
||
13 years ago
|
|
||
|
return values
|
||
|
|
||
|
|
||
|
def get_history_layout(n):
|
||
|
rows = ""
|
||
|
i = 0
|
||
|
values = get_history_values(n)
|
||
|
for v in values:
|
||
|
a,b,c,d = v
|
||
13 years ago
|
color = "#ff00ff00" if a == 'v' else "#ffff0000"
|
||
13 years ago
|
rows += """
|
||
|
<TableRow>
|
||
|
<TextView
|
||
|
android:id="@+id/hl_%d_col1"
|
||
|
android:layout_column="0"
|
||
|
android:text="%s"
|
||
|
android:textColor="%s"
|
||
|
android:padding="3" />
|
||
|
<TextView
|
||
|
android:id="@+id/hl_%d_col2"
|
||
|
android:layout_column="1"
|
||
|
android:text="%s"
|
||
|
android:padding="3" />
|
||
|
<TextView
|
||
|
android:id="@+id/hl_%d_col3"
|
||
|
android:layout_column="2"
|
||
|
android:text="%s"
|
||
|
android:padding="3" />
|
||
|
<TextView
|
||
|
android:id="@+id/hl_%d_col4"
|
||
|
android:layout_column="3"
|
||
|
android:text="%s"
|
||
|
android:padding="4" />
|
||
|
</TableRow>"""%(i,a,color,i,b,i,c,i,d)
|
||
|
i += 1
|
||
|
|
||
|
output = """
|
||
|
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||
|
android:layout_width="fill_parent"
|
||
|
android:layout_height="wrap_content"
|
||
|
android:stretchColumns="0,1,2,3">
|
||
|
%s
|
||
|
</TableLayout>"""% rows
|
||
|
return output
|
||
|
|
||
13 years ago
|
|
||
13 years ago
|
def set_history_layout(n):
|
||
|
values = get_history_values(n)
|
||
|
i = 0
|
||
|
for v in values:
|
||
|
a,b,c,d = v
|
||
|
droid.fullSetProperty("hl_%d_col1"%i,"text", a)
|
||
|
|
||
|
if a == 'v':
|
||
13 years ago
|
droid.fullSetProperty("hl_%d_col1"%i, "textColor","#ff00ff00")
|
||
13 years ago
|
else:
|
||
13 years ago
|
droid.fullSetProperty("hl_%d_col1"%i, "textColor","#ffff0000")
|
||
13 years ago
|
|
||
|
droid.fullSetProperty("hl_%d_col2"%i,"text", b)
|
||
|
droid.fullSetProperty("hl_%d_col3"%i,"text", c)
|
||
|
droid.fullSetProperty("hl_%d_col4"%i,"text", d)
|
||
|
i += 1
|
||
|
|
||
13 years ago
|
|
||
13 years ago
|
|
||
|
|
||
13 years ago
|
status_text = ''
|
||
|
def update_layout():
|
||
|
global status_text
|
||
13 years ago
|
if not wallet.interface.is_connected:
|
||
|
text = "Not connected..."
|
||
|
elif not wallet.up_to_date:
|
||
|
text = "Synchronizing..."
|
||
|
else:
|
||
|
c, u = wallet.get_balance()
|
||
|
text = "Balance:"+format_satoshis(c)
|
||
13 years ago
|
if u : text += ' [' + format_satoshis(u,True).strip() + ']'
|
||
13 years ago
|
|
||
|
|
||
13 years ago
|
# vibrate if status changed
|
||
|
if text != status_text:
|
||
|
if status_text and wallet.interface.is_connected and wallet.up_to_date:
|
||
13 years ago
|
droid.vibrate()
|
||
13 years ago
|
status_text = text
|
||
|
|
||
|
droid.fullSetProperty("balanceTextView", "text", status_text)
|
||
|
|
||
|
if wallet.up_to_date:
|
||
13 years ago
|
set_history_layout(15)
|
||
13 years ago
|
|
||
|
|
||
|
|
||
|
|
||
|
def pay_to(recipient, amount, fee, label):
|
||
|
|
||
|
if wallet.use_encryption:
|
||
|
password = droid.dialogGetPassword('Password').result
|
||
13 years ago
|
if not password: return
|
||
13 years ago
|
else:
|
||
|
password = None
|
||
|
|
||
|
droid.dialogCreateSpinnerProgress("Electrum", "signing transaction...")
|
||
|
droid.dialogShow()
|
||
13 years ago
|
|
||
|
try:
|
||
12 years ago
|
tx = wallet.mktx( [(recipient, amount)], password, fee)
|
||
13 years ago
|
except BaseException, e:
|
||
|
modal_dialog('error', e.message)
|
||
13 years ago
|
droid.dialogDismiss()
|
||
13 years ago
|
return
|
||
13 years ago
|
|
||
12 years ago
|
if label:
|
||
12 years ago
|
wallet.labels[tx.hash()] = label
|
||
12 years ago
|
|
||
13 years ago
|
droid.dialogDismiss()
|
||
|
|
||
13 years ago
|
r, h = wallet.sendtx( tx )
|
||
|
if r:
|
||
|
modal_dialog('Payment sent', h)
|
||
|
return True
|
||
13 years ago
|
else:
|
||
13 years ago
|
modal_dialog('Error', h)
|
||
13 years ago
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
13 years ago
|
|
||
13 years ago
|
def make_new_contact():
|
||
|
code = droid.scanBarcode()
|
||
|
r = code.result
|
||
|
if r:
|
||
13 years ago
|
data = r['extras']['SCAN_RESULT']
|
||
|
if data:
|
||
|
if re.match('^bitcoin:', data):
|
||
12 years ago
|
address, _, _, _, _, _, _ = util.parse_url(data)
|
||
12 years ago
|
elif is_valid(data):
|
||
13 years ago
|
address = data
|
||
|
else:
|
||
|
address = None
|
||
|
if address:
|
||
13 years ago
|
if modal_question('Add to contacts?', address):
|
||
12 years ago
|
wallet.add_contact(address)
|
||
13 years ago
|
else:
|
||
13 years ago
|
modal_dialog('Invalid address', data)
|
||
13 years ago
|
|
||
|
|
||
13 years ago
|
do_refresh = False
|
||
|
|
||
|
def update_callback():
|
||
|
global do_refresh
|
||
|
print "gui callback", wallet.interface.is_connected, wallet.up_to_date
|
||
|
do_refresh = True
|
||
|
droid.eventPost("refresh",'z')
|
||
|
|
||
13 years ago
|
def main_loop():
|
||
13 years ago
|
global do_refresh
|
||
|
|
||
13 years ago
|
update_layout()
|
||
13 years ago
|
out = None
|
||
13 years ago
|
quitting = False
|
||
13 years ago
|
while out is None:
|
||
|
|
||
13 years ago
|
event = droid.eventWait(1000).result
|
||
|
if event is None:
|
||
|
if do_refresh:
|
||
|
update_layout()
|
||
|
do_refresh = False
|
||
13 years ago
|
continue
|
||
|
|
||
13 years ago
|
print "got event in main loop", repr(event)
|
||
|
if event == 'OK': continue
|
||
|
if event is None: continue
|
||
12 years ago
|
if not event.get("name"): continue
|
||
13 years ago
|
|
||
13 years ago
|
# request 2 taps before we exit
|
||
|
if event["name"]=="key":
|
||
|
if event["data"]["key"] == '4':
|
||
|
if quitting:
|
||
|
out = 'quit'
|
||
|
else:
|
||
|
quitting = True
|
||
|
else: quitting = False
|
||
|
|
||
13 years ago
|
if event["name"]=="click":
|
||
|
id=event["data"]["id"]
|
||
|
|
||
|
elif event["name"]=="settings":
|
||
|
out = 'settings'
|
||
|
|
||
13 years ago
|
elif event["name"] in menu_commands:
|
||
|
out = event["name"]
|
||
|
|
||
|
if out == 'contacts':
|
||
|
global contact_addr
|
||
|
contact_addr = select_from_contacts()
|
||
|
if contact_addr == 'newcontact':
|
||
|
make_new_contact()
|
||
|
contact_addr = None
|
||
|
if not contact_addr:
|
||
|
out = None
|
||
|
|
||
|
elif out == "receive":
|
||
|
global receive_addr
|
||
|
receive_addr = select_from_addresses()
|
||
13 years ago
|
if receive_addr:
|
||
|
amount = modal_input('Amount', 'Amount you want receive. ', '', "numberDecimal")
|
||
|
if amount:
|
||
|
receive_addr = 'bitcoin:%s?amount=%s'%(receive_addr, amount)
|
||
13 years ago
|
|
||
13 years ago
|
if not receive_addr:
|
||
|
out = None
|
||
|
|
||
13 years ago
|
|
||
|
return out
|
||
|
|
||
13 years ago
|
|
||
13 years ago
|
def payto_loop():
|
||
13 years ago
|
global recipient
|
||
|
if recipient:
|
||
|
droid.fullSetProperty("recipient","text",recipient)
|
||
|
recipient = None
|
||
|
|
||
13 years ago
|
out = None
|
||
|
while out is None:
|
||
|
event = droid.eventWait().result
|
||
12 years ago
|
if not event: continue
|
||
13 years ago
|
print "got event in payto loop", event
|
||
12 years ago
|
if event == 'OK': continue
|
||
12 years ago
|
if not event.get("name"): continue
|
||
13 years ago
|
|
||
|
if event["name"] == "click":
|
||
|
id = event["data"]["id"]
|
||
|
|
||
|
if id=="buttonPay":
|
||
|
|
||
|
droid.fullQuery()
|
||
|
recipient = droid.fullQueryDetail("recipient").result.get('text')
|
||
|
label = droid.fullQueryDetail("label").result.get('text')
|
||
|
amount = droid.fullQueryDetail('amount').result.get('text')
|
||
13 years ago
|
|
||
12 years ago
|
if not is_valid(recipient):
|
||
13 years ago
|
modal_dialog('Error','Invalid Bitcoin address')
|
||
|
continue
|
||
|
|
||
13 years ago
|
try:
|
||
|
amount = int( 100000000 * Decimal(amount) )
|
||
|
except:
|
||
13 years ago
|
modal_dialog('Error','Invalid amount')
|
||
13 years ago
|
continue
|
||
|
|
||
13 years ago
|
result = pay_to(recipient, amount, wallet.fee, label)
|
||
13 years ago
|
if result:
|
||
|
out = 'main'
|
||
13 years ago
|
|
||
|
elif id=="buttonContacts":
|
||
13 years ago
|
addr = select_from_contacts()
|
||
13 years ago
|
droid.fullSetProperty("recipient","text",addr)
|
||
13 years ago
|
|
||
|
elif id=="buttonQR":
|
||
|
code = droid.scanBarcode()
|
||
|
r = code.result
|
||
|
if r:
|
||
13 years ago
|
data = r['extras']['SCAN_RESULT']
|
||
|
if data:
|
||
|
if re.match('^bitcoin:', data):
|
||
12 years ago
|
payto, amount, label, _, _, _, _ = util.parse_url(data)
|
||
13 years ago
|
droid.fullSetProperty("recipient", "text",payto)
|
||
|
droid.fullSetProperty("amount", "text", amount)
|
||
|
droid.fullSetProperty("label", "text", label)
|
||
|
else:
|
||
|
droid.fullSetProperty("recipient", "text", data)
|
||
|
|
||
13 years ago
|
|
||
13 years ago
|
elif event["name"] in menu_commands:
|
||
|
out = event["name"]
|
||
13 years ago
|
|
||
13 years ago
|
elif event["name"]=="key":
|
||
|
if event["data"]["key"] == '4':
|
||
|
out = 'main'
|
||
|
|
||
13 years ago
|
#elif event["name"]=="screen":
|
||
|
# if event["data"]=="destroy":
|
||
|
# out = 'main'
|
||
|
|
||
|
return out
|
||
|
|
||
|
|
||
13 years ago
|
receive_addr = ''
|
||
|
contact_addr = ''
|
||
13 years ago
|
recipient = ''
|
||
13 years ago
|
|
||
|
def receive_loop():
|
||
13 years ago
|
out = None
|
||
|
while out is None:
|
||
|
event = droid.eventWait().result
|
||
13 years ago
|
print "got event", event
|
||
13 years ago
|
if event["name"]=="key":
|
||
|
if event["data"]["key"] == '4':
|
||
|
out = 'main'
|
||
|
|
||
13 years ago
|
elif event["name"]=="clipboard":
|
||
|
droid.setClipboard(receive_addr)
|
||
|
modal_dialog('Address copied to clipboard',receive_addr)
|
||
|
|
||
13 years ago
|
elif event["name"]=="edit":
|
||
|
edit_label(receive_addr)
|
||
|
|
||
13 years ago
|
return out
|
||
|
|
||
|
def contacts_loop():
|
||
13 years ago
|
global recipient
|
||
13 years ago
|
out = None
|
||
|
while out is None:
|
||
|
event = droid.eventWait().result
|
||
|
print "got event", event
|
||
13 years ago
|
if event["name"]=="key":
|
||
|
if event["data"]["key"] == '4':
|
||
|
out = 'main'
|
||
|
|
||
13 years ago
|
elif event["name"]=="clipboard":
|
||
|
droid.setClipboard(contact_addr)
|
||
|
modal_dialog('Address copied to clipboard',contact_addr)
|
||
|
|
||
13 years ago
|
elif event["name"]=="edit":
|
||
|
edit_label(contact_addr)
|
||
|
|
||
13 years ago
|
elif event["name"]=="paytocontact":
|
||
|
recipient = contact_addr
|
||
|
out = 'send'
|
||
|
|
||
|
elif event["name"]=="deletecontact":
|
||
|
if modal_question('delete contact', contact_addr):
|
||
|
out = 'main'
|
||
|
|
||
13 years ago
|
return out
|
||
|
|
||
13 years ago
|
|
||
12 years ago
|
def server_dialog(servers):
|
||
13 years ago
|
droid.dialogCreateAlert("Public servers")
|
||
12 years ago
|
droid.dialogSetItems( servers.keys() )
|
||
13 years ago
|
droid.dialogSetPositiveButtonText('Private server')
|
||
13 years ago
|
droid.dialogShow()
|
||
13 years ago
|
response = droid.dialogGetResponse().result
|
||
13 years ago
|
droid.dialogDismiss()
|
||
12 years ago
|
if not response: return
|
||
13 years ago
|
|
||
|
if response.get('which') == 'positive':
|
||
|
return modal_input('Private server', None)
|
||
|
|
||
|
i = response.get('item')
|
||
13 years ago
|
if i is not None:
|
||
12 years ago
|
response = servers.keys()[i]
|
||
13 years ago
|
return response
|
||
|
|
||
13 years ago
|
|
||
13 years ago
|
def seed_dialog():
|
||
|
if wallet.use_encryption:
|
||
13 years ago
|
password = droid.dialogGetPassword('Seed').result
|
||
13 years ago
|
if not password: return
|
||
|
else:
|
||
|
password = None
|
||
|
|
||
|
try:
|
||
12 years ago
|
seed = wallet.decode_seed(password)
|
||
13 years ago
|
except:
|
||
|
modal_dialog('error','incorrect password')
|
||
|
return
|
||
|
|
||
|
modal_dialog('Your seed is',seed)
|
||
12 years ago
|
modal_dialog('Mnemonic code:', ' '.join(mnemonic_encode(seed)) )
|
||
13 years ago
|
|
||
|
def change_password_dialog():
|
||
|
if wallet.use_encryption:
|
||
13 years ago
|
password = droid.dialogGetPassword('Your wallet is encrypted').result
|
||
|
if password is None: return
|
||
13 years ago
|
else:
|
||
|
password = None
|
||
|
|
||
|
try:
|
||
12 years ago
|
seed = wallet.decode_seed(password)
|
||
13 years ago
|
except:
|
||
|
modal_dialog('error','incorrect password')
|
||
|
return
|
||
|
|
||
|
new_password = droid.dialogGetPassword('Choose a password').result
|
||
13 years ago
|
if new_password == None:
|
||
13 years ago
|
return
|
||
|
|
||
13 years ago
|
if new_password != '':
|
||
|
password2 = droid.dialogGetPassword('Confirm new password').result
|
||
|
if new_password != password2:
|
||
|
modal_dialog('error','passwords do not match')
|
||
|
return
|
||
|
|
||
13 years ago
|
wallet.update_password(seed, password, new_password)
|
||
13 years ago
|
if new_password:
|
||
13 years ago
|
modal_dialog('Password updated','your wallet is encrypted')
|
||
|
else:
|
||
13 years ago
|
modal_dialog('No password','your wallet is not encrypted')
|
||
13 years ago
|
return True
|
||
|
|
||
13 years ago
|
|
||
|
def settings_loop():
|
||
13 years ago
|
|
||
|
|
||
|
def set_listview():
|
||
12 years ago
|
server, port, p = interface.server.split(':')
|
||
13 years ago
|
fee = str( Decimal( wallet.fee)/100000000 )
|
||
|
is_encrypted = 'yes' if wallet.use_encryption else 'no'
|
||
|
protocol = protocol_name(p)
|
||
|
droid.fullShow(settings_layout)
|
||
13 years ago
|
droid.fullSetList("myListView",['Server: ' + server, 'Protocol: '+ protocol, 'Port: '+port, 'Transaction fee: '+fee, 'Password: '+is_encrypted, 'Seed'])
|
||
13 years ago
|
|
||
|
set_listview()
|
||
13 years ago
|
|
||
|
out = None
|
||
|
while out is None:
|
||
12 years ago
|
event = droid.eventWait()
|
||
|
event = event.result
|
||
13 years ago
|
print "got event", event
|
||
13 years ago
|
if event == 'OK': continue
|
||
|
if not event: continue
|
||
13 years ago
|
|
||
12 years ago
|
servers = interface.get_servers()
|
||
12 years ago
|
name = event.get("name")
|
||
|
if not name: continue
|
||
13 years ago
|
|
||
12 years ago
|
if name == "itemclick":
|
||
13 years ago
|
pos = event["data"]["position"]
|
||
12 years ago
|
host, port, protocol = interface.server.split(':')
|
||
13 years ago
|
|
||
13 years ago
|
if pos == "0": #server
|
||
12 years ago
|
host = server_dialog(servers)
|
||
13 years ago
|
if host:
|
||
12 years ago
|
p = servers[host]
|
||
13 years ago
|
port = p['t']
|
||
|
srv = host + ':' + port + ':t'
|
||
12 years ago
|
wallet.config.set_key("server", srv, True)
|
||
13 years ago
|
try:
|
||
12 years ago
|
wallet.interface.set_server(srv)
|
||
13 years ago
|
except:
|
||
|
modal_dialog('error','invalid server')
|
||
|
set_listview()
|
||
13 years ago
|
|
||
13 years ago
|
elif pos == "1": #protocol
|
||
12 years ago
|
if host in servers:
|
||
|
srv = protocol_dialog(host, protocol, servers[host])
|
||
13 years ago
|
if srv:
|
||
12 years ago
|
wallet.config.set_key("server", srv, True)
|
||
13 years ago
|
try:
|
||
12 years ago
|
wallet.interface.set_server(srv)
|
||
13 years ago
|
except:
|
||
13 years ago
|
modal_dialog('error','invalid server')
|
||
13 years ago
|
set_listview()
|
||
|
|
||
13 years ago
|
elif pos == "2": #port
|
||
|
a_port = modal_input('Port number', 'If you use a public server, this field is set automatically when you set the protocol', port, "number")
|
||
|
if a_port:
|
||
|
if a_port != port:
|
||
|
srv = host + ':' + a_port + ':'+ protocol
|
||
12 years ago
|
wallet.config.set_key("server", srv, True)
|
||
13 years ago
|
try:
|
||
12 years ago
|
wallet.interface.set_server(srv)
|
||
13 years ago
|
except:
|
||
13 years ago
|
modal_dialog('error','invalid port number')
|
||
13 years ago
|
set_listview()
|
||
|
|
||
13 years ago
|
elif pos == "3": #fee
|
||
13 years ago
|
fee = modal_input('Transaction fee', 'The fee will be this amount multiplied by the number of inputs in your transaction. ', str( Decimal( wallet.fee)/100000000 ), "numberDecimal")
|
||
13 years ago
|
if fee:
|
||
|
try:
|
||
|
fee = int( 100000000 * Decimal(fee) )
|
||
|
except:
|
||
|
modal_dialog('error','invalid fee value')
|
||
12 years ago
|
wallet.set_fee(fee)
|
||
|
set_listview()
|
||
|
|
||
13 years ago
|
elif pos == "4":
|
||
13 years ago
|
if change_password_dialog():
|
||
|
set_listview()
|
||
13 years ago
|
|
||
13 years ago
|
elif pos == "5":
|
||
13 years ago
|
seed_dialog()
|
||
13 years ago
|
|
||
13 years ago
|
|
||
12 years ago
|
elif name in menu_commands:
|
||
13 years ago
|
out = event["name"]
|
||
13 years ago
|
|
||
12 years ago
|
elif name == 'cancel':
|
||
13 years ago
|
out = 'main'
|
||
13 years ago
|
|
||
12 years ago
|
elif name == "key":
|
||
13 years ago
|
if event["data"]["key"] == '4':
|
||
|
out = 'main'
|
||
|
|
||
|
return out
|
||
|
|
||
13 years ago
|
def add_menu(s):
|
||
|
droid.clearOptionsMenu()
|
||
|
if s == 'main':
|
||
|
droid.addOptionsMenuItem("Send","send",None,"")
|
||
|
droid.addOptionsMenuItem("Receive","receive",None,"")
|
||
|
droid.addOptionsMenuItem("Contacts","contacts",None,"")
|
||
|
droid.addOptionsMenuItem("Settings","settings",None,"")
|
||
13 years ago
|
elif s == 'receive':
|
||
13 years ago
|
droid.addOptionsMenuItem("Copy","clipboard",None,"")
|
||
|
droid.addOptionsMenuItem("Label","edit",None,"")
|
||
13 years ago
|
elif s == 'contacts':
|
||
13 years ago
|
droid.addOptionsMenuItem("Copy","clipboard",None,"")
|
||
|
droid.addOptionsMenuItem("Label","edit",None,"")
|
||
13 years ago
|
droid.addOptionsMenuItem("Pay to","paytocontact",None,"")
|
||
13 years ago
|
#droid.addOptionsMenuItem("Delete","deletecontact",None,"")
|
||
13 years ago
|
|
||
12 years ago
|
|
||
13 years ago
|
def make_bitmap(addr):
|
||
13 years ago
|
# fixme: this is highly inefficient
|
||
13 years ago
|
droid.dialogCreateSpinnerProgress("please wait")
|
||
|
droid.dialogShow()
|
||
13 years ago
|
try:
|
||
|
import pyqrnative, bmp
|
||
|
qr = pyqrnative.QRCode(4, pyqrnative.QRErrorCorrectLevel.L)
|
||
|
qr.addData(addr)
|
||
|
qr.make()
|
||
|
k = qr.getModuleCount()
|
||
|
assert k == 33
|
||
|
bmp.save_qrcode(qr,"/sdcard/sl4a/qrcode.bmp")
|
||
|
finally:
|
||
|
droid.dialogDismiss()
|
||
13 years ago
|
|
||
13 years ago
|
|
||
13 years ago
|
|
||
13 years ago
|
|
||
12 years ago
|
droid = android.Android()
|
||
|
menu_commands = ["send", "receive", "settings", "contacts", "main"]
|
||
|
wallet = None
|
||
|
interface = None
|
||
|
|
||
|
class ElectrumGui:
|
||
|
|
||
|
def __init__(self, w, config, app=None):
|
||
|
global wallet, interface
|
||
|
wallet = w
|
||
|
interface = wallet.interface
|
||
|
interface.register_callback('updated',update_callback)
|
||
|
interface.register_callback('connected', update_callback)
|
||
|
interface.register_callback('disconnected', update_callback)
|
||
|
interface.register_callback('disconnecting', update_callback)
|
||
|
|
||
|
|
||
|
def main(self, url):
|
||
|
s = 'main'
|
||
|
while True:
|
||
|
add_menu(s)
|
||
|
if s == 'main':
|
||
|
droid.fullShow(main_layout())
|
||
|
s = main_loop()
|
||
|
|
||
|
elif s == 'send':
|
||
|
droid.fullShow(payto_layout)
|
||
|
s = payto_loop()
|
||
|
|
||
|
elif s == 'receive':
|
||
|
make_bitmap(receive_addr)
|
||
|
droid.fullShow(qr_layout(receive_addr))
|
||
|
s = receive_loop()
|
||
|
|
||
|
elif s == 'contacts':
|
||
|
make_bitmap(contact_addr)
|
||
|
droid.fullShow(qr_layout(contact_addr))
|
||
|
s = contacts_loop()
|
||
|
|
||
|
elif s == 'settings':
|
||
|
s = settings_loop()
|
||
13 years ago
|
|
||
12 years ago
|
else:
|
||
|
break
|
||
13 years ago
|
|
||
12 years ago
|
droid.makeToast("Bye!")
|
||
12 years ago
|
|
||
|
def restore_or_create(self):
|
||
|
droid.dialogCreateAlert("Wallet not found","Do you want to create a new wallet, or restore an existing one?")
|
||
|
droid.dialogSetPositiveButtonText('Create')
|
||
|
droid.dialogSetNeutralButtonText('Restore')
|
||
|
droid.dialogSetNegativeButtonText('Cancel')
|
||
|
droid.dialogShow()
|
||
|
response = droid.dialogGetResponse().result
|
||
|
droid.dialogDismiss()
|
||
|
if not response: return
|
||
|
if response.get('which') == 'negative':
|
||
|
return
|
||
|
|
||
|
return 'restore' if response.get('which') == 'neutral' else 'create'
|
||
|
|
||
|
def seed_dialog(self):
|
||
|
if modal_question("Input method",None,'QR Code', 'mnemonic'):
|
||
|
code = droid.scanBarcode()
|
||
|
r = code.result
|
||
|
if r:
|
||
|
seed = r['extras']['SCAN_RESULT']
|
||
|
else:
|
||
|
return
|
||
|
else:
|
||
|
m = modal_input('Mnemonic','please enter your code')
|
||
|
try:
|
||
|
seed = mnemonic_decode(m.split(' '))
|
||
|
except:
|
||
|
modal_dialog('error: could not decode this seed')
|
||
|
return
|
||
12 years ago
|
|
||
|
gap = 5 # default
|
||
|
|
||
|
return str(seed), gap
|
||
12 years ago
|
|
||
|
|
||
|
def network_dialog(self):
|
||
12 years ago
|
return True
|
||
12 years ago
|
|
||
12 years ago
|
def verify_seed(self):
|
||
12 years ago
|
wallet.save_seed()
|
||
12 years ago
|
return True
|
||
12 years ago
|
|
||
12 years ago
|
def show_seed(self):
|
||
12 years ago
|
modal_dialog('Your seed is:', wallet.seed)
|
||
|
modal_dialog('Mnemonic code:', ' '.join(mnemonic_encode(wallet.seed)) )
|
||
12 years ago
|
|
||
|
|
||
|
def password_dialog(self):
|
||
12 years ago
|
change_password_dialog()
|
||
|
|
||
|
|
||
|
def restore_wallet(self):
|
||
|
|
||
|
msg = "recovering wallet..."
|
||
|
droid.dialogCreateSpinnerProgress("Electrum", msg)
|
||
|
droid.dialogShow()
|
||
|
|
||
12 years ago
|
wallet.update()
|
||
12 years ago
|
|
||
|
droid.dialogDismiss()
|
||
|
droid.vibrate()
|
||
|
|
||
|
if wallet.is_found():
|
||
|
wallet.fill_addressbook()
|
||
|
modal_dialog("recovery successful")
|
||
|
else:
|
||
|
if not modal_question("no transactions found for this seed","do you want to keep this wallet?"):
|
||
12 years ago
|
return False
|
||
12 years ago
|
|
||
|
wallet.save()
|
||
12 years ago
|
return True
|
||
12 years ago
|
|