Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion crypto/transactions/builder/htlc_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ class HtlcLock(BaseTransactionBuilder):

transaction_type = TRANSACTION_HTLC_LOCK

def __init__(self, recipient_id, secret_hash, expiration_type, expiration_value, vendorField=None, fee=None):
def __init__(self, recipient_id, amount, secret_hash, expiration_type, expiration_value, vendorField=None, fee=None):
"""Create a timelock transaction

Args:
recipient_id (str): recipient identifier
amount (int): amount of coins you want to transfer
secret_hash (str): a hash of the secret. The SAME hash must be used in the corresponding “claim” transaction
expiration_type (int): type of the expiration. Either block height or network epoch timestamp based
expiration_value (int): Expiration of transaction in seconds or height depending on expiration_type
Expand All @@ -20,6 +21,11 @@ def __init__(self, recipient_id, secret_hash, expiration_type, expiration_value,
super().__init__()

self.transaction.recipientId = recipient_id

if type(amount) == int and amount > 0:
self.transaction.amount = amount
else:
raise ValueError('Amount is not valid')

self.transaction.typeGroup = self.get_type_group()

Expand Down
42 changes: 42 additions & 0 deletions tests/transactions/builder/test_htlc_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,56 @@
from crypto.constants import TRANSACTION_HTLC_LOCK, TRANSACTION_TYPE_GROUP
from crypto.networks.devnet import Devnet
from crypto.transactions.builder.htlc_lock import HtlcLock
import pytest

set_network(Devnet)


def test_htlc_lock_transation_amount_not_int():
with pytest.raises(ValueError):
"""Test error handling in constructor for non-integer amount
"""
HtlcLock(
recipient_id='AGeYmgbg2LgGxRW2vNNJvQ88PknEJsYizC',
amount='bad amount number',
secret_hash='0f128d401958b1b30ad0d10406f47f9489321017b4614e6cb993fc63913c5454',
expiration_type=1,
expiration_value=1573455822
)


def test_htlc_lock_transation_amount_zero():
with pytest.raises(ValueError):
"""Test error handling in constructor for non-integer amount
"""
HtlcLock(
recipient_id='AGeYmgbg2LgGxRW2vNNJvQ88PknEJsYizC',
amount=0,
secret_hash='0f128d401958b1b30ad0d10406f47f9489321017b4614e6cb993fc63913c5454',
expiration_type=1,
expiration_value=1573455822
)


def test_htlc_lock_transation_amount_negative():
with pytest.raises(ValueError):
"""Test error handling in constructor for non-integer amount
"""
HtlcLock(
recipient_id='AGeYmgbg2LgGxRW2vNNJvQ88PknEJsYizC',
amount=-5,
secret_hash='0f128d401958b1b30ad0d10406f47f9489321017b4614e6cb993fc63913c5454',
expiration_type=1,
expiration_value=1573455822
)


def test_htlc_lock_transaction():
"""Test if timelock transaction gets built
"""
transaction = HtlcLock(
recipient_id='AGeYmgbg2LgGxRW2vNNJvQ88PknEJsYizC',
amount=200000000,
secret_hash='0f128d401958b1b30ad0d10406f47f9489321017b4614e6cb993fc63913c5454',
expiration_type=1,
expiration_value=1573455822
Expand All @@ -22,6 +63,7 @@ def test_htlc_lock_transaction():
transaction_dict = transaction.to_dict()

assert transaction_dict['recipientId'] == 'AGeYmgbg2LgGxRW2vNNJvQ88PknEJsYizC'
assert transaction_dict['amount'] == 200000000
assert transaction_dict['nonce'] == 1
assert transaction_dict['signature']
assert transaction_dict['type'] is TRANSACTION_HTLC_LOCK
Expand Down