コード例 #1
0
 public void TestAddressBulk()
 {
     for (int i = 0; i < 1024; i++)
     {
         var pub = new cipher_PubKey();
         var sec = new cipher_SecKey();
         var b   = new GoSlice();
         var err = SKY_cipher_RandByte(32, b);
         Assert.AreEqual(err, SKY_OK);
         err = SKY_cipher_GenerateDeterministicKeyPair(b, pub, sec);
         Assert.AreEqual(err, SKY_OK);
         var a = new cipher__Address();
         err = SKY_cipher_AddressFromPubKey(pub, a);
         Assert.AreEqual(err, SKY_OK);
         err = SKY_cipher_Address_Verify(a, pub);
         Assert.AreEqual(err, SKY_OK);
         var s = new _GoString_();
         err = SKY_cipher_Address_String(a, s);
         Assert.AreEqual(err, SKY_OK);
         var a2 = new cipher__Address();
         err = SKY_cipher_DecodeBase58Address(s.p, a2);
         Assert.AreEqual(err, SKY_OK);
         Assert.AreEqual(a2.isEqual(a), 1);
     }
 }
コード例 #2
0
        public void TestCreateUnspents()
        {
            var a = new cipher__Address();
            var p = new cipher_PubKey();
            var s = new cipher_SecKey();

            var err = SKY_cipher_GenerateKeyPair(p, s);

            Assert.AreEqual(err, SKY_OK);
            err = SKY_cipher_AddressFromPubKey(p, a);
            Assert.AreEqual(err, SKY_OK);
            var h   = new cipher_SHA256();
            var txn = new_Transaction__Handlep();

            makeEmptyTransaction(txn);
            err = SKY_coin_Transaction_PushOutput(txn, a, (ulong)11e6, 255);
            Assert.AreEqual(err, SKY_OK);
            var bh = new coin__BlockHeader();

            bh.Time  = 0;
            bh.BkSeq = 1;
            var uxouts = new coin_UxOutArray();

            err = SKY_coin_CreateUnspents(bh, txn, uxouts);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(uxouts.count, 1);
        }
コード例 #3
0
        public void TestAddressRoundtrip()
        {
            // Tests encode and decode
            var p   = new cipher_PubKey();
            var s   = new cipher_SecKey();
            var err = SKY_cipher_GenerateKeyPair(p, s);

            Assert.AreEqual(err, SKY_OK);
            var a = new cipher__Address();

            err = SKY_cipher_AddressFromPubKey(p, a);
            Assert.AreEqual(err, SKY_OK);
            var b = new GoSlice();

            err = SKY_cipher_Address_Bytes(a, b);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_cipher_AddressFromBytes(b, a);
            Assert.AreEqual(err, SKY_OK);
            var a2 = new cipher__Address();

            err = SKY_cipher_AddressFromBytes(b, a2);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(a.isEqual(a2), 1);
            var str  = new _GoString_();
            var str2 = new _GoString_();

            err = SKY_cipher_Address_String(a, str);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_cipher_Address_String(a2, str2);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(str.p == str2.p, true);
        }
コード例 #4
0
        public void TestAddressFromBytes()
        {
            var  s   = new cipher_SecKey();
            var  p   = new cipher_PubKey();
            uint err = SKY_cipher_GenerateKeyPair(p, s);

            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);
            var b  = new GoSlice();
            var b2 = new GoSlice();

            err = SKY_cipher_Address_Bytes(a, b);
            err = SKY_cipher_Address_Bytes(a, b2);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_cipher_AddressFromBytes(b2, a2);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(a2.isEqual(a), 1);
            // Invalid number of bytes
            long len_b = b.len;

            b.len = b.len - 2;
            err   = SKY_cipher_AddressFromBytes(b, a2);
            Assert.AreEqual(SKY_ErrAddressInvalidLength, err, "Failed SKY_cipher_AddressFromBytes 2");
            b.len = len_b;
            a.setVersion('2');
            err = SKY_cipher_Address_Bytes(a, b);
            Assert.AreEqual(err, SKY_OK, "SKY_cipher_Address_Bytes SKY_ErrAddressInvalidVersion");
            err = SKY_cipher_AddressFromBytes(b, a);
            Assert.AreEqual(SKY_ErrAddressInvalidVersion, err);
        }
