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 Create Account fromDerivePath #2073

Merged
merged 1 commit into from
Aug 24, 2022
Merged

Add Create Account fromDerivePath #2073

merged 1 commit into from
Aug 24, 2022

Conversation

daoauth
Copy link
Contributor

@daoauth daoauth commented Jul 19, 2022

Description

Create Account with hdpath 637

Test Plan

test('generates derive path accounts', () => {
  const address = '0x07968dab936c1bad187c60ce4082f307d030d780e91e694ae03aef16aba73f30';
  const a1 = AptosAccount.fromDerivePath('m/44\'/637\'/0\'/0\'/0\'', mnemonic);
  expect(a1.address().hex()).toBe(address);
});

test('generates derive path accounts', () => {
  expect(() => { AptosAccount.fromDerivePath('', mnemonic); }).toThrow(new Error('Invalid derivation path'));
});

This change is Reviewable

@davidiw
Copy link
Contributor

davidiw commented Jul 20, 2022

Great stuff! Lemme get some typescript ninjas on this.

@davidiw davidiw requested review from CapCap, jjleng and hariria and removed request for CapCap July 20, 2022 16:22
ecosystem/typescript/sdk/src/aptos_account.test.ts Outdated Show resolved Hide resolved
ecosystem/typescript/sdk/package.json Outdated Show resolved Hide resolved
static isValidPath = (path: string): boolean => {
if (
// eslint-disable-next-line prefer-regex-literals
!new RegExp('^m\\/44+\'\\/637+\'\\/[0-9]+\'\\/[0-9]+\'\\/[0-9]+\'+$').test(
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't look right:

  1. First of all, rebase and start using double quotes
  2. Let's follow the BIP44 standard, https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki#path-levels. The change and address_index do not follow by a '. In the link, a path looks like m / purpose' / coin_type' / account' / change / address_index.
  3. The regex has issues. 44+ would match 44444 and 637+ would match 63777777. 44 and 637 should be an exact match. 44 is the current BIP number and 637 is Apto's coin type.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because it uses the ed25519-hd-key module, the regex check uses a ' for all numbers. And other chains, such as near and solana are using a ' for all numbers.

ecosystem/typescript/sdk/src/aptos_account.ts Outdated Show resolved Hide resolved
.some(isNaN as any);
};

static fromDerivePath(path: string, mnemonics: string): AptosAccount {
Copy link
Contributor

@jjleng jjleng Jul 20, 2022

Choose a reason for hiding this comment

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

Also, check if mnemonics are empty and throw meaningful errors, just in case developers forgot to pass in the mnemonics.

Copy link
Contributor

Choose a reason for hiding this comment

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

You still need to check if the mnemonics are empty. Why should we allow that to happen? Throw an error in such case.

Copy link
Contributor

Choose a reason for hiding this comment

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

Add ts-doc for this method

ecosystem/typescript/sdk/src/types/bip39-light.d.ts Outdated Show resolved Hide resolved
@daoauth daoauth requested review from hariria and jjleng July 21, 2022 14:54
Copy link
Contributor

@jjleng jjleng left a comment

Choose a reason for hiding this comment

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

Please squash your commits and make sure your final commit message follows the Angular Conventional Commits. This is to help the SDK tooling find your commit and add it to release notes.

For your final commit, the message should look like this:
feat: add BIP44 to allow the creation of multiple accounts with the same key

Comment on lines 36 to 40
if (
// eslint-disable-next-line prefer-regex-literals
!new RegExp("^m\\/44'\\/637'\\/[0-9]+'\\/[0-9]+'\\/[0-9]+'+$").test(
path,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not one line?

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'll fix it

static isValidPath = (path: string): boolean => {
if (
// eslint-disable-next-line prefer-regex-literals
!new RegExp("^m\\/44'\\/637'\\/[0-9]+'\\/[0-9]+'\\/[0-9]+'+$").test(
Copy link
Contributor

Choose a reason for hiding this comment

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

You can either:
new RegExp("^m/44'/637'/[0-9]+'/[0-9]+'/[0-9]+'+$").test Or /^m\/44'\/637'\/[0-9]+'\/[0-9]+'\/[0-9]+'+$/.test

Copy link
Contributor

Choose a reason for hiding this comment

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

The \\ is not needed

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'll fix it

.some(isNaN as any);
};

static fromDerivePath(path: string, mnemonics: string): AptosAccount {
Copy link
Contributor

Choose a reason for hiding this comment

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

You still need to check if the mnemonics are empty. Why should we allow that to happen? Throw an error in such case.

@@ -30,6 +32,34 @@ export class AptosAccount {
return new AptosAccount(HexString.ensure(obj.privateKeyHex).toUint8Array(), obj.address);
}

static isValidPath = (path: string): boolean => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Add @ts-doc for this method

.some(isNaN as any);
};

static fromDerivePath(path: string, mnemonics: string): AptosAccount {
Copy link
Contributor

Choose a reason for hiding this comment

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

Add ts-doc for this method

@daoauth
Copy link
Contributor Author

daoauth commented Aug 10, 2022

@JJeng @scure/bip39

function normalize(str: string) {
  const norm = nfkd(str);
  const words = norm.split(' ');
  if (![12, 15, 18, 21, 24].includes(words.length)) throw new Error('Invalid mnemonic');
  return { nfkd: norm, words };
}

Length check of mnemonics is in the @scure/bip39 module.

@daoauth daoauth requested a review from jjleng August 10, 2022 15:01
Copy link
Contributor

@jjleng jjleng left a comment

Choose a reason for hiding this comment

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

Thanks

@jjleng jjleng enabled auto-merge (squash) August 10, 2022 17:33
@jjleng jjleng disabled auto-merge August 10, 2022 19:11
@jjleng jjleng enabled auto-merge (rebase) August 10, 2022 19:12
@jjleng
Copy link
Contributor

jjleng commented Aug 10, 2022

please rebase your PR

auto-merge was automatically disabled August 11, 2022 05:58

Head branch was pushed to by a user without write access

@gregnazario
Copy link
Contributor

I think you need to rebase, there's a ton of commits here that are already in the codebase

@davidiw davidiw enabled auto-merge (squash) August 12, 2022 20:26
@github-actions
Copy link
Contributor

❌ Forge test failure on 24aec8cdf906c37ce9fd5f9f14c670b6057be803

Forge test runner terminated

@davidiw davidiw disabled auto-merge August 23, 2022 05:28
@davidiw
Copy link
Contributor

davidiw commented Aug 23, 2022

please update, it seems like this is stale

@daoauth
Copy link
Contributor Author

daoauth commented Aug 23, 2022

@davidiw update was done thanks.

@jjleng jjleng enabled auto-merge (rebase) August 23, 2022 21:27
auto-merge was automatically disabled August 23, 2022 23:33

Rebase failed

https://github.com/satoshilabs/slips/pull/1364/files

remove bip39-light

Update aptos_account.ts

Update aptos_account.ts

Fix comma
@jjleng jjleng enabled auto-merge (rebase) August 24, 2022 04:42
@github-actions
Copy link
Contributor

Forge is running with ac434610e3b7c8f47be9c6991f201f7a47e3d637

✅ Forge test success on ac434610e3b7c8f47be9c6991f201f7a47e3d637

performance benchmark with full nodes : 6944 TPS, 4297 ms latency, 7050 ms p99 latency,no expired txns
Test Ok

Forge is running with 3f015828e9d64ecc32951c0ed63ea5e64012e78d

❌ Forge test failure on 3f015828e9d64ecc32951c0ed63ea5e64012e78d

Test Failed: Failed to mint accounts: API error Error(VmError): Invalid transaction: Type: Validation Code: SENDING_ACCOUNT_DOES_NOT_EXIST

Forge is running with cb62b285235461a63141c09f0c05e5e19d309188

✅ Forge test success on cb62b285235461a63141c09f0c05e5e19d309188

performance benchmark with full nodes : 6596 TPS, 4523 ms latency, 6050 ms p99 latency,no expired txns
Test Ok

Forge is running with e060b8dd9e54f4fe729059578d878681b54c2271

✅ Forge test success on e060b8dd9e54f4fe729059578d878681b54c2271

performance benchmark with full nodes : 6582 TPS, 4547 ms latency, 6000 ms p99 latency,no expired txns
Test Ok

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.

6 participants