Skip to content

Commit

Permalink
Testing version, shorter time
Browse files Browse the repository at this point in the history
  • Loading branch information
KarolTrzeszczkowski committed May 5, 2019
1 parent 87bbb46 commit d5fc860
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 41 deletions.
1 change: 0 additions & 1 deletion last-will-plugin/contract_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def extract_contract_data(wallet, tx, utxo):
return contracts

def remove_duplicates(contracts):
print("checking for duplicates")
for c1, c2 in combinations(contracts,2):
if c1[1].address == c2[1].address:
contracts = contracts.remove(c1)
Expand Down
29 changes: 16 additions & 13 deletions last-will-plugin/last_will_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
UTXO=0
CONTRACT=1
MODE=2
INHERITACNE_TIME=int((0.5*3600*24))
REFRESH_LOCK_TIME=int(((2/24)*3600*24))


def joinbytes(iterable):
"""Joins an iterable of bytes and/or integers into a single byte string"""
return b''.join((bytes((x,)) if isinstance(x,int) else x) for x in iterable)
Expand All @@ -20,14 +24,14 @@ class LastWillContract:

def __init__(self, addresses, initial_tx=None):
self.initial_tx=initial_tx
self.time1 = (180*3600*24)//512
self.time_bytes = (self.time1).to_bytes(2,'little')
self.time2= (7*3600*24)//512
self.time2_bytes = (self.time2).to_bytes(2,'little')
self.i_time = INHERITACNE_TIME // 512
self.i_time_bytes = (self.i_time).to_bytes(2, 'little')
self.rl_time= REFRESH_LOCK_TIME // 512
self.rl_time_bytes = (self.rl_time).to_bytes(2, 'little')
self.addresses=addresses

assert len(self.time2_bytes)==2
assert len(self.time_bytes)==2
assert len(self.rl_time_bytes) == 2
assert len(self.i_time_bytes) == 2


