Exemplo n.º 1
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
Exemplo n.º 2
0
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
Exemplo n.º 3
0
def sign_hash(hashHex, seckeyHex):
    seckey = skycoin.cipher_Sig()
    error = skycoin.SKY_cipher_SecKeyFromHex(seckeyHex.encode(), seckey)
    if error != 0:
        return make_response(
            jsonify(build_error('Invalid Input Format',
                                error_codes.badFormat)), 400)

    sha256 = skycoin.cipher_SHA256()
    error = skycoin.SKY_cipher_SHA256FromHex(hashHex.encode(), sha256)
    if error != 0:
        return make_response(
            jsonify(build_error('Invalid Input Format',
                                error_codes.badFormat)), 400)

    signedHash = skycoin.cipher__Sig()
    error = skycoin.SKY_cipher_SignHash(hash, seckey, signedHash)
    if error != 0:
        return make_response(
            jsonify(build_error('Unknown Server Error', error_codes.unknown)),
            500)

    error, signedHashHex = SKY_cipher_Sig_Hex(signedHash)
    if error != 0:
        return make_response(
            jsonify(build_error('Unknown Server Error', error_codes.unknown)),
            500)

    retvalue = {"signedTransaction": signedHashHex}
    return jsonify(retvalue)
Exemplo n.º 4
0
def test_TestPubKeyFromSig():
    public_key = skycoin.cipher_PubKey()
    secret_key = skycoin.cipher_SecKey()
    skycoin.SKY_cipher_GenerateKeyPair(public_key, secret_key)
    sha_sum = skycoin.cipher_SHA256()
    _, data = skycoin.SKY_cipher_RandByte(256)
    skycoin.SKY_cipher_SumSHA256(data, sha_sum)
    sig_1 = skycoin.cipher_Sig()
    skycoin.SKY_cipher_SignHash(sha_sum, secret_key, sig_1)
    public_key_2 = skycoin.cipher_PubKey()
    assert skycoin.SKY_cipher_PubKeyFromSig(sig_1, sha_sum,
                                            public_key_2) == skycoin.SKY_OK
    assert public_key == public_key_2
    sig_2 = skycoin.cipher_Sig()
    assert skycoin.SKY_cipher_PubKeyFromSig(
        sig_2, sha_sum,
        public_key_2) == skycoin.SKY_ErrInvalidSigPubKeyRecovery
Exemplo n.º 5
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
Exemplo n.º 6
0
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
Exemplo n.º 7
0
def test_TestNewSig():
    sig = skycoin.cipher_Sig()
    _, data = skycoin.SKY_cipher_RandByte(64)
    assert skycoin.SKY_cipher_NewSig(data,
                                     sig) == skycoin.SKY_ErrInvalidLengthSig
    _, data = skycoin.SKY_cipher_RandByte(66)
    assert skycoin.SKY_cipher_NewSig(data,
                                     sig) == skycoin.SKY_ErrInvalidLengthSig
    _, data = skycoin.SKY_cipher_RandByte(67)
    assert skycoin.SKY_cipher_NewSig(data,
                                     sig) == skycoin.SKY_ErrInvalidLengthSig
    _, data = skycoin.SKY_cipher_RandByte(0)
    assert skycoin.SKY_cipher_NewSig(data,
                                     sig) == skycoin.SKY_ErrInvalidLengthSig
    _, data = skycoin.SKY_cipher_RandByte(100)
    assert skycoin.SKY_cipher_NewSig(data,
                                     sig) == skycoin.SKY_ErrInvalidLengthSig
    _, data = skycoin.SKY_cipher_RandByte(65)
    assert skycoin.SKY_cipher_NewSig(data, sig) == skycoin.SKY_OK
    assert sig.toStr() == data
Exemplo n.º 8
0
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