Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
qui8t opened this issue Jul 14, 2024 · 8 comments

Comments

@qui8t
Copy link

qui8t commented Jul 14, 2024

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
Copy link
Contributor

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

@qui8t
Copy link
Author

qui8t commented Jul 15, 2024

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

var script = Script.FromHex(hex);
var address = script.GetDestinationAddress(Network.Main);

@joemphilips
Copy link
Contributor

It is because this is P2PK script pubkey, and p2pk does not have an defined address format.
Reading your comment from previous issue, #1219 (comment)
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
Copy link
Author

qui8t commented Jul 15, 2024

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

@joemphilips
Copy link
Contributor

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

Yes, I suppose.

@qui8t
Copy link
Author

qui8t commented Jul 15, 2024

Any thoughts how I can do that with NBitcoin?

@qui8t
Copy link
Author

qui8t commented Jul 17, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants