Ejemplo n.º 1
0
        public async Task GetBalanceTestAsync()
        {
            var extrinsic_wait = 5000;

            Assert.AreEqual("0x33A6F3093F158A7109F679410BEF1A0C54168145E0CECB4DF006C1C2FFFB1F09925A225D97AA00682D6A59B95B18780C10D7032336E88F3442B42361F4A66011", Utils.Bytes2HexString(Alice.PrivateKey));
            Assert.AreEqual("5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty", Bob.Value);

            var cts = new CancellationTokenSource();
            await _substrateClient.ConnectAsync(cts.Token);

            // [Map] Key: T::AccountId, Hasher: BlakeTwo128Concat, Value: AccountInfo<T::Index, T::AccountData>
            var reqResult1 = await _substrateClient.GetStorageAsync("System", "Account", new[] { Utils.Bytes2HexString(Alice.Bytes) }, null, cts.Token);

            Assert.AreEqual("AccountInfo", reqResult1.GetType().Name);
            var aliceAccountInfo1 = reqResult1 as AccountInfo;
            var bobAccountInfo1   = (AccountInfo)await _substrateClient.GetStorageAsync("System", "Account", new[] { Utils.Bytes2HexString(Bob.Bytes) }, null, cts.Token);

            // Alice sends bob some coins ...
            var subscription = await _substrateClient.Author.SubmitAndWatchExtrinsicAsync(ActionExtrinsicUpdate, ExtrinsicCall.BalanceTransfer(Bob.Value, 100000000000), Alice, 0, 64, cts.Token);

            Thread.Sleep(extrinsic_wait);

            // [Map] Key: T::AccountId, Hasher: BlakeTwo128Concat, Value: AccountInfo<T::Index, T::AccountData>
            var reqResult2 = await _substrateClient.GetStorageAsync("System", "Account", new [] { Utils.Bytes2HexString(Alice.Bytes) }, null, cts.Token);

            Assert.AreEqual("AccountInfo", reqResult2.GetType().Name);
            var aliceAccountInfo2 = reqResult2 as AccountInfo;
            var bobAccountInfo2   = (AccountInfo)await _substrateClient.GetStorageAsync("System", "Account", new[] { Utils.Bytes2HexString(Bob.Bytes) }, null, cts.Token);


            Assert.IsTrue(aliceAccountInfo1.AccountData.Free.Value > aliceAccountInfo2.AccountData.Free.Value);
            Assert.IsTrue(bobAccountInfo1.AccountData.Free.Value < bobAccountInfo2.AccountData.Free.Value);


            var extrinsicHash = (Hash)await _substrateClient.Author.SubmitExtrinsicAsync(ExtrinsicCall.BalanceTransfer(Alice.Value, 90000000000), Bob, 0, 64, cts.Token);

            Thread.Sleep(extrinsic_wait);

            var subscription_state = await _substrateClient.Author.UnwatchExtrinsicAsync(subscription, cts.Token);

            await _substrateClient.CloseAsync();
        }
