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

Get correct compression for encrypted wallet #2610

Merged
merged 3 commits into from
Oct 24, 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
2 changes: 2 additions & 0 deletions src/dfi/rpc_accounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2196,6 +2196,8 @@ UniValue transferdomain(const JSONRPCRequest &request) {

pwallet->BlockUntilSyncedToCurrentChain();

EnsureWalletIsUnlocked(pwallet);

RPCTypeCheck(request.params, {UniValue::VARR}, false);

UniValue srcDstArray(UniValue::VARR);
Expand Down
2 changes: 2 additions & 0 deletions src/dfi/rpc_evm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ UniValue evmtx(const JSONRPCRequest &request) {
}
pwallet->BlockUntilSyncedToCurrentChain();

EnsureWalletIsUnlocked(pwallet);

const auto fromDest = DecodeDestination(request.params[0].get_str());
if (fromDest.index() != WitV16KeyEthHashType) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "from address not an Ethereum address");
Expand Down
9 changes: 2 additions & 7 deletions src/script/signingprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,8 @@ bool FillableSigningProvider::GetPubKey(const CKeyID &address, CPubKey &vchPubKe
if (!GetKey(address, key)) {
return false;
}
auto pubkey = key.GetPubKey();
if (!pubkey.IsCompressed() && address.type == KeyAddressType::COMPRESSED) {
pubkey.Compress();
} else if (pubkey.IsCompressed() && address.type == KeyAddressType::UNCOMPRESSED) {
pubkey.Decompress();
}
vchPubKeyOut = pubkey;
vchPubKeyOut = key.GetPubKey();
ResolveKeyCompression(address.type, vchPubKeyOut);
return true;
}

Expand Down
8 changes: 8 additions & 0 deletions src/script/signingprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,12 @@ class FillableSigningProvider : public SigningProvider
/** Return the CKeyID of the key involved in a script (if there is a unique one). */
CKeyID GetKeyOrDefaultFromDestination(const SigningProvider& store, const CTxDestination& dest);

inline void ResolveKeyCompression(const KeyAddressType type, CPubKey &pubkey) {
if (!pubkey.IsCompressed() && type == KeyAddressType::COMPRESSED) {
pubkey.Compress();
} else if (pubkey.IsCompressed() && type == KeyAddressType::UNCOMPRESSED) {
pubkey.Decompress();
}
}

#endif // DEFI_SCRIPT_SIGNINGPROVIDER_H
7 changes: 1 addition & 6 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4322,12 +4322,7 @@ UniValue addressmap(const JSONRPCRequest &request) {
if (key.IsCompressed()) {
key.Decompress();
}
const auto erc55 = EncodeDestination(WitnessV16EthHash(key));
// Check if it's in the wallet.
// Note: Can be removed if the full wallet is migrated.
// Ref: https://github.com/DeFiCh/ain/issues/2604
AddrToPubKey(pwallet, erc55);
format.pushKV("erc55", erc55);
format.pushKV("erc55", EncodeDestination(WitnessV16EthHash(key)));
break;
}
case AddressConversionType::EVMToDVMAddress: {
Expand Down
8 changes: 4 additions & 4 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4961,9 +4961,9 @@ bool CWallet::GetKey(const CKeyID &address, CKey& keyOut) const
bool CWallet::GetWatchPubKey(const CKeyID &address, CPubKey &pubkey_out) const
{
LOCK(cs_KeyStore);
WatchKeyMap::const_iterator it = mapWatchKeys.find(address);
if (it != mapWatchKeys.end()) {
if (const auto it = mapWatchKeys.find(address); it != mapWatchKeys.end()) {
pubkey_out = it->second;
ResolveKeyCompression(address.type, pubkey_out);
return true;
}
return false;
Expand All @@ -4979,10 +4979,10 @@ bool CWallet::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const
return true;
}

CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
if (mi != mapCryptedKeys.end())
if (const auto mi = mapCryptedKeys.find(address); mi != mapCryptedKeys.end())
{
vchPubKeyOut = (*mi).second.first;
ResolveKeyCompression(address.type, vchPubKeyOut);
return true;
}
// Check for watch-only pubkeys
Expand Down
59 changes: 59 additions & 0 deletions test/functional/feature_evm.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ def run_test(self):
# Toggle EVM
self.toggle_evm_enablement()

# Test Eth on encrypted wallet
self.encrypt_wallet()

def test_tx_without_chainid(self):
node = self.nodes[0]

Expand Down Expand Up @@ -1340,6 +1343,62 @@ def toggle_evm_enablement(self):
int(evm_enabling_block["number"], base=16) + 1,
)

def encrypt_wallet(self):
# Test address
address = "0xa43D1AdBe968BFE45ECfbFa623a25077fd4F5db8"
priv_key = "1ba6d9404e882346a236a6742722fe79d822e2182d6808ab66cc30b7dd07c5b7"
pub_key = "0426f07fbd27600beccd6d4a1a3bbcfd2cced7212e201c2ff2970214ed19ba92f473e102d978c65626b46e61a4b4f770e51b944f10462a111e170aad5b65e638bd"

# Encrypt wallet
self.nodes[0].encryptwallet("test")
self.stop_nodes()
self.start_nodes()

# Unencrypt wallet
self.nodes[0].walletpassphrase("test", 600)

# Import address
self.nodes[0].importprivkey(priv_key)

# Check pubkey
assert_equal(pub_key, self.nodes[0].getaddressinfo(address)["pubkey"])

# Check equivalent DVM address
assert_equal(
"bcrt1qkhd438yhrvnleflep3xlxj4xvnm4q7r0wsxzya",
self.nodes[0].addressmap(address, 2)["format"]["bech32"],
)

# Fund EVM address
self.nodes[0].transferdomain(
[
{
"src": {"address": self.address, "amount": "0.1@DFI", "domain": 2},
"dst": {
"address": address,
"amount": "0.1@DFI",
"domain": 3,
},
}
]
)
self.nodes[0].generate(1)

# Transfer funds back
self.nodes[0].transferdomain(
[
{
"src": {"address": address, "amount": "0.1@DFI", "domain": 3},
"dst": {
"address": self.address,
"amount": "0.1@DFI",
"domain": 2,
},
}
]
)
self.nodes[0].generate(1)


if __name__ == "__main__":
EVMTest().main()