libbitcoin / libbitcoin-explorer

Bitcoin Command Line Tool
Other
598 stars 176 forks source link

hd-to-ec issue when using hd-new version that is disparate from that defined by bx.cfg file #307

Closed skaht closed 8 years ago

skaht commented 8 years ago

I'm toggling the environmental variable BX_CONFIG to point at either bx-mainnet.cfg or bx-dashnet.cfg

bx-mainnet.cfg file contains:

# https://github.com/bitcoin/bitcoin/blob/master/src/chainparams.cpp#L113; (0x04)(0x88)(0xB2)(0x1E)
# The hierarchical deterministic (HD) public key version, defaults to 76067358.
hd_public_version = 76067358
# https://github.com/bitcoin/bitcoin/blob/master/src/chainparams.cpp#L114; (0x04)(0x88)(0xAD)(0xE4)
# The hierarchical deterministic (HD) private key version, defaults to 76066276.
hd_secret_version = 76066276

bx-dashnet.cfg file contains:

# https://github.com/dashpay/dash/blob/master/src/chainparams.cpp#L171
# base58Prefixes[EXT_PUBLIC_KEY] = list_of(0x02)(0xFE)(0x52)(0xF8);
hd_public_version = 50221816
# https://github.com/dashpay/dash/blob/master/src/chainparams.cpp#L172
# base58Prefixes[EXT_SECRET_KEY] = list_of(0x02)(0xFE)(0x52)(0xCC);
hd_secret_version = 50221772

When BX_CONFIG points to bx-mainnet.cfg:

1) Behaves as expected:

% echo 'very complex gibberish' | bx base16-encode | bx sha256 | bx hd-new | bx hd-private -d -i 44 | bx hd-private -d -i 5 | bx hd-private -d -i 0 | bx hd-private -i 0 | bx hd-private -i 0 | bx hd-to-ec | bx ec-to-wif -v 128
L2xdLNMYneEbTuGtYgMMCFASFLHt1iJqQf2EYcCvAzhK8296ieAK

2) Behaves as expected, using "-v 76066276":

% echo 'very complex gibberish' | bx base16-encode | bx sha256 | bx hd-new -v 76066276 | bx hd-private -d -i 44 | bx hd-private -d -i 5 | bx hd-private -d -i 0 | bx hd-private -i 0 | bx hd-private -i 0 | bx hd-to-ec | bx ec-to-wif -v 128
L2xdLNMYneEbTuGtYgMMCFASFLHt1iJqQf2EYcCvAzhK8296ieAK

3) Behaves as expected, subset of step number 2:

% echo 'very complex gibberish' | bx base16-encode | bx sha256 | bx hd-new -v 76066276 | bx hd-private -d -i 44 | bx hd-private -d -i 5 | bx hd-private -d -i 0 | bx hd-private -i 0 | bx hd-private -i 0
xprvA3w1Xvu2jW85HjBQiJSLHXZyuVqxyBfh5gFRkbffRfBP6NfquatTxd2nEX1fxAZZXjzXr2qUQ6dosHSiSi7LdCyq5QCjJvmvfPoW2X42qoi

4) Unexpected behavior:

% echo 'very complex gibberish' | bx base16-encode | bx sha256 | bx hd-new -v 50221772 | bx hd-private -d -i 44 | bx hd-private -d -i 5 | bx hd-private -d -i 0 | bx hd-private -i 0 | bx hd-private -i 0 | bx hd-to-ec | bx ec-to-wif -v 204
Error: the argument ('ERROR_VERSION') is invalid

5) Should be useful for trouble shooting, subset of step 4:

% echo 'very complex gibberish' | bx base16-encode | bx sha256 | bx hd-new -v 50221772 | bx hd-private -d -i 44 | bx hd-private -d -i 5 | bx hd-private -d -i 0 | bx hd-private -i 0 | bx hd-private -i 0
drkpS4gwHYvBt8DfUAbDGWZ7JCKxvZ5w9HFdm6GgNCmofRqUrE7MHfZC4Am7h1iseBdEXHVMtXmhoHSxWCwurGCs7gZkzdGBBJM4mDuARDozoWq

