Esempio n. 1
0
        public void EncodeExtraTest()
        {
            var genesisHash = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            var blockHash   = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

            var era = new Era(2048, 99, false);

            var paramsList = new List <byte>();

            paramsList.Add(0xFF);
            paramsList.AddRange(Utils.HexToByteArray("d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"));  // Utils.GetPublicKeyFrom("5FfBQ3kwXrbdyoqLPvcXRp7ikWydXawpNs2Ceu3WwFdhZ8W4");
            CompactInteger amount = 100;

            paramsList.AddRange(amount.Encode());
            byte[] parameters = paramsList.ToArray();

            Method method = new Method(0x06, 0x00, parameters);

            byte[] methodBytes = Utils.StringValueArrayBytesArray("6, 0, 255, 212, 53, 147, 199, 21, 253, 211, 28, 97, 20, 26, 189, 4, 169, 159, 214, 130, 44, 133, 88, 133, 76, 205, 227, 154, 86, 132, 231, 165, 109, 162, 125, 145, 1");

            Assert.AreEqual(methodBytes, method.Encode());

            SignedExtensions signedExtensions = new SignedExtensions(259, 1, new Hash(genesisHash), new Hash(blockHash), era, 0, 0);

            var payload = new Payload(method, signedExtensions);

            byte[] payloadBytes = Utils.StringValueArrayBytesArray("6, 0, 255, 212, 53, 147, 199, 21, 253, 211, 28, 97, 20, 26, 189, 4, 169, 159, 214, 130, 44, 133, 88, 133, 76, 205, 227, 154, 86, 132, 231, 165, 109, 162, 125, 145, 1, 58, 6, 0, 0, 3, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0");

            Assert.AreEqual(payloadBytes, payload.Encode());
        }
Esempio n. 2
0
        public void BalanceTransferMockedTest1()
        {
            byte publicKeyType = 0x00;

            // Utils.HexToByteArray("0x9EFFC1668CA381C242885516EC9FA2B19C67B6684C02A8A3237B6862E5C8CD7E");
            byte[]         publicKey   = Utils.GetPublicKeyFrom("5CxW5DWQDpXi4cpACd62wzbPjbYrx4y67TZEmRXBcvmDTNaM");
            CompactInteger nonce       = 1;
            byte           moduleIndex = 0x04;
            byte           callIndex   = 0x00;

            // Utils.HexToByteArray("0x9EFFC1668CA381C242885516EC9FA2B19C67B6684C02A8A3237B6862E5C8CD7E");
            byte[]         destPublicKey = Utils.GetPublicKeyFrom("5FfBQ3kwXrbdyoqLPvcXRp7ikWydXawpNs2Ceu3WwFdhZ8W4");
            CompactInteger amount        = 987456321;

            byte[]         parameters         = destPublicKey.Concat(amount.Encode()).ToArray();
            byte[]         genesisHash        = new byte[] { 0x00 };
            byte[]         currentBlockHash   = new byte[] { 0x00 };
            ulong          currentBlockNumber = 47;
            CompactInteger tip = 1234;

            // mocked signature
            byte[] signature = Utils.HexToByteArray("0x14AE74DD7964365038EBA44F51C347B9C7070231D56E38EF1024457EBDC6DC03D20226243B1B2731DF6FD80F7170643221BD8BF8D06215D4BFEAC68A2C9D2305");

            Method method = new Method(moduleIndex, callIndex, parameters);

            Era era = new Era(Constants.EXTRINSIC_ERA_PERIOD_DEFAULT, currentBlockNumber, currentBlockNumber == 0 ? true : false);

            var uncheckedExtrinsic = new UnCheckedExtrinsic(true, new Account(KeyType.ED25519, new byte[0], publicKey), method, era, nonce, tip, new Hash(genesisHash), new Hash(currentBlockHash));

            string balanceTransfer = "0x350284278117fc144c72340f67d0f2316e8386ceffbf2b2428c9c51fef7c597f1d426e0014ae74dd7964365038eba44f51c347b9c7070231d56e38ef1024457ebdc6dc03d20226243b1b2731df6fd80f7170643221bd8bf8d06215d4bfeac68a2c9d2305f50204491304009effc1668ca381c242885516ec9fa2b19c67b6684c02a8a3237b6862e5c8cd7e068d6deb";

            uncheckedExtrinsic.AddPayloadSignature(signature);

            Assert.AreEqual(Utils.HexToByteArray(balanceTransfer), uncheckedExtrinsic.Encode());
        }
 public void EncodeDecodeTest2()
 {
     for (int i = 0; i < 1000000; i++)
     {
         CompactInteger c = i;
         Assert.AreEqual(c, CompactInteger.Decode(c.Encode()));
     }
 }
 public void EncodeDecodeTest()
 {
     ulong[] array = new UInt64[] { 0, 1, 255, 256, 65535, 4294967295, 4294967296, 8000000000000000000, 18446744073709551615 };
     foreach (var t in array)
     {
         CompactInteger v = new CompactInteger(t);
         Assert.AreEqual(v, CompactInteger.Decode(v.Encode()));
     }
 }
