Example #1
0
def isUxArraySorted(uxa):
    n = len(uxa)
    prev = uxa
    current = prev
    current += 1
    hash_1 = skycoin.cipher_SHA256()
    hash_2 = skycoin.cipher_SHA256()
    prevHash = None
    currentHash = None
    result = int()
    for i in n:
        if (prevHash == None):
            result = skycoin.SKY_coin_UxOut_Hash(prev, hash_1)
            assert result == skycoin.SKY_OK
            prevHash = hash_1
        if currentHash == None:
            currentHash = hash_2
            result = skycoin.SKY_coin_UxOut_Hash(current, currentHash)
            assert result == skycoin.SKY_OK
        if prevHash.__eq__(currentHash) > 0:
            return 0
        if i % 2 != 0:
            prevHash = hash_2
            currentHash = hash_1
        else:
            prevHash = hash_1
            currentHash = hash_2
        prev += 1
        current += 1
    return 1
Example #2
0
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
Example #3
0
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
Example #4
0
def manualUxArrayIsSorted(uxa):
    sha_1 = skycoin.cipher_SHA256()
    sha_2 = skycoin.cipher_SHA256()
    isSorte = True
    for i in range(len(uxa) - 1):
        assert skycoin.SKY_coin_UxOut_Hash(uxa[i], sha_1) == skycoin.SKY_OK
        assert skycoin.SKY_coin_UxOut_Hash(uxa[i + 1], sha_2) == skycoin.SKY_OK
        if sha_1 > sha_2:
            isSorte = False
    return isSorte
Example #5
0
def test_TestUxArrayHashArray():
    uxa = utils.makeUxArray(4)
    sha = skycoin.cipher_SHA256()
    err, hashs = skycoin.SKY_coin_UxArray_Hashes(uxa)
    assert err == skycoin.SKY_OK
    assert len(hashs) == len(uxa)
    skycoin.SKY_coin_UxOut_Hash(uxa[0], sha)
    print(sha)
    print(uxa[0])
    assert hashs[0] == sha
    for i in range(len(hashs)):
        assert skycoin.SKY_coin_UxOut_Hash(uxa[i], sha) == 0
        assert sha == hashs[i]
Example #6
0
def test_TestUxOutHash():
    uxb, _ = utils.makeUxBodyWithSecret()
    uxo, _ = utils.makeUxOutWithSecret()
    uxo.Body = uxb
    hash_body = skycoin.cipher_SHA256()
    hash_out = skycoin.cipher_SHA256()
    assert skycoin.SKY_coin_UxBody_Hash(uxb, hash_body) == skycoin.SKY_OK
    assert skycoin.SKY_coin_UxOut_Hash(uxo, hash_out) == skycoin.SKY_OK
    assert hash_body == hash_out
    # Head should not affect hash
    uxh = skycoin.coin__UxHead()
    uxh.Time = 0
    uxh.BkSeq = 1
    uxo.Head = uxh
    assert skycoin.SKY_coin_UxOut_Hash(uxo, hash_out) == skycoin.SKY_OK
    assert hash_body == hash_out
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
Example #8
0
def makeTransactionFromUxOut(ux, s):
    _, handle = skycoin.SKY_coin_Create_Transaction()
    _, tx = skycoin.SKY_coin_GetTransactionObject(handle)
    h = skycoin.cipher_SHA256()
    assert skycoin.SKY_cipher_SecKey_Verify(s) == skycoin.SKY_OK
    assert skycoin.SKY_coin_UxOut_Hash(ux, h) == skycoin.SKY_OK
    r = skycoin.SKY_coin_Transaction_PushInput(handle, h)
    assert skycoin.SKY_coin_Transaction_PushOutput(handle, makeAddress(),
                                                   int(1e6),
                                                   int(50)) == skycoin.SKY_OK
    assert skycoin.SKY_coin_Transaction_PushOutput(handle, makeAddress(),
                                                   int(5e6),
                                                   int(50)) == skycoin.SKY_OK
    secKeys = []
    secKeys.append(s)
    assert skycoin.SKY_coin_Transaction_SignInputs(handle,
                                                   secKeys) == skycoin.SKY_OK
    assert skycoin.SKY_coin_Transaction_UpdateHeader(handle) == skycoin.SKY_OK
    return handle, tx