self.redeemscript = joinbytes([
Expand All @@ -42,7 +46,7 @@ def __init__(self, addresses, initial_tx=None):
Op.OP_CAT, Op.OP_5, Op.OP_PICK, Op.OP_CAT, Op.OP_12, Op.OP_PICK, Op.OP_SIZE, Op.OP_1SUB, Op.OP_SPLIT,
Op.OP_DROP, Op.OP_OVER, Op.OP_SHA256, Op.OP_15, Op.OP_PICK, Op.OP_CHECKDATASIGVERIFY, 2, 232, 3, Op.OP_9,
Op.OP_PICK, Op.OP_BIN2NUM, Op.OP_OVER, Op.OP_SUB, Op.OP_8, Op.OP_NUM2BIN, 1, 135, 1, 169, 1, 20, 1, 23,
Op.OP_15, Op.OP_PICK, Op.OP_TRUE, Op.OP_SPLIT, Op.OP_NIP, 3, self.time2_bytes, 64, Op.OP_CHECKSEQUENCEVERIFY,
Op.OP_15, Op.OP_PICK, Op.OP_TRUE, Op.OP_SPLIT, Op.OP_NIP, 3, self.rl_time_bytes, 64, Op.OP_CHECKSEQUENCEVERIFY,
Op.OP_DROP, 1, 18, Op.OP_PICK, Op.OP_BIN2NUM, Op.OP_2, Op.OP_GREATERTHANOREQUAL, Op.OP_VERIFY, Op.OP_5,
Op.OP_PICK, Op.OP_2, Op.OP_PICK, Op.OP_CAT, Op.OP_4, Op.OP_PICK, Op.OP_CAT, Op.OP_3, Op.OP_PICK, Op.OP_CAT,
Op.OP_OVER, Op.OP_HASH160, Op.OP_CAT, Op.OP_5, Op.OP_PICK, Op.OP_CAT, Op.OP_HASH256, Op.OP_14, Op.OP_PICK,
Expand All @@ -57,7 +61,7 @@ def __init__(self, addresses, initial_tx=None):
Op.OP_ELSE,
Op.OP_3, Op.OP_PICK, Op.OP_3, Op.OP_EQUAL,
Op.OP_IF,
3, self.time_bytes, 64, Op.OP_CHECKSEQUENCEVERIFY, Op.OP_DROP, Op.OP_5, Op.OP_PICK, Op.OP_HASH160, Op.OP_OVER,
3, self.i_time_bytes, 64, Op.OP_CHECKSEQUENCEVERIFY, Op.OP_DROP, Op.OP_5, Op.OP_PICK, Op.OP_HASH160, Op.OP_OVER,
Op.OP_EQUALVERIFY, Op.OP_4, Op.OP_PICK, Op.OP_6, Op.OP_PICK, Op.OP_CHECKSIG, Op.OP_NIP, Op.OP_NIP,
Op.OP_NIP, Op.OP_NIP, Op.OP_NIP, Op.OP_NIP,
Op.OP_ELSE,
Expand Down Expand Up @@ -86,9 +90,9 @@ def __init__(self, contracts, keypairs, public_keys, wallet):
self.wallet = wallet
self.dummy_scriptsig = '00'*(110 + len(self.contract.redeemscript))
if self.mode == 0:
self.sequence=2**22+self.contract.time2
self.sequence=2**22+self.contract.i_time
elif self.mode == 2:
self.sequence=2**22+self.contract.time1
self.sequence=2**22+self.contract.rl_time
else:
self.sequence = 0
self.value = int(self.tx.get('value'))
Expand All @@ -102,9 +106,9 @@ def choice(self, contract, utxo_index, m):
self.contract_index = self.contracts.index(contract)
self.mode = m
if self.mode == 0:
self.sequence=2**22+self.contract.time2
self.sequence=2**22+self.contract.rl_time
elif self.mode == 2:
self.sequence=2**22+self.contract.time1
self.sequence=2**22+self.contract.i_time
else:
self.sequence = 0
utxo = contract[UTXO][utxo_index]
Expand All @@ -125,7 +129,6 @@ def choice(self, contract, utxo_index, m):
value=int(u.get('value')),
))
else:
print("selected utxo")
self.value = int(utxo.get('value'))
self.txin = [dict(
prevout_hash=utxo.get('tx_hash'),
Expand Down
1 change: 0 additions & 1 deletion last-will-plugin/notification_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def __init__(self, parent):
self.disabled=True

def do_anything(self):
print((not self.disabled) and (self.notify_me.isChecked() or self.notify_inheritor.isChecked()))
return (not self.disabled) and (self.notify_me.isChecked() or self.notify_inheritor.isChecked())


Expand Down
67 changes: 41 additions & 26 deletions last-will-plugin/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ def __init__(self, parent, plugin, wallet_name, password, manager=None):
vbox.addWidget(l)
l = QLabel("<b>%s</b>"%(_("Manage my Last Will:")))
vbox.addWidget(l)

vbox.addLayout(hbox)
b = QPushButton(_("Create new Last Will contract"))
b.clicked.connect(lambda: self.plugin.switch_to(Create, self.wallet_name, None, self.manager))
hbox.addWidget(b)
b = QPushButton(_("Find existing Last Will contract"))
b.clicked.connect(self.handle_finding)
hbox.addWidget(b)
Expand All @@ -54,14 +58,11 @@ def __init__(self, parent, plugin, wallet_name, password, manager=None):
vbox.addStretch(1)

def handle_finding(self):
try:
self.contracts = find_contract(self.wallet)
# time.sleep(3)
except Exception as ex:
print(ex)
print("No contract")
else:
self.contracts = find_contract(self.wallet)
if len(self.contracts):
self.start_manager()
else:
self.show_error("You are not a party in any contract yet.")

def load(self):
fileName = self.main_window.getOpenFileName("Load Last Will Contract Info", "*.json")
Expand Down Expand Up @@ -121,6 +122,7 @@ class Create(QDialog, MessageBoxMixin):

def __init__(self, parent, plugin, wallet_name, password, manager):
QDialog.__init__(self, parent)
print("Creating")
self.main_window = parent
self.wallet=parent.wallet
self.plugin = plugin
Expand Down Expand Up @@ -151,10 +153,18 @@ def __init__(self, parent, plugin, wallet_name, password, manager):

vbox = QVBoxLayout()
self.setLayout(vbox)
hbox = QHBoxLayout()
vbox.addLayout(hbox)
l = QLabel("<b>%s</b>" % (_("Creatin Last Will contract:")))
hbox.addWidget(l)
hbox.addStretch(1)
b = QPushButton(_("Home"))
b.clicked.connect(lambda: self.plugin.switch_to(Intro, self.wallet_name, None, None))
hbox.addWidget(b)
l = QLabel(_("Refreshing address") + ": auto (this wallet)") # self.refreshing_address.to_ui_string())
vbox.addWidget(l)
l = QLabel(_("Refreshing address") + ": auto (this wallet)") #self.refreshing_address.to_ui_string())
vbox.addWidget(l)



grid = QGridLayout()
vbox.addLayout(grid)
Expand Down Expand Up @@ -286,12 +296,12 @@ def get_selected_id(self):
def on_update(self):
if len(self.contracts) == 1 and len(self.contracts[0][UTXO])==1:
x = self.contracts[0][UTXO][0]
item = self.add_item(x, self, self.contracts[0])
item = self.add_item(x, self, self.contracts[0],self.contracts[0][MODE][0])
self.setCurrentItem(item)
else:
for c in self.contracts:
for m in c[MODE]:
contract = QTreeWidgetItem([c[1].address.to_ui_string(),'','','',role_name(m)])
contract = QTreeWidgetItem([c[CONTRACT].address.to_ui_string(),'','','',role_name(m)])
contract.setData(1, Qt.UserRole, c)
contract.setData(2,Qt.UserRole, m)
self.addChild(contract)
Expand All @@ -301,46 +311,50 @@ def on_update(self):


def add_item(self, x, parent_item, c, m):
expiration = self.estimate_expiration(x)
lock = self.refresh_lock(x)
expiration = self.estimate_expiration(x,c)
lock = self.refresh_lock(x,c)
amount = self.parent.format_amount(x.get('value'), is_diff=False, whitespaces=True)
mode = role_name(m)
utxo_item = SortableTreeWidgetItem([x['tx_hash'][:10]+'...', expiration, lock, amount, mode])

utxo_item.setData(0, Qt.UserRole, x)
utxo_item.setData(1, Qt.UserRole, c)
utxo_item.setData(2, Qt.UserRole, m)
parent_item.addChild(utxo_item)

return utxo_item


def get_age(self, entry):
txHeight = entry.get("height")
currentHeight=self.main_window.network.get_local_height()
age = ceil((currentHeight-txHeight)/144)
age = (currentHeight-txHeight)//6
return age

def estimate_expiration(self, entry):
def estimate_expiration(self, entry, contr):
"""estimates age of the utxo in days. There are 144 blocks per day on average"""
txHeight = entry.get("height")
age = self.get_age(entry)
contract_i_time=ceil((contr[CONTRACT].i_time*512)/(3600))
print("age, contract itime")
print(age, contract_i_time)
if txHeight==0 :
label = _("Waiting for confirmation.")
elif (180-age) > 0:
label = str(180-age) +" days"
elif (contract_i_time-age) >= 0:
label = '{0:.2f}'.format((contract_i_time - age)/24) +" days"
else :
label = _("Last Will is ready to be inherited.")
return label

def refresh_lock(self,entry):
def refresh_lock(self,entry, contr):
"""Contract can be refreshed only when it's one week old"""
txHeight = entry.get("height")
age = self.get_age(entry)
age = int(self.get_age(entry))
contract_rl_time = ceil((contr[CONTRACT].rl_time*512)/3600)
# print("Age: " +str(age) + " Height: "+str(txHeight))
if txHeight==0 :
label = str(7) + _(" days")
elif (7-age) > 0:
label = str(8-age) + _(" days" )
label = str(contract_rl_time) + _(" h")
elif (contract_rl_time - age) >= 0:
label = str(contract_rl_time-age) + _(" h" )
else :
label = _("Ready for refreshing")
return label
Expand All @@ -361,20 +375,20 @@ def __init__(self, parent, plugin, wallet_name, password, manager):
self.setLayout(vbox)
self.fee=1000
self.contract_tree = contractTree(self.main_window, self.manager.contracts)


self.contract_tree.on_update()
vbox.addWidget(self.contract_tree)
hbox = QHBoxLayout()
hbox.addStretch(1)
vbox.addLayout(hbox)
b = QPushButton(_("Home"))
b.clicked.connect(lambda: self.plugin.switch_to(Intro, self.wallet_name, None, None))
hbox.addWidget(b)
b = QPushButton(_("Create new Last Will contract"))
b.clicked.connect(lambda: self.plugin.switch_to(Create, self.wallet_name, None, self.manager))
hbox.addWidget(b)
b = QPushButton(_("Export"))
b.clicked.connect(self.export)
hbox.addWidget(b)

self.refresh_label = _("Refresh")
self.inherit_label = _("Inherit")
self.end_label = _("End")
Expand All @@ -388,6 +402,7 @@ def __init__(self, parent, plugin, wallet_name, password, manager):
self.update_button()



def update_button(self):
contract, utxo_index, m = self.contract_tree.get_selected_id()
self.manager.choice(contract, utxo_index, m)
Expand Down

0 comments on commit d5fc860

Please sign in to comment.