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
15 changes: 15 additions & 0 deletions sdk-wallet/cookbook.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

## Cookbook (examples of usage)

Note: error handling is out of scope for this cookbook (omitted for brevity).

Generate a mnemonic and derive the first secret key:
iulianpascalau marked this conversation as resolved.
Show resolved Hide resolved

```
Expand Down Expand Up @@ -46,6 +48,19 @@ for i in [0, 1, 2]:
print("Address", i, address.bech32())
```

Load a PEM keystore, then iterate over the first 3 accounts:

```
provider = new UserWalletProvider()
keystore = PEMKeystore.import_from_file(provider, "wallet.pem")

for i in [0, 1, 2]:
sk = keystore.get_secret_key(i)
pk = provider.compute_public_key_from_secret_key(sk)
address = new Address(pk, "erd")
print("Address", i, address.bech32())
```

Change the password of a JSON keystore:

```
Expand Down
15 changes: 8 additions & 7 deletions sdk-wallet/keystores/encrypted_keystore.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ Note: current design is suboptimal. `EncryptedKeystore` handles both legacy keys

```
class EncryptedKeystore:
// The constructor is not strictly captured by the specs; it's up to the implementing library to define it. Suggestion below.
// If kind == 'secretKey', `secret_key` must be provided, while `mnemonic` must be nil.
// 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

// Instance fields to be held (suggestion):
// - keys_computer: IKeysComputer
// - kind: "secretKey|mnemonic"
// - secretKey: ISecretKey (will be nil if kind == 'mnemonic')
// - mnemonic: Mnemonic (will be nil if kind == 'secretKey')

// Named constructor
// This should have a trivial implementation (e.g. a wrapper around the private constructor).
// This should have a trivial implementation.
static new_from_secret_key(keys_computer: IKeysComputer, secret_key: ISecretKey): EncryptedKeystore

// Named constructor
// This should have a trivial implementation (e.g. a wrapper around the private constructor).
// This should have a trivial implementation.
static new_from_mnemonic(keys_computer: IKeysComputer, mnemonic: Mnemonic): EncryptedKeystore

// Importing "constructor"
Expand Down
2 changes: 1 addition & 1 deletion sdk-wallet/user_wallet_provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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).
Additionally, allows one to generate and validate mnemonics, and to derive secret keys from mnemonics (i.e. BIP39).

```
class UserWalletProvider implements ISigner, IVerifier, IKeysGenerator, IMnemonicComputer:
Expand Down