コード例 #5
0
        public void TestAddressVerify()
        {
            var p   = new cipher_PubKey();
            var s   = new cipher_SecKey();
            var err = SKY_cipher_GenerateKeyPair(p, s);

            Assert.AreEqual(err, SKY_OK);
            var a = new cipher__Address();

            err = SKY_cipher_AddressFromPubKey(p, a);
            Assert.AreEqual(err, SKY_OK);
            // Valid pubkey+address
            err = SKY_cipher_Address_Verify(a, p);
            Assert.AreEqual(err, SKY_OK);
            // Invalid pubkey
            err = SKY_cipher_Address_Verify(a, new cipher_PubKey());
            Assert.AreEqual(err, SKY_ErrAddressInvalidPubKey);
            var p2 = new cipher_PubKey();
            var s2 = new cipher_SecKey();

            err = SKY_cipher_GenerateKeyPair(p2, s2);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_cipher_Address_Verify(a, p2);
            Assert.AreEqual(err, SKY_ErrAddressInvalidPubKey);
            // Bad version
            a.setVersion('1');
            err = SKY_cipher_Address_Verify(a, p);
            Assert.AreEqual(err, SKY_ErrAddressInvalidVersion);
        }
コード例 #6
0
        public void TestAddressUxOutsFlatten()
        {
            var empty = new coin_UxOutArray();

            makeUxArray(empty, 0);
            var up  = new_AddressUxOuts__HandlePtr();
            var err = SKY_coin_NewAddressUxOuts(empty, up);

            Assert.AreEqual(err, SKY_OK);
            var uxs = new coin_UxOutArray();

            makeUxArray(uxs, 3);
            var uxo = uxs.getAt(2);

            uxo.Body.Address = uxs.getAt(1).Body.Address;
            uxs.setAt(0, uxo);
            var emptyAddr = new cipher__Address();

            makeAddress(emptyAddr);

            // An empty array
            err = SKY_coin_AddressUxOuts_Set(up, emptyAddr, empty);
            Assert.AreEqual(err, SKY_OK);
            // 1 element array
            var arg = new coin_UxOutArray();

            arg.release();
            arg.allocate(1);
            arg.setAt(0, uxs.getAt(0));
            err = SKY_coin_AddressUxOuts_Set(up, uxs.getAt(0).Body.Address, arg);
            Assert.AreEqual(err, SKY_OK);
            arg.release();
            arg.allocate(2);
            arg.setAt(0, uxs.getAt(1));
            arg.setAt(1, uxs.getAt(2));
            err = SKY_coin_AddressUxOuts_Set(up, uxs.getAt(1).Body.Address, arg);
            Assert.AreEqual(err, SKY_OK);

            var flat = new coin_UxOutArray();

            err = SKY_coin_AddressUxOuts_Flatten(up, flat);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(flat.count, 2);

            for (int i = 0; i < flat.count; i++)
            {
                var ux = flat.getAt(i);
                Assert.AreNotEqual(ux.Body.Address.isEqual(emptyAddr), 1, "Is equal " + i.ToString());
            }
            if (flat.getAt(0).Body.Address.isEqual(uxs.getAt(0).Body.Address) == 1)
            {
                Assert.AreEqual(flat.getAt(0).isEqual(uxs.getAt(0)), 0);
                Assert.AreEqual(flat.getAt(0).Body.Address.isEqual(uxs.getAt(0).Body.Address), 1);
                Assert.AreEqual(flat.getAt(0 + 1).Body.Address.isEqual(uxs.getAt(0 + 1).Body.Address), 1);
                Assert.AreEqual(flat.getAt(0 + 1).isEqual(uxs.getAt(1)), 0);
            }
        }
コード例 #7
0
        public cipher__Address makeAddress()
        {
            var pubkey = new cipher_PubKey();
            var seckey = new cipher_SecKey();
            var addr   = new cipher__Address();
            var result = skycoin.skycoin.SKY_cipher_GenerateKeyPair(pubkey, seckey);

            Assert.AreEqual(result, skycoin.skycoin.SKY_OK);
            result = skycoin.skycoin.SKY_cipher_AddressFromPubKey(pubkey, addr);
            return(addr);
        }
