lian / bitcoin-ruby

bitcoin utils and protocol in ruby.
Other
922 stars 322 forks source link

Can't generate valid private key/address pairs #218

Closed naps62 closed 6 years ago

naps62 commented 7 years ago

I just used this gem to generate addresses, but they turned out to be incorrect. After sending money to the given address, I was unable to retrieve it using the private key

I'm not sure if this is related to OpenSSL issues (I've seen other issues in the history related to that). I've tried this using the latest gem version, using both ruby 2.3 and 2.4, and none would work

I don't know enough about this subject to understand what might be wrong, but I believe that if there's something potentially wrong with the addresses generated here, this should be displayed in huge letters in the Readme, as it could lead to loss of actual money

lian commented 7 years ago

can you show how your generated the key and the following transaction. bitcoin-ruby is/was used to generate countless addresses so far. never had an issue. might be something wrong with how you generated or loaded the address again. about encoding, or compressed vs non-compressed addresses. or testnet vs mainnet.

naps62 commented 7 years ago

Sure thing, I made this function:

def gen_btc_address
  priv, pub = Bitcoin.generate_key

  {
    private: priv,
    public: pub,
    address: Bitcoin.pubkey_to_address(pub),
  }
end

I then tested one of the generated keys, by sending money to the address, which worked. But the given private key doesn't seem to match. I tried to use it in two different ways:

  1. Using an external API, which I have used, and still use with success, with addresses generated through other tools
  2. Importing the private key into a wallet software. Previously, this wallet would get the private key and compute the exact same public address that I had also generated. Using the address I generated with the above function, the wallet would deduce a different address, and wouldn't be able to access the funds

Once I saw this wasn't working, I found issue #208

Since I have had several problems in the past with Ruby + OpenSSL, I didn't want to take any chances here. pretty much every new openssl update breaks something on my Linux computer

I have now reverted to using vanitygen (https://github.com/samr7/vanitygen) instead

Did I do something wrong in the code above?

lian commented 7 years ago

priv, pub = Bitcoin.generate_key this generates a uncompressed public key, and thus address.

when you import this key into other wallet software it will probably think the key is for an compressed address.

a better way to generate/store addresses with bitcoin-ruby is. key = Bitcoin::Key.generate; stored_key = key.to_base58; key = Bitcoin::Key.from_base58

to recover your coins sent to the address you could try priv, pub = Bitcoin.generate_key; Bitcoin::Key.new(priv, nil, {compressed: false}).to_base58 and use this WIF format to import into other wallets instead. or use this key object in bitcoin-ruby to generate your redeeming tx with it.

gsdean commented 6 years ago

Looks like https://github.com/lian/bitcoin-ruby/issues/208 caused me to ended up with an uncompressed private key as well.

Any idea how to convert them to the compressed format?

lian commented 6 years ago

Bitcoin::Key.new(priv, nil, {compressed: true}) will return a key object with forced compressed pubkey/address.