When BX_CONFIG points to bx-dashnet.cfg:

6) Behaves as expected, contrast results to step #5:

% echo 'very complex gibberish' | bx base16-encode | bx sha256 | bx hd-new | bx hd-private -d -i 44 | bx hd-private -d -i 5 | bx hd-private -d -i 0 | bx hd-private -i 0 | bx hd-private -i 0
drkpS4gwHYvBt8DfUAbDGWZ7JCKxvZ5w9HFdm6GgNCmofRqUrE7MHfZC4Am7h1iseBdEXHVMtXmhoHSxWCwurGCs7gZkzdGBBJM4mDuARDozoWq

7) Behaves as expected:

% echo 'very complex gibberish' | bx base16-encode | bx sha256 | bx hd-new | bx hd-private -d -i 44 | bx hd-private -d -i 5 | bx hd-private -d -i 0 | bx hd-private -i 0 | bx hd-private -i 0 | bx hd-to-ec | bx ec-to-wif -v 204
XH2Yndjv6Ks3XEHGaSMDhUMTAMZTTWv5nEN958Y7VMyQXBCJVQmM

8) Behaves as expected, added "-v 50221772" to hd-new:

% echo 'very complex gibberish' | bx base16-encode | bx sha256 | bx hd-new -v 50221772 | bx hd-private -d -i 44 | bx hd-private -d -i 5 | bx hd-private -d -i 0 | bx hd-private -i 0 | bx hd-private -i 0 | bx hd-to-ec | bx ec-to-wif -v 204
XH2Yndjv6Ks3XEHGaSMDhUMTAMZTTWv5nEN958Y7VMyQXBCJVQmM

9) Unexpected behavior:

% echo 'very complex gibberish' | bx base16-encode | bx sha256 | bx hd-new -v 76066276 | bx hd-private -d -i 44 | bx hd-private -d -i 5 | bx hd-private -d -i 0 | bx hd-private -i 0 | bx hd-private -i 0 | bx hd-to-ec | bx ec-to-wif -v 128
Error: the argument ('ERROR_VERSION') is invalid

10) Seems to behave as expected without hd-to-ec, contrast results to step 3:

% echo 'very complex gibberish' | bx base16-encode | bx sha256 | bx hd-new -v 76066276 | bx hd-private -d -i 44 | bx hd-private -d -i 5 | bx hd-private -d -i 0 | bx hd-private -i 0 | bx hd-private -i 0
xprvA3w1Xvu2jW85HjBQiJSLHXZyuVqxyBfh5gFRkbffRfBP6NfquatTxd2nEX1fxAZZXjzXr2qUQ6dosHSiSi7LdCyq5QCjJvmvfPoW2X42qoi
evoskuil commented 8 years ago

The error is coming from one command, but there are 10 commands chained in the repro, as well as a config file. It would be helpful if you could reduce the repro to a single command and determine whether it requires the config file or whether that is independent.

skaht commented 8 years ago

My error...

When using a default bx-mainnet.cfg file, per your recommendation, the hd-to-ec sub-command required an additional bx-dashnet.cfg file argument to function properly.

When bx is configured for the mainnet the following behavior is correct for Dash.

% echo 'drkpS4gwHYvBt8DfUAbDGWZ7JCKxvZ5w9HFdm6GgNCmofRqUrE7MHfZC4Am7h1iseBdEXHVMtXmhoHSxWCwurGCs7gZkzdGBBJM4mDuARDozoWq' | bx hd-to-ec -c bx-dashnet.cfg | bx ec-to-wif -v 204 
XH2Yndjv6Ks3XEHGaSMDhUMTAMZTTWv5nEN958Y7VMyQXBCJVQmM
evoskuil commented 8 years ago

Cool. I'm still a little confused as to what the error message is based on. This is a boost:program_options message, so not entirely under our control. My concern is that the message refers to a non-existent parameter definition (ERROR_VERSION). Anything you could do to help isolate that would be appreciated.

skaht commented 8 years ago

Notice the results when I'm using a default bx-mainnet.cfg file (set by the BX_CONFIG environmental variable), and Dash HD extended private key. The hd-to-ec fails without a bx-dashnet.cfg file being explicitly passed.

