Skip to content

Commit

Permalink
feat(accounts): move to single argument functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcnk committed Sep 12, 2024
1 parent 313b650 commit 392a3f8
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 41 deletions.
18 changes: 9 additions & 9 deletions apps/docs/src/pages/accounts/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ $ npm install @mina-js/accounts
```ts twoslash
import { generateMnemonic, english } from '@mina-js/accounts'

const mnemonic = generateMnemonic(english);
const mnemonic = generateMnemonic({ wordlist: english });
```

### generatePrivateKey
Expand All @@ -35,7 +35,7 @@ const privateKey = generatePrivateKey();
```ts twoslash
import { mnemonicToAccount } from '@mina-js/accounts'

const account = mnemonicToAccount('your mnemonic here');
const account = mnemonicToAccount({ mnemonic: 'your mnemonic here' });
```

### privateKeyToAccount
Expand All @@ -59,9 +59,9 @@ import { hdKeyToAccount, hex, HDKey } from '@mina-js/accounts'

const hdKey = HDKey.fromMasterSeed(hex.decode("59eabf9e9..."));

const addressIndexAccount = hdKeyToAccount(hdKey, { addressIndex: 5 });
const accountIndexAccount = hdKeyToAccount(hdKey, { accountIndex: 5 });
const customPathAccount = hdKeyToAccount(hdKey, { path: "m/44'/12586'/0'/0/5" });
const addressIndexAccount = hdKeyToAccount({ hdKey, addressIndex: 5 });
const accountIndexAccount = hdKeyToAccount({ hdKey, accountIndex: 5 });
const customPathAccount = hdKeyToAccount({ hdKey, path: "m/44'/12586'/0'/0/5" });
```

### hex
Expand All @@ -79,7 +79,7 @@ Exported from `@scure/bip32`.
```ts twoslash
import { mnemonicToAccount } from '@mina-js/accounts'

const account = mnemonicToAccount('your mnemonic here');
const account = mnemonicToAccount({ mnemonic: 'your mnemonic here' });

const signedMessage = await account.signMessage({ message: 'your message here' });
```
Expand All @@ -89,7 +89,7 @@ const signedMessage = await account.signMessage({ message: 'your message here' }
```ts twoslash
import { mnemonicToAccount } from '@mina-js/accounts'

const account = mnemonicToAccount('your mnemonic here');
const account = mnemonicToAccount({ mnemonic: 'your mnemonic here' });