Esempio n. 5
0
        public void BalanceTransferMockedTest2()
        {
            //[
            //  {
            //    "signature": {
            //      "signer": "5CxW5DWQDpXi4cpACd62wzbPjbYrx4y67TZEmRXBcvmDTNaM",
            //      "signature": {
            //          "Ed25519": "0x97176876e9cb26b2449855a332efee478768b31e215ed025ce57dd8e6bdfa63df3adfcf29091eb53c326173a5aad23661e074d132ae804bbd91126cd19093302"
            //      },
            //      "era": {
            //          "MortalEra": "0x7500"
            //      },
            //      "nonce": 6,
            //      "tip": 1234
            //    },
            //    "method": {
            //    "callIndex": "0x0400",
            //      "args": {
            //        "dest": "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
            //        "value": 7654321
            //      }
            //    }
            //  }
            //]
            byte publicKeyType = 0x00;

            byte[]         publicKey   = Utils.GetPublicKeyFrom("5CxW5DWQDpXi4cpACd62wzbPjbYrx4y67TZEmRXBcvmDTNaM");
            CompactInteger nonce       = 6;
            byte           moduleIndex = 0x04;
            byte           callIndex   = 0x00;

            byte[]         destPublicKey = Utils.GetPublicKeyFrom("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY");
            CompactInteger amount        = 7654321;

            byte[]         parameters         = destPublicKey.Concat(amount.Encode()).ToArray();
            byte[]         genesisHash        = Utils.HexToByteArray("0x9b443ea9cd42d9c3e0549757d029d28d03800631f9a9abf1d96d0c414b9aded9");
            byte[]         currentBlockHash   = Utils.HexToByteArray("0xcfa2f9c52f94bc50658735d0f18f72590c981fdc15657636a99c437553c53253");;
            ulong          currentBlockNumber = 7;
            CompactInteger tip = 1234;

            // mocked signature
            byte[] signature = Utils.HexToByteArray("0x97176876e9cb26b2449855a332efee478768b31e215ed025ce57dd8e6bdfa63df3adfcf29091eb53c326173a5aad23661e074d132ae804bbd91126cd19093302");

            Method method = new Method(moduleIndex, callIndex, parameters);

            Era era = new Era(Constants.EXTRINSIC_ERA_PERIOD_DEFAULT, currentBlockNumber, currentBlockNumber == 0 ? true : false);

            var uncheckedExtrinsic = new UnCheckedExtrinsic(true, new Account(KeyType.ED25519, new byte[0], publicKey), method, era, nonce, tip, new Hash(genesisHash), new Hash(currentBlockHash));

            string balanceTransfer = "0x350284278117fc144c72340f67d0f2316e8386ceffbf2b2428c9c51fef7c597f1d426e0097176876e9cb26b2449855a332efee478768b31e215ed025ce57dd8e6bdfa63df3adfcf29091eb53c326173a5aad23661e074d132ae804bbd91126cd1909330275001849130400d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27dc62ed301";

            uncheckedExtrinsic.AddPayloadSignature(signature);

            Assert.AreEqual(Utils.HexToByteArray(balanceTransfer), uncheckedExtrinsic.Encode());

            var payload = uncheckedExtrinsic.GetPayload().Encode();
        }
Esempio n. 6
0
        /// <summary>
        /// Gets the extra.
        /// </summary>
        /// <returns></returns>
        public byte[] GetExtra()
        {
            var bytes = new List <byte>();

            // CheckMortality
            bytes.AddRange(_mortality.Encode());

            // CheckNonce
            bytes.AddRange(_nonce.Encode());

            // ChargeTransactionPayment
            bytes.AddRange(_chargeTransactionPayment.Encode());

            return(bytes.ToArray());
        }
