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

Add auth for EvmOut transfer balance #1850

Merged
merged 3 commits into from
Apr 11, 2023
Merged
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
20 changes: 18 additions & 2 deletions src/masternodes/mn_checks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3870,13 +3870,29 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor {
}
}
} else if (obj.type == CTransferBalanceType::EvmOut) {
for (const auto& [addr, balances] : obj.from) {

for (const auto& [addr, _] : obj.from) {
CTxDestination dest;
if (ExtractDestination(addr, dest)) {
if (dest.index() != WitV16KeyEthHashType) {
return Res::Err("Invalid destination");
return Res::Err("From address must be an ETH address in case of \"evmout\" transfertype");
}
}
bool foundAuth = false;
for (const auto &input : tx.vin) {
const Coin &coin = coins.AccessCoin(input.prevout);
std::vector<TBytes> vRet;
if (Solver(coin.out.scriptPubKey, vRet) == txnouttype::TX_PUBKEYHASH)
{
auto it = input.scriptSig.begin();
CPubKey pubkey(input.scriptSig.begin() + *it + 2, input.scriptSig.end());
auto script = GetScriptForDestination(WitnessV16EthHash(pubkey));
if (script == addr)
foundAuth = true;
}
}
if (!foundAuth)
return Res::Err("authorization not found for %s in the tx", ScriptToString(addr));

const auto fromAddress = std::get<WitnessV16EthHash>(dest);

Expand Down
4 changes: 2 additions & 2 deletions src/masternodes/mn_rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ CMutableTransaction fund(CMutableTransaction & mtx, CWalletCoinsUnlocker& pwalle
return mtx;
}

static CTransactionRef sign(CMutableTransaction& mtx, CWallet* const pwallet, CTransactionRef optAuthTx) {
CTransactionRef sign(CMutableTransaction& mtx, CWallet* const pwallet, CTransactionRef optAuthTx) {

// assemble prevouts from optional linked tx
UniValue prevtxs(UniValue::VARR);
Expand Down Expand Up @@ -316,7 +316,7 @@ static std::optional<CTxIn> GetAuthInputOnly(CWalletCoinsUnlocker& pwallet, CTxD
return txin;
}

static CTransactionRef CreateAuthTx(CWalletCoinsUnlocker& pwallet,
static CTransactionRef CreateAuthTx(CWalletCoinsUnlocker& pwallet,
std::set<CScript> const & auths,
int32_t txVersion,
const CoinSelectionOptions &coinSelectOpts) {
Expand Down
2 changes: 2 additions & 0 deletions src/masternodes/mn_rpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ CMutableTransaction fund(CMutableTransaction &mtx,
CTransactionRef optAuthTx,
CCoinControl *coin_control = nullptr,
const CoinSelectionOptions &coinSelectOpts = CoinSelectionOptions::CreateDefault());
CTransactionRef send(CTransactionRef tx, CTransactionRef optAuthTx);
CTransactionRef sign(CMutableTransaction& mtx, CWallet* const pwallet, CTransactionRef optAuthTx);
CTransactionRef signsend(CMutableTransaction &mtx, CWalletCoinsUnlocker &pwallet, CTransactionRef optAuthTx);
CTransactionRef send(CTransactionRef tx, CTransactionRef optAuthTx);
CWalletCoinsUnlocker GetWallet(const JSONRPCRequest &request);
Expand Down
17 changes: 13 additions & 4 deletions src/masternodes/rpc_accounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2015,8 +2015,16 @@ UniValue transferbalance(const JSONRPCRequest& request) {
CTransactionRef optAuthTx;
std::set<CScript> auths;
if (msg.type != CTransferBalanceType::EvmOut)
for(auto& addresses : msg.from)
auths.insert(addresses.first);
for(auto& address : msg.from)
auths.insert(address.first);
else
for(auto& address : msg.from)
if (IsMine(*pwallet, address.first))
{
const auto key = AddrToPubKey(pwallet, ScriptToString(address.first));
const auto auth = GetScriptForDestination(PKHash(key.GetID()));
auths.insert(auth);
}

UniValue txInputs(UniValue::VARR);
rawTx.vin = GetAuthInputsSmart(pwallet, rawTx.nVersion, auths, false, optAuthTx, txInputs);
Expand All @@ -2033,10 +2041,11 @@ UniValue transferbalance(const JSONRPCRequest& request) {

fund(rawTx, pwallet, optAuthTx, &coinControl);

auto txRef = sign(rawTx, pwallet, optAuthTx);
// check execution
execTestTx(CTransaction(rawTx), targetHeight, optAuthTx);
execTestTx(*txRef, targetHeight, optAuthTx);

return signsend(rawTx, pwallet, optAuthTx)->GetHash().GetHex();
return send(txRef, optAuthTx)->GetHash().GetHex();
}

UniValue getburninfo(const JSONRPCRequest& request) {
Expand Down