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
29 changes: 29 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Lint

on:
push:
branches:
- "master"
- "develop"
pull_request:
types: [ready_for_review, synchronize, opened]

jobs:
flake8:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v7
- name: Set up Python
uses: actions/setup-python@v7
with:
python-version: "3.13"
- name: install dependencies
run: |
python3 -m venv venv
. venv/bin/activate
pip install .[test]
- name: run flake8
run: |
. venv/bin/activate
make lint
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
2. Run the tests to confirm they all pass on your system. If they don’t, you’ll need to investigate why they fail. If you’re unable to diagnose this yourself raise it as a bug report.
3. Make your change.
4. Write tests that demonstrate your bug or feature.
5. Run the entire test suite again, confirming that all tests pass including the ones you just added.
5. Run the entire test suite again, confirming that all tests pass including the ones you just added, and run `make lint` to check for lint errors.
6. Send a GitHub Pull Request. GitHub Pull Requests are the expected method of code collaboration on this project.

If you have any questions, requests or ideas open an issue or ask us in #developers channel on the [ArkEcosystem Discord](https://discord.ark.io/).
Expand Down
1 change: 0 additions & 1 deletion crypto/configuration/fee.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ def set_fee(transaction_type, value):
transaction_type (int): transaction_type for which we wish to set a fee
value (int): fee for a given transaction type
"""
global fees
fees[transaction_type] = value
20 changes: 13 additions & 7 deletions crypto/transactions/builder/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from crypto.identity.public_key import PublicKey
from crypto.schnorr import schnorr
from crypto.transactions.transaction import Transaction
from crypto.utils.message import Message


class BaseTransactionBuilder(object):
Expand Down Expand Up @@ -49,7 +48,7 @@ def second_sign(self, passphrase):
"""
msg = hashlib.sha256(self.transaction.to_bytes(False, True, False)).digest()
secret = unhexlify(PrivateKey.from_passphrase(passphrase).to_hex())
self.transaction.signSignature = hexlify(schnorr.bcrypto410_sign(msg, secret))
self.transaction.signSignature = hexlify(schnorr.bcrypto410_sign(msg, secret))
self.transaction.id = self.transaction.get_id()

def multi_sign(self, passphrase, index):
Expand All @@ -67,7 +66,7 @@ def multi_sign(self, passphrase, index):

def schnorr_verify(self):
return self.transaction.verify_schnorr()

def schnorr_verify_second(self, secondPublicKey):
return self.transaction.verify_schnorr_secondsig(secondPublicKey)

Expand All @@ -84,15 +83,22 @@ def set_sender_public_key(self, public_key):
self.transaction.senderPublicKey = public_key

def set_expiration(self, expiration):
if type(expiration) == int:
if type(expiration) is int:
self.transaction.expiration = expiration
else:
types = {HTLC_LOCK_EXPIRATION_TYPE.EPOCH_TIMESTAMP: 1, HTLC_LOCK_EXPIRATION_TYPE.BLOCK_HEIGHT: 2}
types = {
HTLC_LOCK_EXPIRATION_TYPE.EPOCH_TIMESTAMP: 1,
HTLC_LOCK_EXPIRATION_TYPE.BLOCK_HEIGHT: 2,
}
self.transaction.expiration = types[expiration]

def set_type_group(self, type_group):
if type(type_group) == int:
if type(type_group) is int:
self.transaction.typeGroup = type_group
else:
types = {TRANSACTION_TYPE_GROUP.TEST: 0, TRANSACTION_TYPE_GROUP.CORE: 1, TRANSACTION_TYPE_GROUP.RESERVED: 1000}
types = {
TRANSACTION_TYPE_GROUP.TEST: 0,
TRANSACTION_TYPE_GROUP.CORE: 1,
TRANSACTION_TYPE_GROUP.RESERVED: 1000,
}
self.transaction.typeGroup = types[type_group]
4 changes: 2 additions & 2 deletions crypto/transactions/builder/htlc_claim.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from crypto.constants import TRANSACTION_HTLC_CLAIM, TRANSACTION_TYPE_GROUP
from crypto.exceptions import ArkInvalidTransaction
from crypto.transactions.builder.base import BaseTransactionBuilder


Expand All @@ -12,7 +11,8 @@ def __init__(self, lock_transaction_id, unlock_secret, fee=None):

Args:
lock_transaction_id (str): HTLC lock transaction ID we wish to claim
unlock_secret (str): unlock secret required to claim the transaction, must be 64 chars long
unlock_secret (str): unlock secret required to claim the transaction, must be 64 chars
long
fee (int, optional): fee used for the transaction (default is already set)
"""
super().__init__()
Expand Down
24 changes: 18 additions & 6 deletions crypto/transactions/builder/htlc_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,35 @@ class HtlcLock(BaseTransactionBuilder):

transaction_type = TRANSACTION_HTLC_LOCK

def __init__(self, recipient_id, amount, 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
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
vendorField (str): value for the vendor field aka smartbridge
fee (int, optional): fee used for the transaction (default is already set)
"""
super().__init__()

