コード例 #1
0
        private Pb.PreExecWithSelectUTXOResponse PreExecWithSelectUTXO(
            string bcname, string address, AccountPrivateKey privkey, BigInteger amount, string contractName,
            string method, SortedDictionary <string, byte[]> args, ContactVMType.Type contractType, string initiator,
            List <string> authRequire)
        {
            var header = GetDefaultHeader();
            var req    = new Pb.PreExecWithSelectUTXORequest
            {
                Header      = header,
                Bcname      = bcname,
                Address     = address,
                TotalAmount = (long)amount,
                NeedLock    = false,
                Request     = new Pb.InvokeRPCRequest
                {
                    Header    = header,
                    Bcname    = bcname,
                    Initiator = initiator,
                },
            };

            if (authRequire != null)
            {
                req.Request.AuthRequire.AddRange(authRequire);
            }

            var invoke = new Pb.InvokeRequest
            {
                ModuleName = ContactVMType.GetNameByType(contractType),
                MethodName = method,
            };

            if (contractName != null)
            {
                invoke.ContractName = contractName;
            }
            foreach (var item in args)
            {
                invoke.Args.Add(item.Key, ByteString.CopyFrom(item.Value));
            }
            req.Request.Requests.Add(invoke);
            var res = client.PreExecWithSelectUTXO(req);

            if (res.Header.Error != Pb.XChainErrorEnum.Success)
            {
                Console.WriteLine("PreExecWithSelectUTXO failed, error code=" + (int)res.Header.Error);
                return(null);
            }
            return(res);
        }
コード例 #2
0
        /// <summary>Convert local Transaction to Protobuf Transaction</summary>
        public static Pb.Transaction LocalTxToPbTx(Transaction tx)
        {
            var pbtx = new Pb.Transaction();

            pbtx.Txid = ByteString.CopyFrom(tx.Txid);
            if (tx.Blockid != null)
            {
                pbtx.Blockid = ByteString.CopyFrom(tx.Blockid);
            }
            if (tx.TxInputs != null && tx.TxInputs.Count > 0)
            {
                foreach (var input in tx.TxInputs)
                {
                    pbtx.TxInputs.Add(new Pb.TxInput
                    {
                        FromAddr     = ByteString.CopyFrom(input.FromAddr),
                        Amount       = ByteString.CopyFrom(input.Amount),
                        RefOffset    = input.RefOffset,
                        RefTxid      = ByteString.CopyFrom(input.RefTxid),
                        FrozenHeight = input.FrozenHeight,
                    });
                }
            }

            if (tx.TxOutputs != null && tx.TxOutputs.Count > 0)
            {
                foreach (var output in tx.TxOutputs)
                {
                    pbtx.TxOutputs.Add(new Pb.TxOutput
                    {
                        ToAddr       = ByteString.CopyFrom(output.ToAddr),
                        Amount       = ByteString.CopyFrom(output.Amount),
                        FrozenHeight = output.FrozenHeight,
                    });
                }
            }
            if (tx.Desc != null)
            {
                pbtx.Desc = ByteString.CopyFrom(tx.Desc);
            }
            pbtx.Coinbase  = tx.Coinbase;
            pbtx.Nonce     = tx.Nonce;
            pbtx.Timestamp = tx.Timestamp;
            pbtx.Version   = tx.Version;
            pbtx.Autogen   = tx.Autogen;

            if (tx.TxInputsExt != null && tx.TxInputsExt.Count > 0)
            {
                foreach (var input in tx.TxInputsExt)
                {
                    pbtx.TxInputsExt.Add(new Pb.TxInputExt
                    {
                        Key       = ByteString.CopyFrom(input.Key),
                        Bucket    = input.Bucket,
                        RefTxid   = ByteString.CopyFrom(input.RefTxid),
                        RefOffset = input.RefOffset,
                    });
                }
            }

            if (tx.TxOutputsExt != null && tx.TxOutputsExt.Count > 0)
            {
                foreach (var output in tx.TxOutputsExt)
                {
                    pbtx.TxOutputsExt.Add(new Pb.TxOutputExt
                    {
                        Key    = ByteString.CopyFrom(output.Key),
                        Bucket = output.Bucket,
                        Value  = ByteString.CopyFrom(output.Value),
                    });
                }
            }

            if (tx.ContractRequests != null && tx.ContractRequests.Count > 0)
            {
                foreach (var cr in tx.ContractRequests)
                {
                    var invokeReq = new Pb.InvokeRequest
                    {
                        ModuleName   = cr.ModuleName,
                        ContractName = cr.ContractName,
                        MethodName   = cr.MethodName,
                    };
                    foreach (var arg in cr.Args)
                    {
                        invokeReq.Args.Add(arg.Key, ByteString.CopyFrom(arg.Value));
                    }
                    foreach (var limit in cr.ResourceLimits)
                    {
                        invokeReq.ResourceLimits.Add(new Pb.ResourceLimit
                        {
                            Type  = (Pb.ResourceType)limit.Type,
                            Limit = limit.Limit,
                        });
                    }
                    pbtx.ContractRequests.Add(invokeReq);
                }
            }

            pbtx.Initiator = tx.Initiator;
            if (tx.InitiatorSigns != null && tx.InitiatorSigns.Count > 0)
            {
                foreach (var sign in tx.InitiatorSigns)
                {
                    pbtx.InitiatorSigns.Add(new Pb.SignatureInfo
                    {
                        PublicKey = sign.PublicKey,
                        Sign      = ByteString.CopyFrom(sign.Sign),
                    });
                }
            }
            if (tx.AuthRequire != null && tx.AuthRequire.Count > 0)
            {
                foreach (var addr in tx.AuthRequire)
                {
                    pbtx.AuthRequire.Add(addr);
                }
            }
            if (tx.AuthRequireSigns != null && tx.AuthRequireSigns.Count > 0)
            {
                foreach (var sign in tx.AuthRequireSigns)
                {
                    pbtx.AuthRequireSigns.Add(new Pb.SignatureInfo
                    {
                        PublicKey = sign.PublicKey,
                        Sign      = ByteString.CopyFrom(sign.Sign),
                    });
                }
            }

            return(pbtx);
        }