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 pub option for public keys #87

Merged
merged 6 commits into from
Nov 4, 2020

Conversation

paweljakubas
Copy link
Collaborator

For credentials from public key we need to have public keys without chain code.
cardano-foundation/cardano-wallet#2253

This PR adds a way to get public key (without chain code) if --pub is sneaked. Otherwise extended public key is returned.

@paweljakubas paweljakubas requested review from rvl and KtorZ October 30, 2020 17:47
@paweljakubas paweljakubas self-assigned this Oct 30, 2020
README.md Outdated
$ cat recovery-phrase.prv | cardano-address key from-recovery-phrase Shelley \
| cardano-address key child 1852H/1815H/0H/2/0 \
| cardano-address key public --pub
xpub16apaenn9ut6s40lcw3l8v68xawlrlq20z2966uzcx8jmv2q9uy7qh83kg9
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I would like here pub rather than xpub - but do not know at this moment how to elegantly do it 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

or more precisely I would like addr_vk if we are dealing with payment/delegating address and stake_vk if we are dealing with stake address (reward account). If not possible we will need a way to have --bech32WithHrp prefix

Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps use pub/xpub only in cardano-address, and show how to use the bech32 util to change HRP.

Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think of flipping this around and, have this being the default behavior, but require an explicit --with-chain-code flag to return the public key with chain code.

We should avoid returning the chain code unless the user knows what he/she is doing with it. Changing the default, we would also need to change a bit the error message when failing to parse bytes into a public key + chain code (so that it hints users in the right direction, that a 32-byte chain code is missing).

Copy link
Contributor

Choose a reason for hiding this comment

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

I like that idea of requiring the user to specify what kind of key they need. Such as:

  • (--with-chain-code | --without-chain-code ).
  • cardano-address key xpub / cardano-address key public
  • ( --extended-key | --key-only )

$ cardano-address recovery-phrase generate --size 15 > recovery-phrase.prv
$ cat recovery-phrase.prv | cardano-address key from-recovery-phrase Shelley
xprv1fzu4e8cecxshgzzxzh7557sd8tffqreeq2je7fgsm7f02mq849vdupw7qwgxc3qawyqev0l8ew0f4fkp8hvr8mskz4hz6e6ejzjlevcskcl6lqpr07u7552fsfgteztuclse7luh4cp493zdhkrjdss0250cdw8n
$ cat recovery-phrase.txt | cardano-address key from-recovery-phrase Shelley
Copy link
Contributor

Choose a reason for hiding this comment

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

Why this change ? I think it was clearer before with both instructions visible in the same excerpt.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@KtorZ fair point. I was thinking here in the following way. We use prv, pub, xpub, pub for keys...and for me mnemonic sequence is just text.... If that does not persuade you I will revert this in coming commit. @rvl do you have an opinion on that?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes and yes

  1. It would be better if the recovery-phrase generate command were added back to provide context.
  2. .txt is a good extension to describe the content of the file.

README.md Outdated
$ cat recovery-phrase.prv | cardano-address key from-recovery-phrase Shelley \
| cardano-address key child 1852H/1815H/0H/2/0 \
| cardano-address key public --pub
xpub16apaenn9ut6s40lcw3l8v68xawlrlq20z2966uzcx8jmv2q9uy7qh83kg9
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think of flipping this around and, have this being the default behavior, but require an explicit --with-chain-code flag to return the public key with chain code.

We should avoid returning the chain code unless the user knows what he/she is doing with it. Changing the default, we would also need to change a bit the error message when failing to parse bytes into a public key + chain code (so that it hints users in the right direction, that a 32-byte chain code is missing).

Copy link
Contributor

@rvl rvl left a comment

Choose a reason for hiding this comment

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

Looks good so far - do you think pub or xpub should be the "default"? Or should there be separate subcommands for pub and xpub?

( Parser, flag, long )


data PublicType = WithChainCode | WithoutChainCode
Copy link
Contributor

Choose a reason for hiding this comment

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

Does a single type and parse function need its own module?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I somewhat just complied with the pattern we have for other things. So although I am slightly in favour with your opinion on this this would be outlier and may surprise anyone NEW looking at the code....

Copy link
Contributor

Choose a reason for hiding this comment

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

OK

Comment on lines 46 to 47
, "Extended public key is default. In order to get public key"
, ", ei., without chain code use '--pub'."
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you know which use case is more common?
I am thinking that public key only might be a better default. Not sure though.
Another alternative could be to have completely separate subcommands -- e.g. public and xpub, or something like that. Then you could tell users to run xpub if they need to derive child keys.

Copy link
Contributor

Choose a reason for hiding this comment

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

