def test_TestAddressFromBytes(): public_key = skycoin.cipher_PubKey() secret_key = skycoin.cipher_SecKey() skycoin.SKY_cipher_GenerateKeyPair(public_key, secret_key) address = skycoin.cipher__Address() skycoin.SKY_cipher_AddressFromPubKey(public_key, address) address_2 = skycoin.cipher__Address() _, byte = skycoin.SKY_cipher_Address_Bytes(address) err = skycoin.SKY_cipher_AddressFromBytes(byte, address_2) assert err == skycoin.SKY_OK assert address == address_2 # Invalid number of bytes __ = skycoin.cipher__Address() err = skycoin.SKY_cipher_AddressFromBytes(byte[:len(byte) - 2], __) assert err == skycoin.SKY_ErrAddressInvalidLength # Invalid checksum byte_array = bytearray(byte) byte_array[-1] = 1 byte_new = bytes(byte_array) err = skycoin.SKY_cipher_AddressFromBytes(byte_new, __) assert err == skycoin.SKY_ErrAddressInvalidChecksum address.Version = 2 _, b = skycoin.SKY_cipher_Address_Bytes(address) err = skycoin.SKY_cipher_AddressFromBytes(b, __) assert err == skycoin.SKY_ErrAddressInvalidVersion
def test_TestTransactionSignInputs(): handle = utils.makeEmptyTransaction() # Panics if txns already signed sig = skycoin.cipher_Sig() assert skycoin.SKY_coin_Transaction_PushSignature(handle, sig) == skycoin.SKY_OK secKeys = [] secKeys.append(skycoin.cipher_SecKey()) # Panics if not enough keys handle = utils.makeEmptyTransaction() ux, s = utils.makeUxOutWithSecret() h = skycoin.cipher_SHA256() assert skycoin.SKY_coin_UxOut_Hash(ux, h) == skycoin.SKY_OK err, _ = skycoin.SKY_coin_Transaction_PushInput(handle, h) assert err == skycoin.SKY_OK ux2, s2 = utils.makeUxOutWithSecret() assert skycoin.SKY_coin_UxOut_Hash(ux2, h) == skycoin.SKY_OK err, _ = skycoin.SKY_coin_Transaction_PushInput(handle, h) assert err == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_PushOutput( handle, utils.makeAddress(), 40, 80) == skycoin.SKY_OK err, count = skycoin.SKY_coin_Transaction_GetSignaturesCount(handle) assert err == skycoin.SKY_OK assert count == 0 # Valid signing assert skycoin.SKY_coin_Transaction_HashInner(handle, h) == skycoin.SKY_OK secKeys = [] secKeys.append(s) secKeys.append(s2) assert skycoin.SKY_coin_Transaction_SignInputs( handle, secKeys) == skycoin.SKY_OK err, count = skycoin.SKY_coin_Transaction_GetSignaturesCount(handle) assert err == skycoin.SKY_OK assert count == 2 h2 = skycoin.cipher_SHA256() assert skycoin.SKY_coin_Transaction_HashInner( handle, h2) == skycoin.SKY_OK assert h == h2 p = skycoin.cipher_PubKey() assert skycoin.SKY_cipher_PubKeyFromSecKey(s, p) == skycoin.SKY_OK a = skycoin.cipher__Address() a2 = skycoin.cipher__Address() assert skycoin.SKY_cipher_AddressFromPubKey(p, a) == skycoin.SKY_OK assert skycoin.SKY_cipher_PubKeyFromSecKey(s2, p) == skycoin.SKY_OK assert skycoin.SKY_cipher_AddressFromPubKey(p, a2) == skycoin.SKY_OK sha1 = skycoin.cipher_SHA256() sha2 = skycoin.cipher_SHA256() txin0 = skycoin.cipher_SHA256() txin1 = skycoin.cipher_SHA256() assert skycoin.SKY_coin_Transaction_GetInputAt( handle, 0, txin0) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_GetInputAt( handle, 1, txin1) == skycoin.SKY_OK assert skycoin.SKY_cipher_AddSHA256(h, txin0, sha1) == skycoin.SKY_OK assert skycoin.SKY_cipher_AddSHA256(h, txin1, sha2) == skycoin.SKY_OK txsig0 = skycoin.cipher_Sig() txsig1 = skycoin.cipher_Sig() assert skycoin.SKY_coin_Transaction_GetSignatureAt( handle, 0, txsig0) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_GetSignatureAt( handle, 1, txsig1) == skycoin.SKY_OK
def test_cipherAddress(): address = skycoin.cipher__Address() error = skycoin.SKY_cipher_DecodeBase58Address("2GgFvqoyk9RjwVzj8tqfcXVXB4orBwoc9qv", address) assert error == 0 error, bytes = skycoin.SKY_cipher_Address_BitcoinBytes(address) assert error == 0 assert len( bytes ) > 0 address2 = skycoin.cipher__Address() error = skycoin.SKY_cipher_BitcoinAddressFromBytes( bytes, address2 ) assert error == 0 assert address.isEqual(address2)
def test_TestAddressRoundtrip(): public_key = skycoin.cipher_PubKey() secret_key = skycoin.cipher_SecKey() skycoin.SKY_cipher_GenerateKeyPair(public_key, secret_key) address = skycoin.cipher__Address() address_2 = skycoin.cipher__Address() skycoin.SKY_cipher_AddressFromPubKey(public_key, address) _, byte = skycoin.skycoin.SKY_cipher_Address_Bytes(address) err = skycoin.skycoin.SKY_cipher_AddressFromBytes(byte, address_2) assert err == skycoin.SKY_OK assert address == address_2 _, addres_str = skycoin.SKY_cipher_Address_String(address) _, addres_2_str = skycoin.SKY_cipher_Address_String(address_2) assert addres_2_str == addres_str
def test_GenerateDeterministicKeyPairs(): error, seed = skycoin.SKY_cipher_RandByte(32) assert error == 0 secKeys = skycoin.cipher_SecKeys() error = skycoin.SKY_cipher_GenerateDeterministicKeyPairs(seed, 2, secKeys) assert error == 0 length = secKeys.count assert length == 2 secKey = secKeys.getAt(0) address = skycoin.cipher__Address() error = skycoin.SKY_cipher_AddressFromSecKey(secKey, address) assert error == 0 secKey = secKeys.getAt(1) address = skycoin.cipher__Address() error = skycoin.SKY_cipher_AddressFromSecKey(secKey, address) assert error == 0
def test_TestAddressUxOutsFlatten(): uxa = utils.makeUxArray(3) empty = utils.makeUxArray(0) err, uxH = skycoin.SKY_coin_NewAddressUxOuts(empty) assert err == skycoin.SKY_OK uxa[2].Body.Address = uxa[1].Body.Address emptyAddr = skycoin.cipher__Address() err = skycoin.SKY_coin_AddressUxOuts_Set(uxH, emptyAddr, empty) assert err == skycoin.SKY_OK ux_1 = [uxa[0]] err = skycoin.SKY_coin_AddressUxOuts_Set(uxH, uxa[0].Body.Address, ux_1) assert err == skycoin.SKY_OK ux_2 = uxa[1:] err = skycoin.SKY_coin_AddressUxOuts_Set(uxH, uxa[1].Body.Address, ux_2) assert err == skycoin.SKY_OK err, flatArray = skycoin.SKY_coin_AddressUxOuts_Flatten(uxH) assert err == skycoin.SKY_OK assert len(flatArray) == 3 for x in flatArray: assert x.Body.Address != emptyAddr if flatArray[0].Body.Address == uxa[0].Body.Address: assert flatArray[0] == uxa[0] assert flatArray[0].Body.Address == uxa[0].Body.Address assert flatArray[1] == uxa[1] assert flatArray[1].Body.Address == uxa[1].Body.Address assert flatArray[2] == uxa[2] assert flatArray[2].Body.Address == uxa[2].Body.Address else: assert flatArray[0] == uxa[1] assert flatArray[0].Body.Address == uxa[1].Body.Address assert flatArray[1] == uxa[2] assert flatArray[1].Body.Address == uxa[2].Body.Address assert flatArray[2] == uxa[0] assert flatArray[2].Body.Address == uxa[0].Body.Address
def test_TestAddressString(): public_key = skycoin.cipher_PubKey() secret_key = skycoin.cipher_SecKey() skycoin.SKY_cipher_GenerateKeyPair(public_key, secret_key) address = skycoin.cipher__Address() skycoin.SKY_cipher_AddressFromPubKey(public_key, address) _, addres_str = skycoin.SKY_cipher_Address_String(address) address_2 = skycoin.cipher__Address() err = skycoin.SKY_cipher_DecodeBase58Address(addres_str, address_2) assert err == skycoin.SKY_OK assert address == address_2 _, addres_2_str = skycoin.SKY_cipher_Address_String(address_2) addres_3 = skycoin.cipher__Address() err = skycoin.SKY_cipher_DecodeBase58Address(addres_2_str, addres_3) assert err == skycoin.SKY_OK assert address_2 == addres_3
def test_TestAddressBulk(): for _ in range(1024): public_key = skycoin.cipher_PubKey() secret_key = skycoin.cipher_SecKey() addres_1 = skycoin.cipher__Address() address_2 = skycoin.cipher__Address() _, data = skycoin.SKY_cipher_RandByte(32) skycoin.SKY_cipher_GenerateDeterministicKeyPair( data, public_key, secret_key) skycoin.SKY_cipher_AddressFromPubKey(public_key, addres_1) err = skycoin.SKY_cipher_Address_Verify(addres_1, public_key) assert err == skycoin.SKY_OK _, addres_str = skycoin.SKY_cipher_Address_String(addres_1) err = skycoin.SKY_cipher_DecodeBase58Address(addres_str, address_2) assert err == skycoin.SKY_OK assert addres_1 == address_2
def test_GenerateKeyPairs(): error, data = skycoin.SKY_cipher_RandByte(32) assert error == 0 pubkey = skycoin.cipher_PubKey() seckey = skycoin.cipher_SecKey() error = skycoin.SKY_cipher_GenerateDeterministicKeyPair(data, pubkey, seckey) assert error == 0 address = skycoin.cipher__Address() error = skycoin.SKY_cipher_AddressFromPubKey(pubkey, address) assert error == 0 error = skycoin.SKY_cipher_Address_Verify(address, pubkey) assert error == 0 error, address_string = skycoin.SKY_cipher_Address_String( address ) assert error == 0 address2 = skycoin.cipher__Address() error = skycoin.SKY_cipher_DecodeBase58Address( address_string, address2 ) assert error == 0 assert address.isEqual(address2)
def makeKeysAndAddress(): ppubkey = skycoin.cipher_PubKey() pseckey = skycoin.cipher_SecKey() err = skycoin.SKY_cipher_GenerateKeyPair(ppubkey, pseckey) assert err == skycoin.SKY_OK paddress = skycoin.cipher__Address() err = skycoin.SKY_cipher_AddressFromPubKey(ppubkey, paddress) assert err == skycoin.SKY_OK return err, ppubkey, pseckey, paddress
def test_TestTransactionHashInner(): handle, tx = utils.makeTransaction() h = skycoin.cipher_SHA256() assert skycoin.SKY_coin_Transaction_HashInner(handle, h) == skycoin.SKY_OK assert h != skycoin.cipher_SHA256() # If tx.In is changed, hash should change handle2, tx2 = utils.copyTransaction(handle) ux = utils.makeUxOut() h = skycoin.cipher_SHA256() h1 = skycoin.cipher_SHA256() assert skycoin.SKY_coin_UxOut_Hash(ux, h) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_SetInputAt( handle2, 0, h) == skycoin.SKY_OK assert tx != tx2 assert skycoin.SKY_coin_UxOut_Hash(ux, h1) == skycoin.SKY_OK assert h == h1 assert skycoin.SKY_coin_Transaction_HashInner(handle, h) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_HashInner( handle2, h1) == skycoin.SKY_OK assert h != h1 # If tx.Out is changed, hash should change handle2, tx2 = utils.copyTransaction(handle) a = utils.makeAddress() a2 = skycoin.cipher__Address() pOut = skycoin.coin__TransactionOutput() assert skycoin.SKY_coin_Transaction_GetOutputAt( handle2, 0, pOut) == skycoin.SKY_OK pOut.Address = a assert skycoin.SKY_coin_Transaction_SetOutputAt( handle2, 0, pOut) == skycoin.SKY_OK assert tx != tx2 assert skycoin.SKY_coin_Transaction_GetOutputAt( handle2, 0, pOut) == skycoin.SKY_OK assert pOut.Address == a sha1 = skycoin.cipher_SHA256() sha2 = skycoin.cipher_SHA256() assert skycoin.SKY_coin_Transaction_HashInner( handle, sha1) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_HashInner( handle2, sha2) == skycoin.SKY_OK assert sha1 != sha2 # If tx.Head is changed, hash should not change handle2, tx2 = utils.copyTransaction(handle) sig = skycoin.cipher_Sig() assert skycoin.SKY_coin_Transaction_PushSignature( handle, sig) == skycoin.SKY_OK sha1 = skycoin.cipher_SHA256() sha2 = skycoin.cipher_SHA256() assert skycoin.SKY_coin_Transaction_HashInner( handle, sha1) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_HashInner( handle2, sha2) == skycoin.SKY_OK assert sha1 == sha2
def test_TestAddressNull(): address = skycoin.cipher__Address() _error, isNull = skycoin.SKY_cipher_Address_Null(address) assert _error == skycoin.SKY_OK assert isNull == 1 public_key = skycoin.cipher_PubKey() secret_key = skycoin.cipher_SecKey() skycoin.SKY_cipher_GenerateKeyPair(public_key, secret_key) skycoin.SKY_cipher_AddressFromPubKey(public_key, address) assert address is not None
def test_TestDecodeBase58Address(): public_key = skycoin.cipher_PubKey() secret_key = skycoin.cipher_SecKey() skycoin.SKY_cipher_GenerateKeyPair(public_key, secret_key) address = skycoin.cipher__Address() skycoin.SKY_cipher_AddressFromPubKey(public_key, address) err = skycoin.SKY_cipher_Address_Verify(address, public_key) assert err == skycoin.SKY_OK address_2 = skycoin.cipher__Address() err = skycoin.SKY_cipher_DecodeBase58Address(b'""', address_2) assert err == skycoin.SKY_ERROR err = skycoin.SKY_cipher_DecodeBase58Address(b'"cascs"', address_2) assert err == skycoin.SKY_ERROR _, byte = skycoin.SKY_cipher_Address_Bytes(address) _, h = skycoin.SKY_base58_Hex2Base58(byte[:int(len(byte) / 2)]) err = skycoin.SKY_cipher_DecodeBase58Address(h, address_2) assert err == skycoin.SKY_ErrAddressInvalidLength _, h = skycoin.SKY_base58_Hex2Base58(byte) err = skycoin.SKY_cipher_DecodeBase58Address(h, address_2) assert err == skycoin.SKY_OK assert address == address_2 _, addres_str = skycoin.SKY_cipher_Address_String(address) err = skycoin.SKY_cipher_DecodeBase58Address(addres_str, address_2) assert err == skycoin.SKY_OK assert address == address_2 # preceding whitespace is invalid addres_2_str = b'" " + a_str' err = skycoin.SKY_cipher_DecodeBase58Address(addres_2_str, address_2) assert err == skycoin.SKY_ERROR # preceding zeroes are invalid addres_2_str = b'"000" + a_str' err = skycoin.SKY_cipher_DecodeBase58Address(addres_2_str, address_2) assert err == skycoin.SKY_ERROR # trailing whitespace is invalid addres_2_str = b'a_str + " "' err = skycoin.SKY_cipher_DecodeBase58Address(addres_2_str, address_2) assert err == skycoin.SKY_ERROR # trailing zeroes are invalid addres_2_str = b'a_str + "000"' err = skycoin.SKY_cipher_DecodeBase58Address(addres_2_str, address_2) assert err == skycoin.SKY_ERROR
def address_valid(address): """ Check if an address is valid A SKY address uses an alphanumeric base58 encoding, without 0, O, I or l. Important note: the last four bytes are a checksum check. They are the first four bytes of a double SHA-256 digest of the previous 21 bytes Read the first twenty-one bytes, compute the checksum, and check that it corresponds to the last four bytes. """ addressObj = skycoin.cipher__Address() error = skycoin.SKY_cipher_DecodeBase58Address(address.encode(), addressObj) return jsonify({"isValid": error == 0})
def test_TestPubKeyToAddress(): public_key = skycoin.cipher_PubKey() secret_key = skycoin.cipher_SecKey() skycoin.SKY_cipher_GenerateKeyPair(public_key, secret_key) addres = skycoin.cipher__Address() skycoin.SKY_cipher_AddressFromPubKey(public_key, addres) # func (self Address) Verify(key PubKey) error assert skycoin.SKY_cipher_Address_Verify(addres, public_key) == skycoin.SKY_OK # func DecodeBase58Address(addr string) (Address, error) _, addres_str = skycoin.SKY_cipher_Address_String(addres) assert skycoin.SKY_cipher_DecodeBase58Address(addres_str, addres) == skycoin.SKY_OK
def test_TestSignHash(): public_key = skycoin.cipher_PubKey() secret_key = skycoin.cipher_SecKey() addres = skycoin.cipher__Address() sha_sum = skycoin.cipher_SHA256() sig_1 = skycoin.cipher_Sig() sig_2 = skycoin.cipher_Sig() skycoin.SKY_cipher_GenerateKeyPair(public_key, secret_key) skycoin.SKY_cipher_AddressFromPubKey(public_key, addres) _, data = skycoin.SKY_cipher_RandByte(256) skycoin.SKY_cipher_SumSHA256(data, sha_sum) skycoin.SKY_cipher_SignHash(sha_sum, secret_key, sig_1) assert sig_1 != sig_2
def makeUxBodyWithSecret(): p = skycoin.cipher_PubKey() s = skycoin.cipher_SecKey() assert skycoin.SKY_cipher_GenerateKeyPair(p, s) == skycoin.SKY_OK uxb = skycoin.coin__UxBody() err, b = skycoin.SKY_cipher_RandByte(128) assert err == skycoin.SKY_OK h = skycoin.cipher_SHA256() assert skycoin.SKY_cipher_SumSHA256(b, h) == skycoin.SKY_OK assert h.assignTo(uxb.SrcTransaction) == None a = skycoin.cipher__Address() assert skycoin.SKY_cipher_AddressFromPubKey(p, a) == skycoin.SKY_OK uxb.Address = a uxb.Coins = int(1e6) uxb.Hours = int(100) return uxb, s
def KeysTestDataFromJSON(KeysTestDataJSON): address = skycoin.cipher__Address() err = skycoin.SKY_cipher_DecodeBase58Address( KeysTestDataJSON["address"].encode(), address) if err != skycoin.SKY_OK: return skycoin.SKY_ERROR, None err, hex_str = skycoin.SKY_base58_String2Hex( KeysTestDataJSON["secret"].encode()) assert err == skycoin.SKY_OK secret_key = skycoin.cipher_SecKey() err = skycoin.SKY_cipher_NewSecKey(hex_str, secret_key) assert err == skycoin.SKY_OK err, secret_key_hex = skycoin.SKY_cipher_SecKey_Hex(secret_key) if err != skycoin.SKY_OK: return skycoin.SKY_ERROR, None err, hex_str = skycoin.SKY_base58_String2Hex( KeysTestDataJSON["public"].encode()) assert err == skycoin.SKY_OK public_key = skycoin.cipher_PubKey() err = skycoin.SKY_cipher_NewPubKey(hex_str, public_key) assert err == skycoin.SKY_OK err, public_key_hex = skycoin.SKY_cipher_PubKey_Hex(public_key) if err != skycoin.SKY_OK: return skycoin.SKY_ERROR, None r = KeysTestData() r.Address = address r.Public = public_key_hex r.Secret = secret_key_hex r.Signatures = 0 if KeysTestDataJSON.get("signatures") == None: return skycoin.SKY_OK, r sigs = [] if len(KeysTestDataJSON["signatures"]) >= 0: for s in KeysTestDataJSON["signatures"]: sig_fromHex = skycoin.cipher_Sig() err = skycoin.SKY_cipher_SigFromHex(s.encode(), sig_fromHex) assert err == skycoin.SKY_OK sigs.append(sig_fromHex) assert err == skycoin.SKY_OK r.Signatures = sigs return skycoin.SKY_OK, r
def test_TestAddressVerify(): public_key = skycoin.cipher_PubKey() secret_key = skycoin.cipher_SecKey() skycoin.SKY_cipher_GenerateKeyPair(public_key, secret_key) address = skycoin.cipher__Address() skycoin.SKY_cipher_AddressFromPubKey(public_key, address) # Valid pubkey+address err = skycoin.SKY_cipher_Address_Verify(address, public_key) assert err == skycoin.SKY_OK # Invalid pubkey public_key_temp = skycoin.cipher_PubKey() err = skycoin.SKY_cipher_Address_Verify(address, public_key_temp) assert err == skycoin.SKY_ErrAddressInvalidPubKey skycoin.SKY_cipher_GenerateKeyPair(public_key_temp, secret_key) err = skycoin.SKY_cipher_Address_Verify(address, public_key_temp) assert err == skycoin.SKY_ErrAddressInvalidPubKey # Bad version address.Version = 0x01 err = skycoin.SKY_cipher_Address_Verify(address, public_key) assert err == skycoin.SKY_ErrAddressInvalidVersion
def test_TestUxOutSnapshotHash(): p = skycoin.cipher_PubKey() s = skycoin.cipher_SecKey() skycoin.SKY_cipher_GenerateKeyPair(p, s) uxb = skycoin.coin__UxBody() _, b = skycoin.SKY_cipher_RandByte(128) h = skycoin.cipher_SHA256() assert skycoin.SKY_cipher_SumSHA256(b, h) == skycoin.SKY_OK uxb.SetSrcTransaction(h.toStr()) a = skycoin.cipher__Address() skycoin.SKY_cipher_AddressFromPubKey(p, a) uxb.Address = a uxb.Coins = int(1e6) uxb.Hours = int(100) uxo = skycoin.coin__UxOut() uxh = skycoin.coin__UxHead() uxh.Time = 100 uxh.BkSeq = 2 uxo.Head = uxh uxo.Body = uxb hn = skycoin.cipher_SHA256() assert skycoin.SKY_coin_UxOut_SnapshotHash(uxo, hn) == skycoin.SKY_OK # snapshot hash should be dependent on every field in body and head # Head Time uxo_2 = uxo uxh.Time = 20 uxo_2.Head = uxh hn_2 = skycoin.cipher_SHA256() assert skycoin.SKY_coin_UxOut_SnapshotHash(uxo_2, hn_2) == skycoin.SKY_OK assert hn != hn_2 # Head BkSeq uxo_2 = uxo uxh.BkSeq = 4 uxo_2.Head = uxh hn_2 = skycoin.cipher_SHA256() assert skycoin.SKY_coin_UxOut_SnapshotHash(uxo_2, hn_2) == skycoin.SKY_OK assert hn != hn_2 # Body SetSrcTransaction uxo_2 = uxo uxb = skycoin.coin__UxBody() _, b = skycoin.SKY_cipher_RandByte(128) h = skycoin.cipher_SHA256() assert skycoin.SKY_cipher_SumSHA256(b, h) == skycoin.SKY_OK uxb.SetSrcTransaction(h.toStr()) uxo_2.Body = uxb hn_2 = skycoin.cipher_SHA256() assert skycoin.SKY_coin_UxOut_SnapshotHash(uxo_2, hn_2) == skycoin.SKY_OK assert hn != hn_2 # Body Address p_2 = skycoin.cipher_PubKey() s_2 = skycoin.cipher_SecKey() skycoin.SKY_cipher_GenerateKeyPair(p_2, s_2) a_2 = skycoin.cipher__Address() skycoin.SKY_cipher_AddressFromPubKey(p_2, a_2) uxo_2 = uxo uxb = skycoin.coin__UxBody() uxb.Address = a_2 uxo_2.Body = uxb hn_2 = skycoin.cipher_SHA256() assert skycoin.SKY_coin_UxOut_SnapshotHash(uxo_2, hn_2) == skycoin.SKY_OK assert hn != hn_2 # Body Coins uxo_2 = uxo uxb = skycoin.coin__UxBody() uxb.Coins = int(2) uxo_2.Body = uxb hn_2 = skycoin.cipher_SHA256() assert skycoin.SKY_coin_UxOut_SnapshotHash(uxo_2, hn_2) == skycoin.SKY_OK assert hn != hn_2 # Body Hours uxo_2 = uxo uxb = skycoin.coin__UxBody() uxb.Hours = int(2) uxo_2.Body = uxb hn_2 = skycoin.cipher_SHA256() assert skycoin.SKY_coin_UxOut_SnapshotHash(uxo_2, hn_2) == skycoin.SKY_OK assert hn != hn_2
def makeAddress(): p, s = makecipher_PubKeyAndcipher_SecKey() a = skycoin.cipher__Address() assert skycoin.SKY_cipher_AddressFromPubKey(p, a) == skycoin.SKY_OK return a
def ValidateSeedData(SeedTestData=None, InputTestData=None): err, keys = skycoin.SKY_cipher_GenerateDeterministicKeyPairs( SeedTestData.Seed, len(SeedTestData.Keys)) assert err == skycoin.SKY_OK if len(SeedTestData.Keys) != len(keys): return skycoin.SKY_ERROR for i, s in enumerate(keys): secret_Key_null = skycoin.cipher_SecKey() if s == secret_Key_null: return skycoin.SKY_ErrInvalidSecKey if (SeedTestData.Keys[i].Secret).decode() != binascii.hexlify( bytearray(s.toStr())).decode('ascii'): assert err == skycoin.SKY_ERROR p = skycoin.cipher_PubKey() p_null = skycoin.cipher_PubKey() err = skycoin.SKY_cipher_PubKeyFromSecKey(s, p) if p == p_null: return skycoin.SKY_ErrInvalidPubKey if (SeedTestData.Keys[i].Public).decode() != binascii.hexlify( bytearray(p.toStr())).decode('ascii'): return skycoin.SKY_ErrInvalidPubKey addr1 = skycoin.cipher__Address() addr_null = skycoin.cipher__Address() err = skycoin.SKY_cipher_AddressFromPubKey(p, addr1) assert err == skycoin.SKY_OK if addr1 == addr_null: return skycoin.SKY_ErrAddressInvalidPubKey if not (SeedTestData.Keys[i].Address == addr1): return skycoin.SKY_ErrAddressInvalidChecksum addr2 = skycoin.cipher__Address() err = skycoin.SKY_cipher_AddressFromSecKey(s, addr2) assert err == skycoin.SKY_OK if not (addr1 == addr2): return skycoin.SKY_ErrAddressInvalidChecksum if InputTestData == None and SeedTestData.Keys[i].Signatures != 0: return skycoin.SKY_ERROR if InputTestData != None: if len(SeedTestData.Keys[i].Signatures) != len(InputTestData): return skycoin.SKY_ERROR for j in range(len(InputTestData)): sig = SeedTestData.Keys[i].Signatures[j] sig_null = skycoin.cipher_Sig() if sig == sig_null: return skycoin.SKY_ERROR err = skycoin.SKY_cipher_VerifySignedHash( sig, InputTestData[j]) if err != skycoin.SKY_OK: return skycoin.SKY_ERROR p2 = skycoin.cipher_PubKey() err = skycoin.SKY_cipher_PubKeyFromSig(sig, InputTestData[j], p2) if err != skycoin.SKY_OK: return skycoin.SKY_ERROR if not (p == p2): return 1 return skycoin.SKY_ERROR sig2 = skycoin.cipher_Sig() skycoin.SKY_cipher_SignHash(InputTestData[j], s, sig2) if sig2 == sig_null: return skycoin.SKY_ERROR return skycoin.SKY_OK
def test_TestUxOutCoinHours(): p = skycoin.cipher_PubKey() s = skycoin.cipher_SecKey() skycoin.SKY_cipher_GenerateKeyPair(p, s) uxb = skycoin.coin__UxBody() _, b = skycoin.SKY_cipher_RandByte(128) h = skycoin.cipher_SHA256() assert skycoin.SKY_cipher_SumSHA256(b, h) == skycoin.SKY_OK uxb.SetSrcTransaction(h.toStr()) a = skycoin.cipher__Address() skycoin.SKY_cipher_AddressFromPubKey(p, a) uxb.Address = a uxb.Coins = int(1e6) uxb.Hours = int(100) uxo = skycoin.coin__UxOut() uxh = skycoin.coin__UxHead() uxh.Time = 100 uxh.BkSeq = 2 uxo.Head = uxh uxo.Body = uxb # Less than an hour passed now = uxh.Time + 100 err, hours = skycoin.SKY_coin_UxOut_CoinHours(uxo, now) assert hours == uxh.Time assert err == skycoin.SKY_OK # 1 hours passed now = uxh.Time + 3600 err, hours = skycoin.SKY_coin_UxOut_CoinHours(uxo, now) assert hours == uxh.Time + uxb.Coins / 1000000 assert err == skycoin.SKY_OK # 6 hours passed now = uxh.Time + 3600 * 6 err, hours = skycoin.SKY_coin_UxOut_CoinHours(uxo, now) assert hours == uxh.Time + (uxb.Coins / 1000000) * 6 assert err == skycoin.SKY_OK # Time is backwards (treated as no hours passed) now = uxh.Time // 2 err, hours = skycoin.SKY_coin_UxOut_CoinHours(uxo, now) assert hours == uxh.Time assert err == skycoin.SKY_OK # 1 hour has passed, output has 1.5 coins, should gain 1 coinhour uxb.Coins = 1500000 now = uxh.Time + 3600 err, hours = skycoin.SKY_coin_UxOut_CoinHours(uxo, now) assert hours == uxb.Hours + 1 assert err == skycoin.SKY_OK # 2 hours have passed, output has 1.5 coins, should gain 3 coin hours uxb.Coins = 1500000 uxo.Body = uxb now = uxh.Time + 3600 * 2 err, hours = skycoin.SKY_coin_UxOut_CoinHours(uxo, now) assert hours == uxb.Hours + 3 assert err == skycoin.SKY_OK # 1 second has passed, output has 3600 coins, should gain 1 coin hour uxb.Coins = 3600000000 uxo.Body = uxb now = uxh.Time + 1 err, hours = skycoin.SKY_coin_UxOut_CoinHours(uxo, now) assert hours == uxb.Hours + 1 assert err == skycoin.SKY_OK # 1000000 hours minus 1 second have passed, output has 1 droplet, should gain 0 coin hour uxb.Coins = 1 uxo.Body = uxb now = uxh.Time + 1000000 * 3600 - 1 err, hours = skycoin.SKY_coin_UxOut_CoinHours(uxo, now) assert hours == uxb.Hours assert err == skycoin.SKY_OK # 1000000 hours have passed, output has 1 droplet, should gain 1 coin hour uxb.Coins = 1 uxo.Body = uxb now = uxh.Time + 1000000 * 3600 err, hours = skycoin.SKY_coin_UxOut_CoinHours(uxo, now) assert hours == uxb.Hours + 1 assert err == skycoin.SKY_OK # No hours passed, using initial coin hours uxb.Coins = 1000000000 uxb.Hours = 1000 * 1000 uxo.Body = uxb now = uxh.Time err, hours = skycoin.SKY_coin_UxOut_CoinHours(uxo, now) assert hours == uxb.Hours assert err == skycoin.SKY_OK # One hour passed, using initial coin hours now = uxh.Time + 3600 err, hours = skycoin.SKY_coin_UxOut_CoinHours(uxo, now) assert hours == uxb.Hours + 1000000000 / 1000000 assert err == skycoin.SKY_OK # No hours passed and no hours to begin with0 uxb.Hours = 0 uxo.Body = uxb now = uxh.Time err, hours = skycoin.SKY_coin_UxOut_CoinHours(uxo, now) assert hours == 0 assert err == skycoin.SKY_OK # Centuries have passed, time-based calculation overflows uint64 # when calculating the whole coin seconds uxb.Coins = 2000000 uxo.Body = uxb now = 0xFFFFFFFFFFFFFFFF err, hours = skycoin.SKY_coin_UxOut_CoinHours(uxo, now) assert err == skycoin.SKY_ERROR # Centuries have passed, time-based calculation overflows uint64 # when calculating the droplet seconds uxb.Coins = 1500000 uxo.Body = uxb now = 0xFFFFFFFFFFFFFFFF err, hours = skycoin.SKY_coin_UxOut_CoinHours(uxo, now) assert err == skycoin.SKY_ERROR # Output would overflow if given more hours, has reached its limit uxb.Coins = 3600000000 uxo.Body = uxb now = 0xFFFFFFFFFFFFFFFF err, hours = skycoin.SKY_coin_UxOut_CoinHours(uxo, now) assert err == skycoin.SKY_ERROR
def test_TestTransactionVerify(): # Mismatch header hash handle, tx = utils.makeTransaction() h = skycoin.cipher_SHA256() h.assignTo(tx.InnerHash) assert skycoin.SKY_coin_Transaction_Verify(handle) == skycoin.SKY_ERROR # No inputs handle, tx = utils.makeTransaction() assert skycoin.SKY_coin_Transaction_ResetInputs( handle, 0) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_UpdateHeader(handle) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_Verify(handle) == skycoin.SKY_ERROR # No outputs handle, _ = utils.makeTransaction() assert skycoin.SKY_coin_Transaction_ResetOutputs( handle, 0) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_UpdateHeader(handle) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_Verify(handle) == skycoin.SKY_ERROR # Invalid number of Sigs handle, _ = utils.makeTransaction() assert skycoin.SKY_coin_Transaction_ResetSignatures( handle, 0) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_UpdateHeader(handle) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_Verify(handle) == skycoin.SKY_ERROR assert skycoin.SKY_coin_Transaction_ResetSignatures( handle, 20) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_UpdateHeader(handle) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_Verify(handle) == skycoin.SKY_ERROR # Too many sigs & inputs handle, _ = utils.makeTransaction() assert skycoin.SKY_coin_Transaction_ResetSignatures( handle, utils.MaxUint16) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_ResetInputs( handle, utils.MaxUint16) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_UpdateHeader(handle) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_Verify(handle) == skycoin.SKY_ERROR # Duplicate inputs ux, s = utils.makeUxOutWithSecret() handle, _ = utils.makeTransactionFromUxOut(ux, s) h = skycoin.cipher_SHA256() assert skycoin.SKY_coin_Transaction_GetInputAt( handle, 0, h) == skycoin.SKY_OK err, _ = skycoin.SKY_coin_Transaction_PushInput(handle, h) assert err == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_ResetSignatures( handle, 0) == skycoin.SKY_OK secKeys = [] secKeys.append(s) secKeys.append(s) assert skycoin.SKY_coin_Transaction_SignInputs( handle, secKeys) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_UpdateHeader(handle) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_Verify(handle) == skycoin.SKY_ERROR # Duplicate outputs handle, _ = utils.makeTransaction() pOutput = skycoin.coin__TransactionOutput() assert skycoin.SKY_coin_Transaction_GetOutputAt( handle, 0, pOutput) == skycoin.SKY_OK pOutput.Address = skycoin.cipher__Address() assert skycoin.SKY_coin_Transaction_ResetOutputs( handle, 0) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_PushOutput( handle, pOutput.Address, pOutput.Coins, pOutput.Hours) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_PushOutput( handle, pOutput.Address, pOutput.Coins, pOutput.Hours) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_UpdateHeader(handle) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_Verify(handle) == skycoin.SKY_ERROR # Output coins are 0 handle, _ = utils.makeTransaction() assert skycoin.SKY_coin_Transaction_GetOutputAt( handle, 0, pOutput) == skycoin.SKY_OK pOutput.Coins = 0 assert skycoin.SKY_coin_Transaction_ResetOutputs( handle, 0) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_PushOutput( handle, pOutput.Address, pOutput.Coins, pOutput.Hours) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_UpdateHeader(handle) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_Verify(handle) == skycoin.SKY_ERROR # Output coin overflow handle, _ = utils.makeTransaction() assert skycoin.SKY_coin_Transaction_GetOutputAt( handle, 0, pOutput) == skycoin.SKY_OK pOutput.Coins = int(utils.MaxUint64 - int(3e6)) assert skycoin.SKY_coin_Transaction_PushOutput( handle, pOutput.Address, pOutput.Coins, pOutput.Hours) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_UpdateHeader(handle) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_Verify(handle) == skycoin.SKY_ERROR # Output coins are not multiples of 1e6 (valid, decimal restriction is not # enforced here) handle, _ = utils.makeTransaction() assert skycoin.SKY_coin_Transaction_GetOutputAt( handle, 0, pOutput) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_ResetOutputs( handle, 0) == skycoin.SKY_OK pOutput.Coins += 10 assert skycoin.SKY_coin_Transaction_PushOutput( handle, pOutput.Address, pOutput.Coins, pOutput.Hours) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_UpdateHeader(handle) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_ResetSignatures( handle, 0) == skycoin.SKY_OK p = skycoin.cipher_PubKey() s = skycoin.cipher_SecKey() assert skycoin.SKY_cipher_GenerateKeyPair(p, s) == skycoin.SKY_OK secKeys = [] secKeys.append(s) assert skycoin.SKY_coin_Transaction_SignInputs( handle, secKeys) == skycoin.SKY_OK assert int(pOutput.Coins % int(1e6)) != int(0) assert skycoin.SKY_coin_Transaction_Verify(handle) == skycoin.SKY_OK # Valid handle, _ = utils.makeTransaction() assert skycoin.SKY_coin_Transaction_GetOutputAt( handle, 0, pOutput) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_ResetOutputs( handle, 0) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_PushOutput( handle, pOutput.Address, int(10e6), pOutput.Hours) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_PushOutput( handle, pOutput.Address, int(1e6), pOutput.Hours) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_UpdateHeader(handle) == skycoin.SKY_OK assert skycoin.SKY_coin_Transaction_Verify(handle) == skycoin.SKY_OK