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

Add ECPair.toUncheckedHex() and ECPair.fromUncheckedHex(hex, network, compressed) #1077

Closed
wants to merge 4 commits into from

Conversation

woodser
Copy link

@woodser woodser commented May 18, 2018

This PR adds the ability to get the private key as unchecked hex (without network or compression information) and initialize a new ECPair from unchecked hex, network, and compression.

This is helpful to minimize the encoded data for encryption and splitting with Shamir's Secret Sharing which is why other libraries usually offer this in their API.

I'm using this API in the CryptoStorage tool here and here.

@dcousens
Copy link
Contributor

NACK, already in #1070

@dcousens dcousens closed this May 22, 2018
@woodser
Copy link
Author

woodser commented May 31, 2018

The motivation of this PR is to encapsulate initialization from hex. It looks like #1070 does not permit a caller to initialize an ECPair from a hex string without using BitcoinJS's internal Buffer class. Please correct me if I'm wrong. Otherwise I think initializing from unchecked hex should be added and I can make the PR off of #1070 if you agree?

@dcousens
Copy link
Contributor

dcousens commented May 31, 2018

@woodser Buffer isn't internal to bitcoinjs, it is required for many things to interact with this library.

@woodser
Copy link
Author

woodser commented Jun 9, 2018

I've been reading through #1070. If I understand correctly, ECPair is going to accept a buffer as its first constructor argument, and the way to get the hexadecimal private key will be keypair.privateKey.toString('hex'), correct?

The hex will not be padded so it may contain fewer than 64 characters for some private keys, such as this LTC private key: T34MuTEaRxt3kj6dn2MMHbvSPQHrsBjKrdz6WtwrLe1KzCzYkMup.

Shouldn't it be padded to conform to 64 characters?

Also, is d.toBuffer(32).toString('hex') a valid way of creating a padded hex from a BigInteger?

Thanks.

@dcousens
Copy link
Contributor

dcousens commented Jun 9, 2018

@woodser

function fromPrivateKey (buffer, options) {
typeforce(types.Buffer256bit, buffer)
if (!ecc.isPrivate(buffer)) throw new TypeError('Private key not in range [1, n)')
typeforce(isOptions, options)
return new ECPair(buffer, null, options)
}

Note specifically

typeforce(types.Buffer256bit, buffer)

@dcousens
Copy link
Contributor

dcousens commented Jun 9, 2018

Also, is d.toBuffer(32).toString('hex') a valid way of creating a padded hex from a BigInteger?

That is what we did previously 👍

@woodser
Copy link
Author

woodser commented Jun 10, 2018

Ok, thanks!

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