Example #1
0
        public void Ed25519KeyPairTest()
        {
            var priKey0x =
                "0xf5e5767cf153319517630f226876b86c8160cc583bc013744c6bf255f5cc0ee5278117fc144c72340f67d0f2316e8386ceffbf2b2428c9c51fef7c597f1d426e";
            var pubKey0x = "0x278117fc144c72340f67d0f2316e8386ceffbf2b2428c9c51fef7c597f1d426e";

            var priKey = Utils.HexToByteArray(priKey0x);
            var pubKey = Utils.HexToByteArray(pubKey0x);
            var seed   = priKey.Take(32).ToArray();

            Chaos.NaCl.Ed25519.KeyPairFromSeed(out pubKey, out priKey, seed);

            Assert.AreEqual("0xF5E5767CF153319517630F226876B86C8160CC583BC013744C6BF255F5CC0EE5",
                            Utils.Bytes2HexString(seed));
            Assert.AreEqual(
                "0xF5E5767CF153319517630F226876B86C8160CC583BC013744C6BF255F5CC0EE5278117FC144C72340F67D0F2316E8386CEFFBF2B2428C9C51FEF7C597F1D426E",
                Utils.Bytes2HexString(priKey));
            Assert.AreEqual("0x278117FC144C72340F67D0F2316E8386CEFFBF2B2428C9C51FEF7C597F1D426E",
                            Utils.Bytes2HexString(pubKey));

            var accountId = new AccountId();

            accountId.Create("0x278117FC144C72340F67D0F2316E8386CEFFBF2B2428C9C51FEF7C597F1D426E");
            Assert.AreEqual("5CxW5DWQDpXi4cpACd62wzbPjbYrx4y67TZEmRXBcvmDTNaM", accountId.Value);
        }
Example #2
0
        public void EncodingTest()
        {
            var accountId = new AccountId();

            accountId.Create("0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d");

            var balance = new Balance();

            balance.Create(100);

            var callArguments = new GenericExtrinsicCall("", "", accountId, balance);

            switch (Constants.AddressVersion)
            {
            case 0:
                Assert.AreEqual("D43593C715FDD31C61141ABD04A99FD6822C8558854CCDE39A5684E7A56DA27D9101",
                                Utils.Bytes2HexString(callArguments.Encode(), Utils.HexStringFormat.Pure));
                break;

            case 1:
                Assert.AreEqual("FFD43593C715FDD31C61141ABD04A99FD6822C8558854CCDE39A5684E7A56DA27D9101",
                                Utils.Bytes2HexString(callArguments.Encode(), Utils.HexStringFormat.Pure));
                break;

            case 2:
                Assert.AreEqual("00D43593C715FDD31C61141ABD04A99FD6822C8558854CCDE39A5684E7A56DA27D9101",
                                Utils.Bytes2HexString(callArguments.Encode(), Utils.HexStringFormat.Pure));
                break;
            }
        }
Example #3
0
        private static void TestKey(string[] args)
        {
            var accountId = new AccountId();

            accountId.Create(Utils.GetPublicKeyFrom("5CxW5DWQDpXi4cpACd62wzbPjbYrx4y67TZEmRXBcvmDTNaM"));
            Console.WriteLine($"AccountId: {accountId}");
            Console.WriteLine($"Public Key: {Utils.Bytes2HexString(accountId.Bytes).ToLower()}");

            var str = "0x200101020304050607";

            var memory    = Utils.HexToByteArray(str).AsMemory();
            var vecU8     = new List <U8>();
            var byteArray = memory.ToArray();
            var p         = 0;
            var length    = CompactInteger.Decode(byteArray, ref p);

            Console.WriteLine($"Length: {length}, p = {p}");
            for (var i = 0; i < length; i++)
            {
                var u8 = new U8();
                u8.Decode(byteArray, ref p);
                vecU8.Add(u8);
            }

            Console.WriteLine(JsonConvert.SerializeObject(vecU8));
        }
        public static GenericExtrinsicCall BalanceTransfer(string address, BigInteger balanceAmount)
        {
            var accountId = new AccountId();

            accountId.Create(Utils.GetPublicKeyFrom(address));

            var balance = new Balance();

            balance.Create(balanceAmount);

            return(BalanceTransfer(accountId, balance));
        }
Example #5
0
        /// <summary>
        /// Keys the type to bytes.
        /// </summary>
        /// <param name="keyType">Type of the key.</param>
        /// <param name="parameter">The parameter.</param>
        /// <returns></returns>
        /// <exception cref="Exception">Unimplemented item function key 'item.Function.Key1'!</exception>
        internal static byte[] KeyTypeToBytes(string keyType, string parameter)
        {
            switch (keyType)
            {
            case "u16":
                return(BitConverter.GetBytes(ushort.Parse(parameter)));

            case "u32":
                return(BitConverter.GetBytes(uint.Parse(parameter)));

            case "u64":
                return(BitConverter.GetBytes(ulong.Parse(parameter)));

            case "T::Hash":
                var hash = new Hash();
                hash.Create(parameter);
                return(hash.Bytes);

            case "T::AccountId":
                var accountId = new AccountId();
                accountId.Create(parameter);
                return(accountId.Bytes);

            case "Vec<u8>":
                var vecU8 = Utils.SizePrefixedByteArray(Utils.HexToByteArray(parameter).ToList());
                return(vecU8);

            case "T::AssetId":
                var assetId = new AssetId();
                assetId.Create(uint.Parse(parameter));
                return(assetId.Bytes);

            default:
                throw new Exception($"Unimplemented item function key 'item.Function.Key1' = {keyType}!");
            }
        }
Example #6
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=="
            //}
        }
Example #7
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
        }