コード例 #8
0
        public void TestAddressNull()
        {
            var a = new cipher__Address();

            Assert.AreEqual(a.isEqual(new cipher__Address()), 1);
            var p   = new cipher_PubKey();
            var s   = new cipher_SecKey();
            var err = SKY_cipher_GenerateKeyPair(p, s);

            Assert.AreEqual(err, SKY_OK);
            err = SKY_cipher_AddressFromPubKey(p, a);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(a.isEqual(new cipher__Address()), 0);
        }
コード例 #9
0
        public void TestCreateUnspent()
        {
            var p = new cipher_PubKey();
            var s = new cipher_SecKey();
            var a = new cipher__Address();

            var err = SKY_cipher_GenerateKeyPair(p, s);

            Assert.AreEqual(err, SKY_OK);
            err = SKY_cipher_AddressFromPubKey(p, a);
            Assert.AreEqual(err, SKY_OK);
            var h      = new cipher_SHA256();
            var handle = new_Transaction__Handlep();

            makeEmptyTransaction(handle);
            err = SKY_coin_Transaction_PushOutput(handle, a, 11000000, 255);
            Assert.AreEqual(err, SKY_OK);
            var bh = new coin__BlockHeader();

            bh.Time  = 0;
            bh.BkSeq = 1;
            testcase[] t  = new testcase[2];
            var        tc = new testcase();

            tc.index   = 0;
            tc.failure = SKY_OK;
            t[0]       = tc;
            tc         = new testcase();
            tc.failure = SKY_ERROR;
            tc.index   = 10;
            t[1]       = tc;
            var ux          = new coin__UxOut();
            var tests_count = t.Length;

            for (int i = 0; i < tests_count; i++)
            {
                err = SKY_coin_CreateUnspent(bh, handle, t[i].index, ux);
                if (t[i].failure == SKY_ERROR)
                {
                    continue;
                }
                Assert.AreEqual(bh.Time, ux.Head.Time);
                Assert.AreEqual(bh.BkSeq, ux.Head.BkSeq);
            }
        }
コード例 #10
0
        public void TestDecodeBase58Address()
        {
            uint err = 0;
            var  s   = new cipher_SecKey();
            var  p   = new cipher_PubKey();

            Assert.AreEqual(SKY_cipher_GenerateKeyPair(p, s), SKY_OK, "Failed SKY_cipher_GenerateKeyPair");
            err = SKY_cipher_SecKey_Verify(s);
            Assert.AreEqual(err, SKY_OK, "Failed SKY_cipher_SecKey_Verify ");
            err = SKY_cipher_PubKey_Verify(p);
            Assert.AreEqual(err, SKY_OK, "Failed SKY_cipher_PubKey_Verify");
            var a  = new cipher__Address();
            var a1 = new cipher__Address();
            var a2 = new cipher__Address();

            err = SKY_cipher_AddressFromPubKey(p, a);
            Assert.AreEqual(err, SKY_OK, "Failed SKY_cipher_AddressFromPubKey");
            Assert.AreEqual(err, SKY_OK, "Failed SKY_cipher_AddressFromPubKey");
            err = SKY_cipher_Address_Verify(a, p);
            Assert.AreEqual(err, SKY_OK, " Failed SKY_cipher_Address_Verify");
            err = SKY_cipher_DecodeBase58Address("", a1);
            Assert.AreEqual(err, SKY_ERROR, "Failed SKY_cipher_DecodeBase58Address 1");
            err = SKY_cipher_DecodeBase58Address("asa", a1);
            Assert.AreEqual(err, SKY_ErrAddressInvalidLength, "Failed SKY_cipher_DecodeBase58Address 2");

            var addrStr = new _GoString_();

            err = SKY_cipher_Address_String(a, addrStr);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_cipher_DecodeBase58Address(addrStr.p, a2);
            Assert.AreEqual(err, SKY_OK, "Failed SKY_cipher_DecodeBase58Address");
            Assert.IsTrue(a.isEqual(a2) == 1);
            var as2 = " " + addrStr.p;

            err = SKY_cipher_DecodeBase58Address(as2, a1);
            Assert.AreEqual(err, SKY_ERROR, "Failed SKY_cipher_DecodeBase58Address 31");

            as2 = "000" + addrStr.p;
            err = SKY_cipher_DecodeBase58Address(as2, a1);
            Assert.AreEqual(err, SKY_ERROR, "Failed SKY_cipher_DecodeBase58Address 3");

            as2 = addrStr.p + "000";
            err = SKY_cipher_DecodeBase58Address(as2, a1);
            Assert.AreEqual(err, SKY_ERROR, "Failed SKY_cipher_DecodeBase58Address 30");
        }
