Skip to content

Commit a116452

Browse files
nhussein11brunopgalvaoeshaben
authored
Add create accounts (new IA) (#1291)
* fix: create account * Update .chain-interactions/accounts/create-account.md Co-authored-by: Bruno Galvao <[email protected]> * formatting --------- Co-authored-by: Bruno Galvao <[email protected]> Co-authored-by: Erin Shaben <[email protected]>
1 parent 873be96 commit a116452

File tree

7 files changed

+227
-1
lines changed

7 files changed

+227
-1
lines changed
Lines changed: 180 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,180 @@
1-
TODO
1+
---
2+
title: Create an Account
3+
description: Step-by-step guide to creating Polkadot accounts using different programming languages and libraries, including JavaScript, Python, and Rust examples.
4+
categories: Basics, Accounts, Developer Tools
5+
---
6+
7+
# Create an Account
8+
9+
## Introduction
10+
11+
Creating accounts is a fundamental operation when building applications on Polkadot and its parachains. Accounts serve as the basis for identity, asset ownership, and transaction signing. Understanding how to generate and manage accounts programmatically enables you to build wallets, automate operations, and create seamless user experiences.
12+
13+
Polkadot accounts are based on the SR25519 signature scheme by default, though ED25519 and ECDSA are also supported. Each account consists of a public key (address) and a private key (seed/mnemonic). **Keep your private keys secure and never share them**.
14+
15+
This tutorial will guide you through creating accounts using different programming languages and libraries.
16+
17+
## Prerequisites
18+
19+
Before starting, make sure you have:
20+
21+
- Basic understanding of public-key cryptography concepts
22+
- Development environment set up for your chosen language
23+
- Familiarity with the programming language you'll be using
24+
25+
## Use JavaScript/TypeScript
26+
27+
JavaScript/TypeScript developers can use the Polkadot.js API to create and manage Polkadot accounts.
28+
29+
1. Create a new project directory and initialize it:
30+
31+
```bash
32+
mkdir account-creator
33+
cd account-creator
34+
npm init -y && npm pkg set type=module
35+
```
36+
37+
2. Install the required packages:
38+
39+
```bash
40+
npm install @polkadot/util-crypto @polkadot/keyring
41+
npm install --save-dev typescript tsx
42+
```
43+
44+
3. Create a file named `create-account.ts` and add the following code to it:
45+
46+
```typescript title="create-account.ts"
47+
--8<-- 'code/chain-interactions/accounts/create-account/create-account.ts'
48+
```
49+
50+
Key aspects of the code:
51+
52+
- **Mnemonic generation**: Uses `mnemonicGenerate()` to create a 12-word BIP39 mnemonic phrase for human-readable key backup.
53+
- **Keyring**: The `Keyring` class manages accounts with a specified signature scheme and address format.
54+
- **SS58 format**: Setting `ss58Format: 0` configures addresses for the Polkadot relay chain.
55+
56+
4. Execute the script using `tsx`:
57+
58+
```bash
59+
npx tsx create-account.ts
60+
```
61+
62+
You should see output similar to:
63+
64+
--8<-- 'code/chain-interactions/accounts/create-account/create-account-ts.html'
65+
66+
## Python
67+
68+
Python developers can use the `substrate-interface` library to create and manage Polkadot accounts.
69+
70+
1. Create a new project directory and set up a virtual environment:
71+
72+
```bash
73+
mkdir account-creator-python
74+
cd account-creator-python
75+
python3 -m venv venv
76+
source venv/bin/activate # On Windows: venv\Scripts\activate
77+
```
78+
79+
2. Install the required package:
80+
81+
```bash
82+
pip install substrate-interface
83+
```
84+
85+
3. Create a file named `create_account.py`:
86+
87+
```python title="create_account.py"
88+
--8<-- 'code/chain-interactions/accounts/create-account/create_account.py'
89+
```
90+
91+
Key aspects of the code:
92+
93+
- **Mnemonic generation**: The `generate_mnemonic()` function creates a BIP39-compatible phrase.
94+
- **Keypair creation**: `Keypair.create_from_mnemonic()` derives keys from the mnemonic.
95+
96+
4. Execute the script:
97+
98+
```bash
99+
python create_account.py
100+
```
101+
102+
You should see output similar to:
103+
104+
--8<-- 'code/chain-interactions/accounts/create-account/create-account-py.html'
105+
106+
## Rust
107+
108+
Rust provides low-level access to Substrate primitives for account creation through the `sp-core` and `sp-keyring` crates.
109+
110+
1. Create a new Rust project:
111+
112+
```bash
113+
cargo new account-creator-rust
114+
cd account-creator-rust
115+
```
116+
117+
2. Add dependencies to your `Cargo.toml`:
118+
119+
```toml title="Cargo.toml"
120+
[package]
121+
name = "account-creator-rust"
122+
version = "0.1.0"
123+
edition = "2021"
124+
125+
[dependencies]
126+
sp-core = "28.0"
127+
sp-runtime = "31.0"
128+
```
129+
130+
3. Create your account generation code in `src/main.rs`:
131+
132+
```rust title="src/main.rs"
133+
--8<-- 'code/chain-interactions/accounts/create-account/create-account.rs'
134+
```
135+
136+
Key aspects of the code:
137+
138+
- **Keypair generation**: [`sr25519::Pair::generate_with_phrase()`](https://docs.rs/sp-core/latest/sp_core/crypto/trait.Pair.html#method.generate_with_phrase){target=\_blank} creates a new key pair with mnemonic.
139+
- **Public key extraction**: The [`public()`](https://docs.rs/sp-core/latest/sp_core/crypto/trait.Pair.html#tymethod.public){target=\_blank} method retrieves the public key from the pair.
140+
- **SS58 encoding**: Uses Polkadot's address format for the human-readable address.
141+
142+
4. Build and run the project:
143+
144+
```bash
145+
cargo run
146+
```
147+
148+
You should see output similar to:
149+
150+
--8<-- 'code/chain-interactions/accounts/create-account/create-account-rs.html'
151+
152+
## Where to Go Next
153+
154+
<div class="grid cards" markdown>
155+
156+
- <span class="badge guide">Guide</span> __Send Transactions with SDKs__
157+
158+
---
159+
160+
Learn how to send signed transactions using your newly created accounts with Polkadot-API and Polkadot.js API libraries.
161+
162+
[:octicons-arrow-right-24: Get Started](/chain-interactions/send-transactions/with-sdks/)
163+
164+
- <span class="badge guide">Guide</span> __Calculate Transaction Fees__
165+
166+
---
167+
168+
Learn how to estimate transaction fees before sending transactions from your accounts.
169+
170+
[:octicons-arrow-right-24: Get Started](/chain-interactions/send-transactions/calculate-transaction-fees/)
171+
172+
- <span class="badge guide">Guide</span> __Query Chain Data__
173+
174+
---
175+
176+
Explore different methods for querying blockchain data, including account balances and other chain state.
177+
178+
[:octicons-arrow-right-24: Get Started](/chain-interactions/query-data/query-sdks/)
179+
180+
</div>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div class="termynal" data-termynal>
2+
<span data-ty="input"><span class="file-path"></span>python create_account.py</span>
3+
<span data-ty>Address: 15oF4uVJwmo4TdGW7VfQxNLavjCXviqxT9S1MgbjMNHr6Sp5</span>
4+
<span data-ty>Mnemonic: cushion dog echo people vendor curve truck begin latin romance rebuild ...</span>
5+
</div>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div class="termynal" data-termynal>
2+
<span data-ty="input"><span class="file-path"></span>cargo run</span>
3+
<span data-ty>Address: 15oF4uVJwmo4TdGW7VfQxNLavjCXviqxT9S1MgbjMNHr6Sp5</span>
4+
<span data-ty>Mnemonic: cushion dog echo people vendor curve truck begin latin romance rebuild ...</span>
5+
</div>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<div class="termynal" data-termynal>
2+
<span data-ty="input"><span class="file-path"></span>npx tsx create-account.ts</span>
3+
<span data-ty="progress"></span>
4+
<span data-ty>Address: 15oF4uVJwmo4TdGW7VfQxNLavjCXviqxT9S1MgbjMNHr6Sp5</span>
5+
<span data-ty>Mnemonic: cushion dog echo people vendor curve truck begin latin romance rebuild ...</span>
6+
</div>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use sp_core::{crypto::Ss58Codec, Pair};
2+
3+
fn main() {
4+
let (pair, phrase, _) = sp_core::sr25519::Pair::generate_with_phrase(None);
5+
let address = pair.public().to_ss58check();
6+
7+
println!("Address: {}", address);
8+
println!("Mnemonic: {}", phrase);
9+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { cryptoWaitReady, mnemonicGenerate } from '@polkadot/util-crypto';
2+
import { Keyring } from '@polkadot/keyring';
3+
4+
async function main() {
5+
await cryptoWaitReady();
6+
7+
const mnemonic = mnemonicGenerate(12);
8+
const keyring = new Keyring({ type: 'sr25519', ss58Format: 0 });
9+
const pair = keyring.addFromMnemonic(mnemonic);
10+
11+
console.log(`Address: ${pair.address}`);
12+
console.log(`Mnemonic: ${mnemonic}`);
13+
}
14+
15+
main().catch(console.error);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from substrateinterface import Keypair
2+
3+
mnemonic = Keypair.generate_mnemonic()
4+
keypair = Keypair.create_from_mnemonic(mnemonic)
5+
6+
print(f"Address: {keypair.ss58_address}")
7+
print(f"Mnemonic: {mnemonic}")

0 commit comments

Comments
 (0)