예제 #1
0
        public async Task GetBlockTest()
        {
            var args = new GetBlockParams
            {
                BlockNumOrId = "4"
            };

            var resp = await Api.GetBlockAsync(args, CancellationToken).ConfigureAwait(false);

            TestPropetries(resp);
        }
예제 #2
0
        public async Task GetBlockTest()
        {
            var infoResp = await Api.GetInfo(CancellationToken);

            var info = infoResp.Result;

            var args = new GetBlockParams
            {
                BlockNumOrId = info.LastIrreversibleBlockId
            };

            var resp = await Api.GetBlock(args, CancellationToken);

            TestPropetries(resp);
        }
        public void Ok()
        {
            var testBlock = new GetBlockParams {
                BlockHash = "0xbd6c40490d8272f2c03bb19627add2ac6fddc1fd7e0eefd690ca47e4f9729d8f"
            };

            using (IApplication app = PolkaApi.GetAppication())
            {
                app.Connect();
                var result1 = app.GetBlock(testBlock);
                var result2 = app.GetBlockHeader(testBlock);

                output.WriteLine($"Parent hash from block  : { result1.Block.Header.ParentHash}");
                output.WriteLine($"Parent hash from header : { result2.ParentHash}");

                Assert.Equal(result1.Block.Header.ParentHash, result2.ParentHash);
                app.Disconnect();
            }
        }
        public void Ok()
        {
            var testBlock = new GetBlockParams {
                BlockHash = "0x37096ff58d1831c2ee64b026f8b70afab1942119c022d1dcfdbdc1558ebf63fa"
            };

            using (IApplication app = PolkaApi.GetAppication())
            {
                app.Connect();
                var result1 = app.GetBlock(testBlock);
                var result2 = app.GetBlockHeader(testBlock);

                output.WriteLine($"Parent hash from block  : { result1.Block.Header.ParentHash}");
                output.WriteLine($"Parent hash from header : { result2.ParentHash}");

                Assert.Equal(result1.Block.Header.ParentHash, result2.ParentHash);
                app.Disconnect();
            }
        }
예제 #5
0
        public BlockHeader GetBlockHeader(GetBlockParams param)
        {
            JArray prm = new JArray {
            };

            if (param != null)
            {
                prm = new JArray {
                    param.BlockHash
                }
            }
            ;
            JObject query = new JObject {
                { "method", "chain_getHeader" }, { "params", prm }
            };

            JObject response = _jsonRpc.Request(query);

            return(Deserialize <BlockHeader, ParseBlockHeader>(response));
        }
예제 #6
0
        public async Task <SignedTransaction> CreateTransaction(CreateTransactionArgs args, CancellationToken token)
        {
            //1
            var infoResp = await GetInfo(token);

            if (infoResp.IsError)
            {
                return(null);
            }

            var info = infoResp.Result;

            //2
            var blockArgs = new GetBlockParams
            {
                BlockNumOrId = info.HeadBlockId
            };
            var getBlock = await GetBlock(blockArgs, token);

            if (getBlock.IsError)
            {
                return(null);
            }

            var block = getBlock.Result;

            //3
            var transaction = new SignedTransaction
            {
                RefBlockNum    = (ushort)(block.BlockNum & 0xffff),
                RefBlockPrefix = block.RefBlockPrefix,
                Expiration     = block.Timestamp.Value.AddSeconds(30),
                Actions        = args.Actions
            };

            return(transaction);
        }
예제 #7
0
        public async Task <OperationResult <PushTransactionResults> > BroadcastActionsAsync(BaseAction[] baseActions, List <byte[]> privateKeys, CancellationToken token)
        {
            var initOpRez = await AbiJsonToBinAsync(baseActions, token).ConfigureAwait(false);

            if (initOpRez.IsError)
            {
                return(new OperationResult <PushTransactionResults>(initOpRez));
            }

            var infoResp = await GetInfoAsync(token).ConfigureAwait(false);

            if (infoResp.IsError)
            {
                return(new OperationResult <PushTransactionResults>(infoResp));
            }

            var info = infoResp.Result;

            var blockArgs = new GetBlockParams
            {
                BlockNumOrId = info.HeadBlockId
            };
            var getBlock = await GetBlockAsync(blockArgs, token).ConfigureAwait(false);

            if (getBlock.IsError)
            {
                return(new OperationResult <PushTransactionResults>(getBlock));
            }

            var block = getBlock.Result;

            var trx = new SignedTransaction
            {
                Actions        = baseActions,
                RefBlockNum    = (ushort)(block.BlockNum & 0xffff),
                RefBlockPrefix = block.RefBlockPrefix,
                Expiration     = block.Timestamp.Value.AddSeconds(30)
            };

            var packedTrx = MessageSerializer.Serialize <SignedTransaction>(trx);

            var chainId = Hex.HexToBytes(info.ChainId);
            var msg     = new byte[chainId.Length + packedTrx.Length + 32];

            Array.Copy(chainId, msg, chainId.Length);
            Array.Copy(packedTrx, 0, msg, chainId.Length, packedTrx.Length);
            var sha256 = Sha256Manager.GetHash(msg);

            var pack = new PackedTransaction
            {
                PackedTrx             = packedTrx,
                Signatures            = new string[privateKeys.Count],
                PackedContextFreeData = new Bytes(),
                Compression           = CompressionType.None
            };

            for (var i = 0; i < privateKeys.Count; i++)
            {
                var key    = privateKeys[i];
                var sig    = Secp256K1Manager.SignCompressedCompact(sha256, key);
                var sigHex = Base58.EncodeSig(sig);
                pack.Signatures[i] = sigHex;
            }

            return(await PushTransactionAsync(pack, token).ConfigureAwait(false));
        }