More than which one is most common, I'd go for the "safest" from a privacy standpoint here. Since sharing your chain code allows other to create new (public) child keys from your public key, you may inadvertently gives access to an entire part of your sub-tree by sharing your extended public key.

Plus, cardano-cli and other cardano-tools mostly deals with non-extended ed25519 keys only.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

introduced --with-chain-code and --without-chain-code here c2e3c78

README.md Outdated
$ cat recovery-phrase.prv | cardano-address key from-recovery-phrase Shelley \
| cardano-address key child 1852H/1815H/0H/2/0 \
| cardano-address key public --pub
xpub16apaenn9ut6s40lcw3l8v68xawlrlq20z2966uzcx8jmv2q9uy7qh83kg9
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps use pub/xpub only in cardano-address, and show how to use the bech32 util to change HRP.

])
where
parser = Public
<$> encodingOpt [humanReadablePart|xpub|]
<*> publicOpt
Copy link
Contributor

Choose a reason for hiding this comment

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

Sometthing like this will let you change the HRP...

Suggested change
<*> publicOpt
parser = do
pub <- publicOpt
Public <$> encodingOpt (if pub == WithChainCode then [humanReadablePart|xpub|] else [humanReadablePart|pub|])
<*> pure pub

(clean it up though)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

thanks for suggestion! in the end solved this like that 4ad1a31

Copy link
Contributor

Choose a reason for hiding this comment

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

if you use monadic rather than applicative then the ??? placeholder is not needed.

@paweljakubas paweljakubas requested review from rvl and KtorZ November 4, 2020 11:50
Copy link
Contributor

@rvl rvl left a comment

Choose a reason for hiding this comment

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

A couple style issues, but otherwise good.

$ cardano-address recovery-phrase generate --size 15 > recovery-phrase.prv
$ cat recovery-phrase.prv | cardano-address key from-recovery-phrase Shelley
xprv1fzu4e8cecxshgzzxzh7557sd8tffqreeq2je7fgsm7f02mq849vdupw7qwgxc3qawyqev0l8ew0f4fkp8hvr8mskz4hz6e6ejzjlevcskcl6lqpr07u7552fsfgteztuclse7luh4cp493zdhkrjdss0250cdw8n
$ cat recovery-phrase.txt | cardano-address key from-recovery-phrase Shelley
Copy link
Contributor

Choose a reason for hiding this comment

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

Yes and yes

  1. It would be better if the recovery-phrase generate command were added back to provide context.
  2. .txt is a good extension to describe the content of the file.

])
where
parser = Public
<$> encodingOpt [humanReadablePart|xpub|]
<*> publicOpt
Copy link
Contributor

Choose a reason for hiding this comment

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

if you use monadic rather than applicative then the ??? placeholder is not needed.

xprv <- hGetXPrv stdin
let xpub = toXPub xprv
hPutBytes stdout (xpubToBytes xpub) encoding
(bytes, encoding') <- case chainCode of
Copy link
Contributor

Choose a reason for hiding this comment

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

This line doesn't need to be an IO action. It could be a plain let or where clause.

I would be inclined to set up hrp in the CLI Parser, and use a function.

let toBytes = case chainCode of
  WithChainCode -> xpubToBytes
  WithoutChainCode -> xpubPublicKey
hPutBytes stdout (toBytes xpub) encoding

( Parser, flag, long )


data PublicType = WithChainCode | WithoutChainCode
Copy link
Contributor

Choose a reason for hiding this comment

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

OK

Comment on lines 36 to +37
{ encoding :: Encoding
, chainCode :: PublicType
Copy link
Contributor

Choose a reason for hiding this comment

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

Parser will look better if the order of constructor fields are swapped.

@paweljakubas paweljakubas merged commit eaa946c into master Nov 4, 2020
@paweljakubas paweljakubas deleted the paweljakubas/add-pub-option-for-public-keys branch November 4, 2020 17:39
iohk-bors bot added a commit to cardano-foundation/cardano-wallet that referenced this pull request Nov 5, 2020
2253: ADP-494 - Add POST endpoint for script address r=paweljakubas a=paweljakubas

# Issue Number

https://jira.iohk.io/browse/ADP-494

# Overview

- [x] I have updated swagger with script as string
- [x]  I have added script as json 
- [x] I have extended Api 
- [x] I have extended core unit tests
- [x] I have added cardano-addresses based impl
- [x] I have added illustrative integration tests 

# Comments

- Uses IntersectMBO/cardano-addresses#87


Co-authored-by: Pawel Jakubas <[email protected]>
Co-authored-by: Rodney Lorrimar <[email protected]>
Co-authored-by: IOHK <[email protected]>
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.

3 participants