From 16eb50a29114a3726d2e5c16e0c84bf0f5fcc9ef Mon Sep 17 00:00:00 2001 From: Lex Berezhny Date: Mon, 4 Apr 2022 23:42:30 -0400 Subject: [PATCH] working jsonrpc_account_deposit --- lbry/wallet/script.py | 6 +++++- lbry/wallet/transaction.py | 15 +++++++++++---- .../blockchain/test_account_commands.py | 19 ++++++++++++++----- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/lbry/wallet/script.py b/lbry/wallet/script.py index 5a39359be..5f9869698 100644 --- a/lbry/wallet/script.py +++ b/lbry/wallet/script.py @@ -277,7 +277,7 @@ class Template: elif isinstance(opcode, PUSH_INTEGER): data = values[opcode.name] source.write_many(push_data( - data.to_bytes((data.bit_length() + 7) // 8, byteorder='little') + data.to_bytes((data.bit_length() + 8) // 8, byteorder='little', signed=True) )) elif isinstance(opcode, PUSH_SUBSCRIPT): data = values[opcode.name] @@ -417,6 +417,10 @@ class InputScript(Script): 'script': script }) + @property + def is_script_hash(self): + return self.template.name.startswith('script_hash+') + class OutputScript(Script): diff --git a/lbry/wallet/transaction.py b/lbry/wallet/transaction.py index fdc225a06..7b81661b9 100644 --- a/lbry/wallet/transaction.py +++ b/lbry/wallet/transaction.py @@ -718,8 +718,11 @@ class Transaction: stream.write_compact_size(len(self._inputs)) for i, txin in enumerate(self._inputs): if signing_input == i: - assert txin.txo_ref.txo is not None - txin.serialize_to(stream, txin.txo_ref.txo.script.source) + if txin.script.is_script_hash: + txin.serialize_to(stream, txin.script.values['script'].source) + else: + assert txin.txo_ref.txo is not None + txin.serialize_to(stream, txin.txo_ref.txo.script.source) else: txin.serialize_to(stream, b'') self._serialize_outputs(stream) @@ -949,9 +952,13 @@ class Transaction: return cls.create([], [payment, data], funding_accounts, change_account) @classmethod - def spend_time_lock(cls, time_locked_txo: Output, script: bytes, account: 'Account'): + async def spend_time_lock(cls, time_locked_txo: Output, script: bytes, account: 'Account'): txi = Input.spend_time_lock(time_locked_txo, script) - return cls.create([txi], [], [account], account, sign=False) + txi.sequence = 0xFFFFFFFE + tx = await cls.create([txi], [], [account], account, sign=False) + tx.locktime = txi.script.values['script'].values['height'] + tx._reset() + return tx @property def my_inputs(self): diff --git a/tests/integration/blockchain/test_account_commands.py b/tests/integration/blockchain/test_account_commands.py index ec5295118..27805c9d0 100644 --- a/tests/integration/blockchain/test_account_commands.py +++ b/tests/integration/blockchain/test_account_commands.py @@ -1,8 +1,10 @@ -from binascii import unhexlify +from binascii import hexlify, unhexlify from lbry.testcase import CommandTestCase +from lbry.wallet.script import InputScript from lbry.wallet.dewies import dewies_to_lbc from lbry.wallet.account import DeterministicChannelKeyManager +from lbry.crypto.hash import hash160 from lbry.crypto.base58 import Base58 @@ -293,15 +295,22 @@ class AccountManagement(CommandTestCase): async def test_time_locked_transactions(self): address = await self.account.receiving.get_or_create_usable_address() private_key = await self.ledger.get_private_key_for_address(self.wallet, address) - redeem = await self.blockchain.add_time_locked_address(210, address) + + script = InputScript( + template=InputScript.TIME_LOCK_SCRIPT, + values={'height': 210, 'pubkey_hash': self.ledger.address_to_hash160(address)} + ) + script_address = self.ledger.hash160_to_script_address(hash160(script.source)) + script_source = hexlify(script.source).decode() + await self.assertBalance(self.account, '10.0') - tx = await self.daemon.jsonrpc_account_send('4.0', redeem['address']) + tx = await self.daemon.jsonrpc_account_send('4.0', script_address) await self.confirm_tx(tx.id) await self.generate(510) await self.assertBalance(self.account, '5.999877') tx = await self.daemon.jsonrpc_account_deposit( - tx.id, 0, redeem['redeemScript'], + tx.id, 0, script_source, Base58.encode_check(self.ledger.private_key_to_wif(private_key.private_key_bytes)) ) await self.confirm_tx(tx.id) - await self.assertBalance(self.account, '9.999877') + await self.assertBalance(self.account, '9.9997545')