Skip to content

Commit 798a589

Browse files
committed
wallet: extract PubKey from P2PK script with Solver
The function ExtractPubKey() checks if a given script matches the P2PK pattern (<PubKey> OP_CHECKSIG), extracts the PubKey and additionally checks if it is cryptographically valid (full validation with ECC library via .IsFullyValid()). Currently this is done manually in the following order: 1) check if first script OP is data push with valid PubKey length (first part of pattern match), extract PubKey 2) create CPubKey object with extracted PubKey 3) fully validate public key 4) check if last script OP is OP_CHECKSIG (second part of pattern match) Using Solver, the pattern matching and PubKey extraction can be done via a single step, leading to the following simplified order with shorter code: 1) check if given script matches P2PK pattern with Solver (also contains valid PubKey length check), extracts Pubkey 2) create CPubKey object with extracted Pubkey 3) fully validate public key
1 parent fc5b756 commit 798a589

File tree

1 file changed

+3
-12
lines changed

1 file changed

+3
-12
lines changed

src/wallet/wallet.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -523,18 +523,9 @@ bool CWallet::LoadCScript(const CScript& redeemScript)
523523

524524
static bool ExtractPubKey(const CScript &dest, CPubKey& pubKeyOut)
525525
{
526-
//TODO: Use Solver to extract this?
527-
CScript::const_iterator pc = dest.begin();
528-
opcodetype opcode;
529-
std::vector<unsigned char> vch;
530-
if (!dest.GetOp(pc, opcode, vch) || !CPubKey::ValidSize(vch))
531-
return false;
532-
pubKeyOut = CPubKey(vch);
533-
if (!pubKeyOut.IsFullyValid())
534-
return false;
535-
if (!dest.GetOp(pc, opcode, vch) || opcode != OP_CHECKSIG || dest.GetOp(pc, opcode, vch))
536-
return false;
537-
return true;
526+
std::vector<std::vector<unsigned char>> solutions;
527+
return Solver(dest, solutions) == TX_PUBKEY &&
528+
(pubKeyOut = CPubKey(solutions[0])).IsFullyValid();
538529
}
539530

540531
bool CWallet::AddWatchOnlyInMem(const CScript &dest)

0 commit comments

Comments
 (0)