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

sdk-wallet: some refactoring, additional explanations #32

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

andreibancioiu
Copy link
Contributor

@andreibancioiu andreibancioiu commented Dec 11, 2023

  • Added extra explanations, plus a small cookbook (examples for usage).
  • Split IWalletProvider into: ISigner, IVerifier, IKeysComputer, IMnemonicComputer.
  • Merged MnemonicComputer back into UserWalletProvider.

@andreibancioiu andreibancioiu self-assigned this Dec 11, 2023
Base automatically changed from updates-12-08 to main December 11, 2023 12:43
@andreibancioiu andreibancioiu changed the title sdk-wallet: additional explanations sdk-wallet: some refactoring, additional explanations Dec 12, 2023
Comment on lines 72 to 75
if keystore.get_kind() == "secretKey":
first_secret_key = keystore.get_secret_key()
else:
mnemonic = keystore.get_mnemonic()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wallet-like applications that support JSON keystores (e.g. web wallet, mxpy) have to handle the keystore with respect to it's kind.

@@ -5,12 +5,20 @@ For languages that support **structural typing** (e.g. Go, Python, TypeScript),
For languages that only support **nominal typing** (e.g. C#), these interfaces can be _exported_.

```
interface IWalletProvider:
generate_keypair(): (ISecretKey, IPublicKey)
interface ISigner:
Copy link
Contributor Author

@andreibancioiu andreibancioiu Dec 12, 2023

Choose a reason for hiding this comment

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

IWalletProvider was segregated.

See: #31 (comment).

Comment on lines +35 to +46

// Should not throw.
generate_mnemonic(): Mnemonic

// Should not throw.
validate_mnemonic(mnemonic: Mnemonic): bool

// Can throw:
// - ErrInvalidMnemonic
// Below, "passphrase" is the optional bip39 passphrase used to derive a secret key from a mnemonic.
// Reference: https://en.bitcoin.it/wiki/Seed_phrase#Two-factor_seed_phrases
derive_secret_key_from_mnemonic(mnemonic: Mnemonic, address_index: int, passphrase: string = ""): ISecretKey
Copy link
Contributor Author

@andreibancioiu andreibancioiu Dec 12, 2023

Choose a reason for hiding this comment

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

MnemonicComputer has been merged into this.

@andreibancioiu andreibancioiu marked this pull request as ready for review December 12, 2023 19:09
@@ -1,33 +1,48 @@
## EncryptedKeystore

`EncryptedKeystore` handles encrypted JSON files.

Note: current design is suboptimal. `EncryptedKeystore` handles both legacy keystore files (that hold encrypted secret keys) and new keystore files (that hold encrypted mnemonics). `get_kind()` can be used as a differentiator. A future design might involve two separate classes.
Copy link
Contributor Author

@andreibancioiu andreibancioiu Dec 12, 2023

Choose a reason for hiding this comment

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

Question for review: all right for the moment? 🙏

Also, see the example(s) in cookbook.md.

@@ -1,7 +1,11 @@
## UserWalletProvider

Provides functionality for generating secret keys, derivating public keys, signing & verifying data.

Additionally, allows one to generate and validate mnemonics, and to deriving secret keys from mnemonics (i.e. BIP39).

Choose a reason for hiding this comment

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

deriving => derive

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@iulianpascalau iulianpascalau self-requested a review December 14, 2023 11:51
sdk-wallet/cookbook.md Show resolved Hide resolved
sdk-wallet/cookbook.md Show resolved Hide resolved
```
class EncryptedKeystore:
// The constructor is not captured by the specs; it's up to the implementing library to define it.
// The constructor is not strictly captured by the specs; it's up to the implementing library to define it. Suggestion below.
Copy link

@iulianpascalau iulianpascalau Dec 14, 2023

Choose a reason for hiding this comment

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

Please remove this private constructor, it's out of the scope of the provided specs. We should have only those 2 named constructors as example
+ providing a type on which the constructor will do a switch is not a very good example (clean code wise)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed phrasing, removed private constructor. No switch should be made in the implementation.

// If kind == 'mnemonic', `secret_key` must be nil, while `mnemonic` must be provided.
// In the implementation, all the parameters would be held as instance state (private fields).
private constructor(keys_computer: IKeysComputer, kind: string, secret_key?: ISecretKey, mnemonic?: Mnemonic)
// The constructor is not captured by the specs; it's up to the implementing library to define it.

Choose a reason for hiding this comment

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

As stated in the last comment this can be implemented in a cleaner way by the following 2 named constructors: one for the mnemonic and one for the secret key. This is clearly not required

static import_from_object(wallet_provider: IWalletProvider, object: KeyfileObject, password: string): EncryptedKeystore
// This should decrypt the encrypted data, then call the (private) constructor.
// "password" should not be retained.
static import_from_object(keys_computer: IKeysComputer, object: KeyfileObject, password: string): EncryptedKeystore

Choose a reason for hiding this comment

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

object: KeyfileObject should be something like keystore_content: EncryptedKeystoreContent

```

```
interface IKeysComputer:
Copy link

@axenteoctavian axenteoctavian Dec 27, 2023

Choose a reason for hiding this comment

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

In User/Validator wallet provider this is called IKeysGenerator. Which name is correct?

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.

4 participants