Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/rpc sendmany from #614

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 85 additions & 3 deletions neo/Network/RPC/RpcServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,8 @@ private JObject Process(string method, JArray _params)
Blockchain.Singleton.MemPool.GetVerifiedAndUnverifiedTransactions(
out IEnumerable<Transaction> verifiedTransactions,
out IEnumerable<Transaction> unverifiedTransactions);
json["verified"] = new JArray(verifiedTransactions.Select(p => (JObject) p.Hash.ToString()));
json["unverified"] = new JArray(unverifiedTransactions.Select(p => (JObject) p.Hash.ToString()));
json["verified"] = new JArray(verifiedTransactions.Select(p => (JObject)p.Hash.ToString()));
json["unverified"] = new JArray(unverifiedTransactions.Select(p => (JObject)p.Hash.ToString()));
return json;
}
case "getrawtransaction":
Expand Down Expand Up @@ -386,6 +386,44 @@ private JObject Process(string method, JArray _params)
throw new RpcException(-400, "Access denied.");
else
return (Wallet.WalletHeight > 0) ? Wallet.WalletHeight - 1 : 0;
case "importkey":
if (Wallet == null) throw new RpcException(-400, "Access denied.");
else
{
if (_params.Count == 0) throw new RpcException(-32602, "Invalid params");
var successList = new JArray();
var errorList = new JArray();
foreach (var wifKey in _params.Select(p => p.AsString()))
{
try
{
byte[] prikey = Wallet.GetPrivateKeyFromWIF(wifKey);
var account = Wallet.CreateAccount(prikey);
successList.Add(new JObject()
{
["key"] = wifKey,
["address"] = account.Address,
["haskey"] = account.HasKey,
["label"] = account.Label,
["watchonly"] = account.WatchOnly,
["pubkey"] = account.GetKey().PublicKey.EncodePoint(true).ToHexString(),
});
}
catch (FormatException)
{
errorList.Add(new JObject()
{
["key"] = wifKey,
});
}
}
if (Wallet is NEP6Wallet wallet) wallet.Save();
return new JObject()
{
["accounts"] = successList,
["errors"] = errorList,
};
}
case "invoke":
{
UInt160 script_hash = UInt160.Parse(_params[0].AsString());
Expand Down Expand Up @@ -451,7 +489,7 @@ private JObject Process(string method, JArray _params)
Value = value,
ScriptHash = to
}
}, from: from, change_address: change_address, fee: fee);
}, @from: from, change_address: change_address, fee: fee);
if (tx == null)
throw new RpcException(-300, "Insufficient funds");
ContractParametersContext context = new ContractParametersContext(tx);
Expand Down Expand Up @@ -511,6 +549,50 @@ private JObject Process(string method, JArray _params)
return context.ToJson();
}
}
case "sendmanyfrom":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this really belongs or is needed in the core RPC methods. It is easy enough to formulate the transaction outside and just call sendrawtransaction.

if (Wallet == null)
throw new RpcException(-400, "Access denied");
else
{
UInt160 from = _params.Count >= 1 ? _params[0].AsString().ToScriptHash() : null;
JArray to = _params.Count >= 2 ? (JArray)_params[1] : null;
if (to == null || to.Count == 0)
throw new RpcException(-32602, "Invalid params");
TransferOutput[] outputs = new TransferOutput[to.Count];
for (int i = 0; i < to.Count; i++)
{
UIntBase asset_id = UIntBase.Parse(to[i]["asset"].AsString());
AssetDescriptor descriptor = new AssetDescriptor(asset_id);
outputs[i] = new TransferOutput
{
AssetId = asset_id,
Value = BigDecimal.Parse(to[i]["value"].AsString(), descriptor.Decimals),
ScriptHash = to[i]["address"].AsString().ToScriptHash()
};
if (outputs[i].Value.Sign <= 0)
throw new RpcException(-32602, "Invalid params");
}
Fixed8 fee = _params.Count >= 3 ? Fixed8.Parse(_params[2].AsString()) : Fixed8.Zero;
if (fee < Fixed8.Zero)
throw new RpcException(-32602, "Invalid params");
UInt160 change_address = _params.Count >= 4 ? _params[3].AsString().ToScriptHash() : null;
Transaction tx = Wallet.MakeTransaction(null, outputs, @from: from, change_address: change_address, fee: fee);
if (tx == null)
throw new RpcException(-300, "Insufficient funds");
ContractParametersContext context = new ContractParametersContext(tx);
Wallet.Sign(context);
if (context.Completed)
{
tx.Witnesses = context.GetWitnesses();
Wallet.ApplyTransaction(tx);
system.LocalNode.Tell(new LocalNode.Relay { Inventory = tx });
return tx.ToJson();
}
else
{
return context.ToJson();
}
}
case "sendrawtransaction":
{
Transaction tx = Transaction.DeserializeFrom(_params[0].AsString().HexToBytes());
Expand Down
1 change: 1 addition & 0 deletions neo/Wallets/Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ public virtual WalletAccount Import(string nep2, string passphrase)
}
else
{
//todo: check specified from valid
UInt160[] accounts = from == null ? GetAccounts().Where(p => !p.Lock && !p.WatchOnly).Select(p => p.ScriptHash).ToArray() : new[] { from };
HashSet<UInt160> sAttributes = new HashSet<UInt160>();
using (ScriptBuilder sb = new ScriptBuilder())
Expand Down