Skip to content

Commit 723ad4a

Browse files
authored
Merge pull request #599 from multiversx/TOOL-553-move-documentation-from-sdk-examples-to-sdk-core
Move cookbook from examples
2 parents 741ea6e + 6f3cb39 commit 723ad4a

25 files changed

+6451
-8
lines changed

.eslintrc.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@ module.exports = {
1010
env: {
1111
node: true,
1212
},
13-
ignorePatterns: [".eslintrc.js", "node_modules", "out", "out-tests", "out-browser", "out-browser-tests"],
13+
ignorePatterns: [
14+
".eslintrc.js",
15+
"node_modules",
16+
"out",
17+
"out-tests",
18+
"out-browser",
19+
"out-browser-tests",
20+
"cookbook",
21+
],
1422
rules: {
1523
"@typescript-eslint/interface-name-prefix": "off",
1624
"@typescript-eslint/explicit-function-return-type": "off",

.github/workflows/test-localnet.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jobs:
4242
mkdir -p ~/localnet && cd ~/localnet
4343
mxpy localnet setup --configfile=${GITHUB_WORKSPACE}/localnet.toml
4444
nohup mxpy localnet start --configfile=${GITHUB_WORKSPACE}/localnet.toml > localnet.log 2>&1 & echo $! > localnet.pid
45-
sleep 10 # Allow time for the testnet to fully start
45+
sleep 120 # Allow time for the testnet to fully start
4646
4747
# Step 6: Install Node.js and dependencies
4848
- name: Set up Node.js environment

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
src/testdata/**
22
src/testutils/**
33
localnet.toml
4+
cookbook

cookbook/account.ts

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import path from "path"; // md-ignore
2+
import { Account, DevnetEntrypoint, KeyPair, Mnemonic, UserSecretKey } from "../src"; // md-ignore
3+
// md-start
4+
(async () => {
5+
// ## Creating Accounts
6+
7+
// You can initialize an account directly from the entrypoint. Keep in mind that the account is network agnostic, meaning it doesn't matter which entrypoint is used.
8+
// Accounts are used for signing transactions and messages and managing the account's nonce. They can also be saved to a PEM or keystore file for future use.
9+
10+
// ```js
11+
{
12+
const entrypoint = new DevnetEntrypoint();
13+
const account = entrypoint.createAccount();
14+
}
15+
// ```
16+
17+
// ### Other Ways to Instantiate an Account
18+
19+
// #### From a Secret Key
20+
// ```js
21+
{
22+
const secretKeyHex = "413f42575f7f26fad3317a778771212fdb80245850981e48b58a4f25e344e8f9";
23+
const secretKey = new UserSecretKey(Buffer.from(secretKeyHex, "hex"));
24+
25+
const accountFromSecretKey = new Account(secretKey);
26+
}
27+
// ```
28+
29+
// #### From a PEM file
30+
// ```js
31+
{
32+
const filePath = path.join("../src", "testdata", "testwallets", "alice.pem");
33+
const accountFromPem = Account.newFromPem(filePath);
34+
}
35+
// ```
36+
37+
// #### From a Keystore File
38+
// ```js
39+
{
40+
const keystorePath = path.join("../src", "testdata", "testwallets", "alice.json");
41+
const accountFromKeystore = Account.newFromKeystore(keystorePath, "password");
42+
}
43+
// ```
44+
45+
// #### From a Mnemonic
46+
// ```js
47+
48+
const mnemonic = Mnemonic.generate();
49+
const accountFromMnemonic = Account.newFromMnemonic(mnemonic.toString());
50+
// ```
51+
52+
// #### From a KeyPair
53+
54+
// ```js
55+
const keypair = KeyPair.generate();
56+
const accountFromKeyPairs = Account.newFromKeypair(keypair);
57+
// ```
58+
59+
// ### Managing the Account Nonce
60+
61+
// An account has a `nonce` property that the user is responsible for managing.
62+
// You can fetch the nonce from the network and increment it after each transaction.
63+
// Each transaction must have the correct nonce, otherwise it will fail to execute.
64+
65+
// ```js
66+
{
67+
const secretKeyHex = "413f42575f7f26fad3317a778771212fdb80245850981e48b58a4f25e344e8f9";
68+
const key = new UserSecretKey(Buffer.from(secretKeyHex, "hex"));
69+
70+
const accountWithNonce = new Account(key);
71+
const entrypoint = new DevnetEntrypoint();
72+
73+
// Fetch the current nonce from the network // md-as-comment
74+
accountWithNonce.nonce = await entrypoint.recallAccountNonce(accountWithNonce.address);
75+
76+
// Create and send a transaction here...
77+
78+
// Increment nonce after each transaction // md-as-comment
79+
const nonce = accountWithNonce.getNonceThenIncrement();
80+
}
81+
// ```
82+
83+
// For more details, see the [Creating Transactions](#creating-transactions) section.
84+
85+
// #### Saving the Account to a File
86+
87+
// Accounts can be saved to either a PEM file or a keystore file.
88+
// While PEM wallets are less secure for storing secret keys, they are convenient for testing purposes.
89+
// Keystore files offer a higher level of security.
90+
91+
// #### Saving the Account to a PEM File
92+
// ```js
93+
{
94+
const secretKeyHex = "413f42575f7f26fad3317a778771212fdb80245850981e48b58a4f25e344e8f9";
95+
const secretKey = new UserSecretKey(Buffer.from(secretKeyHex, "hex"));
96+
97+
const account = new Account(secretKey);
98+
account.saveToPem(path.resolve("wallet.pem"));
99+
}
100+
// ```
101+
102+
// #### Saving the Account to a Keystore File
103+
// ```js
104+
{
105+
const secretKeyHex = "413f42575f7f26fad3317a778771212fdb80245850981e48b58a4f25e344e8f9";
106+
const secretKey = new UserSecretKey(Buffer.from(secretKeyHex, "hex"));
107+
108+
const account = new Account(secretKey);
109+
account.saveToKeystore(path.resolve("keystoreWallet.json"), "password");
110+
}
111+
112+
// ```
113+
114+
// ### Using a Ledger Device
115+
116+
// You can manage your account with a Ledger device, allowing you to sign both transactions and messages while keeping your keys secure.
117+
118+
// Note: **The multiversx-sdk package does not include Ledger support by default. To enable it, install the package with Ledger dependencies**:
119+
/* // md-ignore
120+
// ```bash
121+
npm install @multiversx/sdk-hw-provider
122+
// ```
123+
*/ // md-ignore
124+
125+
// #### Creating a Ledger Account
126+
// This can be done using the dedicated library. You can find more information [here](https://docs.multiversx.com/sdk-and-tools/sdk-js/sdk-js-signing-providers/#the-hardware-wallet-provider).
127+
128+
// When signing transactions or messages, the Ledger device will prompt you to confirm the details before proceeding.
129+
130+
// ### Compatibility with IAccount Interface
131+
132+
// The `Account` implements the `IAccount` interface, making it compatible with transaction controllers and any other component that expects this interface.
133+
})().catch((e) => {
134+
console.log({ e });
135+
});

0 commit comments

Comments
 (0)