MetacoSA / NBitcoin

Comprehensive Bitcoin library for the .NET framework.
MIT License
1.86k stars 844 forks source link

`GetDestinationAddress` returns a different address compared to the online explorers #1220

Open qui8t opened 1 month ago

qui8t commented 1 month ago

I am trying to get the destination address of txout n=1 in the following TX.

{
    "txid": "7729045ec9b7cfe62811c7fee5b9b2839a1e45f3827867776b6671cabae84462",
    "hash": "7729045ec9b7cfe62811c7fee5b9b2839a1e45f3827867776b6671cabae84462",
    "version": 1,
    "size": 341,
    "vsize": 341,
    "weight": 1364,
    "locktime": 0,
    "vin": [ ... ],
    "vout": [
        { ... },
        {
            "value": 0.00000001,
            "n": 1,
            "scriptPubKey": {
                "asm": "04f816b480f87144ec4de5862adf028ff66cc6964250325d53fd22bf8922824b6f1e1f2c881ae0608ec77ebf88a75c66d3099113a7343238f2f7a0ebb91a4ed335 OP_CHECKSIG",
                "desc": "pk(04f816b480f87144ec4de5862adf028ff66cc6964250325d53fd22bf8922824b6f1e1f2c881ae0608ec77ebf88a75c66d3099113a7343238f2f7a0ebb91a4ed335)#f580dnzn",
                "hex": "4104f816b480f87144ec4de5862adf028ff66cc6964250325d53fd22bf8922824b6f1e1f2c881ae0608ec77ebf88a75c66d3099113a7343238f2f7a0ebb91a4ed335ac",
                "type": "pubkey"
            }
        }
    ],
    "fee": 0.00010000,
    "hex": "010000000135b0092a869bca1bc43e1628b3cb9e56ff9099a271fe95755e6f9289cf885b98010000008a47304402203bb15015fd7f06c8bdc70439141b74d8a5bb7958ab61ad60a8d505a4012468ba02203a2c3aa0a5284eccc9b9d0cafa7331b03e3e92dea584c7bd5c43fae6f56534650141045e89bf6661862d86014b6d2ea38606c47269a848d6e7a9f67903778e5e11555a3b9732c5c3cdbf9d7e7edcf18c50b8c39ef3ac9292d925e75c7f99b550582c0affffffff026f6f9800000000004341047cd6982f115651118f360a2d07f243ceea975b8c001822b99207093e06c091b392dd3dffe6d989e00e2144f6a6540cc46300e8746275a5efc4f25fc0b083244eac0100000000000000434104f816b480f87144ec4de5862adf028ff66cc6964250325d53fd22bf8922824b6f1e1f2c881ae0608ec77ebf88a75c66d3099113a7343238f2f7a0ebb91a4ed335ac00000000"
},

Using the following code:

var hex = "4104f816b480f87144ec4de5862adf028ff66cc6964250325d53fd22bf8922824b6f1e1f2c881ae0608ec77ebf88a75c66d3099113a7343238f2f7a0ebb91a4ed335ac";
var script = new Script(hex);
var address = script.GetDestinationAddress(Network.Main);

I get:

address: "38BaJ4Vsv4XgyooXiZyLthTFZUxn97RWAA"

However, the online explorers, such as this, return the following address for this transaction.

1HSrPfMA5joCS5vTnRWQF7GyeodLQZHu6e
joemphilips commented 1 month ago

Use Script.FromHex for parsing a hex string, new Script(str) treats str as an asm.

qui8t commented 1 month ago

Do you mean the following? if so, it returns null address.

var script = Script.FromHex(hex);
var address = script.GetDestinationAddress(Network.Main);
joemphilips commented 1 month ago

It is because this is P2PK script pubkey, and p2pk does not have an defined address format. Reading your comment from previous issue, https://github.com/MetacoSA/NBitcoin/issues/1219#issuecomment-2224407074 you must either do the following depending on your goal.

  1. Just use scriptpubkey (hex or asm) everywhere for the sake of uniformity
  2. Skip p2pk, it has been legacy a long time ago anyway
  3. build p2pkh address from the pk, which does not exist on the blockchain, so you must add metadata to tell that this is actually not built from a p2pkh but from a p2pk scriptpubkey.

I don't know your goal, so I cannot give any advice more than this.

qui8t commented 1 month ago

Thanks! Is option 3 the approach implemented by the chain explorers similar to the one I linked above?

joemphilips commented 1 month ago

Is option 3 the approach implemented by the chain explorers similar to the one I linked above?

Yes, I suppose.

qui8t commented 1 month ago

Any thoughts how I can do that with NBitcoin?

joemphilips commented 1 month ago

https://programmingblockchain.gitbook.io/programmingblockchain/other_types_of_ownership/p2pk-h-_pay_to_public_key_-hash

qui8t commented 1 month ago

Thanks! Have you tried the method in the link you suggested on the specific output I mentioned above? I tried, and still I get zero destination public keys, and not sure how I can get the same address as the online explorers for that specific output.