Bump! Sorry if it's too soon to do a bump, also I edited my above reply.
I have another question, it's only to confirm I understood the process.
I read the following stackexchange posts:
-
https://bitcoin.stackexchange.com/questions/8250/what-is-relation-between-scriptsig-and-scriptpubkey-
https://bitcoin.stackexchange.com/questions/93966/getting-public-key-from-any-raw-transaction-what-to-take-care-of-
https://bitcoin.stackexchange.com/questions/8235/how-is-the-output-script-formed-
https://bitcoin.stackexchange.com/questions/91088/how-do-i-get-the-scriptpubkey-type-from-the-raw-output-script-
https://bitcoin.stackexchange.com/questions/100907/retrieving-an-addresses-public-key-from-pkscript-sigscript-witnessSo, if I understand correctly when you generate a transaction, then you have a sender (input) and receiver (output). When everything is signed and sent into the blockchain, two things happen,
the sender (input) sends this into the blockchain (created by its wallet client):
scriptPubKey = OP_DUP OP_HASH160
OP_EQUALVERIFY OP_CHECKSIGand the receiver (output):
Everything right until here? (correct me please)
Then, if I would like to retrieve the public key I must define what it's going on here and which options do I have left.
I'd like to have as many public keys as possible.
To clarify things,
to crawl pubkey from the sender (input) then I'll need to look into the signature on the scriptSig part of the receiver (output), and try to decode it?
to crawl pubkey from the receiver (output) then I'll need to look into the type of script as
@pooya87 stated here:
When spending a P2PKH output, the public key is the top stack element when reaching OP_CHECKSIG.
When spending a P2WPKH output, the public key is the second witness item.
When spending a P2PK output, the public key is in the output.
When spending a P2TR output, the public key is a tweaked pubkey in the output (the witness program).
When spending other scripts such as P2SH when you reach any of the above op codes it should be the top stack element or in case of multi signatures there are multiple pubkeys.
So, Idk if I'm missing something more, I think that's the only way to get the public keys on both cases.
There are some made examples that are useful:
-
https://github.com/bitcoin-core/btcdeb#script-compiler> btcc OP_DUP OP_HASH160 897c81ac37ae36f7bc5b91356cfb0138bfacb3c1 OP_EQUALVERIFY OP_CHECKSIG
> 76a914897c81ac37ae36f7bc5b91356cfb0138bfacb3c188ac
But the hash generated here is not a public key?
And also I saw a php implementation for a rpc client for btc node, and a very interesting issue:
-
https://github.com/Bit-Wasp/bitcoin-p2p-php/issues/29$db = new PDO('pgsql:dbname=explore;host=localhost;','pgsql','');
$count = $rpc->getblockcount();
for ($i=0;$i<=$count;$i++) {
$hash = $rpc->getblock($rpc->getblockhash($i));
foreach ($hash['tx'] as $tx) {
$raw = $rpc->getrawtransaction($tx);
if ($raw == -5) {
continue;
} else {
$x = $rpc->decoderawtransaction($raw);
if(is_array($x)) {
foreach ($x['vin'] as $vin) {
if (isset($vin['coinbase']))
continue;
$txid = $vin['txid'];
$stmt = $db->prepare('delete from tx where txid = :id');
$stmt->bindValue('id',$x['txid']);
$stmt->execute();
}
foreach ($x['vout'] as $index=>$out) {var_dump($out['value']);
$stmt = $db->prepare('insert into tx (txid,"value",n,pubkey,address) values (:id,:value,:n,:pubkey,:address)');
$stmt->bindValue(':id',$x['txid']);
$stmt->bindValue(':value',$out['value']);
$stmt->bindValue(':n',$index);
$stmt->bindValue(':pubkey',$out['scriptPubKey']['hex']);
$stmt->bindValue(':address',$out['scriptPubKey']['addresses'][0]);
$stmt->execute();
}
}
}
}
}
So,
$stmt->bindValue(':pubkey',$out['scriptPubKey']['hex']);
$stmt->bindValue(':address',$out['scriptPubKey']['addresses'][0]);
- :pubkey is the hash
- :address is the public key (I need to research this)?More questions,
- so the easy way is to have the sender (input) pubkey?
- because, the receiver (output) must have to recover the public key from it's signature which is a expensive process for a lot of addreses?
- we have 720M of transactions, txid (right?)?
https://www.blockchain.com/charts/n-transactions-total if I list txid I can still use btcc to have pubkey (I think not)?
- having bitcoin core rpc calls:
https://developer.bitcoin.org/reference/rpc/index.html which has listunspent that does something similar to btcposbal2csv (as I understood)?
- but isn't there any rpc call to retrieve public keys from an txid? (EDIT: well, I saw this:
https://gist.github.com/t4sk/68dbde1ef75762753214c0b3823097e8 and on documentation you can see:
https://developer.bitcoin.org/reference/rpc/validateaddress.html but I don't know if it will work)
- and last, even if I have the pubkey from the sender/receiver I must check the balance if it's positive?
From the last question, I have some premises that Idk if they are right:
Most of the public keys exposed:
- have a high bit range (above 120 bits)
- the ones left with lower bit range are all spent/cracked (their corresponding btc address has a 0 BTC balance)