TestCryptBitcoin.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from Crypt import CryptBitcoin
  2. class TestCryptBitcoin:
  3. def testSignOld(self):
  4. privatekey = "23DKQpDz7bXM7w5KN5Wnmz7bwRNqNHcdQjb2WwrdB1QtTf5gM3pFdf"
  5. privatekey_bad = "23DKQpDz7bXM7w5KN5Wnmz6bwRNqNHcdQjb2WwrdB1QtTf5gM3pFdf"
  6. # Get address by privatekey
  7. address = CryptBitcoin.privatekeyToAddress(privatekey)
  8. assert address == "12vTsjscg4hYPewUL2onma5pgQmWPMs3ez"
  9. address_bad = CryptBitcoin.privatekeyToAddress(privatekey_bad)
  10. assert not address_bad == "12vTsjscg4hYPewUL2onma5pgQmWPMs3ez"
  11. # Text signing
  12. sign = CryptBitcoin.signOld("hello", privatekey)
  13. assert CryptBitcoin.verify("hello", address, sign) # Original text
  14. assert not CryptBitcoin.verify("not hello", address, sign) # Different text
  15. # Signed by bad privatekey
  16. sign_bad = CryptBitcoin.signOld("hello", privatekey_bad)
  17. assert not CryptBitcoin.verify("hello", address, sign_bad)
  18. def testSign(self):
  19. privatekey = "5K9S6dVpufGnroRgFrT6wsKiz2mJRYsC73eWDmajaHserAp3F1C"
  20. privatekey_bad = "5Jbm9rrusXyApAoM8YoM4Rja337zMMoBUMRJ1uijiguU2aZRnwC"
  21. # Get address by privatekey
  22. address = CryptBitcoin.privatekeyToAddress(privatekey)
  23. assert address == "1MpDMxFeDUkiHohxx9tbGLeEGEuR4ZNsJz"
  24. address_bad = CryptBitcoin.privatekeyToAddress(privatekey_bad)
  25. assert address_bad != "1MpDMxFeDUkiHohxx9tbGLeEGEuR4ZNsJz"
  26. # Text signing
  27. sign = CryptBitcoin.sign("hello", privatekey)
  28. assert CryptBitcoin.verify("hello", address, sign)
  29. assert not CryptBitcoin.verify("not hello", address, sign)
  30. # Signed by bad privatekey
  31. sign_bad = CryptBitcoin.sign("hello", privatekey_bad)
  32. assert not CryptBitcoin.verify("hello", address, sign_bad)
  33. def testNewPrivatekey(self):
  34. assert CryptBitcoin.newPrivatekey() != CryptBitcoin.newPrivatekey()
  35. assert CryptBitcoin.privatekeyToAddress(CryptBitcoin.newPrivatekey())
  36. def testNewSeed(self):
  37. assert CryptBitcoin.newSeed() != CryptBitcoin.newSeed()
  38. assert CryptBitcoin.privatekeyToAddress(
  39. CryptBitcoin.hdPrivatekey(CryptBitcoin.newSeed(), 0)
  40. )
  41. assert CryptBitcoin.privatekeyToAddress(
  42. CryptBitcoin.hdPrivatekey(CryptBitcoin.newSeed(), 2**256)
  43. )