self.transaction.recipientId = recipient_id
if type(amount) == int and amount > 0:

if type(amount) is int and amount > 0:
self.transaction.amount = amount
else:
raise ValueError('Amount is not valid')
Expand Down
2 changes: 1 addition & 1 deletion crypto/transactions/builder/transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(self, recipientId, amount, vendorField=None, fee=None):

self.transaction.recipientId = recipientId

if type(amount) == int and amount > 0:
if type(amount) is int and amount > 0:
self.transaction.amount = amount
else:
raise ValueError('Amount is not valid')
Expand Down
2 changes: 1 addition & 1 deletion crypto/transactions/deserializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from hashlib import sha256
from importlib import import_module

from binary.unsigned_integer.reader import read_bit8, read_bit16, read_bit32, read_bit64
from binary.unsigned_integer.reader import read_bit16, read_bit32, read_bit64, read_bit8

from crypto.constants import TRANSACTION_TYPES
from crypto.transactions.deserializers.base import BaseDeserializer
Expand Down
5 changes: 3 additions & 2 deletions crypto/transactions/deserializers/htlc_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from base58 import b58encode_check

from binary.unsigned_integer.reader import read_bit8, read_bit32, read_bit64
from binary.unsigned_integer.reader import read_bit32, read_bit64, read_bit8

from crypto.transactions.deserializers.base import BaseDeserializer

Expand All @@ -14,7 +14,8 @@ def deserialize(self):

self.transaction.amount = read_bit64(self.serialized, offset=starting_position)

secret_hash = hexlify(self.serialized)[(starting_position + 8) * 2:(starting_position + 8 + 32) * 2]
secret_hash_start_index = (starting_position + 8) * 2
secret_hash = hexlify(self.serialized)[secret_hash_start_index:secret_hash_start_index + 64]

expiration_type = read_bit8(self.serialized, offset=99)

Expand Down
5 changes: 4 additions & 1 deletion crypto/transactions/deserializers/multi_payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ def deserialize(self):
recipientId = hexlify(self.serialized)[recipient_start_index:recipient_start_index + 42]
recipientId = b58encode_check(unhexlify(recipientId)).decode()

self.transaction.asset['payments'].append({'amount': amount, 'recipientId': recipientId})
self.transaction.asset['payments'].append({
'amount': amount,
'recipientId': recipientId,
})

index += 21 + 8

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ def deserialize(self):
public_key = hexlify(self.serialized)[index_start:index_start + 66].decode()
self.transaction.asset['multiSignature']['publicKeys'].append(public_key)

self.transaction.signatures = [] if self.transaction.signatures is None else self.transaction.signatures
self.transaction.signatures = (
[] if self.transaction.signatures is None else self.transaction.signatures
)

