Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update second_sign method to use schnorr #106

Merged
merged 10 commits into from
Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from 8 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
9 changes: 7 additions & 2 deletions crypto/transactions/builder/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ def second_sign(self, passphrase):
Args:
passphrase (str): 2nd passphrase associated with the account sending this transaction
"""
message = Message.sign(self.transaction.to_bytes(skip_signature=False), passphrase)
self.transaction.signSignature = message.signature
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.id = self.transaction.get_id()

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

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

def schnorr_verify_second(self, passphrase):
secondPublicKey = PublicKey.from_passphrase(passphrase)
ItsANameToo marked this conversation as resolved.
Show resolved Hide resolved
return self.transaction.verify_schnorr_secondsig(secondPublicKey)

def schnorr_verify_multisig(self):
return self.transaction.verify_schnorr_multisig()
Expand Down
3 changes: 1 addition & 2 deletions crypto/transactions/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ def _handle_signature(self, bytes_data, skip_signature, skip_second_signature, s

if not skip_second_signature and self.transaction.get('secondSignature'):
bytes_data += unhexlify(self.transaction['secondSignature'])
elif self.transaction.get('signSignature'):
if not skip_second_signature and self.transaction.get('signSignature'):
bytes_data += unhexlify(self.transaction['signSignature'])

if not skip_multi_signature and self.transaction.get('signatures'):
bytes_data += unhexlify(''.join(self.transaction['signatures']))

Expand Down
9 changes: 9 additions & 0 deletions crypto/transactions/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ def verify_schnorr(self):

return is_valid

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)

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

def verify_schnorr_multisig(self):
"""Verify the multisignatures transaction. Method will raise an exception if invalid, it will
returns True
Expand Down
23 changes: 23 additions & 0 deletions tests/transactions/builder/test_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,29 @@ def test_transfer_transaction():
transaction.schnorr_verify() # if no exception is raised, it means the transaction is valid


def test_transfer_secondsig_transaction():
"""Test if a transfer transaction with second signature gets built
"""
transaction = Transfer(
recipientId='AGeYmgbg2LgGxRW2vNNJvQ88PknEJsYizC',
amount=200000000,
)
transaction.set_type_group(TRANSACTION_TYPE_GROUP.CORE)
transaction.set_nonce(1)
transaction.schnorr_sign('this is a top secret passphrase')
transaction.second_sign('second top secret passphrase')
transaction_dict = transaction.to_dict()

assert transaction_dict['nonce'] == 1
assert transaction_dict['signature']
assert transaction_dict['signSignature']
assert transaction_dict['type'] is TRANSACTION_TRANSFER
assert transaction_dict['typeGroup'] == 1
assert transaction_dict['typeGroup'] == TRANSACTION_TYPE_GROUP.CORE.value

transaction.schnorr_verify_second('second top secret passphrase') # if no exception is raised, it means the transaction is valid
ItsANameToo marked this conversation as resolved.
Show resolved Hide resolved


def test_parse_signatures(transaction_type_0):
"""Test if parse signature works when parsing serialized data
"""
Expand Down