コード例 #11
0
        public void TestPubKeyToAddress()
        {
            var p     = new cipher_PubKey();
            var s     = new cipher_SecKey();
            var addr  = new cipher__Address();
            var addr1 = new cipher__Address();
            var err   = SKY_cipher_GenerateKeyPair(p, s);

            err = SKY_cipher_AddressFromPubKey(p, addr);
            err = SKY_cipher_Address_Verify(addr, p);
            Assert.AreEqual(err, SKY_OK);
            var addrStr = new _GoString_();

            err = SKY_cipher_Address_String(addr, addrStr);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_cipher_DecodeBase58Address(addrStr.p, addr1);
            Assert.AreEqual(err, SKY_OK);
        }
コード例 #12
0
        public void makeUxBodyWithSecret(coin__UxBody uxBody, cipher_SecKey secKey)
        {
            var p = new cipher_PubKey();

            skycoin.skycoin.SKY_cipher_GenerateKeyPair(p, secKey);
            var b = new GoSlice();

            skycoin.skycoin.SKY_cipher_RandByte(128, b);
            var h = new cipher_SHA256();

            skycoin.skycoin.SKY_cipher_SumSHA256(b, h);
            uxBody.SetSrcTransaction(h);
            var a = new cipher__Address();

            skycoin.skycoin.SKY_cipher_AddressFromPubKey(p, a);
            uxBody.Address = a;
            uxBody.Coins   = (ulong)(1e6);
            uxBody.Hours   = 100;
        }
コード例 #13
0
        public void TestSignHash()
        {
            var p = new cipher_PubKey();
            var s = new cipher_SecKey();
            var a = new cipher__Address();

            SKY_cipher_GenerateKeyPair(p, s);
            var err = SKY_cipher_AddressFromPubKey(p, a);

            Assert.AreEqual(err, SKY_OK);
            var b_temp = new GoSlice();

            SKY_cipher_RandByte(256, b_temp);
            var h = new cipher_SHA256();

            SKY_cipher_SumSHA256(b_temp, h);
            var sig = new cipher_Sig();

            SKY_cipher_SignHash(h, s, sig);
            Assert.AreEqual(sig.isEqual(new cipher_Sig()), 0);
        }
コード例 #14
0
        public void TestNewGenesisBlock()
        {
            var   pubkey   = new cipher_PubKey();
            var   seckey   = new cipher_SecKey();
            ulong genTime  = 1000;
            ulong genCoins = (ulong)1000e6;

            var err = SKY_cipher_GenerateKeyPair(pubkey, seckey);

            Assert.AreEqual(err, SKY_OK);
            var addr = new cipher__Address();

            err = SKY_cipher_AddressFromPubKey(pubkey, addr);
            Assert.AreEqual(err, SKY_OK);
            var block = new_Block__HandlePtr();

            err = SKY_coin_NewGenesisBlock(addr, genCoins, genTime, block);
            Assert.AreEqual(err, SKY_OK);
            var pBlockPP = new_coin__BlockPtr();

            err = SKY_coin_GetBlockObject(block, pBlockPP);
            var pBlock = coin__BlockPtr_value(pBlockPP);

            Assert.AreEqual(err, SKY_OK);
            var nullHash = new cipher_SHA256();
            var Head     = new GoSlice();

            err = SKY_coin_BlockHeader_Bytes(pBlock.Head, Head);
            Assert.AreEqual(err, SKY_OK);
            var strnullHash = nullHash.getStr();
            var strHead     = new _GoString_();

            Head.getString(strHead);
            Assert.AreEqual(strnullHash.p, strHead.p);
            Assert.AreEqual(genTime, pBlock.Head.Time);
            Assert.AreEqual(0, pBlock.Head.BkSeq);
            Assert.AreEqual(0, pBlock.Head.Version);
            Assert.AreEqual(0, pBlock.Head.Fee);
        }