self.transaction.parse_signatures(
hexlify(self.serialized).decode(),
Expand Down
25 changes: 21 additions & 4 deletions crypto/transactions/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from importlib import import_module

from binary.hex.writer import write_high
from binary.unsigned_integer.writer import write_bit8, write_bit16, write_bit32, write_bit64
from binary.unsigned_integer.writer import write_bit16, write_bit32, write_bit64, write_bit8

from crypto.configuration.network import get_network
from crypto.constants import TRANSACTION_TYPES
Expand All @@ -20,7 +20,13 @@ def __init__(self, transaction):
raise ArkSerializerException('No transaction data provided')
self.transaction = transaction

def serialize(self, skip_signature=True, skip_second_signature=True, skip_multi_signature=True, raw=False):
def serialize(
self,
skip_signature=True,
skip_second_signature=True,
skip_multi_signature=True,
raw=False,
):
"""Perform AIP11 compliant serialization

Returns:
Expand Down Expand Up @@ -51,7 +57,12 @@ def serialize(self, skip_signature=True, skip_second_signature=True, skip_multi_
else:
bytes_data += write_bit8(0x00)
bytes_data = self._handle_transaction_type(bytes_data)
bytes_data = self._handle_signature(bytes_data, skip_signature, skip_second_signature, skip_multi_signature)
bytes_data = self._handle_signature(
bytes_data,
skip_signature,
skip_second_signature,
skip_multi_signature,
)

return bytes_data if raw else hexlify(bytes_data).decode()

Expand All @@ -76,7 +87,13 @@ def _handle_transaction_type(self, bytes_data):
][0]
return serializer(self.transaction, bytes_data).serialize()

def _handle_signature(self, bytes_data, skip_signature, skip_second_signature, skip_multi_signature):
def _handle_signature(
self,
bytes_data,
skip_signature,
skip_second_signature,
skip_multi_signature,
):
"""Serialize signature data of the transaction

Args:
Expand Down
4 changes: 2 additions & 2 deletions crypto/transactions/serializers/htlc_claim.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from binascii import unhexlify

from crypto.transactions.serializers.base import BaseSerializer
from crypto.exceptions import ArkSerializerException
from crypto.transactions.serializers.base import BaseSerializer


class HtlcClaimSerializer(BaseSerializer):
Expand All @@ -12,7 +12,7 @@ def serialize(self):
self.bytes_data += unhexlify(self.transaction['asset']['claim']['lockTransactionId'])
unlock_secret_bytes = unhexlify(self.transaction['asset']['claim']['unlockSecret'].encode())
if len(unlock_secret_bytes) != 32:
raise ArkSerializerException("Unlock secret must be 32 bytes long")
raise ArkSerializerException('Unlock secret must be 32 bytes long')

self.bytes_data += unlock_secret_bytes
return self.bytes_data
2 changes: 1 addition & 1 deletion crypto/transactions/serializers/htlc_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from base58 import b58decode_check

from binary.hex.writer import write_high
from binary.unsigned_integer.writer import write_bit8, write_bit32, write_bit64
from binary.unsigned_integer.writer import write_bit32, write_bit64, write_bit8

from crypto.transactions.serializers.base import BaseSerializer

Expand Down
3 changes: 2 additions & 1 deletion crypto/transactions/serializers/htlc_refund.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ class HtlcRefundSerializer(BaseSerializer):
"""

def serialize(self):
self.bytes_data += unhexlify(self.transaction['asset']['refund']['lockTransactionId'].encode())
lock_transaction_id = self.transaction['asset']['refund']['lockTransactionId']
self.bytes_data += unhexlify(lock_transaction_id.encode())
return self.bytes_data
39 changes: 30 additions & 9 deletions crypto/transactions/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ def get_id(self):
Returns:
str:
"""
return sha256(self.to_bytes(skip_signature=False, skip_second_signature=False, skip_multi_signature=False)).hexdigest()
return sha256(self.to_bytes(
skip_signature=False,
skip_second_signature=False,
skip_multi_signature=False,
)).hexdigest()

def to_dict(self):
"""Convert the transaction into a dictionary representation
Expand Down Expand Up @@ -100,10 +104,11 @@ def parse_signatures(self, serialized, start_offset):
self.signature = serialized[start_offset:signature_end_offset]

second_signature_end_offset = signature_end_offset + (64 * 2)
if len(serialized) - signature_end_offset > 0 and (len(serialized) - signature_end_offset) % 64 == 0:
remaining_length = len(serialized) - signature_end_offset
if remaining_length > 0 and remaining_length % 64 == 0:
self.signSignature = serialized[signature_end_offset:second_signature_end_offset]

if len(serialized) - second_signature_end_offset > 0 and (len(serialized) - signature_end_offset) % 65 == 0:
if len(serialized) - second_signature_end_offset > 0 and remaining_length % 65 == 0:
multi_sig_part = serialized[signature_end_offset:]
index = 0
index_size = 2
Expand All @@ -130,7 +135,11 @@ def serialize(self, skip_signature=True, skip_second_signature=True, skip_multi_
str: Serialized string
"""
data = self.to_dict()
return Serializer(data).serialize(skip_signature, skip_second_signature, skip_multi_signature)
return Serializer(data).serialize(
skip_signature,
skip_second_signature,
skip_multi_signature,
)

def deserialize(self, serialized):
"""Perform AIP11 compliant deserialization.
Expand All @@ -147,7 +156,11 @@ def verify_schnorr(self):
"""Verify the transaction. Method will raise an exception if invalid, if it's valid it will
returns True
"""
is_valid = schnorr.b410_schnorr_verify(self.to_bytes(), self.senderPublicKey, self.signature)
is_valid = schnorr.b410_schnorr_verify(
self.to_bytes(),
self.senderPublicKey,
self.signature,
)

if not is_valid:
raise ArkInvalidTransaction('Transaction could not be verified')
Expand All @@ -158,18 +171,26 @@ def verify_schnorr_secondsig(self, secondPublicKey):
"""Verify the transaction. Method will raise an exception if invalid, if it's valid it will
returns True
"""
is_valid = schnorr.b410_schnorr_verify(self.to_bytes(False, True), secondPublicKey, self.signSignature)
is_valid = schnorr.b410_schnorr_verify(
self.to_bytes(False, True),
secondPublicKey,
self.signSignature,
)

if not is_valid:
raise ArkInvalidTransaction('Transaction could not be verified')

return is_valid

def verify_schnorr_multisig(self):
"""Verify the multisignatures transaction. Method will raise an exception if invalid, it will
returns True
"""Verify the multisignatures transaction. Method will raise an exception if invalid, if
it's valid it will returns True
"""
is_valid = schnorr.b410_schnorr_verify(self.to_bytes(True, True, False), self.senderPublicKey, self.signature)
is_valid = schnorr.b410_schnorr_verify(
self.to_bytes(True, True, False),
self.senderPublicKey,
self.signature,
)

if not is_valid:
raise ArkInvalidTransaction('Transaction could not be verified')
Expand Down
Loading