예제 #8
0
        public async Task <OperationResult <PushTransactionResults> > BroadcastActionsAsync(BaseAction[] baseActions, PublicKey[] publicKeys, Func <SignedTransaction, PublicKey[], string, CancellationToken, Task <OperationResult <SignedTransaction> > > signFunc, CancellationToken token)
        {
            var initOpRez = await AbiJsonToBinAsync(baseActions, token).ConfigureAwait(false);

            if (initOpRez.IsError)
            {
                return(new OperationResult <PushTransactionResults>(initOpRez));
            }

            var infoResp = await GetInfoAsync(token).ConfigureAwait(false);

            if (infoResp.IsError)
            {
                return(new OperationResult <PushTransactionResults>(infoResp));
            }

            var info = infoResp.Result;

            var blockArgs = new GetBlockParams
            {
                BlockNumOrId = info.HeadBlockId
            };
            var getBlock = await GetBlockAsync(blockArgs, token).ConfigureAwait(false);

            if (getBlock.IsError)
            {
                return(new OperationResult <PushTransactionResults>(getBlock));
            }

            var block = getBlock.Result;

            var trx = new SignedTransaction
            {
                Actions        = baseActions,
                RefBlockNum    = (ushort)(block.BlockNum & 0xffff),
                RefBlockPrefix = block.RefBlockPrefix,
                Expiration     = block.Timestamp.Value.AddSeconds(30)
            };

            var strx = await signFunc.Invoke(trx, publicKeys, info.ChainId, token).ConfigureAwait(false);

            if (strx.IsError)
            {
                return(new OperationResult <PushTransactionResults>(strx));
            }

            var packedTrx = MessageSerializer.Serialize <SignedTransaction>(trx);

            var chainId = Hex.HexToBytes(info.ChainId);
            var msg     = new byte[chainId.Length + packedTrx.Length + 32];

            Array.Copy(chainId, msg, chainId.Length);
            Array.Copy(packedTrx, 0, msg, chainId.Length, packedTrx.Length);

            var pack = new PackedTransaction
            {
                PackedTrx             = packedTrx,
                PackedContextFreeData = new Bytes(),
                Compression           = CompressionType.None,
                Signatures            = strx.Result.Signatures
            };

            return(await PushTransactionAsync(pack, token).ConfigureAwait(false));
        }
예제 #9
0
        public async Task SignTransactionTest()
        {
            var op = new BuyramAction
            {
                Account = User.Login,

                Args = new Buyram
                {
                    Payer    = User.Login,
                    Receiver = User.Login,
                    Quant    = new Asset("0.001 EOS")
                },
                Authorization = new[]
                {
                    new PermissionLevel
                    {
                        Actor      = User.Login,
                        Permission = "active"
                    }
                }
            };

            var initOpRez = await Api.AbiJsonToBinAsync(new[] { op }, CancellationToken).ConfigureAwait(false);

            if (initOpRez.IsError)
            {
                WriteLine(initOpRez);
                Assert.Fail();
            }

            var infoResp = await Api.GetInfoAsync(CancellationToken).ConfigureAwait(false);

            if (infoResp.IsError)
            {
                WriteLine(infoResp);
                Assert.Fail();
            }

            var info = infoResp.Result;

            var blockArgs = new GetBlockParams
            {
                BlockNumOrId = info.HeadBlockId
            };
            var getBlock = await Api.GetBlockAsync(blockArgs, CancellationToken).ConfigureAwait(false);

            if (getBlock.IsError)
            {
                WriteLine(getBlock);
                Assert.Fail();
            }

            var block = getBlock.Result;

            var trx = new SignedTransaction
            {
                Actions        = new[] { op },
                RefBlockNum    = (ushort)(block.BlockNum & 0xffff),
                RefBlockPrefix = block.RefBlockPrefix,
                Expiration     = block.Timestamp.Value.AddSeconds(30)
            };


            await Api.WalletOpenAsync(User.Login, CancellationToken).ConfigureAwait(false);

            await Api.WalletUnlockAsync(User.Login, User.Password, CancellationToken).ConfigureAwait(false);

            var resp = await Api.WalletSignTransactionAsync(trx, new[] { new PublicKey(User.PublicActiveWif), }, info.ChainId, CancellationToken.None).ConfigureAwait(false);

            await Api.WalletLockAsync(User.Login, CancellationToken).ConfigureAwait(false);

            WriteLine(resp);

            Assert.IsFalse(resp.IsError);
            Assert.IsTrue(resp.Result.Signatures.Length == 1);
        }
