Skip to content

Commit 392a3f8

Browse files
committed
feat(accounts): move to single argument functions
1 parent 313b650 commit 392a3f8

File tree

7 files changed

+51
-41
lines changed

7 files changed

+51
-41
lines changed

apps/docs/src/pages/accounts/getting-started.mdx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ $ npm install @mina-js/accounts
1919
```ts twoslash
2020
import { generateMnemonic, english } from '@mina-js/accounts'
2121

22-
const mnemonic = generateMnemonic(english);
22+
const mnemonic = generateMnemonic({ wordlist: english });
2323
```
2424

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

38-
const account = mnemonicToAccount('your mnemonic here');
38+
const account = mnemonicToAccount({ mnemonic: 'your mnemonic here' });
3939
```
4040

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

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

62-
const addressIndexAccount = hdKeyToAccount(hdKey, { addressIndex: 5 });
63-
const accountIndexAccount = hdKeyToAccount(hdKey, { accountIndex: 5 });
64-
const customPathAccount = hdKeyToAccount(hdKey, { path: "m/44'/12586'/0'/0/5" });
62+
const addressIndexAccount = hdKeyToAccount({ hdKey, addressIndex: 5 });
63+
const accountIndexAccount = hdKeyToAccount({ hdKey, accountIndex: 5 });
64+
const customPathAccount = hdKeyToAccount({ hdKey, path: "m/44'/12586'/0'/0/5" });
6565
```
6666

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

82-
const account = mnemonicToAccount('your mnemonic here');
82+
const account = mnemonicToAccount({ mnemonic: 'your mnemonic here' });
8383

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

92-
const account = mnemonicToAccount('your mnemonic here');
92+
const account = mnemonicToAccount({ mnemonic: 'your mnemonic here' });
9393

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

110-
const account = mnemonicToAccount('your mnemonic here');
110+
const account = mnemonicToAccount({ mnemonic: 'your mnemonic here' });
111111

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

120-
const account = mnemonicToAccount('your mnemonic here');
120+
const account = mnemonicToAccount({ mnemonic: 'your mnemonic here' });
121121

