How can I detect which network I'm connected to?

0

I'm in a unit test and I would like to use different test addresses depending on which network I'm connected to, to keep test output limited to the network I'm actually connected to.

I know I can run validateaddress to figure it out, but how can I detect this directly?

toddmo

Posted 2018-04-22T01:17:11.673

Reputation: 674

1are you Asking about bitwasp-php library or Bitcoin-core JSONRPC?Adam 2018-04-22T08:38:02.180

Hey toddmo, could you please add some more details what software you're working with and then flag your question for reopening?Murch 2018-04-22T14:56:20.487

Do you mean "which network" as in "testnet or mainnet"?Murch 2018-04-22T20:01:32.000

@Murch, yes sir. I guess a chain is a network and that's why they call it that? Should I put chain?toddmo 2018-04-22T20:11:53.880

Answers

3

getblockchaininfo should tell you which chain you're on. For instance, on the main chain, you should get something like:

{
    "result": {
        "chain": "main",   <======= This is what you want
        "blocks": 519338,
        "headers": 519338,
        "bestblockhash": "0000000000000000001822f09f1821deb6b3b74e36ea1de4e232e5c60fa30ed9",
        "difficulty": 3839316899029.672,
        "mediantime": 1524358671,
        "verificationprogress": 0.9999957624003699,
        "chainwork": "0000000000000000000000000000000000000000018fa8011b05ef506160c4ff",
        "pruned": false,
        "softforks": [
            {
                "id": "bip34",
                "version": 2,
                "reject": {
                    "status": true
                }
            },
            {
                "id": "bip66",
                "version": 3,
                "reject": {
                    "status": true
                }
            },
            {
                "id": "bip65",
                "version": 4,
                "reject": {
                    "status": true
                }
            }
        ],
        "bip9_softforks": {
            "csv": {
                "status": "active",
                "startTime": 1462060800,
                "timeout": 1493596800,
                "since": 419328
            },
            "segwit": {
                "status": "active",
                "startTime": 1479168000,
                "timeout": 1510704000,
                "since": 481824
            }
        }
    },
    "error": null,
    "id": null
}

Raghav Sood

Posted 2018-04-22T01:17:11.673

Reputation: 10 897

Seems to be that OP is asking about bitwasp-php library,Adam 2018-04-22T08:37:26.737

getblockchaininfo is part of the standard rpc interface, so I suspect any library will support it by default. bitwasp-php seems to include it in its tests too.Raghav Sood 2018-04-22T08:48:20.627

@Adam, I tagged it json-rpc. (anyway, bitwasp never had ValidateAddress in the Bitcoin class). The Bitcoin class name is used all over the place, in EasyBitcoin, etc. But you can keep it closed if you wish, since I have my answer.toddmo 2018-04-22T19:29:45.980

Sorry, but it was not clear at all. because Bitcoind chain is not something that you change regularly through an RPC command, so you would know which chain you're connected to by the way you've started it. bitcoind -testnet or bitcoind -regtest etc..Adam 2018-04-23T07:10:29.503