Esempio n. 7
0
        public void BalanceTransferCreateMortalAccountToAliceTest1()
        {
            var _runtime = new RuntimeVersion
            {
                SpecVersion        = 1,
                TransactionVersion = 1
            };

            Constants.AddressVersion = 0;

            // 0x310284278117fc144c72340f67d0f2316e8386ceffbf2b2428c9c51fef7c597f1d426e007c9777cf14fe0e14e8aef019695043be2fd153a75ff3381f4cc4850755d537b1a9d7920e509ee2e4e1f244dad670dc44ec3fc24388181e6465fdda13d59ae70063001c000400d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d02890700
            // sender 5FfBQ3kwXrbdyoqLPvcXRp7ikWydXawpNs2Ceu3WwFdhZ8W4
            // recent blocks 15,689 0xf8438a058c66ab33628249459d1f0bc8114c6550d3ddea45351a96cbf2999813
            // nonce 7
            // receiver 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY
            // Lifetime 12

            //length: 140[2]
            //signatureVersion: 0x84
            //sendPublicKey: 5CxW5DWQDpXi4cpACd62wzbPjbYrx4y67TZEmRXBcvmDTNaM[0x278117FC144C72340F67D0F2316E8386CEFFBF2B2428C9C51FEF7C597F1D426E]
            //sendPublicKeyType: 0x00
            //signature: 0x7C9777CF14FE0E14E8AEF019695043BE2FD153A75FF3381F4CC4850755D537B1A9D7920E509EE2E4E1F244DAD670DC44EC3FC24388181E6465FDDA13D59AE700
            //era: 0x6300
            //nonce: 7[1]
            //tip: 0[1]
            //moduleIndex: 0x0400
            //destPublicKey: 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY[0xD43593C715FDD31C61141ABD04A99FD6822C8558854CCDE39A5684E7A56DA27D]
            //amount: 123456[4]


            var privatKey =
                Utils.HexToByteArray(
                    "0xf5e5767cf153319517630f226876b86c8160cc583bc013744c6bf255f5cc0ee5278117fc144c72340f67d0f2316e8386ceffbf2b2428c9c51fef7c597f1d426e");
            var publicKey = Utils.HexToByteArray("0x278117fc144c72340f67d0f2316e8386ceffbf2b2428c9c51fef7c597f1d426e");
            var account   = Account.Build(KeyType.Ed25519, privatKey, publicKey);

            var bytes = new List <byte>();

            bytes.AddRange(Utils.GetPublicKeyFrom("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"));
            CompactInteger amount = 123456;

            bytes.AddRange(amount.Encode());
            var method = new Method(0x04, 0x00, bytes.ToArray());

            var era = Era.Create(12, 15686);

            CompactInteger nonce = 7;

            CompactInteger tip = 0;

            var genesis = new Hash();

            genesis.Create(Utils.HexToByteArray("0x9b443ea9cd42d9c3e0549757d029d28d03800631f9a9abf1d96d0c414b9aded9"));

            var startEra = new Hash();

            startEra.Create(
                Utils.HexToByteArray(
                    "0x4c4e0d1594e526c2392e7b6306f890fd0705085a5f83f9114caebb369bc1511f")); // FinalizedHead 15686

            // mocked signature
            var signature =
                Utils.HexToByteArray(
                    "0x7C9777CF14FE0E14E8AEF019695043BE2FD153A75FF3381F4CC4850755D537B1A9D7920E509EE2E4E1F244DAD670DC44EC3FC24388181E6465FDDA13D59AE700");

            var uncheckedExtrinsic = new UnCheckedExtrinsic(true, account, method, era, nonce, tip, genesis, startEra);

            uncheckedExtrinsic.AddPayloadSignature(signature);

            var balanceTransferNode =
                "0x310284278117fc144c72340f67d0f2316e8386ceffbf2b2428c9c51fef7c597f1d426e007c9777cf14fe0e14e8aef019695043be2fd153a75ff3381f4cc4850755d537b1a9d7920e509ee2e4e1f244dad670dc44ec3fc24388181e6465fdda13d59ae70063001c000400d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d02890700";

            //var uncheckedExtrinsicStr = Utils.Bytes2HexString(uncheckedExtrinsic.Encode());

            Assert.AreEqual(Utils.HexToByteArray(balanceTransferNode), uncheckedExtrinsic.Encode());


            var payload = uncheckedExtrinsic.GetPayload(_runtime).Encode();

            //var payloadStr = Utils.Bytes2HexString(payload);

            if (payload.Length > 256)
            {
                payload = HashExtension.Blake2(payload, 256);
            }

            var simpleSign = Chaos.NaCl.Ed25519.Sign(payload, account.PrivateKey);

            //var simpleSignStr = Utils.Bytes2HexString(simpleSign);

            Assert.True(Chaos.NaCl.Ed25519.Verify(simpleSign, payload, publicKey));
            Assert.True(Chaos.NaCl.Ed25519.Verify(signature, payload, publicKey));

            var extrinsic =
                RequestGenerator.SubmitExtrinsic(true, account, method, era, nonce, tip, genesis, startEra, _runtime);

            Assert.True(Chaos.NaCl.Ed25519.Verify(extrinsic.Signature, extrinsic.GetPayload(_runtime).Encode(),
                                                  publicKey));
            Assert.True(Chaos.NaCl.Ed25519.Verify(signature, extrinsic.GetPayload(_runtime).Encode(), publicKey));
        }