コード例 #15
0
        public void TestVerifyTransactionFee()
        {
            FullburnFactor2verifyTxFeeTestCase();
            var empty = transutils.makeEmptyTransaction();
            var hours = new_GoUint64p();
            var err   = SKY_coin_Transaction_OutputHours(empty, hours);

            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(GoUint64p_value(hours), 0);

            // A txn with no outputs hours and no coinhours burn fee is valid
            err = SKY_fee_VerifyTransactionFee(empty, 0, 2);
            Assert.AreEqual(err, SKY_ErrTxnNoFee);

            // A txn with no outputs hours but with a coinhours burn fee is valid
            err = SKY_fee_VerifyTransactionFee(empty, 100, 2);
            Assert.AreEqual(err, SKY_OK);
            var txn  = transutils.makeEmptyTransaction();
            var addr = new cipher__Address();

            addr = transutils.makeAddress();
            Assert.AreEqual(err, 0);
            err = SKY_coin_Transaction_PushOutput(txn, addr, 0, 1000000);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_PushOutput(txn, addr, 0, 3000000);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_OutputHours(txn, hours);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(GoUint64p_value(hours), 4000000);

            // A txn with insufficient net coinhours burn fee is invalid
            err = SKY_fee_VerifyTransactionFee(txn, 0, 2);
            Assert.AreEqual(err, SKY_ErrTxnNoFee);
            err = SKY_fee_VerifyTransactionFee(txn, 1, 2);
            Assert.AreEqual(err, SKY_ErrTxnInsufficientFee);

            // A txn with sufficient net coinhours burn fee is valid
            err = SKY_coin_Transaction_OutputHours(txn, hours);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_fee_VerifyTransactionFee(txn, GoUint64p_value(hours), 2);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_coin_Transaction_OutputHours(txn, hours);
            Assert.AreEqual(err, SKY_OK);
            err = SKY_fee_VerifyTransactionFee(txn, ((ulong)(GoUint64p_value(hours) * 10)), 2);
            Assert.AreEqual(err, SKY_OK);

            // fee + hours overflows
            err = SKY_fee_VerifyTransactionFee(txn, ((ulong.MaxValue - 3000000)), 2);
            Assert.AreEqual(err, SKY_ERROR);

            // txn has overflowing output hours
            err = SKY_coin_Transaction_PushOutput(txn, addr, 0,
                                                  (ulong.MaxValue - 1000000 - 3000000 + 1));
            Assert.AreEqual(err, SKY_OK, "txn has overflowing output hours");
            err = SKY_fee_VerifyTransactionFee(txn, 10, 2);
            Assert.AreEqual(err, SKY_ERROR, "SKY_fee_VerifyTransactionFee failed");

            int len = burnFactor2verifyTxFeeTestCase.Length;

            for (int i = 0; i < len; i++)
            {
                txn = new_Transaction__Handlep();
                txn = transutils.makeEmptyTransaction();
                verifyTxFeeTestCase tc = burnFactor2verifyTxFeeTestCase[i];
                err = SKY_coin_Transaction_PushOutput(txn, addr, 0, tc.ouputHours);
                Assert.AreEqual(err, SKY_OK);
                Assert.IsTrue(tc.inputHours >= tc.ouputHours);
                err = SKY_fee_VerifyTransactionFee(txn, (ulong)(tc.inputHours - tc.ouputHours), 2);
                Assert.AreEqual(tc.err, err, "Iter " + i.ToString() + " is " + tc.err.ToString() + " != " + err.ToString());;
            }
        }
コード例 #16
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);
        }
