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

Base CLI parsing #17

Merged
merged 29 commits into from
Mar 15, 2024
Merged

Base CLI parsing #17

merged 29 commits into from
Mar 15, 2024

Conversation

koxu1996
Copy link
Contributor

@koxu1996 koxu1996 commented Feb 5, 2024

CLI interface

Implemented interface with three key transaction commands (Deposit, Transfer, Withdraw) and integrated Casper's cryptography functionalities.

Fixes #14.

Deposit:

$ ./kairos-cli deposit \
  --amount 123 \
  --private-key ~/wallet/secret_key.pem

Transfer:

$ ./kairos-cli transfer \
  --recipient 01a26419... \
  --amount 123 \
  --private-key ~/wallet/secret_key.pem

Withdraw:

$ ./kairos-cli withdraw \
  --amount 123 \
  --private-key ~/wallet/secret_key.pem

Outputs

  • amount: u64 - no need to handle u512 at this point
  • signer: CasperSigner (created from CasperPrivateKey)
  • recipient: CasperPublicKey

Notably, signer allows generating signature of arbitrary byte data. It will be used to sign transactions sent to Kairos server.

Error handling

  1. Invalid transaction amount, for example 123a:
error: invalid value '123a' for '--amount <NUM_MOTES>': invalid digit found in string
  1. Invalid recipient foobar, when making transfer:
error: invalid value 'foobar' for '--recipient <PUBLIC_KEY>': failed to parse hex string: Invalid character 'o' at position 1
  1. Invalid private key path, when content cannot be read:
cryptography error: failed to parse private key: secret key load failed: could not read '/non/exisiting/file/path': No such file or directory (os error 2)
  1. Invalid private key content:
cryptography error: failed to parse private key: pem error: malformedframing

Tests

Both 3 happy paths and error cases are tested in cli_tests.rs.

@koxu1996 koxu1996 marked this pull request as ready for review February 6, 2024 13:53
kairos-cli/bin/commands/mod.rs Outdated Show resolved Hide resolved
kairos-cli/bin/main.rs Outdated Show resolved Hide resolved
kairos-cli/tests/cli_tests.rs Outdated Show resolved Hide resolved
@koxu1996 koxu1996 requested a review from Avi-D-coder February 6, 2024 21:03
Copy link
Contributor

@Avi-D-coder Avi-D-coder left a comment

Choose a reason for hiding this comment

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

lgtm

@koxu1996 koxu1996 force-pushed the feature/base-cli-parsing branch 3 times, most recently from 89955b1 to ade64d1 Compare February 6, 2024 22:40
@koxu1996
Copy link
Contributor Author

koxu1996 commented Feb 6, 2024

Solved issue with failing Nix tests: ade64d1 💪. Now it works:

image

Copy link
Contributor

@marijanp marijanp left a comment

Choose a reason for hiding this comment

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

I would like to see code that leverages the type system and is more more declarative. That leaves us with code that is easier to follow and extend. Moreover, the compiler supports us to catch missing/ unhandled commands.

kairos-cli/bin/commands/deposit.rs Outdated Show resolved Hide resolved
kairos-cli/bin/main.rs Outdated Show resolved Hide resolved
kairos-cli/bin/crypto/error.rs Outdated Show resolved Hide resolved
kairos-cli/bin/crypto/private_key.rs Outdated Show resolved Hide resolved
kairos-cli/bin/crypto/public_key.rs Outdated Show resolved Hide resolved
kairos-cli/bin/error.rs Outdated Show resolved Hide resolved
kairos-cli/bin/main.rs Outdated Show resolved Hide resolved
kairos-cli/bin/main.rs Outdated Show resolved Hide resolved
kairos-cli/tests/cli_tests.rs Show resolved Hide resolved
kairos-cli/Cargo.toml Outdated Show resolved Hide resolved
@koxu1996
Copy link
Contributor Author

koxu1996 commented Feb 12, 2024

PR and description updated - ready for re-review 👀.

kairos-cli/bin/crypto/private_key.rs Outdated Show resolved Hide resolved
kairos-cli/bin/crypto/signer.rs Outdated Show resolved Hide resolved
kairos-cli/bin/crypto/signer.rs Outdated Show resolved Hide resolved
kairos-cli/bin/crypto/signer.rs Outdated Show resolved Hide resolved
kairos-cli/bin/crypto/error.rs Outdated Show resolved Hide resolved
kairos-cli/bin/crypto/private_key.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@marijanp marijanp left a comment

Choose a reason for hiding this comment

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

Great, I think we are almost there. One last remark. I've put the main.rs into build to create a separation between library and binary. If you could put all the modules except main.rs into a src directory that would be perfect.