% echo 'drkpS4gwHYvBt8DfUAbDGWZ7JCKxvZ5w9HFdm6GgNCmofRqUrE7MHfZC4Am7h1iseBdEXHVMtXmhoHSxWCwurGCs7gZkzdGBBJM4mDuARDozoWq' | bx hd-to-ec
ERROR_VERSION
evoskuil commented 8 years ago

What about with no config file and without the env var set, or if you don't pipe the parameter but set it directly on the command line? Also, use the settings command to ensure the config file is being read as expected.

skaht commented 8 years ago

With Default Config File:

% echo $BX_CONFIG
/Users/XXX/Projects/bitcoin-explorer/bx-mainnet.cfg

Immediately followed by:

% bx settings
settings
{
    network
    {
        channel_handshake_seconds 30
        connect_retries 0
        connect_timeout_seconds 5
        debug_file debug.log
        error_file error.log
        hosts_file hosts.cache
        identifier 3652501241
        seeds seed.bitnodes.io:8333,seed.bitcoinstats.com:8333,seed.bitcoin.sipa.be:8333,dnsseed.bluematt.me:8333,seed.bitcoin.jonasschnelli.ch:8333,dnsseed.bitcoin.dashjr.org:8333
    }
    server
    {
        cert_file ""
        connect_retries 0
        connect_timeout_seconds 5
        server_cert_key ""
        url tcp://obelisk.airbitz.co:9091
    }
    wallet
    {
        hd_public_version 76067358
        hd_secret_version 76066276
        pay_to_public_key_hash_version 0
        pay_to_script_hash_version 5
        transaction_version 1
        wif_version 128
    }
}

Immediately followed by:

% echo 'drkpS4gwHYvBt8DfUAbDGWZ7JCKxvZ5w9HFdm6GgNCmofRqUrE7MHfZC4Am7h1iseBdEXHVMtXmhoHSxWCwurGCs7gZkzdGBBJM4mDuARDozoWq' | bx hd-to-ec
ERROR_VERSION

Without Default Config File:

Immediately followed by:

% unsetenv BX_CONFIG
% echo $BX_CONFIG
BX_CONFIG: Undefined variable.

Immediately followed by:

% bx settings
settings
{
    network
    {
        channel_handshake_seconds 30
        connect_retries 0
        connect_timeout_seconds 5
        debug_file debug.log
        error_file error.log
        hosts_file hosts.cache
        identifier 3652501241
        seeds seed.bitnodes.io:8333,seed.bitcoinstats.com:8333,seed.bitcoin.sipa.be:8333,dnsseed.bluematt.me:8333,seed.bitcoin.jonasschnelli.ch:8333,dnsseed.bitcoin.dashjr.org:8333
    }
    server
    {
        cert_file ""
        connect_retries 0
        connect_timeout_seconds 5
        server_cert_key ""
        url tcp://obelisk.airbitz.co:9091
    }
    wallet
    {
        hd_public_version 76067358
        hd_secret_version 76066276
        pay_to_public_key_hash_version 0
        pay_to_script_hash_version 5
        transaction_version 1
        wif_version 128
    }
}

Immediately followed by:

% echo 'drkpS4gwHYvBt8DfUAbDGWZ7JCKxvZ5w9HFdm6GgNCmofRqUrE7MHfZC4Am7h1iseBdEXHVMtXmhoHSxWCwurGCs7gZkzdGBBJM4mDuARDozoWq' | bx hd-to-ec
ERROR_VERSION

Immediately followed by:

% echo 'drkpS4gwHYvBt8DfUAbDGWZ7JCKxvZ5w9HFdm6GgNCmofRqUrE7MHfZC4Am7h1iseBdEXHVMtXmhoHSxWCwurGCs7gZkzdGBBJM4mDuARDozoWq' | bx hd-to-ec -c bx-dashnet.cfg | bx ec-to-wif -v 204
XH2Yndjv6Ks3XEHGaSMDhUMTAMZTTWv5nEN958Y7VMyQXBCJVQmM

