Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various changes #1

Merged
merged 2 commits into from
Apr 19, 2020
Merged

Various changes #1

merged 2 commits into from
Apr 19, 2020

Conversation

doc-hex
Copy link
Contributor

@doc-hex doc-hex commented Apr 17, 2020

  • removed use of base58
  • restructured to suit my tastes (sorry)
  • add pytest / rework for pytest test cases
  • add xprv=>xprv case, that does not use HMAC step
  • add "hex" application as discussed

Now all in sync with latest on Coldcard PR#39

Hopefully a source of ideas & test vectors. I'll understand if this never gets merged.

Copy link
Owner

@ethankosakovsky ethankosakovsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind refactor, but I want to keep the intention of underlying raw functions without restricting them to specific use. A wrapper API is fine.

Thank you for taking the time to look over things!

return self.__get_k_from_node(node)

def __hmac_sha512(self, message_k):
return hmac.new(message_k, msg=b'bip-entropy-from-k', digestmod=hashlib.sha512).digest()
Copy link
Owner

@ethankosakovsky ethankosakovsky Apr 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong. The format of the python function is hmac.new(key, msg, digest), where key is a shared key or salt, and message is the payload you are hashing like a password etc. See the python reference https://docs.python.org/3/library/hmac.html#hmac.new

And examples in the wild in pycoin, python-mnemonic, and Bitcoin Core here and here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, okay.

return self.__hmac_sha512(self.__get_k_from_wif(wif))
def bip32_xprv_to_hex(self, index, width, xprv_string):
# export entropy as hex
path = f"83696968p/128169p/{width}p/{index}p"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't agree we should fix the paths here. While you can use those paths, you may not want to, in the same was people may chose not to use BIP44 derivation paths for their wallets. This is the wrong place to enforce the path, as the library is simply performing the transformation of entropy to end result. If you want to standardize it it would need to be in a wrapper above (but I think this is not desirable).

return BTC.keys.private(secret_exponent=int(entropy[:32].hex(), 16)).wif()
def bip32_xprv_to_xprv(self, index, xprv_string):
# app_no = 32 => XPRV to XPRV
path = f"83696968p/32p/{index}p"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Path should not be enforced here. see previous comment

bits = (words - 1) * 11 // 8 + 1
m = bip39(language)
return m.to_mnemonic(entropy[:bits])
# if API to pycoin hadn't been shitcoined:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wins the internet today.


def test_xprv_application():
e = bipentropy.BIPEntropy()
result = e.bip32_xprv_to_xprv(0, XPRV)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would definitely prefer the library does not use fixed paths. The underlying is reason for these particular functions is the transformation, and if I want to use an arbitrary path, I shouldn't have to rewrite the underlying function to do so. You can always write a wrapper API layer around this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, the purpose of demo code like this is to fix the paths. It's the embodiment of the BIP.


def test_mnemonic():
e = bipentropy.BIPEntropy()
mnemonic = 'install scatter logic circle pencil average fall shoe quantum disease suspect usage'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to revert the test cases because hmac param order is flipped incorrectly elsewhere in this PR here

assert e.entropy_to_bip39(entropy, 24) == words24

def test_wif_from_entropy():
# PDG: not sure about this?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't tested this specific case against Coldcard, that's all.

@ethankosakovsky
Copy link
Owner

ethankosakovsky commented Apr 19, 2020

I have incorporated my suggestions here 5dd0317 as I was unable to push to your pull request branch.

The changeset introduces a clear API, without having to dig into internals, as well as maintaining the base layer.

I didnt correct the param order in the hmac function in this commit to make it readable.

@ethankosakovsky
Copy link
Owner

ethankosakovsky commented Apr 19, 2020

Fixed parameter order and reverted tests in 4a7735c

I can go ahead and merge your PR and pull the other two commits.

@ethankosakovsky
Copy link
Owner

add xprv=>xprv case, that does not use HMAC step

We previously discussed this case - if it's necessary. I'm agnostic. But maybe in the context of this BIP, since the purpose of the BIP is to use a root key to derive entropy to then create something else, maybe your original proposal of using the 512bit HMAC entropy to create the XPRV is the more consistent and logical approach.

@ethankosakovsky ethankosakovsky merged commit d179b9a into ethankosakovsky:master Apr 19, 2020
@doc-hex
Copy link
Contributor Author

doc-hex commented Apr 20, 2020

Closing since key stuff already in master now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants