Beispiel #1
0
        public Transaction Transfer(NeoKey from_key, byte[] to_address_hash, decimal value)
        {
            BigInteger amount = ConvertToBigInt(value);

            var sender_address_hash = from_key.address.GetScriptHashFromAddress();
            var response            = api.CallContract(from_key, ScriptHash, "transfer", new object[] { sender_address_hash, to_address_hash, amount });

            return(response);
        }
Beispiel #2
0
        public void WaitForTransaction(BlockIterator iterator, NeoKey keys, Transaction tx, int maxBlocksToWait = 9999)
        {
            if (tx == null)
            {
                throw new ArgumentNullException();
            }

            WaitForTransaction(iterator, x => x.Hash == tx.Hash, maxBlocksToWait);
            lastTransactions[keys.address] = tx;
        }
Beispiel #3
0
        public static Transaction MintTokens(NEP5 token, NeoKey buyer_key, string symbol, decimal amount)
        {
            var attachs = new List <Transaction.Output>();

            attachs.Add(new Transaction.Output()
            {
                assetID = NeoAPI.GetAssetID(symbol), scriptHash = token.ScriptHash, value = amount
            });
            var response = token.api.CallContract(buyer_key, token.ScriptHash, "mintTokens", new object[] { }, symbol, attachs);

            return(response);
        }
Beispiel #4
0
        public Transaction ClaimGas(NeoKey ownerKey)
        {
            var targetScriptHash = new UInt160(ownerKey.address.AddressToScriptHash());

            decimal amount;
            var     claimable = GetClaimable(targetScriptHash, out amount);

            var references = new List <Transaction.Input>();

            foreach (var entry in claimable)
            {
                references.Add(new Transaction.Input()
                {
                    prevHash = entry.hash, prevIndex = entry.index
                });
            }

            if (amount <= 0)
            {
                throw new ArgumentException("No GAS to claim at this address");
            }

            List <Transaction.Input>  inputs;
            List <Transaction.Output> outputs;

            GenerateInputsOutputs(ownerKey, "GAS", null, out inputs, out outputs);

            outputs.Add(
                new Transaction.Output()
            {
                scriptHash = targetScriptHash,
                assetID    = NeoAPI.GetAssetID("GAS"),
                value      = amount
            });

            Transaction tx = new Transaction()
            {
                type            = TransactionType.ClaimTransaction,
                version         = 0,
                script          = null,
                gas             = -1,
                claimReferences = references.ToArray(),
                inputs          = inputs.ToArray(),
                outputs         = outputs.ToArray(),
            };

            tx.Sign(ownerKey);

            var ok = SendTransaction(tx);

            return(ok ? tx : null);
        }
Beispiel #5
0
        public Transaction WithdrawAsset(NeoKey toKey, UInt160 fromScripthash, string symbol, IEnumerable <Transaction.Output> targets, byte[] verificationScript)
        {
            var check = verificationScript.ToScriptHash();

            if (check != fromScripthash)
            {
                throw new ArgumentException("Invalid verification script");
            }

            List <Transaction.Input>  inputs;
            List <Transaction.Output> outputs;

            GenerateInputsOutputs(fromScripthash, symbol, targets, out inputs, out outputs);


            List <Transaction.Input>  gas_inputs;
            List <Transaction.Output> gas_outputs;

            GenerateInputsOutputs(toKey, "GAS", null, out gas_inputs, out gas_outputs);

            foreach (var entry in gas_inputs)
            {
                inputs.Add(entry);
            }

            foreach (var entry in gas_outputs)
            {
                outputs.Add(entry);
            }

            Transaction tx = new Transaction()
            {
                type    = TransactionType.ContractTransaction,
                version = 0,
                script  = null,
                gas     = -1,
                inputs  = inputs.ToArray(),
                outputs = outputs.ToArray()
            };

            var witness = new Witness {
                invocationScript = ("0014" + toKey.address.AddressToScriptHash().ByteToHex()).HexToBytes(), verificationScript = verificationScript
            };

            tx.Sign(toKey, new Witness[] { witness });

            var ok = SendTransaction(tx);

            return(ok ? tx : null);
        }
Beispiel #6
0
        public Transaction WithdrawAsset(NeoKey toKey, string fromAddress, string symbol, decimal amount, byte[] verificationScript)
        {
            var fromScriptHash = new UInt160(fromAddress.GetScriptHashFromAddress());
            var target         = new Transaction.Output()
            {
                scriptHash = new UInt160(toKey.address.GetScriptHashFromAddress()), value = amount
            };
            var targets = new List <Transaction.Output>()
            {
                target
            };

            return(WithdrawAsset(toKey, fromScriptHash, symbol, targets, verificationScript));
        }
Beispiel #7
0
        // transfer to multiple addresses
        public Transaction Transfer(NeoKey from_key, Dictionary <byte[], decimal> transfers)
        {
            if (transfers.Count > max_transfer_count)
            {
                throw new ArgumentException("Max transfers per call = " + max_transfer_count);
            }

            var scripts = new List <byte[]>();

            var sender_address_hash = from_key.address.GetScriptHashFromAddress();

            int index = 0;

            foreach (var entry in transfers)
            {
                if (entry.Value <= 0)
                {
                    var addr = new UInt160(entry.Key).ToAddress();
                    throw new ArgumentException($"Invalid amount {entry.Value} for address {addr}");
                }

                BigInteger amount = ConvertToBigInt(entry.Value);

                var isLast = index == transfers.Count - 1;
                var args   = new object[] { sender_address_hash, entry.Key, amount };
                var bytes  = NeoAPI.GenerateScript(ScriptHash, new object[] { "transfer", args }, isLast);

                scripts.Add(bytes);
                index++;
            }

            var final_size = scripts.Sum(x => x.Length);

            byte[] final_script = new byte[final_size];

            using (var stream = new MemoryStream(final_script))
            {
                foreach (byte[] bytes in scripts)
                {
                    stream.Write(bytes, 0, bytes.Length);
                }
            }

            var response = api.CallContract(from_key, ScriptHash, final_script);

            return(response);
        }
Beispiel #8
0
        // transfer to multiple addresses
        public Transaction Transfer(NeoKey from_key, Dictionary <string, decimal> transfers)
        {
            var temp = new Dictionary <byte[], decimal>(new ByteArrayComparer());

            foreach (var entry in transfers)
            {
                if (!entry.Key.IsValidAddress())
                {
                    throw new ArgumentException($"{entry.Key} is not a valid address");
                }

                var hash = entry.Key.AddressToScriptHash();
                temp[hash] = entry.Value;
            }

            return(Transfer(from_key, temp));
        }
Beispiel #9
0
        public void GenerateInputsOutputs(NeoKey key, string symbol, IEnumerable <Transaction.Output> targets, out List <Transaction.Input> inputs, out List <Transaction.Output> outputs, decimal system_fee = 0)
        {
            var from_script_hash = new UInt160(key.signatureHash.ToArray());
            var info             = GetAssetsInfo();
            var targetAssetID    = LuxUtils.ReverseHex(info[symbol]).HexToBytes();

            if (targets != null)
            {
                foreach (var t in targets)
                {
                    if (t.assetID == null)
                    {
                        t.assetID = targetAssetID;
                    }
                }
            }
            //else Console.WriteLine("ASSETID target already existed: " + symbol);
            GenerateInputsOutputs(from_script_hash, targets, out inputs, out outputs, system_fee);
        }
Beispiel #10
0
        public Transaction SendAsset(NeoKey fromKey, string toAddress, string symbol, decimal amount)
        {
            if (String.Equals(fromKey.address, toAddress, StringComparison.OrdinalIgnoreCase))
            {
                throw new NeoException("Source and dest addresses are the same");
            }

            var toScriptHash = toAddress.GetScriptHashFromAddress();
            var target       = new Transaction.Output()
            {
                scriptHash = new UInt160(toScriptHash), value = amount
            };
            var targets = new List <Transaction.Output>()
            {
                target
            };

            return(SendAsset(fromKey, symbol, targets));
        }
