|
| 1 | +# Getting Started [Start with MinaJS Accounts.] |
| 2 | + |
| 3 | +:::warning |
| 4 | +For now there are only nightly builds available before we reach a beta version. You won't be able to install it from npm. |
| 5 | +::: |
| 6 | + |
| 7 | +MinaJS Accounts shares an API similar to [Viem](https://viem.sh/). |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +```sh |
| 12 | +$ npm install @mina-js/accounts |
| 13 | +``` |
| 14 | + |
| 15 | +## Utilities |
| 16 | + |
| 17 | +### generateMnemonic |
| 18 | + |
| 19 | +```ts twoslash |
| 20 | +import { generateMnemonic, english } from '@mina-js/accounts' |
| 21 | + |
| 22 | +const mnemonic = generateMnemonic(english); |
| 23 | +``` |
| 24 | + |
| 25 | +### generatePrivateKey |
| 26 | + |
| 27 | +```ts twoslash |
| 28 | +import { generatePrivateKey } from '@mina-js/accounts' |
| 29 | + |
| 30 | +const privateKey = generatePrivateKey(); |
| 31 | +``` |
| 32 | + |
| 33 | +### mnemonicToAccount |
| 34 | + |
| 35 | +```ts twoslash |
| 36 | +import { mnemonicToAccount } from '@mina-js/accounts' |
| 37 | + |
| 38 | +const account = mnemonicToAccount('your mnemonic here'); |
| 39 | +``` |
| 40 | + |
| 41 | +### privateKeyToAccount |
| 42 | + |
| 43 | +```ts twoslash |
| 44 | +import { privateKeyToAccount } from '@mina-js/accounts' |
| 45 | + |
| 46 | +// Mainnet account |
| 47 | +const mainnetAccount = privateKeyToAccount({ privateKey: 'your private key here' }); |
| 48 | +// Testnet account |
| 49 | +const testnetAccount = privateKeyToAccount({ |
| 50 | + privateKey: 'your private key here', |
| 51 | + network: 'testnet' |
| 52 | +}); |
| 53 | +``` |
| 54 | + |
| 55 | +### hdKeyToAccount |
| 56 | + |
| 57 | +```ts twoslash |
| 58 | +import { hdKeyToAccount, hex, HDKey } from '@mina-js/accounts' |
| 59 | + |
| 60 | +const hdKey = HDKey.fromMasterSeed(hex.decode("59eabf9e9...")); |
| 61 | + |
| 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" }); |
| 65 | +``` |
| 66 | + |
| 67 | +### hex |
| 68 | + |
| 69 | +Exported from `@scure/base`. |
| 70 | + |
| 71 | +### HDKey |
| 72 | + |
| 73 | +Exported from `@scure/bip32`. |
| 74 | + |
| 75 | +## Account operations |
| 76 | + |
| 77 | +### Sign a message |
| 78 | + |
| 79 | +```ts twoslash |
| 80 | +import { mnemonicToAccount } from '@mina-js/accounts' |
| 81 | + |
| 82 | +const account = mnemonicToAccount('your mnemonic here'); |
| 83 | + |
| 84 | +const signedMessage = await account.signMessage({ message: 'your message here' }); |
| 85 | +``` |
| 86 | + |
| 87 | +### Sign a transaction |
| 88 | + |
| 89 | +```ts twoslash |
| 90 | +import { mnemonicToAccount } from '@mina-js/accounts' |
| 91 | + |
| 92 | +const account = mnemonicToAccount('your mnemonic here'); |
| 93 | + |
| 94 | +const signedTransaction = await account.signTransaction({ |
| 95 | + transaction: { |
| 96 | + nonce: 1n, |
| 97 | + from: "B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5", |
| 98 | + to: "B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5", |
| 99 | + amount: 3000000000n, |
| 100 | + fee: 100000000n, |
| 101 | + } |
| 102 | +}); |
| 103 | +``` |
| 104 | + |
| 105 | +### Sign fields |
| 106 | + |
| 107 | +```ts twoslash |
| 108 | +import { mnemonicToAccount } from '@mina-js/accounts' |
| 109 | + |
| 110 | +const account = mnemonicToAccount('your mnemonic here'); |
| 111 | + |
| 112 | +const signedFields = await account.signFields({ fields: [1n, 2n, 3n] }); |
| 113 | +``` |
| 114 | + |
| 115 | +### Create a nullifier |
| 116 | + |
| 117 | +```ts twoslash |
| 118 | +import { mnemonicToAccount } from '@mina-js/accounts' |
| 119 | + |
| 120 | +const account = mnemonicToAccount('your mnemonic here'); |
| 121 | + |
| 122 | +const nullifier = await account.createNullifier({ message: [1n, 2n, 3n] }); |
| 123 | +``` |
0 commit comments