Esempio n. 8
0
        public void BalanceTransferAliceTest()
        {
            var _runtime = new RuntimeVersion
            {
                SpecVersion        = 1,
                TransactionVersion = 1
            };

            Constants.AddressVersion = 0;

            //[
            //  {
            //    isSigned: true,
            //    method: {
            //      args: [
            //        5CxW5DWQDpXi4cpACd62wzbPjbYrx4y67TZEmRXBcvmDTNaM,
            //        4.321n Unit
            //      ],
            //      method: transfer,
            //      section: balances
            //    },
            //    era: {
            //      MortalEra: {
            //        period: 64,
            //        phase: 10
            //      }
            //    },
            //    nonce: 4,
            //    signature: 0x726ba1fab06d3e1bf6abfa0d5af85e25f2a970e11384162b7caf83935c58f769b6fef3b83a29ffd8d813a037d01cd6bcb21beaa88e9a18b3abe366b0458a8a82,
            //    signer: 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY,
            //    tip: 1.234n Unit
            //  }
            //]
            // [{ "signature":{ "signer":"5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY","signature":{ "Sr25519":"0x726ba1fab06d3e1bf6abfa0d5af85e25f2a970e11384162b7caf83935c58f769b6fef3b83a29ffd8d813a037d01cd6bcb21beaa88e9a18b3abe366b0458a8a82"},"era":{ "MortalEra":"0xa500"},"nonce":4,"tip":1234},"method":{ "callIndex":"0x0400","args":{ "dest":"5CxW5DWQDpXi4cpACd62wzbPjbYrx4y67TZEmRXBcvmDTNaM","value":4321} } }]

            var privatKey =
                Utils.HexToByteArray(
                    "0x33A6F3093F158A7109F679410BEF1A0C54168145E0CECB4DF006C1C2FFFB1F09925A225D97AA00682D6A59B95B18780C10D7032336E88F3442B42361F4A66011");

            byte           publicKeyType = 0x01;
            var            publicKey     = Utils.GetPublicKeyFrom("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"); // Alice
            CompactInteger nonce         = 4;
            byte           moduleIndex   = 0x04;
            byte           callIndex     = 0x00;

            var bytes = new List <byte>();

            bytes.AddRange(Utils.GetPublicKeyFrom("5CxW5DWQDpXi4cpACd62wzbPjbYrx4y67TZEmRXBcvmDTNaM"));
            CompactInteger amount = 4321;

            bytes.AddRange(amount.Encode());
            var parameters = bytes.ToArray();

            var genesisHash =
                Utils.HexToByteArray("0x9b443ea9cd42d9c3e0549757d029d28d03800631f9a9abf1d96d0c414b9aded9");
            var startEra =
                Utils.HexToByteArray(
                    "0xcfa2f9c52f94bc50658735d0f18f72590c981fdc15657636a99c437553c53253"); // CurrentBlock 780, startErar 778
            ulong          currentBlockNumber = 10;
            CompactInteger tip = 1234;

            //var Era = new Era(64, 10, false);

            // mocked signature
            var signature =
                Utils.HexToByteArray(
                    "0x726ba1fab06d3e1bf6abfa0d5af85e25f2a970e11384162b7caf83935c58f769b6fef3b83a29ffd8d813a037d01cd6bcb21beaa88e9a18b3abe366b0458a8a82");

            var method = new Method(moduleIndex, callIndex, parameters);

            var era = new Era(Constants.ExtrinsicEraPeriodDefault, currentBlockNumber,
                              currentBlockNumber == 0 ? true : false);

            var genesis = new Hash();

            genesis.Create(genesisHash);

            var startEraHash = new Hash();

            startEraHash.Create(startEra);

            var uncheckedExtrinsic = new UnCheckedExtrinsic(true,
                                                            Account.Build(KeyType.Sr25519, new byte[0], publicKey), method, era, nonce, tip, genesis, startEraHash);

            var balanceTransfer =
                "0x2d0284d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d01726ba1fab06d3e1bf6abfa0d5af85e25f2a970e11384162b7caf83935c58f769b6fef3b83a29ffd8d813a037d01cd6bcb21beaa88e9a18b3abe366b0458a8a82a5001049130400278117fc144c72340f67d0f2316e8386ceffbf2b2428c9c51fef7c597f1d426e8543";

            uncheckedExtrinsic.AddPayloadSignature(signature);

            Assert.AreEqual(Utils.HexToByteArray(balanceTransfer), uncheckedExtrinsic.Encode());

            var payload = uncheckedExtrinsic.GetPayload(_runtime).Encode();

            var simpleSign = Sr25519v091.SignSimple(publicKey, privatKey, payload);

            Assert.True(Sr25519v091.Verify(simpleSign, publicKey, payload));

            Assert.True(Sr25519v091.Verify(signature, publicKey, payload));
        }