Beispiel #11
0
        public Transaction CallContract(NeoKey key, UInt160 scriptHash, byte[] bytes, string attachSymbol = null, IEnumerable <Transaction.Output> attachTargets = null)
        {
            List <Transaction.Input>  inputs  = null;
            List <Transaction.Output> outputs = null;

            if (attachSymbol == null)
            {
                attachSymbol = "GAS";
            }

            if (!string.IsNullOrEmpty(attachSymbol))
            {
                GenerateInputsOutputs(key, attachSymbol, attachTargets, out inputs, out outputs);

                if (inputs.Count == 0)
                {
                    throw new NeoException($"Not enough inputs for transaction");
                }
            }

            var transaction = new Transaction()
            {
                type    = TransactionType.InvocationTransaction,
                version = 0,
                script  = bytes,
                gas     = 0,
                inputs  = inputs != null?inputs.ToArray() : null,
                              outputs = outputs != null?outputs.ToArray() : null,
                                            attributes = inputs == null ? (new TransactionAttribute[] { new TransactionAttribute(TransactionAttributeUsage.Script, key.address.AddressToScriptHash()) }) : null
            };

            transaction.Sign(key);

            if (SendTransaction(key, transaction))
            {
                return(transaction);
            }

            return(null);
        }
Beispiel #12
0
        public Transaction SendAsset(NeoKey fromKey, string symbol, IEnumerable <Transaction.Output> targets)
        {
            List <Transaction.Input>  inputs;
            List <Transaction.Output> outputs;

            GenerateInputsOutputs(fromKey, symbol, targets, out inputs, out outputs);

            Transaction tx = new Transaction()
            {
                type    = TransactionType.ContractTransaction,
                version = 0,
                script  = null,
                gas     = -1,
                inputs  = inputs.ToArray(),
                outputs = outputs.ToArray()
            };

            tx.Sign(fromKey);

            var ok = SendTransaction(tx);

            return(ok ? tx : null);
        }
Beispiel #13
0
 public Transaction Transfer(NeoKey from_key, UInt160 to_address_hash, decimal value)
 {
     return(Transfer(from_key, to_address_hash.ToArray(), value));
 }
Beispiel #14
0
 public Transaction Transfer(NeoKey from_key, string to_address, decimal value)
 {
     return(Transfer(from_key, to_address.GetScriptHashFromAddress(), value));
 }
Beispiel #15
0
 public decimal BalanceOf(NeoKey keys)
 {
     return(BalanceOf(keys.address));
 }
Beispiel #16
0
        // claim from contract, without having private key
        public Transaction ClaimGas(NeoKey ownerKey, UInt160 fromScripthash, byte[] verificationScript)
        {
            var check = verificationScript.ToScriptHash();

            if (check != fromScripthash)
            {
                throw new ArgumentException("Invalid verification script");
            }

            decimal amount;
            var     claimable = GetClaimable(fromScripthash, out amount);

            var references = new List <Transaction.Input>();

            foreach (var entry in claimable)
            {
                references.Add(new Transaction.Input()
                {
                    prevHash = entry.hash, prevIndex = entry.index
                });
            }

            if (amount <= 0)
            {
                throw new ArgumentException("No GAS to claim at this address");
            }

            List <Transaction.Input>  inputs;
            List <Transaction.Output> outputs;

            GenerateInputsOutputs(ownerKey, "GAS", null, out inputs, out outputs);

            outputs.Add(
                new Transaction.Output()
            {
                scriptHash = fromScripthash,
                assetID    = NeoAPI.GetAssetID("GAS"),
                value      = amount
            });

            Transaction tx = new Transaction()
            {
                type            = TransactionType.ClaimTransaction,
                version         = 0,
                script          = null,
                gas             = -1,
                claimReferences = references.ToArray(),
                inputs          = inputs.ToArray(),
                outputs         = outputs.ToArray(),
            };

            var witness = new Witness {
                invocationScript = ("0014" + ownerKey.address.AddressToScriptHash().ByteToHex()).HexToBytes(), verificationScript = verificationScript
            };

            tx.Sign(ownerKey, new Witness[] { witness });

            var ok = SendTransaction(tx);

            return(ok ? tx : null);
        }
