Closed AmbitiousScriptKiddy closed 7 years ago
Hey, the scriptPubKey does not have any pubkey, just the hash. You can use classes like PayToPubkeyHashTemplate.Extract*
to parse scriptSig or scriptPubKey.
Please take a look at https://www.gitbook.com/book/programmingblockchain/programmingblockchain/details
Thank you for the link. I did not use PayToPubkeyHashTemplate.Extract as you suggested in the end, but I actually found what I was looking for in the first place, and it's far simpler than I thought: ScriptPubKey might not have an Address property, but it does have a GetDestinationAddress() function. I don't exactly get why GetDestinationAddress() returns NULL when the TxOut was sent directly to a PubKey, instead of deriving the correct address from that PubKey (which I can easily do now via GetDestinationPublicKeys and then using the GetAddress function from that public key). So conclusio: It's not intuitive, but everything necessary to get the job done is there. Thanks again for the help
GetDestinationAddress() returns NULL when the TxOut was sent directly to a PubKey
because an "address" has a very specific meaning: It is a user friendly way to represent a payment destination.
When you send directly to a pubkey (P2PK), there is no standard widely supported to represent it as an address. You must send to a pubkey hash (P2PKH), this will correctly generate an address.
Why are you using P2PK? it is deprecated, nobody use it, and you can't find any wallet which support sending money to it.
So conclusio: It's not intuitive, but everything necessary to get the job done is there.
I think this is intuitive, just that you are using P2PK which don't have any address representation, that's why it returns null. I think there is some incomprehension on how bitcoin work here.
I am not trying to send to a PubKey, I am just trying to read through the blockchain and correctly interpret the transactions. My attempt to convert the P2PK into an address had two reasons there: For 1, I am trying to get a standardized format to make my code easier and For 2, I am trying to match the records to what I see on blockchain explorer... And there they display addresses even for such transactions...
@AmbitiousScriptKiddy which blockchain explorer? An example blockchain.info
shows that the genesis address is 1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa. However, this is clearly wrong. If you send money to this address with today's wallet, you won't send to the same ScriptPubKey
than what is in the genesis block.
When you interpret transactions, you are currently making two incorrect assumptions:
Yes, I was looking at blockchain.info, so thank you for that insight... I so far assumed that information on sites like this is a "set of truth" that I can build upon. Your comment makes me wonder though: Why can't all scriptPubKeys represented by an address? From what I understand of the underlying theory, every Private Key does have an associated Public Key, and every Public Key does have an associated address. (two actually, if you count the compressed format.) So while it might not be possible to calculate the correct address for a given scriptPubKey, it doesn't invalidate the fact that for the underlying Private Key an address does exist. So... Is your correct statement that I cannot GET to an address from the data in the blockchain? Or am I missing something and there IS NO address for certain transactions?
It is because a scriptPubKey is a script asking a challenge, a scriptSig answers the challenge. There might or might not have public keys inside the scriptPubKey
. (might be the hash of the public key, which is an address, or something completely different, like an addition)
Take a look at This e-book, I explain simply bitcoin from a conceptual level, for devs. I explain the most known type of scriptPubKey/ScriptSig you can find as well.
I know that this issue is actually mostly PEBKAC, but given that there are numerous threads in forums discussing exactly that, and apparently more people having the same kind of issue I wanted to toss this in here, so maybe one of you really smart people can solve this once and for all: I am trying to interpret the ScriptPubKey from a TxOut transaction in the blockchain, trying to extract either the public key (if the transaction was sent directly to a public key) or the address from that TxOut. Trying and failing that is, most likely because I am always getting the encoding wrong. Is there any chance you can add this as properties to the NBitcoin library? PublicKey would of course only be valid if the transaction was sent to a public key directly, otherwise null. Address should always be valid, because even in case the transaction was sent to a public key it should still be possible to calculate the address from that public key.
Getting this sorted out would make a lot of misery go away, so I would really appreciate if someone could lend a hand here.