Immediately followed by:

% bx settings -c bx-dashnet.cfg
settings
{
    network
    {
        channel_handshake_seconds 30
        connect_retries 0
        connect_timeout_seconds 5
        debug_file debug.log
        error_file error.log
        hosts_file hosts.cache
        identifier 3652501241
        seeds testnet-seed.alexykot.me:9999,testnet-seed.bitcoin.petertodd.org:9999,testnet-seed.bluematt.me:9999,testnet-seed.bitcoin.schildbach.de:9999
    }
    server
    {
        cert_file ""
        connect_retries 0
        connect_timeout_seconds 5
        server_cert_key ""
        url tcp://obelisk-testnet.airbitz.co:9091
    }
    wallet
    {
        hd_public_version 50221816
        hd_secret_version 50221772
        pay_to_public_key_hash_version 76
        pay_to_script_hash_version 16
        transaction_version 1
        wif_version 204
    }
}
skaht commented 8 years ago

Watch this hat trick:

% echo 'very complex gibberish' | bx base16-encode | bx sha256 | bx hd-new | bx hd-private -d -i 44 | bx hd-private -d -i 5 | bx hd-private -d -i 0 | bx hd-private -i 0 | bx hd-private -i 0 | bx hd-to-ec | bx ec-to-wif -v 204
XH2Yndjv6Ks3XEHGaSMDhUMTAMZTTWv5nEN958Y7VMyQXBCJVQmM

The result above produces the correct results without introducing the -v 50221772 to the hd-new in the middle above and appears to work with any default altcoin config file. The wallet settings below are for DOGE.

bx settings
settings
{
    network
    {
        channel_handshake_seconds 30
        connect_retries 0
        connect_timeout_seconds 5
        debug_file debug.log
        error_file error.log
        hosts_file hosts.cache
        identifier 3652501241
        seeds testnet-seed.alexykot.me:9999,testnet-seed.bitcoin.petertodd.org:9999,testnet-seed.bluematt.me:9999,testnet-seed.bitcoin.schildbach.de:9999
    }
    server
    {
        cert_file ""
        connect_retries 0
        connect_timeout_seconds 5
        server_cert_key ""
        url tcp://obelisk-testnet.airbitz.co:9091
    }
    wallet
    {
        hd_public_version 49990397
        hd_secret_version 49988504
        pay_to_public_key_hash_version 30
        pay_to_script_hash_version 22
        transaction_version 1
        wif_version 158
    }
}

Note the results below are the correct BIP 44 results for Dash when using a default bx-doge.cfg wallet parameters.

% echo 'very complex gibberish' | bx base16-encode | bx sha256 | bx hd-new | bx hd-private -d -i 44 | bx hd-private -d -i 5 | bx hd-private -d -i 0 | bx hd-private -i 0 | bx hd-private -i 0 | bx hd-to-ec | bx ec-to-wif -v 204
XH2Yndjv6Ks3XEHGaSMDhUMTAMZTTWv5nEN958Y7VMyQXBCJVQmM <- Dash WIF Key

While the following is correct for default BIP 44 results for DOGE using a default bx-doge.cfg wallet parameters.

% echo 'very complex gibberish' | bx base16-encode | bx sha256 | bx hd-new | bx hd-private -d -i 44 | bx hd-private -d -i 3 | bx hd-private -d -i 0 | bx hd-private -i 0 | bx hd-private -i 0 | bx hd-to-ec | bx ec-to-wif
QVi4p8ZFQnA2MWCURCEpdDR67CNtyxgWVEs8ki1LR9DhTwCxFu9k <- DOGE WIF Key
% echo 'very complex gibberish' | bx base16-encode | bx sha256 | bx hd-new | bx hd-private -d -i 44 | bx hd-private -d -i 3 | bx hd-private -d -i 0 | bx hd-private -i 0 | bx hd-private -i 0 | bx hd-to-ec | bx ec-to-public | bx ec-to-address
DSRMRapZ7GNwPTxEN7k3YoCnDJBzvhS2mg <- DOGE Address
evoskuil commented 8 years ago

So do you get ERROR_VERSION if you don't echo the parameter?