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?

  • 1
    are you Asking about bitwasp-php library or Bitcoin-core JSONRPC? – Adam Apr 22 '18 at 8:38
  • Hey toddmo, could you please add some more details what software you're working with and then flag your question for reopening? – Murch Apr 22 '18 at 14:56
  • Do you mean "which network" as in "testnet or mainnet"? – Murch Apr 22 '18 at 20:01
  • @Murch, yes sir. I guess a chain is a network and that's why they call it that? Should I put chain? – toddmo Apr 22 '18 at 20:11
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
}
  • Seems to be that OP is asking about bitwasp-php library, – Adam Apr 22 '18 at 8:37
  • 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 Apr 22 '18 at 8:48
  • @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 Apr 22 '18 at 19:29
  • 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 Apr 23 '18 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.