Ejemplo n.º 2
0
        public void BalanceTransferAccountTest()
        {
            var _runtime = new RuntimeVersion
            {
                SpecVersion        = 259,
                TransactionVersion = 1
            };

            Constants.AddressVersion = 1;

            // 797447 --> 0xe7b99ee484e6369dd3c2a66d6306bffde5048ddf2090e990faae83e66f5275f4

            //var accountZurich = Account.Build(
            //    KeyType.Ed25519,
            //    Utils.HexToByteArray(
            //        "0xf5e5767cf153319517630f226876b86c8160cc583bc013744c6bf255f5cc0ee5278117fc144c72340f67d0f2316e8386ceffbf2b2428c9c51fef7c597f1d426e"),
            //    Utils.GetPublicKeyFrom("5CxW5DWQDpXi4cpACd62wzbPjbYrx4y67TZEmRXBcvmDTNaM"));

            var privatKey =
                Utils.HexToByteArray(
                    "0xf5e5767cf153319517630f226876b86c8160cc583bc013744c6bf255f5cc0ee5278117fc144c72340f67d0f2316e8386ceffbf2b2428c9c51fef7c597f1d426e");
            var publicKey = Utils.HexToByteArray("0x278117fc144c72340f67d0f2316e8386ceffbf2b2428c9c51fef7c597f1d426e");

            //var receiverPublicKey =
            //    Utils.Bytes2HexString(Utils.GetPublicKeyFrom("5DotMog6fcsVhMPqniyopz5sEJ5SMhHpz7ymgubr56gDxXwH"));

            var referenceExtrinsic =
                "0x450284ff278117fc144c72340f67d0f2316e8386ceffbf2b2428c9c51fef7c597f1d426e00d6a14aac2c0da8330f67a04f9ff4154b3c31d02529eaf112a23d59f5a5e1d1766efbb7f4dd56e6ed84a543de94342bdec8c80bdac62373d22387ea980a42270f36000c000600ff4d2b23d27e1f6e3733d7ebf3dc04f3d5d0010cd18038055f9bbbab48f460b61e0b00b04e2bde6f";
            //                          "0x450284FF278117FC144C72340F67D0F2316E8386CEFFBF2B2428C9C51FEF7C597F1D426E00D6A14AAC2C0DA8330F67A04F9FF4154B3C31D02529EAF112A23D59F5A5E1D1766EFBB7F4DD56E6ED84A543DE94342BDEC8C80BDAC62373D22387EA980A42270F35000C000600FF4D2B23D27E1F6E3733D7EBF3DC04F3D5D0010CD18038055F9BBBAB48F460B61E0B00B04E2BDE6F"
            //                          "0x450284FF278117FC144C72340F67D0F2316E8386CEFFBF2B2428C9C51FEF7C597F1D426E00D6A14AAC2C0DA8330F67A04F9FF4154B3C31D02529EAF112A23D59F5A5E1D1766EFBB7F4DD56E6ED84A543DE94342BDEC8C80BDAC62373D22387EA980A42270F36000C000600FF4D2B23D27E1F6E3733D7EBF3DC04F3D5D0010CD18038055F9BBBAB48F460B61E0B00B04E2BDE6F"

            //var bytes = new List<byte>();
            //bytes.Add(0xFF);
            //bytes.AddRange(Utils.GetPublicKeyFrom("5DotMog6fcsVhMPqniyopz5sEJ5SMhHpz7ymgubr56gDxXwH"));
            //CompactInteger amount = 123000000000000;
            //bytes.AddRange(amount.Encode());
            //byte[] parameters = bytes.ToArray();
            ////var method = new Method(0x06, 0x00, parameters);
            var accountId = new AccountId();

            accountId.Create(Utils.GetPublicKeyFrom("5DotMog6fcsVhMPqniyopz5sEJ5SMhHpz7ymgubr56gDxXwH"));

            var balance = new Balance();

            balance.Create(123000000000000);

            var extrinsic = ExtrinsicCall.BalanceTransfer(accountId, balance);

            var method = new Method(0x06, 0x00, extrinsic.Encode());

            //var era = Era.Create(64, 797443);

            var era = new Era(128, 3, false);

            CompactInteger nonce = 3;

            CompactInteger tip = 0;

            var genesis = new Hash();

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

            var startEra = new Hash();

            startEra.Create(Utils.HexToByteArray("0xd5a0f4467c6c8885b531f12028789e83c2e473b8d2d44edbc09811fd2f903f1f"));

            var uncheckedExtrinsic = new UnCheckedExtrinsic(true
                                                            , Account.Build(KeyType.Ed25519, privatKey, publicKey)
                                                            , method
                                                            , era
                                                            , nonce
                                                            , tip
                                                            , genesis
                                                            , startEra // currentblock
                                                            );

            //uncheckedExtrinsic.AddPayloadSignature(Utils.HexToByteArray("0xd6a14aac2c0da8330f67a04f9ff4154b3c31d02529eaf112a23d59f5a5e1d1766efbb7f4dd56e6ed84a543de94342bdec8c80bdac62373d22387ea980a42270f"));

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

            /// Payloads longer than 256 bytes are going to be `blake2_256`-hashed.
            if (payload.Length > 256)
            {
                payload = HashExtension.Blake2(payload, 256);
            }

            byte[] signature;
            signature = Chaos.NaCl.Ed25519.Sign(payload, privatKey);
            //var signatureStr = Utils.Bytes2HexString(signature);

            uncheckedExtrinsic.AddPayloadSignature(signature);

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

            Assert.AreEqual(referenceExtrinsic, uncheckedExtrinsicStr.ToLower());

            //{
            //    isSigned: true,
            //    method:
            //            {
            //            args:[
            //             5DotMog6fcsVhMPqniyopz5sEJ5SMhHpz7ymgubr56gDxXwH,
            //             123.0000 mUnit
            //      ],
            //      method: transfer,
            //      section: balances
            //    },
            //    era:
            //            {
            //            MortalEra:
            //                {
            //                period: 128,
            //        phase: 3
            //            }
            //            },
            //    nonce: 3,
            //    signature: 0xd6a14aac2c0da8330f67a04f9ff4154b3c31d02529eaf112a23d59f5a5e1d1766efbb7f4dd56e6ed84a543de94342bdec8c80bdac62373d22387ea980a42270f,
            //    signer: 5CxW5DWQDpXi4cpACd62wzbPjbYrx4y67TZEmRXBcvmDTNaM,
            //    tip: 0
            //}


            //{
            //    "Signed": true,
            //    "TransactionVersion": 4,
            //    "Account": {
            //                "KeyType": 0,
            //      "Address": "5CxW5DWQDpXi4cpACd62wzbPjbYrx4y67TZEmRXBcvmDTNaM",
            //      "PublicKey": "J4EX/BRMcjQPZ9DyMW6Dhs7/vyskKMnFH+98WX8dQm4="
            //    },
            //    "Era": {
            //                "IsImmortal": false,
            //      "Period": 128,
            //      "Phase": 3
            //    },
            //    "Nonce": {
            //                "Value": 3
            //    },
            //    "Tip": {
            //                "Value": 0
            //    },
            //    "Method": {
            //                "ModuleName": "Balances",
            //      "ModuleIndex": 6,
            //      "CallName": "transfer",
            //      "CallIndex": 0,
            //      "Arguments": [
            //        {
            //                    "Name": "dest",
            //          "Type": "<T::Lookup as StaticLookup>::Source",
            //          "Value": "5DotMog6fcsVhMPqniyopz5sEJ5SMhHpz7ymgubr56gDxXwH"
            //        },
            //        {
            //                    "Name": "value",
            //          "Type": "Compact<T::Balance>",
            //          "Value": {
            //                        "Value": 123000000000000
            //          }
            //                }
            //      ],
            //      "Parameters": "/00rI9J+H243M9fr89wE89XQAQzRgDgFX5u7q0j0YLYeCwCwTivebw=="
            //    },
            //    "Signature": "1qFKrCwNqDMPZ6BPn/QVSzwx0CUp6vESoj1Z9aXh0XZu+7f03Vbm7YSlQ96UNCveyMgL2sYjc9Ijh+qYCkInDw=="
            //}
        }
