GemHQ / money-tree

A Ruby implementation of Bitcoin HD Wallets (Hierarchical Deterministic) BIP32
MIT License
185 stars 68 forks source link

Ethereum Support #25

Closed mhluska closed 6 years ago

mhluska commented 6 years ago

Can this be used for generating Ethereum addresses from a seed? Looks like some gems like this one are attempting it but I can't get their generated addresses to work properly. The addresses the gem I linked generates don't match the ones that MyEtherWallet or this tool would generate. I'm not experienced enough to know why though.

nofxx commented 6 years ago

It works just fine. You must use Key#private_hex. (Myetherwallet and eth gem). Now curious about mnemonic words ones... will investigate.

mhluska commented 6 years ago

@nofxx Can you elaborate? I'm trying to use an extended public key to create Ethereum addresses. Here's how I'm trying it:

require 'money-tree'
require 'eth'

key = 'xpub6Dv28vySKcyJSvYivLP8PEedEZf6Cut2D7cUUaPgushD7MNWvpsy34oZaxrAbdbtuGN61hMLbv17tc4hFPfiYN4oMLJxt9SyvfoxtJadbiR'
master = MoneyTree::Node.from_bip32(key)
node = master.node_for_path("m/44'/60'/0'/0/0") # This step throws MoneyTree::Node::PrivatePublicMismatch
Eth::Utils.public_key_to_address(node.public_key.to_hex)

I suspect the from_bip32 method isn't set up to handle Ethereum addresses but I'm not familiar enough with this to know for sure.

This would be useful in a related gem: https://github.com/Sailias/cryptocoin_payable/issues/3

What does work, however, is this:

require 'eth'
require 'bitcoin'
require 'money-tree'

master = MoneyTree::Master.new(seed_hex: Bitcoin::Trezor::Mnemonic.to_seed(mnemonic))
node = master.node_for_path("m/44'/60'/0'/0/0")
key = Eth::Key.new(priv: node.private_key.to_hex)
key.address

This is not ideal because I would prefer to generate the addresses from the extended public key rather than from the mnemonic.

nofxx commented 6 years ago

Sorry @mhluska , apprentice on the wheel here, but maybe related to #22 ?

mhluska commented 6 years ago

Seems to be related. Looks like not much is happening to help fix the issue? I can give it a shot this week.

mhluska commented 6 years ago

So the MoneyTree::Node::PrivatePublicMismatch happens because hardened derivations can't happen without a master private key. If I replace the xpub with an xprv, it runs fine but the resulting Ethereum address is still incorrect.

jackkinsella commented 6 years ago

I ran into the same problem today. Everything works just dandy with the following code

   # Assumes that the 'eth' gem is available
   Eth::Utils.public_key_to_address(node.public_key.uncompressed.to_hex)
thedoctor commented 6 years ago

@mhluska Correct regarding the hardened derivation issue. @jackkinsella is (ostensibly) correct with the use of uncompressed. I don't know the Eth::Utils class myself, but it clearly expects uncompressed public keys.

It's probably not a likely that we'll add support for ethereum addresses directly to this repo as that's beyond the scope of money-tree

krtschmr commented 5 years ago

@thedoctor "money-tree" the name itself should support everything. not just bitcoin 👍