Beispiel #17
0
 public Transaction CallContract(NeoKey key, UInt160 scriptHash, string operation, object[] args, string attachSymbol = null, IEnumerable <Transaction.Output> attachTargets = null)
 {
     return(CallContract(key, scriptHash, new object[] { operation, args }, attachSymbol, attachTargets));
 }
Beispiel #18
0
        public static Transaction Deploy(NEP5 token, NeoKey owner_key)
        {
            var response = token.api.CallContract(owner_key, token.ScriptHash, "deploy", new object[] { });

            return(response);
        }
Beispiel #19
0
        public Transaction DeployContract(NeoKey keys, byte[] script, byte[] parameter_list, byte return_type, ContractProperties properties, string name, string version, string author, string email, string description)
        {
            if (script.Length > 1024 * 1024)
            {
                return(null);
            }

            byte[] gen_script;
            using (var sb = new ScriptBuilder())
            {
                sb.EmitPush(description);
                sb.EmitPush(email);
                sb.EmitPush(author);
                sb.EmitPush(version);
                sb.EmitPush(name);
                sb.EmitPush((byte)properties);
                sb.EmitPush(return_type);
                sb.EmitPush(parameter_list);
                sb.EmitPush(script);

                sb.EmitSysCall("Neo.Contract.Create");

                gen_script = sb.ToArray();

                //string hex = bytes.ByteToHex();
                //System.IO.File.WriteAllBytes(@"D:\code\Crypto\neo-debugger-tools\ICO-Template\bin\Debug\inputs.avm", bytes);
            }

            decimal fee = 100;

            if (properties.HasFlag(ContractProperties.HasStorage))
            {
                fee += 400;
            }

            if (properties.HasFlag(ContractProperties.HasDynamicInvoke))
            {
                fee += 500;
            }

            fee -= 10; // first 10 GAS is free

            List <Transaction.Input>  inputs;
            List <Transaction.Output> outputs;

            GenerateInputsOutputs(keys, "GAS", null, out inputs, out outputs, fee);

            Transaction tx = new Transaction()
            {
                type    = TransactionType.InvocationTransaction,
                version = 0,
                script  = gen_script,
                gas     = fee,
                inputs  = inputs.ToArray(),
                outputs = outputs.ToArray()
            };

            tx.Sign(keys);

            var ok = SendTransaction(tx);

            return(ok ? tx : null);
        }
Beispiel #20
0
 public bool SendTransaction(NeoKey keys, Transaction tx)
 {
     return(SendTransaction(tx));
 }
Beispiel #21
0
        public void GenerateInputsOutputs(NeoKey key, IEnumerable <Transaction.Output> targets, out List <Transaction.Input> inputs, out List <Transaction.Output> outputs, decimal system_fee = 0)
        {
            var from_script_hash = new UInt160(key.signatureHash.ToArray());

            GenerateInputsOutputs(from_script_hash, targets, out inputs, out outputs, system_fee);
        }
Beispiel #22
0
        public Transaction ClaimGas(NeoKey ownerKey, string fromAddress, byte[] verificationScript)
        {
            var fromScriptHash = new UInt160(fromAddress.GetScriptHashFromAddress());

            return(ClaimGas(ownerKey, fromScriptHash, verificationScript));
        }
Beispiel #23
0
 public Dictionary <string, decimal> GetAssetBalancesOf(NeoKey key)
 {
     return(GetAssetBalancesOf(key.address));
 }
Beispiel #24
0
        public Transaction CallContract(NeoKey key, UInt160 scriptHash, object[] args, string attachSymbol = null, IEnumerable <Transaction.Output> attachTargets = null)
        {
            var bytes = GenerateScript(scriptHash, args);

            return(CallContract(key, scriptHash, bytes, attachSymbol, attachTargets));
        }