Ejemplo n.º 3
0
        public async Task GetPendingExtrinsicTestAsync()
        {
            var extrinsic_wait = 5000;

            var cts = new CancellationTokenSource();
            await _substrateClient.ConnectAsync(cts.Token);

            var result = await _substrateClient.Author.PendingExtrinsicAsync();

            Assert.AreEqual("Extrinsic[]", result.GetType().Name);
            var extrinsics = result as Extrinsic[];

            Assert.AreEqual(0, extrinsics.Length);

            var subscription = (string)await _substrateClient.Author.SubmitAndWatchExtrinsicAsync(ActionExtrinsicUpdate, ExtrinsicCall.BalanceTransfer(Bob.Value, 100000000000), Alice, 0, 64);

            extrinsics = (Extrinsic[])await _substrateClient.Author.PendingExtrinsicAsync();

            Assert.AreEqual(1, extrinsics.Length);

            Thread.Sleep(extrinsic_wait);
            Thread.Sleep(extrinsic_wait);

            extrinsics = (Extrinsic[])await _substrateClient.Author.PendingExtrinsicAsync(cts.Token);

            Assert.AreEqual(0, extrinsics.Length);


            var subscription_state = await _substrateClient.Author.UnwatchExtrinsicAsync(subscription);

            Assert.AreEqual(true, subscription_state);

            await _substrateClient.CloseAsync();
        }