122122
const nullifier = await account.createNullifier({ message: [1n, 2n, 3n] });
123123
```

packages/accounts/src/accounts/generate-mnemonic.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import { english } from "../";
33
import { generateMnemonic } from "./generate-mnemonic";
44

55
it("generates 12 word mnemonic", () => {
6-
const mnemonic = generateMnemonic(english);
6+
const mnemonic = generateMnemonic({ wordlist: english });
77
expect(mnemonic.split(" ").length).toBe(12);
88
});
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { generateMnemonic as generateMnemonic_ } from "@scure/bip39";
22

3+
type GenerateMnemonicOptions = {
4+
wordlist: string[];
5+
strength?: number | undefined;
6+
};
7+
38
/**
49
* @description Generates a random mnemonic phrase with a given wordlist.
510
*
@@ -8,9 +13,9 @@ import { generateMnemonic as generateMnemonic_ } from "@scure/bip39";
813
*
914
* @returns A randomly generated mnemonic phrase.
1015
*/
11-
export function generateMnemonic(
12-
wordlist: string[],
13-
strength?: number | undefined,
14-
): string {
16+
export function generateMnemonic({
17+
wordlist,
18+
strength,
19+
}: GenerateMnemonicOptions): string {
1520
return generateMnemonic_(wordlist, strength);
1621
}

packages/accounts/src/accounts/hd-key-to-account.spec.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ const hdKey = HDKey.fromMasterSeed(
1010
);
1111

1212
it("matches the snapshot", () => {
13-
const hdAccount = hdKeyToAccount(hdKey);
13+
const hdAccount = hdKeyToAccount({ hdKey });
1414
expect(hdAccount).toMatchSnapshot();
1515
});
1616

1717
describe("args: addressIndex", () => {
1818
Array.from({ length: 10 }).forEach((_, index) => {
1919
it(`derives addressIndex: ${index}`, () => {
20-
const account = hdKeyToAccount(hdKey, {
20+
const account = hdKeyToAccount({
21+
hdKey,
2122
addressIndex: index,
2223
});
2324
expect(account.publicKey.length).toEqual(55);
@@ -29,7 +30,8 @@ describe("args: addressIndex", () => {
2930
describe("args: path", () => {
3031
Array.from({ length: 10 }).forEach((_, index) => {
3132
it(`derives path: m/44'/12586'/0'/0/${index}`, () => {
32-
const account = hdKeyToAccount(hdKey, {
33+
const account = hdKeyToAccount({
34+
hdKey,
3335
path: `m/44'/12586'/0'/0/${index}`,
3436
});
3537
expect(account.publicKey.length).toEqual(55);
@@ -39,17 +41,17 @@ describe("args: path", () => {
3941
});
4042

4143
it("derives with accountIndex", () => {
42-
const hdAccount = hdKeyToAccount(hdKey, { accountIndex: 1 }).publicKey;
44+
const hdAccount = hdKeyToAccount({ hdKey, accountIndex: 1 }).publicKey;
4345
expect(hdAccount).toMatchSnapshot();
4446
});
4547

4648
it("derives with changeIndex", () => {
47-
const hdAccount = hdKeyToAccount(hdKey, { changeIndex: 1 }).publicKey;
49+
const hdAccount = hdKeyToAccount({ hdKey, changeIndex: 1 }).publicKey;
4850
expect(hdAccount).toMatchSnapshot();
4951
});
5052

5153
it("signs a message", async () => {
52-
const account = hdKeyToAccount(hdKey);
54+
const account = hdKeyToAccount({ hdKey });
5355
const signature = await account.signMessage({ message: "hello word" });
5456
expect(signature).toMatchSnapshot();
5557
});

packages/accounts/src/accounts/hd-key-to-account.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { sha256 } from "@noble/hashes/sha256";
22
import { bytesToHex } from "@noble/hashes/utils";
33
import { base58check } from "@scure/base";
4+
import type { Simplify } from "type-fest";
45
import {
56
type HDAccount,
67
type HDKey,
@@ -12,25 +13,24 @@ import {
1213
privateKeyToAccount,
1314
} from "./private-key-to-account";
1415

15-
export type HDKeyToAccountOptions = HDOptions &
16-
Partial<PrivateKeyToAccountParams>;
16+
export type HDKeyToAccountOptions = Simplify<
17+
{ hdKey: HDKey } & HDOptions & Partial<PrivateKeyToAccountParams>
18+
>;
1719

1820
/**
1921
* @description Creates an Account from a HD Key.
2022
*
2123
* @returns A HD Account.
2224
*/
23-
export function hdKeyToAccount(
24-
hdKey_: HDKey,
25-
{
26-
accountIndex = 0,
27-
addressIndex = 0,
28-
changeIndex = 0,
29-
path,
30-
...options
31-
}: HDKeyToAccountOptions = {},
32-
): HDAccount {
33-
const childNode = hdKey_.derive(
25+
export function hdKeyToAccount({
26+
hdKey,
27+
accountIndex = 0,
28+
addressIndex = 0,
29+
changeIndex = 0,
30+
path,
31+
...options
32+
}: HDKeyToAccountOptions): HDAccount {
33+
const childNode = hdKey.derive(
3434
path ||
3535
`m/${MinaKeyConst.PURPOSE}'/${MinaKeyConst.MINA_COIN_TYPE}'/${accountIndex}'/${changeIndex}/${addressIndex}`,
3636
);

packages/accounts/src/accounts/mnemonic-to-account.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import { mnemonic } from "../test/constants";
33
import { mnemonicToAccount } from "./mnemonic-to-account";
44

55
it("matches the snapshot", () => {
6-
const mnemonicAccount = mnemonicToAccount(mnemonic);
6+
const mnemonicAccount = mnemonicToAccount({ mnemonic });
77
expect(mnemonicAccount).toMatchSnapshot();
88
});
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
import { HDKey } from "@scure/bip32";
22
import { mnemonicToSeedSync } from "@scure/bip39";
33

4-
import type { HDAccount, HDOptions } from "../types";
4+
import type { Simplify } from "type-fest";
5+
import type { HDAccount } from "../types";
56
import {
67
type HDKeyToAccountOptions,
78
hdKeyToAccount,
89
} from "./hd-key-to-account.js";
910

10-
export type MnemonicToAccountOptions = HDKeyToAccountOptions;
11+
export type MnemonicToAccountOptions = Simplify<
12+
{ mnemonic: string } & Partial<HDKeyToAccountOptions>
13+
>;
1114

1215
/**
1316
* @description Creates an Account from a mnemonic phrase.
1417
*
1518
* @returns A HD Account.
1619
*/
17-
export function mnemonicToAccount(
18-
mnemonic: string,
19-
opts: HDOptions = {},
20-
): HDAccount {
20+
export function mnemonicToAccount({
21+
mnemonic,
22+
...opts
23+
}: MnemonicToAccountOptions): HDAccount {
2124
const seed = mnemonicToSeedSync(mnemonic);
22-
return hdKeyToAccount(HDKey.fromMasterSeed(seed), opts);
25+
return hdKeyToAccount({ ...opts, hdKey: HDKey.fromMasterSeed(seed) });
2326
}

0 commit comments

Comments
 (0)