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_TestTransactionPushInput(): handle = utils.makeEmptyTransaction() ux = utils.makeUxOut() sha = skycoin.cipher_SHA256() assert skycoin.SKY_coin_UxOut_Hash(ux, sha) == skycoin.SKY_OK r = skycoin.SKY_coin_Transaction_PushInput(handle, sha) assert r == 0 _, count = skycoin.SKY_coin_Transaction_GetInputsCount(handle) assert count == 1 sha1 = skycoin.cipher_SHA256() skycoin.SKY_coin_Transaction_GetInputAt(handle, 0, sha1) assert sha == sha1 skycoin.SKY_coin_Transaction_ResetInputs(handle, 0) for _ in range(utils.MaxUint16): skycoin.SKY_coin_Transaction_PushInput( handle, skycoin.cipher_SHA256()) ux = utils.makeUxOut() assert skycoin.SKY_coin_UxOut_Hash(ux, sha) == skycoin.SKY_OK
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