kairos-cli/bin/commands/transfer.rs Outdated Show resolved Hide resolved
kairos-cli/bin/commands/transfer.rs Outdated Show resolved Hide resolved
kairos-cli/bin/commands/mod.rs Outdated Show resolved Hide resolved
kairos-cli/bin/crypto/error.rs Outdated Show resolved Hide resolved
kairos-cli/bin/commands/transfer.rs Outdated Show resolved Hide resolved
kairos-cli/bin/crypto/private_key.rs Outdated Show resolved Hide resolved
kairos-cli/bin/crypto/public_key.rs Outdated Show resolved Hide resolved
kairos-cli/bin/crypto/signer.rs Outdated Show resolved Hide resolved
kairos-cli/bin/common/args.rs Outdated Show resolved Hide resolved
kairos-cli/bin/error.rs Outdated Show resolved Hide resolved
@koxu1996
Copy link
Contributor Author

@marijanp

Great, I think we are almost there. One last remark. I've put the main.rs into build to create a separation between library and binary. If you could put all the modules except main.rs into a src directory that would be perfect.

Okay! Files moved in 152858a.

kairos-cli/src/crypto/private_key.rs Outdated Show resolved Hide resolved
kairos-cli/src/crypto/signer.rs Outdated Show resolved Hide resolved
@koxu1996 koxu1996 self-assigned this Feb 15, 2024
@koxu1996 koxu1996 requested a review from Avi-D-coder February 16, 2024 13:01
Avi-D-coder
Avi-D-coder previously approved these changes Feb 16, 2024
@Avi-D-coder
Copy link
Contributor

LGTM

marijanp
marijanp previously approved these changes Feb 17, 2024
Copy link
Contributor

@marijanp marijanp left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@quinn-dougherty quinn-dougherty left a comment

Choose a reason for hiding this comment

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

LGTM

kairos-cli/src/crypto/signer.rs Show resolved Hide resolved
@@ -0,0 +1,26 @@
use crate::common::args::{AmountArg, PrivateKeyPathArg};
use crate::crypto::error::CryptoError;
use crate::crypto::signer::CasperSigner;

Choose a reason for hiding this comment

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

Why did we make a custom signer?

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 want to isolate Casper code, to make it easily replaceable in the future. I imagine we might have Signer trait and blockchain specific implementations for it.

pub struct CasperPublicKey(pub casper_types::PublicKey);

impl CasperPublicKey {
pub fn from_bytes(bytes: &[u8]) -> Result<Self, CryptoError> {

Choose a reason for hiding this comment

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

Don’t make this part of the inherent implementation, just impl the FromBytes trait if you want to do 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.

FromBytes is a trait existing in casper_types, and my intention was to make Kairos as much independent as possible.

It can be constructed directly via public field.
Better separation with executable at `./bin/main.rs`.
More precise errors for caller, with the cost of manual
casting into `CryptoError` in handlers.
@koxu1996 koxu1996 force-pushed the feature/base-cli-parsing branch from 5556046 to 455872d Compare February 26, 2024 12:45
@koxu1996
Copy link
Contributor Author

Changes rebased with the latest main.

@koxu1996 koxu1996 requested a review from xcthulhu February 26, 2024 12:46
xcthulhu
xcthulhu previously approved these changes Feb 26, 2024
@koxu1996
Copy link
Contributor Author

Per @xcthulhu request, I added additional tests for parsing public keys from CSPR.live. All four approvals were automatically dismissed though 😨.

@asladeofgreen PTAL 🙏

marijanp
marijanp previously approved these changes Mar 5, 2024
kairos-cli/src/crypto/public_key.rs Outdated Show resolved Hide resolved
kairos-cli/src/crypto/public_key.rs Outdated Show resolved Hide resolved
Now it is possible to differentiate serialization and
deserialization error.
Copy link
Contributor

@Avi-D-coder Avi-D-coder left a comment

Choose a reason for hiding this comment

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

lgtm

Copy link
Member

@asladeofgreen asladeofgreen left a comment

Choose a reason for hiding this comment

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

I have caveats, notably with reinventing wheels in respect of the cryptography, however I do understand the wish to reduce dependency upon casper libs as much as possible. Wouldn't a dedicated low-level kairos-crypto crate be of use here ?

So am approving for now but imho would like to revisit some of this code as the solution evolves.

@koxu1996
Copy link
Contributor Author

I have caveats, notably with reinventing wheels in respect of the cryptography, however I do understand the wish to reduce dependency upon casper libs as much as possible. Wouldn't a dedicated low-level kairos-crypto crate be of use here ?

@asladeofgreen Yes, cryptography will be moved out of CLI package into kairos-crypto in #35. It provides pub trait CryptoSigner, with Casper implementation under feature flag.

@koxu1996 koxu1996 merged commit 948f15a into main Mar 15, 2024
6 checks passed
@koxu1996 koxu1996 deleted the feature/base-cli-parsing branch March 18, 2024 09:38
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.

Implement CLI parsing
6 participants