コード例 #17
0
        public void TestUxOutCoinHours()
        {
            var p = new cipher_PubKey();
            var s = new cipher_SecKey();

            SKY_cipher_GenerateKeyPair(p, s);
            var uxb = new coin__UxBody();
            var b   = new GoSlice();

            SKY_cipher_RandByte(128, b);
            var h   = new cipher_SHA256();
            var err = SKY_cipher_SumSHA256(b, h);

            Assert.AreEqual(err, SKY_OK);
            uxb.SetSrcTransaction(h);
            var a = new cipher__Address();

            SKY_cipher_AddressFromPubKey(p, a);
            uxb.Address = a;
            uxb.Coins   = (ulong)1e6;
            uxb.Hours   = 100;
            var uxo = new coin__UxOut();
            var uxh = new coin__UxHead();

            uxh.Time  = 100;
            uxh.BkSeq = 2;
            uxo.Head  = uxh;
            uxo.Body  = uxb;

            // Less than an hour passed
            var now   = uxh.Time + 100;
            var hours = new_GoUint64p();

            err = SKY_coin_UxOut_CoinHours(uxo, now, hours);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(uxh.Time, GoUint64p_value(hours), "Less than an hour passed");
            // 1 hours passed
            now = uxh.Time + 3600;
            err = SKY_coin_UxOut_CoinHours(uxo, now, hours);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(GoUint64p_value(hours), uxh.Time + (uxb.Coins / 1000000), "1 hours passed");
            // 6 hours passed
            now = uxh.Time + 3600 * 6;
            err = SKY_coin_UxOut_CoinHours(uxo, now, hours);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(GoUint64p_value(hours), uxh.Time + (uxb.Coins / 1000000) * 6, "1 hours passed");
            // Time is backwards (treated as no hours passed)
            now = uxh.Time / 2;
            err = SKY_coin_UxOut_CoinHours(uxo, now, hours);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(GoUint64p_value(hours), uxh.Time);
            // 1 hour has passed, output has 1.5 coins, should gain 1 coinhour
            uxb.Coins = 1500000;
            now       = uxh.Time + 3600;
            err       = SKY_coin_UxOut_CoinHours(uxo, now, hours);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(GoUint64p_value(hours), uxb.Hours + 1);
            // 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       = SKY_coin_UxOut_CoinHours(uxo, now, hours);
            Assert.AreEqual(err, 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       = SKY_coin_UxOut_CoinHours(uxo, now, hours);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(GoUint64p_value(hours), uxb.Hours + 1);
            // 1000000 hours minus 1 second have passed, output has 1 droplet, should gain 0 coin hour
            uxb.Coins = 1;
            uxo.Body  = uxb;
            now       = (ulong)(uxh.Time + (ulong)Convert.ToInt64(1e6 * 3600) - 1);
            err       = SKY_coin_UxOut_CoinHours(uxo, now, hours);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(GoUint64p_value(hours), uxb.Hours);
            // 1000000 hours have passed, output has 1 droplet, should gain 1 coin hour
            uxb.Coins = 1;
            uxo.Body  = uxb;
            now       = uxh.Time + Convert.ToUInt64(10e5 * 3600);
            err       = SKY_coin_UxOut_CoinHours(uxo, now, hours);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(GoUint64p_value(hours), uxb.Hours + 1);
            // No hours passed, using initial coin hours
            uxb.Coins = (ulong)10e8;
            uxb.Hours = 1000 * 1000;
            uxo.Body  = uxb;
            now       = uxh.Time;
            err       = SKY_coin_UxOut_CoinHours(uxo, now, hours);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(GoUint64p_value(hours), uxb.Hours);
            // One hour passed, using initial coin hours
            now = uxh.Time + 3600;
            err = SKY_coin_UxOut_CoinHours(uxo, now, hours);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(GoUint64p_value(hours), uxb.Hours + (10e8 / 10e5));
            // No hours passed and no hours to begin with 0
            uxb.Hours = 0;
            uxo.Body  = uxb;
            now       = uxh.Time;
            err       = SKY_coin_UxOut_CoinHours(uxo, now, hours);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(GoUint64p_value(hours), 0);
            // Centuries have passed, time-based calculation overflows uint64 when calculating the whole coin seconds
            uxb.Coins = (ulong)20e5;
            uxo.Body  = uxb;
            now       = 0xFFFFFFFFFFFFFFFF;
            err       = SKY_coin_UxOut_CoinHours(uxo, now, hours);
            Assert.AreEqual(err, SKY_ERROR);
            // Centuries have passed, time-based calculation overflows uint64 when calculating the droplet seconds
            uxb.Coins = (ulong)15e5;
            uxo.Body  = uxb;
            now       = 0xFFFFFFFFFFFFFFFF;
            err       = SKY_coin_UxOut_CoinHours(uxo, now, hours);
            Assert.AreEqual(err, SKY_ERROR);
            // Output would overflow if given more hours, has reached its limit
            uxb.Coins = (ulong)36e8;
            uxo.Body  = uxb;
            now       = 0xFFFFFFFFFFFFFFFF;
            err       = SKY_coin_UxOut_CoinHours(uxo, now, hours);
            Assert.AreEqual(err, SKY_ERROR);
        }
コード例 #18
0
        public void TestUxOutSnapshotHash()
        {
            var p   = new cipher_PubKey();
            var s   = new cipher_SecKey();
            var err = SKY_cipher_GenerateKeyPair(p, s);

            Assert.AreEqual(err, SKY_OK);
            var uxb = new coin__UxBody();
            var b   = new GoSlice();

            err = SKY_cipher_RandByte(128, b);
            Assert.AreEqual(err, SKY_OK);
            var h = new cipher_SHA256();

            err = SKY_cipher_SumSHA256(b, h);
            Assert.AreEqual(err, SKY_OK);
            uxb.SetSrcTransaction(h);
            var a = new cipher__Address();

            err = SKY_cipher_AddressFromPubKey(p, a);
            Assert.AreEqual(err, SKY_OK);
            uxb.Address = a;
            uxb.Coins   = (ulong)1e6;
            uxb.Hours   = (ulong)100;
            var uxo = new coin__UxOut();
            var uxh = new coin__UxHead();

            uxh.Time  = 100;
            uxh.BkSeq = 2;
            uxo.Head  = uxh;
            uxo.Body  = uxb;
            var hn = new cipher_SHA256();

            err = SKY_coin_UxOut_SnapshotHash(uxo, hn);
            Assert.AreEqual(err, SKY_OK);
            // snapshot hash should be dependent on every field in body and head
            // Head Time
            var uxo_2 = uxo;

            uxh.Time   = 20;
            uxo_2.Head = uxh;
            var hn_2 = new cipher_SHA256();

            err = SKY_coin_UxOut_SnapshotHash(uxo_2, hn_2);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(hn.isEqual(hn_2), 0);
            // Head BkSeq
            uxo_2      = uxo;
            uxh.BkSeq  = 4;
            uxo_2.Head = uxh;
            hn_2       = new cipher_SHA256();
            err        = SKY_coin_UxOut_SnapshotHash(uxo_2, hn_2);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(hn.isEqual(hn_2), 0);
            // Body SetSrcTransaction
            uxo_2 = uxo;
            uxb   = new coin__UxBody();
            err   = SKY_cipher_RandByte(128, b);
            Assert.AreEqual(err, SKY_OK);
            h   = new cipher_SHA256();
            err = SKY_cipher_SumSHA256(b, h);
            Assert.AreEqual(err, SKY_OK);
            uxb.SetSrcTransaction(h);
            uxo_2.Body = uxb;
            hn_2       = new cipher_SHA256();
            err        = SKY_coin_UxOut_SnapshotHash(uxo_2, hn_2);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(hn.isEqual(hn_2), 0);
            // Body Address
            var p_2 = new cipher_PubKey();
            var s_2 = new cipher_SecKey();

            err = SKY_cipher_GenerateKeyPair(p_2, s_2);
            Assert.AreEqual(err, SKY_OK);
            var a_2 = new cipher__Address();

            err = SKY_cipher_AddressFromPubKey(p_2, a_2);
            Assert.AreEqual(err, SKY_OK);
            uxo_2       = uxo;
            uxb         = new coin__UxBody();
            uxb.Address = a_2;
            uxo_2.Body  = uxb;
            hn_2        = new cipher_SHA256();
            err         = SKY_coin_UxOut_SnapshotHash(uxo_2, hn_2);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(hn.isEqual(hn_2), 0);
            // Body Coins
            uxo_2      = uxo;
            uxb        = new coin__UxBody();
            uxb.Coins  = 2;
            uxo_2.Body = uxb;
            hn_2       = new cipher_SHA256();
            err        = SKY_coin_UxOut_SnapshotHash(uxo_2, hn_2);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(hn.isEqual(hn_2), 0);
            // Body Hours
            uxo_2      = uxo;
            uxb        = new coin__UxBody();
            uxb.Hours  = 2;
            uxo_2.Body = uxb;
            hn_2       = new cipher_SHA256();
            err        = SKY_coin_UxOut_SnapshotHash(uxo_2, hn_2);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(hn.isEqual(hn_2), 0);
        }