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] sdk v2 account classes #10005

Closed
wants to merge 7 commits into from
Closed

[SDK] sdk v2 account classes #10005

wants to merge 7 commits into from

Conversation

0xmigo
Copy link
Contributor

@0xmigo 0xmigo commented Sep 11, 2023

Description

Previous stack (Crypto classs): #9889


Account class updated to the following

Instance Method

  • sign
  • verifySignature

Getter

  • publicKey
  • privateKey
  • accountAddress

Static Creator Methods

  • create
  • fromPrivateKey
  • fromPrivateKeyAndAddress
  • fromDerivationPath

Static Helpers

  • isValidPath
  • authKey

Test Plan

PASS tests/unit/hex.test.ts
PASS tests/unit/deserializer.test.ts
PASS tests/unit/serializer.test.ts
PASS tests/unit/account_address.test.ts
PASS tests/unit/aptos_config.test.ts
PASS tests/e2e/api/account.test.ts
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 0 | 0 | 0 | 0 |
----------|---------|----------|---------|---------|-------------------

Test Suites: 6 passed, 6 total
Tests: 106 passed, 106 total
Snapshots: 0 total
Time: 1.748 s, estimated 34 s

@0xmigo 0xmigo requested review from gregnazario, banool, 0xmaayan, xbtmatt and a team September 11, 2023 23:04
return authKey.data;
}

sign(data: Uint8Array): Hex {
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 was hesitating whether I want this data input to be Uint8Array or HexInput

* @returns Hex - public key of the account
*/
get publicKey(): Hex {
return new Hex({ data: this._signingKey.publicKey });
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 think using obj for argument for a single input param seems odd. Personally I would love to see single input param without obj

Copy link
Contributor

Choose a reason for hiding this comment

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

yes we all mentioned it at some point... decided to keep it as is and later, when we actually test the sdk_v2 and use it we can decide how it is from a dev user perspective

@0xmigo 0xmigo force-pushed the jin/sdk_v2_account_classes branch from c0bf162 to 109d06b Compare September 14, 2023 22:45
Comment on lines +85 to +86
static fromPrivateKey(privateKey: HexInput): Account {
const privatekeyHex = Hex.fromHexInput({ hexInput: privateKey });
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 see there are many places where we have to build the Hex from HexInput. I wonder if we can just accept the Hex type as argument?

Copy link
Contributor

Choose a reason for hiding this comment

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

Hex is not a type but a Class. The whole idea is to use HexInput and then transform it to the desired format, this enables the greatest flexibility for the developer.

Copy link
Contributor

Choose a reason for hiding this comment

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

Beyond that, please make this take in an object as the argument. I know it's weird but we agreed we'd try this lol, let's see how it goes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep gotcha!

Copy link
Contributor

@banool banool left a comment

Choose a reason for hiding this comment

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

Looking good! I'd love to see examples in all of the doc comments on the public constructor methods.

// SPDX-License-Identifier: Apache-2.0

import nacl from "tweetnacl";
import * as bip39 from "@scure/bip39";
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason we don't just import bip39 and do bip39.blah? I feel like it'd be more readable, I'd rather we avoid import * from x.

import { HexInput } from "../types";

/**
* Class for creating and managing account on Aptos network
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* Class for creating and managing account on Aptos network
* Class for creating and managing an account on Aptos network

import { HexInput } from "../types";

/**
* Class for creating and managing account on Aptos network
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you clarify that creating an instance of this account doesn't create an instance of the account on the network. Some more examples in the doc comment here would be nice, e.g. how to use this to create an account on the network, getting account address, etc.

*/
export class Account {
/**
* signing key of the account, which holds the public and private key
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* signing key of the account, which holds the public and private key
* Signing key of the account, which holds the public and private key

private readonly _signingKey: nacl.SignKeyPair;

/**
* Account address associated with the account
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe some more information and a link to the docs site explaining why this is necessary and can't just be derived from the private / public keys (because of rotation).

*
* @returns Hex - private key of the account
*/
get privateKey(): Hex {
Copy link
Contributor

Choose a reason for hiding this comment

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

Personally I think getters suck, I prefer just a good old function. Do we have guidelines on this Maayan?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I am thinking of using getter here b/c that private and public key came from _signingKey. Not sure if we want to make the signingKey as public. Any thoughts?

Comment on lines +85 to +86
static fromPrivateKey(privateKey: HexInput): Account {
const privatekeyHex = Hex.fromHexInput({ hexInput: privateKey });
Copy link
Contributor

Choose a reason for hiding this comment

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

Beyond that, please make this take in an object as the argument. I know it's weird but we agreed we'd try this lol, let's see how it goes.

Comment on lines +93 to +94
* Creates new account with provided private key and address
* This is intended to be used for account that has it's key rotated
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* Creates new account with provided private key and address
* This is intended to be used for account that has it's key rotated
* Creates a new account with the provided private key and address.
* This is intended to be used for an account that has had it's key rotated.

Comment on lines +107 to +108
* Creates new account with bip44 path and mnemonics,
* @param path. (e.g. m/44'/637'/0'/0'/0')
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* Creates new account with bip44 path and mnemonics,
* @param path. (e.g. m/44'/637'/0'/0'/0')
* Creates new account with bip44 path and mnemonics.
*
* @param path. (e.g. m/44'/637'/0'/0'/0')

* Creates new account with bip44 path and mnemonics,
* @param path. (e.g. m/44'/637'/0'/0'/0')
* Detailed description: {@link https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki}
* @param mnemonics.
Copy link
Contributor

Choose a reason for hiding this comment

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

More information about the mnemonics argument would be very helpful. If you could include examples in the doc comment that'd be great too.

Copy link
Contributor

@banool banool left a comment

Choose a reason for hiding this comment

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

Make sure to use objects as parameters across the board.

}

/**
* Check's if the derive path is valid
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* Check's if the derive path is valid
* Checks if the derive path is valid.

return authKey.data;
}

sign(data: HexInput): Hex {
Copy link
Contributor

Choose a reason for hiding this comment

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

Doc comments on this and veryifySignature please captain!

Copy link
Contributor

@banool banool left a comment

Choose a reason for hiding this comment

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

There seems to be a lot of duplication with #9889. If this is a stack, could you stack them properly?

@0xmigo
Copy link
Contributor Author

0xmigo commented Sep 19, 2023

There seems to be a lot of duplication with #9889. If this is a stack, could you stack them properly?

Ah yes, this is actually the top stack of #9889 . I will do a rebase again once I fix all from comments on #9889

@0xmigo
Copy link
Contributor Author

0xmigo commented Sep 25, 2023

This is an old PR. for the new ones, please refer to #10210

@0xmigo 0xmigo closed this Sep 25, 2023
@0xmigo 0xmigo deleted the jin/sdk_v2_account_classes branch October 2, 2023 18:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants