public void makeTransactionFromUxOut(coin__UxOut ux, cipher_SecKey s, SWIGTYPE_p_Transaction__Handle handle, coin__Transaction ptx)
        {
            handle = makeEmptyTransaction();
            var h = new cipher_SHA256();

            Assert.AreEqual(skycoin.skycoin.SKY_cipher_SecKey_Verify(s), skycoin.skycoin.SKY_OK);
            var err = skycoin.skycoin.SKY_coin_UxOut_Hash(ux, h);

            Assert.AreEqual(err, skycoin.skycoin.SKY_OK);
            var r = new uint();

            r = skycoin.skycoin.SKY_coin_Transaction_PushInput(handle, h);
            Assert.AreEqual(err, skycoin.skycoin.SKY_OK);
            err = skycoin.skycoin.SKY_coin_Transaction_PushOutput(handle, makeAddress(), (ulong)1e6, 50);
            Assert.AreEqual(err, skycoin.skycoin.SKY_OK);
            err = skycoin.skycoin.SKY_coin_Transaction_PushOutput(handle, makeAddress(), (ulong)5e6, 50);
            Assert.AreEqual(err, skycoin.skycoin.SKY_OK);
            var seckeys = new cipher_SecKeys();

            seckeys.allocate(1);
            seckeys.setAt(0, s);
            err = skycoin.skycoin.SKY_coin_Transaction_SignInputs(handle, seckeys);
            Assert.AreEqual(err, skycoin.skycoin.SKY_OK);
            err = skycoin.skycoin.SKY_coin_Transaction_UpdateHeader(handle);
            Assert.AreEqual(err, skycoin.skycoin.SKY_OK);
            err = skycoin.skycoin.SKY_coin_GetTransactionObject(handle, ptx);
            Assert.AreEqual(err, skycoin.skycoin.SKY_OK);
            err = skycoin.skycoin.SKY_coin_Transaction_Verify(handle);
            Assert.AreEqual(err, skycoin.skycoin.SKY_OK);
        }
Beispiel #2
0
        public void TestTransactionSignInputs()
        {
            var handle = transutils.makeEmptyTransaction();
            //  Panics if txns already signed
            var sig = new cipher_Sig();
            var err = SKY_coin_Transaction_PushSignature(handle, sig);

            Assert.AreEqual(err, SKY_OK);
            var seckeys = new cipher_SecKeys();

            seckeys.allocate(1);
            seckeys.setAt(0, new cipher_SecKey());
            // Panics if not enough keys
            handle = transutils.makeEmptyTransaction();
            var s   = new cipher_SecKey();
            var s2  = new cipher_SecKey();
            var ux  = new coin__UxOut();
            var ux2 = new coin__UxOut();

            makeUxOutWithSecret(ux, s);
            var h = new cipher_SHA256();

            err = SKY_coin_UxOut_Hash(ux, h);
            Assert.AreEqual(err, SKY_OK);
            var r = new uint();

            r = SKY_coin_Transaction_PushInput(handle, h);
            Assert.AreEqual(r, SKY_OK);
            makeUxOutWithSecret(ux2, s2);
            err = SKY_coin_UxOut_Hash(ux2, h);
            Assert.AreEqual(err, SKY_OK);
            r = SKY_coin_Transaction_PushInput(handle, h);
            Assert.AreEqual(r, SKY_OK);
            err = SKY_coin_Transaction_PushOutput(handle, transutils.makeAddress(), 40, 80);
            Assert.AreEqual(err, SKY_OK);
            var count = new_Gointp();

            err = SKY_coin_Transaction_GetSignaturesCount(handle, count);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(Gointp_value(count), 0);
            // Valid signing
            h = new cipher_SHA256();
            SKY_coin_Transaction_HashInner(handle, h);
            seckeys = new cipher_SecKeys();
            seckeys.allocate(2);
            seckeys.setAt(0, s);
            seckeys.setAt(1, s2);
            err = SKY_coin_Transaction_SignInputs(handle, seckeys);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_GetSignaturesCount(handle, count);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(Gointp_value(count), 2);
            var h2 = new cipher_SHA256();

            err = SKY_coin_Transaction_HashInner(handle, h2);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(h.isEqual(h2), 1);
            var p = new cipher_PubKey();

            err = SKY_cipher_PubKeyFromSecKey(s, p);
            Assert.AreEqual(err, SKY_OK);
            var a  = new cipher__Address();
            var a2 = new cipher__Address();

            err = SKY_cipher_AddressFromPubKey(p, a);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_cipher_PubKeyFromSecKey(s2, p);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_cipher_AddressFromPubKey(p, a2);
            Assert.AreEqual(err, SKY_OK);
            var sha1  = new cipher_SHA256();
            var sha2  = new cipher_SHA256();
            var txin0 = new cipher_SHA256();
            var txin1 = new cipher_SHA256();

            err = SKY_coin_Transaction_GetInputAt(handle, 0, txin0);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_GetInputAt(handle, 1, txin1);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_cipher_AddSHA256(h, txin0, sha1);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_cipher_AddSHA256(h, txin1, sha2);
            Assert.AreEqual(err, SKY_OK);
            var txsig0 = new cipher_Sig();
            var txsig1 = new cipher_Sig();

            err = SKY_coin_Transaction_GetSignatureAt(handle, 0, txsig0);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_GetSignatureAt(handle, 1, txsig1);
            Assert.AreEqual(err, SKY_OK);
        }