Ejemplo n.º 4
0
        private static void ParseExtrinsic(string[] args)
        {
            // Reference Substrate 3.0.0
            // Zurich to DotMog, 0.123 DMOG's
            // Nonce 0, Lifetime 64
            // 0x4502
            // 8400
            // 278117fc144c72340f67d0f2316e8386ceffbf2b2428c9c51fef7c597f1d426e --> 5CxW5DWQDpXi4cpACd62wzbPjbYrx4y67TZEmRXBcvmDTNaM
            // 00a1486b48665121686eddf7029d4f3b2ccf9335824d91df1ff11ffa739756717fe5570f204596fbd27c893981883b25ac797d3935405580d6144b356b469d6709f5020000060000
            // 4d2b23d27e1f6e3733d7ebf3dc04f3d5d0010cd18038055f9bbbab48f460b61e --> 5DotMog6fcsVhMPqniyopz5sEJ5SMhHpz7ymgubr56gDxXwH
            // 0b00b04e2bde6f


            //    {
            //        isSigned: true,
            //        method:
            //        {
            //            args:
            //            [
            //            {
            //                Id: 5DotMog6fcsVhMPqniyopz5sEJ5SMhHpz7ymgubr56gDxXwH
            //            },
            //            1.0000 DMOG
            //                ],
            //            method: transfer,
            //            section: balances
            //        },
            //        era:
            //        {
            //            MortalEra:
            //            {
            //                period: 128,
            //                phase: 50
            //            }
            //        },
            //        nonce: 2,
            //        signature: 0x351b31c6ad373f176a020acf168ca0412a3f410ef6d9936f46f4ce7bc893f76fcf3ff4389de0f86ad6b55df9af0d8ae3b868a544df107698056b7a93202faf00,
            //        signer:
            //        {
            //            Id: 5CxW5DWQDpXi4cpACd62wzbPjbYrx4y67TZEmRXBcvmDTNaM
            //        },
            //        tip: 0
            //    }
            //    ]
            //},
            //0x4902
            //8400
            //278117fc144c72340f67d0f2316e8386ceffbf2b2428c9c51fef7c597f1d426e
            //00
            //351b31c6ad373f176a020acf168ca0412a3f410ef6d9936f46f4ce7bc893f76fcf3ff4389de0f86ad6b55df9af0d8ae3b868a544df107698056b7a93202faf00 --> signature
            //26030800060000
            //4d2b23d27e1f6e3733d7ebf3dc04f3d5d0010cd18038055f9bbbab48f460b61e
            //0f0080c6a47e8d03

            var accountZurich = Account.Build(
                KeyType.Ed25519,
                Utils.HexToByteArray(
                    "0xf5e5767cf153319517630f226876b86c8160cc583bc013744c6bf255f5cc0ee5278117fc144c72340f67d0f2316e8386ceffbf2b2428c9c51fef7c597f1d426e"),
                Utils.GetPublicKeyFrom("5CxW5DWQDpXi4cpACd62wzbPjbYrx4y67TZEmRXBcvmDTNaM"));

            var accountId = new AccountId();

            accountId.Create(Utils.GetPublicKeyFrom("5DotMog6fcsVhMPqniyopz5sEJ5SMhHpz7ymgubr56gDxXwH"));

            var balance = new Balance();

            balance.Create(2000000000000);

            var extrinsic = ExtrinsicCall.BalanceTransfer(accountId, balance);


            Console.WriteLine(CompactInteger.Decode(Utils.HexToByteArray("0x490284")).ToString());
            Console.WriteLine(Utils.Bytes2HexString(new CompactInteger(146).Encode()));

            // 0x4902 Length 146
            // 84
            // 00
            // 278117fc144c72340f67d0f2316e8386ceffbf2b2428c9c51fef7c597f1d426e --> 5CxW5DWQDpXi4cpACd62wzbPjbYrx4y67TZEmRXBcvmDTNaM
            // 00fb6ec6a0e127329b564367527b3d6c4f28c197d2e205a4d37270e7fe5eee764e1d678e46e2c2d55a1d2cfd7869d24e40ba5f6bd9827c0b95d3db51bc633d050445032400060000
            // 4d2b23d27e1f6e3733d7ebf3dc04f3d5d0010cd18038055f9bbbab48f460b61e --> 5DotMog6fcsVhMPqniyopz5sEJ5SMhHpz7ymgubr56gDxXwH
            // 0f00806d8176de18
        }