Skip to content

Commit bbb1db2

Browse files
committed
fix: create account
1 parent ff2c42b commit bbb1db2

File tree

7 files changed

+228
-1
lines changed

7 files changed

+228
-1
lines changed
Lines changed: 181 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,181 @@
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+
This tutorial will guide you through creating accounts using different programming languages and libraries.
14+
15+
## Prerequisites
16+
17+
Before starting, make sure you have:
18+
19+
- Basic understanding of public-key cryptography concepts
20+
- Development environment set up for your chosen language
21+
- Familiarity with the programming language you'll be using
22+
23+
!!! note
24+
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.
25+
26+
## JavaScript/TypeScript
27+
28+
Create a new project directory and initialize it:
29+
30+
```bash
31+
mkdir account-creator
32+
cd account-creator
33+
npm init -y
34+
```
35+
36+
Install the required packages:
37+
38+
```bash
39+
npm install @polkadot/util-crypto @polkadot/keyring
40+
npm install --save-dev typescript tsx
41+
```
42+
43+
Create a file named `create-account.ts`:
44+
45+
```typescript title="create-account.ts"
46+
--8<-- 'code/chain-interactions/accounts/create-account/create-account.ts'
47+
```
48+
49+
Key aspects of the code:
50+
51+
- **Mnemonic generation**: Uses `mnemonicGenerate()` to create a 12-word BIP39 mnemonic phrase for human-readable key backup
52+
- **Keyring**: The `Keyring` class manages accounts with specified signature scheme and address format
53+
- **SS58 format**: Setting `ss58Format: 0` configures addresses for the Polkadot relay chain
54+
55+
Execute the script using `tsx`:
56+
57+
```bash
58+
npx tsx create-account.ts
59+
```
60+
61+
You should see output similar to:
62+
63+
--8<-- 'code/chain-interactions/accounts/create-account/create-account-ts.html'
64+
65+
## Python
66+
67+
Python developers can use the `substrate-interface` library to create and manage Polkadot accounts.
68+
69+
Create a new project directory and set up a virtual environment:
70+
71+
```bash
72+
mkdir account-creator-python
73+
cd account-creator-python
74+
python3 -m venv venv
75+
source venv/bin/activate # On Windows: venv\Scripts\activate
76+
```
77+
78+
Install the required package:
79+
80+
```bash
81+
pip install substrate-interface
82+
```
83+
84+
Create a file named `create_account.py`:
85+
86+
```python title="create_account.py"
87+
--8<-- 'code/chain-interactions/accounts/create-account/create_account.py'
88+
```
89+
90+
Key aspects of the code:
91+
92+
- **Mnemonic generation**: The `generate_mnemonic()` function creates a BIP39-compatible phrase
93+
- **Keypair creation**: `Keypair.create_from_mnemonic()` derives keys from the mnemonic
94+
95+
Execute the script:
96+
97+
```bash
98+
python create_account.py
99+
```
100+
101+
You should see output similar to:
102+
103+
--8<-- 'code/chain-interactions/accounts/create-account/create-account-py.html'
104+
105+
## Rust
106+
107+
Rust provides low-level access to Substrate primitives for account creation through the `sp-core` and `sp-keyring` crates.
108+
109+
Create a new Rust project:
110+
111+
```bash
112+
cargo new account-creator-rust
113+
cd account-creator-rust
114+
```
115+
116+
Add dependencies to your `Cargo.toml`:
117+
118+
```toml title="Cargo.toml"
119+
[package]
120+
name = "account-creator-rust"
121+
version = "0.1.0"
122+
edition = "2021"
123+
124+
[dependencies]
125+
sp-core = "28.0"
126+
sp-runtime = "31.0"
127+
```
128+
129+
Create your account generation code in `src/main.rs`:
130+
131+
```rust title="src/main.rs"
132+
--8<-- 'code/chain-interactions/accounts/create-account/create-account.rs'
133+
```
134+
135+
Key aspects of the code:
136+
137+
- **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
138+
- **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
139+
- **SS58 encoding**: Uses Polkadot's address format for the human-readable address
140+
141+
Build and run the project:
142+
143+
```bash
144+
cargo run
145+
```
146+
147+
You should see output similar to:
148+
149+
--8<-- 'code/chain-interactions/accounts/create-account/create-account-rs.html'
150+
151+
## Where to Go Next
152+
153+
Now that you can create accounts programmatically, explore related guides to fund accounts and send transactions.
154+
155+
<div class="grid cards" markdown>
156+
157+
- <span class="badge guide">Guide</span> __Send Transactions with SDKs__
158+
159+
---
160+
161+
Learn how to send signed transactions using your newly created accounts with Polkadot-API and Polkadot.js API libraries.
162+
163+
[:octicons-arrow-right-24: Get Started](/chain-interactions/send-transactions/with-sdks/)
164+
165+
- <span class="badge guide">Guide</span> __Calculate Transaction Fees__
166+
167+
---
168+
169+
Learn how to estimate transaction fees before sending transactions from your accounts.
170+
171+
[:octicons-arrow-right-24: Get Started](/chain-interactions/send-transactions/calculate-transaction-fees/)
172+
173+
- <span class="badge guide">Guide</span> __Query Chain Data__
174+
175+
---
176+
177+
Explore different methods for querying blockchain data including account balances and other chain state.
178+
179+
[:octicons-arrow-right-24: Get Started](/chain-interactions/query-data/query-sdks/)
180+
181+
</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)