예제 #1
0
        public async Task <PagedResult> GetBlocksAsync(BlocksRequest request)
        {
            var num = request.Num;

            if (!num.HasValue || num.Value == 0)
            {
                num = (await m_EosApi.GetInfo()).head_block_num;
            }
            var  blocks = new List <Block>();
            long start  = (long)num - (long)request.Skip - (long)request.Limit;
            long end    = start + (long)request.Limit;

            if (start < 0)
            {
                start = 0;
            }
            for (var i = end; i > start; i--)
            {
                var block = await m_EosApi.GetBlock(new GetBlockRequest()
                {
                    block_num_or_id = i.ToString()
                });

                blocks.Add(new Block
                {
                    Id       = block.id,
                    Time     = block.timestamp,
                    Num      = block.block_num,
                    Producer = block.producer,
                    Txns     = block.transactions.Count
                });
            }
            return(PagedResult.Create(blocks, (int)num.Value));
        }
예제 #2
0
        public static async Task <string> CreateTransaction(Transaction trx)
        {
            core.Services.AbiSerializationProvider.Prefix = "VHKD";
            var eosConfig = new EosConfigurator()
            {
                HttpEndpoint  = "http://192.168.1.61", //Mainnet
                ExpireSeconds = 60,
                ChainId       = "afe97f023511453c09c64f5bb655e7f4dc6694685aff7231219964e9cc521585",
                SignProvider  = new core.Services.DefaultSignProvider("5JNNWKaCoWvN5xojDXsW38Z6uoLmvM9NPh3coQD5z3TgsuY4nmt", "VHKD")
            };
            var api           = new EosApi(eosConfig, new HttpHandler());
            var abiSerializer = new core.Services.AbiSerializationProvider(api);

            if (eosConfig.SignProvider == null)
            {
                throw new ArgumentNullException("SignProvider");
            }

            GetInfoResponse getInfoResult = null;
            string          chainId       = eosConfig.ChainId;

            if (string.IsNullOrWhiteSpace(chainId))
            {
                getInfoResult = await api.GetInfo();

                chainId = getInfoResult.chain_id;
            }

            if (trx.expiration == DateTime.MinValue ||
                trx.ref_block_num == 0 ||
                trx.ref_block_prefix == 0)
            {
                if (getInfoResult == null)
                {
                    getInfoResult = await api.GetInfo();
                }

                var getBlockResult = await api.GetBlock(new GetBlockRequest()
                {
                    block_num_or_id = getInfoResult.last_irreversible_block_num.ToString()
                });

                trx.expiration       = getInfoResult.head_block_time.AddSeconds(eosConfig.ExpireSeconds);
                trx.ref_block_num    = (UInt16)(getInfoResult.last_irreversible_block_num & 0xFFFF);
                trx.ref_block_prefix = getBlockResult.ref_block_prefix;
            }

            var packedTrx = await abiSerializer.SerializePackedTransaction(trx);

            var availableKeys = await eosConfig.SignProvider.GetAvailableKeys();

            var requiredKeys = await GetRequiredKeys(api, abiSerializer, availableKeys.ToList(), trx);

            IEnumerable <string> abis = null;

            if (trx.actions != null)
            {
                abis = trx.actions.Select(a => a.account);
            }

            var signatures = await eosConfig.SignProvider.Sign(chainId, requiredKeys, packedTrx, abis);

            var result = await api.PushTransaction(new PushTransactionRequest()
            {
                signatures  = signatures.ToArray(),
                compression = 0,
                packed_context_free_data = "",
                packed_trx = SerializationHelper.ByteArrayToHexString(packedTrx)
            });

            return(result.transaction_id);
        }
예제 #3
0
        public async Task <string> CreatePackedTransaction(TransactionsPackedRequest request)
        {
            var action = new EosSharp.Core.Api.v1.Action
            {
                account       = request.Account,
                authorization = request.Authorization?.Select(p => new PermissionLevel()
                {
                    actor = p.Actor, permission = p.Permission
                }).ToList(),
                name = request.Name,
                data = request.Data
            };

            if (action.data != null && action.data is JObject)
            {
                action.data = ((JObject)action.data).ToDictionary();
            }
            if (string.IsNullOrEmpty(action.account))
            {
                throw new ArgumentNullException(nameof(request.Account));
            }

            if (string.IsNullOrEmpty(action.name))
            {
                throw new ArgumentNullException(nameof(request.Name));
            }

            if (action.authorization == null || action.authorization.Count == 0)
            {
                throw new ArgumentNullException(nameof(request.Authorization));
            }

            var trx = new Transaction()
            {
                actions = new List <EosSharp.Core.Api.v1.Action>()
                {
                    action
                }
            };

            GetInfoResponse getInfoResult = null;
            string          chainId       = m_EosConfig.ChainId;

            if (string.IsNullOrWhiteSpace(chainId))
            {
                getInfoResult = await m_EosApi.GetInfo();

                chainId = getInfoResult.chain_id;
            }

            if (trx.expiration == DateTime.MinValue ||
                trx.ref_block_num == 0 ||
                trx.ref_block_prefix == 0)
            {
                if (getInfoResult == null)
                {
                    getInfoResult = await m_EosApi.GetInfo();
                }

                var getBlockResult = await m_EosApi.GetBlock(new GetBlockRequest()
                {
                    block_num_or_id = getInfoResult.last_irreversible_block_num.ToString()
                });

                trx.expiration       = getInfoResult.head_block_time.AddSeconds(m_EosConfig.ExpireSeconds);
                trx.ref_block_num    = (UInt16)(getInfoResult.last_irreversible_block_num & 0xFFFF);
                trx.ref_block_prefix = getBlockResult.ref_block_prefix;
            }

            var packedTrx = await m_AbiSerializer.SerializePackedTransaction(trx);

            return(SerializationHelper.ByteArrayToHexString(packedTrx));
        }
예제 #4
0
 public Task GetInfo()
 {
     return(DefaultApi.GetInfo());
 }