コード例 #1
0
 private Witness WitnessFor(Transaction tx, KeyPair key)
 {
     return(new Witness()
     {
         InvocationScript = ("40" + tx.Sign(key).ToHexString()).HexToBytes(),
         VerificationScript = (key.AsSignatureScript()).HexToBytes()
     });
 }
コード例 #2
0
        public bool CallContract(KeyPair key, string scriptHash, byte[] bytes, out byte[] txhash)
        {
            var     inputs  = new List <CoinReference>();
            var     outputs = new List <TransactionOutput>();
            decimal gasCost = 0;

            if (key != null)
            {
                var unspent = GetUnspent(key.AsAddress(), UInt256.Zero);

                if (!unspent.ContainsKey("CRON"))
                {
                    throw new RpcException(-3227, "No CRONs available");
                }

                var sources = unspent["CRON"];

                decimal selectedGas = 0;
                foreach (var src in sources)
                {
                    selectedGas += src.value;

                    var input = new CoinReference()
                    {
                        PrevHash  = UInt256.Parse(src.txid),
                        PrevIndex = (ushort)src.index,
                    };

                    inputs.Add(input);

                    if (selectedGas >= gasCost)
                    {
                        break;
                    }
                }

                if (selectedGas < gasCost)
                {
                    throw new RpcException(-3228, "Not enough CRONs available");
                }

                var targetAssetID = reverseHex(ASSET_CRON).HexToBytes();

                if (selectedGas > gasCost)
                {
                    var left = selectedGas - gasCost;

                    var change = new TransactionOutput()
                    {
                        AssetId    = new UInt256(targetAssetID),
                        ScriptHash = key.AsSignatureScript().HexToBytes().ToScriptHash(),
                        Value      = Fixed8.FromDecimal(left)
                    };
                    outputs.Add(change);
                }
            }

            InvocationTransaction tx = new InvocationTransaction()
            {
                Attributes = new TransactionAttribute[0],
                Version    = 0,
                Script     = bytes,
                Gas        = Fixed8.FromDecimal(gasCost),
                Inputs     = inputs.ToArray(),
                Outputs    = outputs.ToArray()
            };

            if (key == null)
            {
                return(CallNoInvoke(tx, out txhash));
            }


            txhash = tx.Hash.ToArray().Reverse().ToArray();

            return(SignAndRelay(tx, new[] { key }));
        }