Skip to content

Commit a8ecd0d

Browse files
committed
Merge #16753: wallet: extract PubKey from P2PK script with Solver
798a589 wallet: extract PubKey from P2PK script with Solver (Sebastian Falbesoner) Pull request description: 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 `CPubKey::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 ACKs for top commit: instagibbs: utACK bitcoin/bitcoin@798a589 theStack: > utACK [798a589](bitcoin/bitcoin@798a589) sipa: ACK 798a589 achow101: Code Review ACK 798a589 Tree-SHA512: 350358a89afed8c2a7967c50e9714a2d4a909259b50e694ce68dde3e7d0fa0bf3238d33642e73f2bdb53860f6d3f7327ca3eb6426b74eaffacfbca0a384d68cd
2 parents 74da99e + 798a589 commit a8ecd0d

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)