예제 #10
0
        public async Task <PackedTransaction> CreatePackedTransaction(CreateTransactionArgs args, CancellationToken token)
        {
            //1
            var infoResp = await GetInfo(token);

            if (infoResp.IsError)
            {
                return(null);
            }

            var info = infoResp.Result;

            //2
            var blockArgs = new GetBlockParams
            {
                BlockNumOrId = info.HeadBlockId
            };
            var getBlock = await GetBlock(blockArgs, token);

            if (getBlock.IsError)
            {
                return(null);
            }

            var block = getBlock.Result;

            //3
            var transaction = new SignedTransaction
            {
                RefBlockNum    = (ushort)(block.BlockNum & 0xffff),
                RefBlockPrefix = block.RefBlockPrefix,
                Expiration     = block.Timestamp.Value.AddSeconds(30),
                Actions        = args.Actions
            };

            var packedTrx = MessageSerializer.Serialize <SignedTransaction>(transaction);

            var chainId = Hex.HexToBytes(info.ChainId);
            var msg     = new byte[chainId.Length + packedTrx.Length + 32];

            Array.Copy(chainId, msg, chainId.Length);
            Array.Copy(packedTrx, 0, msg, chainId.Length, packedTrx.Length);
            var sha256 = Sha256Manager.GetHash(msg);

            transaction.Signatures = new string[args.PrivateKeys.Count];
            for (var i = 0; i < args.PrivateKeys.Count; i++)
            {
                var key    = args.PrivateKeys[i];
                var sig    = Secp256K1Manager.SignCompressedCompact(sha256, key);
                var sigHex = Base58.EncodeSig(sig);
                transaction.Signatures[i] = sigHex;
            }

            return(new PackedTransaction
            {
                PackedTrx = Hex.ToString(packedTrx),
                Signatures = transaction.Signatures,
                PackedContextFreeData = "",
                Compression = "none"
            });
        }
예제 #11
0
        /// <summary>
        /// Get information related to a block.
        ///
        ///  curl http://127.0.0.1:8888/v1/chain/get_block -X POST -d '{"block_num_or_id":5}'
        ///  curl http://127.0.0.1:8888/v1/chain/get_block -X POST -d '{"block_num_or_id":0000000445a9f27898383fd7de32835d5d6a978cc14ce40d9f327b5329de796b}'
        /// </summary>
        /// <param name="args"></param>
        /// <param name="token">Throws a <see cref="T:System.OperationCanceledException" /> if this token has had cancellation requested.</param>
        /// <returns></returns>
        public async Task <OperationResult <GetBlockResults> > GetBlock(GetBlockParams args, CancellationToken token)
        {
            var endpoint = $"{ChainUrl}/v1/chain/get_block";

            return(await CustomPostRequest <GetBlockResults>(endpoint, args, token));
        }
예제 #12
0
        /// <summary>
        /// Get information related to a block.
        ///
        ///  curl http://127.0.0.1:8888/v1/chain/get_block -X POST -d '{"block_num_or_id":5}'
        ///  curl http://127.0.0.1:8888/v1/chain/get_block -X POST -d '{"block_num_or_id":0000000445a9f27898383fd7de32835d5d6a978cc14ce40d9f327b5329de796b}'
        /// </summary>
        /// <param name="args"></param>
        /// <param name="token">Throws a <see cref="T:System.OperationCanceledException" /> if this token has had cancellation requested.</param>
        /// <returns></returns>
        public async Task <OperationResult <GetBlockResults> > GetBlockAsync(GetBlockParams args, CancellationToken token)
        {
            var endpoint = $"{ChainUrl}/v1/chain/get_block";

            return(await CustomPutRequestAsync <GetBlockResults>(endpoint, args, token).ConfigureAwait(false));
        }