const signedTransaction = await account.signTransaction({
transaction: {
Expand All @@ -107,7 +107,7 @@ const signedTransaction = await account.signTransaction({
```ts twoslash
import { mnemonicToAccount } from '@mina-js/accounts'

const account = mnemonicToAccount('your mnemonic here');
const account = mnemonicToAccount({ mnemonic: 'your mnemonic here' });

const signedFields = await account.signFields({ fields: [1n, 2n, 3n] });
```
Expand All @@ -117,7 +117,7 @@ const signedFields = await account.signFields({ fields: [1n, 2n, 3n] });
```ts twoslash
import { mnemonicToAccount } from '@mina-js/accounts'

const account = mnemonicToAccount('your mnemonic here');
const account = mnemonicToAccount({ mnemonic: 'your mnemonic here' });

const nullifier = await account.createNullifier({ message: [1n, 2n, 3n] });
```
2 changes: 1 addition & 1 deletion packages/accounts/src/accounts/generate-mnemonic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { english } from "../";
import { generateMnemonic } from "./generate-mnemonic";

it("generates 12 word mnemonic", () => {
const mnemonic = generateMnemonic(english);
const mnemonic = generateMnemonic({ wordlist: english });
expect(mnemonic.split(" ").length).toBe(12);
});
13 changes: 9 additions & 4 deletions packages/accounts/src/accounts/generate-mnemonic.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { generateMnemonic as generateMnemonic_ } from "@scure/bip39";

type GenerateMnemonicOptions = {
wordlist: string[];
strength?: number | undefined;
};

/**
* @description Generates a random mnemonic phrase with a given wordlist.
*
Expand All @@ -8,9 +13,9 @@ import { generateMnemonic as generateMnemonic_ } from "@scure/bip39";
*
* @returns A randomly generated mnemonic phrase.
*/
export function generateMnemonic(
wordlist: string[],
strength?: number | undefined,
): string {
export function generateMnemonic({
wordlist,
strength,
}: GenerateMnemonicOptions): string {
return generateMnemonic_(wordlist, strength);
}
14 changes: 8 additions & 6 deletions packages/accounts/src/accounts/hd-key-to-account.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ const hdKey = HDKey.fromMasterSeed(
);

it("matches the snapshot", () => {
const hdAccount = hdKeyToAccount(hdKey);
const hdAccount = hdKeyToAccount({ hdKey });
expect(hdAccount).toMatchSnapshot();
});

describe("args: addressIndex", () => {
Array.from({ length: 10 }).forEach((_, index) => {
it(`derives addressIndex: ${index}`, () => {
const account = hdKeyToAccount(hdKey, {
const account = hdKeyToAccount({
hdKey,
addressIndex: index,
});
expect(account.publicKey.length).toEqual(55);
Expand All @@ -29,7 +30,8 @@ describe("args: addressIndex", () => {
describe("args: path", () => {
Array.from({ length: 10 }).forEach((_, index) => {
it(`derives path: m/44'/12586'/0'/0/${index}`, () => {
const account = hdKeyToAccount(hdKey, {
const account = hdKeyToAccount({
hdKey,
path: `m/44'/12586'/0'/0/${index}`,
});
expect(account.publicKey.length).toEqual(55);
Expand All @@ -39,17 +41,17 @@ describe("args: path", () => {
});

it("derives with accountIndex", () => {
const hdAccount = hdKeyToAccount(hdKey, { accountIndex: 1 }).publicKey;
const hdAccount = hdKeyToAccount({ hdKey, accountIndex: 1 }).publicKey;
expect(hdAccount).toMatchSnapshot();
});

it("derives with changeIndex", () => {
const hdAccount = hdKeyToAccount(hdKey, { changeIndex: 1 }).publicKey;
const hdAccount = hdKeyToAccount({ hdKey, changeIndex: 1 }).publicKey;
expect(hdAccount).toMatchSnapshot();
});

it("signs a message", async () => {
const account = hdKeyToAccount(hdKey);
const account = hdKeyToAccount({ hdKey });
const signature = await account.signMessage({ message: "hello word" });
expect(signature).toMatchSnapshot();
});
26 changes: 13 additions & 13 deletions packages/accounts/src/accounts/hd-key-to-account.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { sha256 } from "@noble/hashes/sha256";
import { bytesToHex } from "@noble/hashes/utils";
import { base58check } from "@scure/base";
import type { Simplify } from "type-fest";
import {
type HDAccount,
type HDKey,
Expand All @@ -12,25 +13,24 @@ import {
privateKeyToAccount,
} from "./private-key-to-account";

export type HDKeyToAccountOptions = HDOptions &
Partial<PrivateKeyToAccountParams>;
export type HDKeyToAccountOptions = Simplify<
{ hdKey: HDKey } & HDOptions & Partial<PrivateKeyToAccountParams>
>;

/**
* @description Creates an Account from a HD Key.
*
* @returns A HD Account.
*/
export function hdKeyToAccount(
hdKey_: HDKey,
{
accountIndex = 0,
addressIndex = 0,
changeIndex = 0,
path,
...options
}: HDKeyToAccountOptions = {},
): HDAccount {
const childNode = hdKey_.derive(
export function hdKeyToAccount({
hdKey,
accountIndex = 0,
addressIndex = 0,
changeIndex = 0,
path,
...options
}: HDKeyToAccountOptions): HDAccount {
const childNode = hdKey.derive(
path ||
`m/${MinaKeyConst.PURPOSE}'/${MinaKeyConst.MINA_COIN_TYPE}'/${accountIndex}'/${changeIndex}/${addressIndex}`,
);
Expand Down
2 changes: 1 addition & 1 deletion packages/accounts/src/accounts/mnemonic-to-account.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { mnemonic } from "../test/constants";
import { mnemonicToAccount } from "./mnemonic-to-account";

it("matches the snapshot", () => {
const mnemonicAccount = mnemonicToAccount(mnemonic);
const mnemonicAccount = mnemonicToAccount({ mnemonic });
expect(mnemonicAccount).toMatchSnapshot();
});
17 changes: 10 additions & 7 deletions packages/accounts/src/accounts/mnemonic-to-account.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import { HDKey } from "@scure/bip32";
import { mnemonicToSeedSync } from "@scure/bip39";

import type { HDAccount, HDOptions } from "../types";
import type { Simplify } from "type-fest";
import type { HDAccount } from "../types";
import {
type HDKeyToAccountOptions,
hdKeyToAccount,
} from "./hd-key-to-account.js";

export type MnemonicToAccountOptions = HDKeyToAccountOptions;
export type MnemonicToAccountOptions = Simplify<
{ mnemonic: string } & Partial<HDKeyToAccountOptions>
>;

/**
* @description Creates an Account from a mnemonic phrase.
*
* @returns A HD Account.
*/
export function mnemonicToAccount(
mnemonic: string,
opts: HDOptions = {},
): HDAccount {
export function mnemonicToAccount({
mnemonic,
...opts
}: MnemonicToAccountOptions): HDAccount {
const seed = mnemonicToSeedSync(mnemonic);
return hdKeyToAccount(HDKey.fromMasterSeed(seed), opts);
return hdKeyToAccount({ ...opts, hdKey: HDKey.fromMasterSeed(seed) });
}

0 comments on commit 392a3f8

Please sign in to comment.