Skip to content
This repository has been archived by the owner on Sep 10, 2019. It is now read-only.

Commit

Permalink
update tests per #177 (review)
Browse files Browse the repository at this point in the history
  • Loading branch information
jseagrave21 committed Mar 21, 2019
1 parent a5db26a commit 9cb2797
Showing 1 changed file with 37 additions and 19 deletions.
56 changes: 37 additions & 19 deletions tests/test_cryptography.py
Expand Up @@ -250,25 +250,6 @@ def test_sign_and_verify_str(self):
self.assertEqual(verification_result, verification_result2)
self.assertTrue(verification_result)

def test_curve_initialization(self):
# test the normal order of operations
message = binascii.hexlify(b'Hello World')
key = KeyPair(bytes.fromhex('8631cd2635c416ba5f043561e9d2ff40b79c3bb2eb245e176615298b8372d0a4'))
signature = Crypto.Sign(message, '8631cd2635c416ba5f043561e9d2ff40b79c3bb2eb245e176615298b8372d0a4')

self.assertTrue(Crypto.VerifySignature(message, signature, key.PublicKey))
sig1 = signature.hex()

# now test abnormal order of operations
message = binascii.hexlify(b'Hello World')
signature = Crypto.Sign(message, '8631cd2635c416ba5f043561e9d2ff40b79c3bb2eb245e176615298b8372d0a4')
key = KeyPair(bytes.fromhex('8631cd2635c416ba5f043561e9d2ff40b79c3bb2eb245e176615298b8372d0a4'))

self.assertTrue(Crypto.VerifySignature(message, signature, key.PublicKey))
sig2 = signature.hex()

self.assertEqual(sig1, sig2)

def test_script_hash(self):
# Expected output taken from running: getHash(Buffer.from('abc', 'utf8')).toString('hex')
# using https://github.com/CityOfZion/neon-wallet-react-native/blob/master/app/api/crypto/index.js
Expand All @@ -284,3 +265,40 @@ def test_faulty_message_param_to_verify_signature(self):

result = Crypto.VerifySignature(faulty_message, fake_signature, fake_pubkey)
self.assertFalse(result)


class TestSigningWithoutCryptoInstance(TestCase):
def setUp(self):
import bitcoin
# reset Elliptic curve parameters to secp256k1
bitcoin.change_curve(bitcoin.P, bitcoin.N, bitcoin.A, bitcoin.B, bitcoin.Gx, bitcoin.Gy)

sig1 = None

def test_normal_signing(self):
# test the normal order of operations: Keypair will initialize secp256r1 Elliptic curve parameters
message = binascii.hexlify(b'Hello World')
key = KeyPair(bytes.fromhex('8631cd2635c416ba5f043561e9d2ff40b79c3bb2eb245e176615298b8372d0a4'))
signature = Crypto.Sign(message, '8631cd2635c416ba5f043561e9d2ff40b79c3bb2eb245e176615298b8372d0a4')

self.assertTrue(Crypto.VerifySignature(message, signature, key.PublicKey))
TestSigningWithoutCryptoInstance.sig1 = signature.hex()

def tearDown(self):
import bitcoin
# reset Elliptic curve parameters to secp256k1
bitcoin.change_curve(bitcoin.P, bitcoin.N, bitcoin.A, bitcoin.B, bitcoin.Gx, bitcoin.Gy)

sig2 = None

def test_signing_without_cyptoinstance(self):
# test signing prior to initializing secp256r1 Elliptic curve parameters with Keypair
message = binascii.hexlify(b'Hello World')
signature = Crypto.Sign(message, '8631cd2635c416ba5f043561e9d2ff40b79c3bb2eb245e176615298b8372d0a4')
key = KeyPair(bytes.fromhex('8631cd2635c416ba5f043561e9d2ff40b79c3bb2eb245e176615298b8372d0a4'))

self.assertTrue(Crypto.VerifySignature(message, signature, key.PublicKey))
TestSigningWithoutCryptoInstance.sig2 = signature.hex()

# ensure the signatures are identical
self.assertEqual(TestSigningWithoutCryptoInstance.sig1, TestSigningWithoutCryptoInstance.sig2)

0 comments on commit 9cb2797

Please sign in to comment.