예제 #1
0
        public bool SignAndSendTx(Transaction tx)
        {
            ContractParametersContext context;

            try
            {
                context = new ContractParametersContext(tx);
            }
            catch (InvalidOperationException ex)
            {
                Console.WriteLine($"Error creating contract params: {ex}");
                throw;
            }
            Program.Wallet.Sign(context);
            string msg;

            if (context.Completed)
            {
                tx.Witness = context.GetWitness();

                system.LocalNode.Tell(new LocalNode.Relay {
                    Inventory = tx
                });

                msg = $"Signed and relayed transaction with hash={tx.Hash}";
                Console.WriteLine(msg);
                return(true);
            }

            msg = $"Failed sending transaction with hash={tx.Hash}";
            Console.WriteLine(msg);
            return(true);
        }
예제 #2
0
        private void SignPayload(ConsensusPayload payload)
        {
            ContractParametersContext sc;

            try
            {
                sc = new ContractParametersContext(payload);
                wallet.Sign(sc);
            }
            catch (InvalidOperationException)
            {
                return;
            }
            payload.Witness = sc.GetWitness();
        }
예제 #3
0
        public Block CreateBlock()
        {
            Contract contract            = Contract.CreateMultiSigContract(M, Validators);
            ContractParametersContext sc = new ContractParametersContext(Block);

            for (int i = 0, j = 0; i < Validators.Length && j < M; i++)
            {
                if (CommitPayloads[i]?.ConsensusMessage.ViewNumber != ViewNumber)
                {
                    continue;
                }
                sc.AddSignature(contract, Validators[i], CommitPayloads[i].GetDeserializedMessage <Commit>().Signature);
                j++;
            }
            Block.Witness      = sc.GetWitness();
            Block.Transactions = TransactionHashes.Select(p => Transactions[p]).ToArray();
            return(Block);
        }
예제 #4
0
        private bool OnRelayCommand(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("You must input JSON object to relay.");
                return(true);
            }
            var jsonObjectToRelay = string.Join(string.Empty, args.Skip(1));

            if (string.IsNullOrWhiteSpace(jsonObjectToRelay))
            {
                Console.WriteLine("You must input JSON object to relay.");
                return(true);
            }
            try
            {
                ContractParametersContext context = ContractParametersContext.Parse(jsonObjectToRelay);
                if (!context.Completed)
                {
                    Console.WriteLine("The signature is incomplete.");
                    return(true);
                }
                if (!(context.Verifiable is Transaction tx))
                {
                    Console.WriteLine($"Only support to relay transaction.");
                    return(true);
                }
                tx.Witness = context.GetWitness();
                system.LocalNode.Tell(new LocalNode.Relay {
                    Inventory = tx
                });
                Console.WriteLine($"Data relay success, the hash is shown as follows:\r\n{tx.Hash}");
            }
            catch (Exception e)
            {
                Console.WriteLine($"One or more errors occurred:\r\n{e.Message}");
            }
            return(true);
        }
예제 #5
0
        private bool OnSendCommand(string[] args)
        {
            if (args.Length < 4 || args.Length > 5)
            {
                Console.WriteLine("error");
                return(true);
            }
            if (NoWallet())
            {
                return(true);
            }
            string password = ReadUserInput("password", true);

            if (password.Length == 0)
            {
                Console.WriteLine("cancelled");
                return(true);
            }
            if (!Program.Wallet.VerifyPassword(password))
            {
                Console.WriteLine("Incorrect password");
                return(true);
            }
            UInt160 assetId;

            switch (args[1].ToLower())
            {
            case "neo":
                assetId = NativeContract.NEO.Hash;
                break;

            case "gas":
                assetId = NativeContract.GAS.Hash;
                break;

            default:
                assetId = UInt160.Parse(args[1]);
                break;
            }
            UInt160         to = args[2].ToScriptHash();
            Transaction     tx;
            AssetDescriptor descriptor = new AssetDescriptor(assetId);

            if (!BigDecimal.TryParse(args[3], descriptor.Decimals, out BigDecimal amount) || amount.Sign <= 0)
            {
                Console.WriteLine("Incorrect Amount Format");
                return(true);
            }
            tx = Program.Wallet.MakeTransaction(null, new[]
            {
                new TransferOutput
                {
                    AssetId    = assetId,
                    Value      = amount,
                    ScriptHash = to
                }
            });

            if (tx == null)
            {
                Console.WriteLine("Insufficient funds");
                return(true);
            }

            ContractParametersContext context = new ContractParametersContext(tx);

            Program.Wallet.Sign(context);
            if (context.Completed)
            {
                tx.Witness = context.GetWitness();
                system.LocalNode.Tell(new LocalNode.Relay {
                    Inventory = tx
                });
                Console.WriteLine($"TXID: {tx.Hash}");
            }
            else
            {
                Console.WriteLine("SignatureContext:");
                Console.WriteLine(context.ToString());
            }

            return(true);
        }