Beispiel #3
0
        public void TestTransactionVerify()
        {
            // Mismatch header hash
            var tx  = transutils.makeEmptyTransaction();
            var ptx = makeTransaction(tx);

            ptx.setInnerHash(new cipher_SHA256());
            var err = SKY_coin_Transaction_Verify(tx);

            Assert.AreEqual(err, SKY_ERROR);

            // No inputs
            tx = new_Transaction__Handlep();
            makeTransaction(tx);
            err = SKY_coin_Transaction_ResetInputs(tx, 0);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_UpdateHeader(tx);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_Verify(tx);
            Assert.AreEqual(err, SKY_ERROR);

            // No outputs
            makeTransaction(tx);
            err = SKY_coin_Transaction_ResetOutputs(tx, 0);
            Assert.AreEqual(err, SKY_OK, "");
            err = SKY_coin_Transaction_UpdateHeader(tx);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_Verify(tx);
            Assert.AreEqual(err, SKY_ERROR);

            // Invalid number of Sigs
            makeTransaction(tx);
            err = SKY_coin_Transaction_ResetSignatures(tx, 0);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_UpdateHeader(tx);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_Verify(tx);
            Assert.AreEqual(err, SKY_ERROR);
            err = SKY_coin_Transaction_ResetSignatures(tx, 20);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_UpdateHeader(tx);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_Verify(tx);
            Assert.AreEqual(err, SKY_ERROR);

            // Too many sigs & inputs
            makeTransaction(tx);
            err = SKY_coin_Transaction_ResetSignatures(tx, short.MaxValue);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_ResetInputs(tx, short.MaxValue);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_UpdateHeader(tx);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_Verify(tx);
            Assert.AreEqual(err, SKY_ERROR);

            // Duplicate inputs
            var ux = new coin__UxOut();
            var s  = new cipher_SecKey();
            var h  = new cipher_SHA256();

            makeUxOutWithSecret(ux, s);
            makeTransactionFromUxOut(ux, s, tx);
            err = SKY_coin_Transaction_GetInputAt(tx, 0, h);
            Assert.AreEqual(err, SKY_OK);
            var r = new uint();

            r = SKY_coin_Transaction_PushInput(tx, h);
            Assert.AreEqual(r, SKY_OK);
            err = SKY_coin_Transaction_ResetSignatures(tx, 0);
            Assert.AreEqual(err, SKY_OK);
            var secKeys = new cipher_SecKeys();

            secKeys.allocate(2);
            secKeys.setAt(0, s);
            secKeys.setAt(1, s);
            err = SKY_coin_Transaction_SignInputs(tx, secKeys);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_UpdateHeader(tx);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_Verify(tx);
            Assert.AreEqual(err, SKY_ERROR);

            // Duplicate outputs
            makeTransaction(tx);
            var pOutput = new coin__TransactionOutput();

            err             = SKY_coin_Transaction_GetOutputAt(tx, 0, pOutput);
            pOutput.Address = new cipher__Address();
            err             = SKY_coin_Transaction_ResetOutputs(tx, 0);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_PushOutput(tx, pOutput.Address, pOutput.Coins, pOutput.Hours);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_PushOutput(tx, pOutput.Address, pOutput.Coins, pOutput.Hours);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_UpdateHeader(tx);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_Verify(tx);
            Assert.AreEqual(err, SKY_ERROR);

            // Output coins are 0
            makeTransaction(tx);
            err           = SKY_coin_Transaction_GetOutputAt(tx, 0, pOutput);
            pOutput.Coins = 0;
            err           = SKY_coin_Transaction_ResetOutputs(tx, 0);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_PushOutput(tx, pOutput.Address, pOutput.Coins, pOutput.Hours);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_UpdateHeader(tx);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_Verify(tx);
            Assert.AreEqual(err, SKY_ERROR);

            // Output coin overflow
            makeTransaction(tx);
            err           = SKY_coin_Transaction_GetOutputAt(tx, 0, pOutput);
            pOutput.Coins = (ulong)(ulong.MaxValue - 3e6);
            err           = SKY_coin_Transaction_PushOutput(tx, pOutput.Address, pOutput.Coins, pOutput.Hours);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_UpdateHeader(tx);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_Verify(tx);
            Assert.AreEqual(err, SKY_ERROR);

            // Output coins are not multiples of 1e6 (valid, decimal restriction is not enforced here)
            makeTransaction(tx);
            err = SKY_coin_Transaction_GetOutputAt(tx, 0, pOutput);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_ResetOutputs(tx, 0);
            Assert.AreEqual(err, SKY_OK);
            pOutput.Coins += 10;
            err            = SKY_coin_Transaction_PushOutput(tx, pOutput.Address, pOutput.Coins, pOutput.Hours);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_UpdateHeader(tx);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_ResetSignatures(tx, 0);
            Assert.AreEqual(err, SKY_OK);
            var p = new cipher_PubKey();

            s   = new cipher_SecKey();
            err = SKY_cipher_GenerateKeyPair(p, s);
            Assert.AreEqual(err, SKY_OK);
            secKeys = new cipher_SecKeys();
            secKeys.allocate(1);
            secKeys.setAt(0, s);
            err = SKY_coin_Transaction_SignInputs(tx, secKeys);
            Assert.AreEqual(err, SKY_OK);
            Assert.IsTrue(pOutput.Coins % 1e6 != 0);
            err = SKY_coin_Transaction_Verify(tx);
            Assert.AreEqual(err, SKY_OK);
            // Valid
            makeTransaction(tx);
            err = SKY_coin_Transaction_GetOutputAt(tx, 0, pOutput);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_ResetOutputs(tx, 0);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_PushOutput(tx, pOutput.Address, (ulong)(10e6), pOutput.Hours);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_PushOutput(tx, pOutput.Address, (ulong)(1e6), pOutput.Hours);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_UpdateHeader(tx);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_Verify(tx);
            Assert.AreEqual(err, SKY_OK);
        }