-
Notifications
You must be signed in to change notification settings - Fork 77
Add create accounts (new IA) #1291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nhussein11
wants to merge
1
commit into
staging/product-ia
Choose a base branch
from
nhussein11/add-create-accounts
base: staging/product-ia
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,181 @@ | ||
| TODO | ||
| --- | ||
| title: Create an Account | ||
| description: Step-by-step guide to creating Polkadot accounts using different programming languages and libraries, including JavaScript, Python, and Rust examples. | ||
| categories: Basics, Accounts, Developer Tools | ||
| --- | ||
|
|
||
| # Create an Account | ||
|
|
||
| ## Introduction | ||
|
|
||
| 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. | ||
|
|
||
| This tutorial will guide you through creating accounts using different programming languages and libraries. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| Before starting, make sure you have: | ||
|
|
||
| - Basic understanding of public-key cryptography concepts | ||
| - Development environment set up for your chosen language | ||
| - Familiarity with the programming language you'll be using | ||
|
|
||
| !!! note | ||
| 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. | ||
|
|
||
| ## JavaScript/TypeScript | ||
|
|
||
| Create a new project directory and initialize it: | ||
|
|
||
| ```bash | ||
| mkdir account-creator | ||
| cd account-creator | ||
| npm init -y | ||
| ``` | ||
|
|
||
| Install the required packages: | ||
|
|
||
| ```bash | ||
| npm install @polkadot/util-crypto @polkadot/keyring | ||
| npm install --save-dev typescript tsx | ||
| ``` | ||
|
|
||
| Create a file named `create-account.ts`: | ||
|
|
||
| ```typescript title="create-account.ts" | ||
| --8<-- 'code/chain-interactions/accounts/create-account/create-account.ts' | ||
| ``` | ||
|
|
||
| Key aspects of the code: | ||
|
|
||
| - **Mnemonic generation**: Uses `mnemonicGenerate()` to create a 12-word BIP39 mnemonic phrase for human-readable key backup | ||
| - **Keyring**: The `Keyring` class manages accounts with specified signature scheme and address format | ||
| - **SS58 format**: Setting `ss58Format: 0` configures addresses for the Polkadot relay chain | ||
|
|
||
| Execute the script using `tsx`: | ||
|
|
||
| ```bash | ||
| npx tsx create-account.ts | ||
| ``` | ||
|
|
||
| You should see output similar to: | ||
|
|
||
| --8<-- 'code/chain-interactions/accounts/create-account/create-account-ts.html' | ||
|
|
||
| ## Python | ||
|
|
||
| Python developers can use the `substrate-interface` library to create and manage Polkadot accounts. | ||
|
|
||
| Create a new project directory and set up a virtual environment: | ||
|
|
||
| ```bash | ||
| mkdir account-creator-python | ||
| cd account-creator-python | ||
| python3 -m venv venv | ||
| source venv/bin/activate # On Windows: venv\Scripts\activate | ||
| ``` | ||
|
|
||
| Install the required package: | ||
|
|
||
| ```bash | ||
| pip install substrate-interface | ||
| ``` | ||
|
|
||
| Create a file named `create_account.py`: | ||
|
|
||
| ```python title="create_account.py" | ||
| --8<-- 'code/chain-interactions/accounts/create-account/create_account.py' | ||
| ``` | ||
|
|
||
| Key aspects of the code: | ||
|
|
||
| - **Mnemonic generation**: The `generate_mnemonic()` function creates a BIP39-compatible phrase | ||
| - **Keypair creation**: `Keypair.create_from_mnemonic()` derives keys from the mnemonic | ||
nhussein11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Execute the script: | ||
|
|
||
| ```bash | ||
| python create_account.py | ||
| ``` | ||
|
|
||
| You should see output similar to: | ||
|
|
||
| --8<-- 'code/chain-interactions/accounts/create-account/create-account-py.html' | ||
|
|
||
| ## Rust | ||
|
|
||
| Rust provides low-level access to Substrate primitives for account creation through the `sp-core` and `sp-keyring` crates. | ||
|
|
||
| Create a new Rust project: | ||
|
|
||
| ```bash | ||
| cargo new account-creator-rust | ||
| cd account-creator-rust | ||
| ``` | ||
|
|
||
| Add dependencies to your `Cargo.toml`: | ||
|
|
||
| ```toml title="Cargo.toml" | ||
| [package] | ||
| name = "account-creator-rust" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
| [dependencies] | ||
| sp-core = "28.0" | ||
| sp-runtime = "31.0" | ||
nhussein11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| Create your account generation code in `src/main.rs`: | ||
|
|
||
| ```rust title="src/main.rs" | ||
| --8<-- 'code/chain-interactions/accounts/create-account/create-account.rs' | ||
| ``` | ||
|
|
||
| Key aspects of the code: | ||
|
|
||
| - **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 | ||
| - **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 | ||
| - **SS58 encoding**: Uses Polkadot's address format for the human-readable address | ||
nhussein11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Build and run the project: | ||
|
|
||
| ```bash | ||
| cargo run | ||
| ``` | ||
|
|
||
| You should see output similar to: | ||
|
|
||
| --8<-- 'code/chain-interactions/accounts/create-account/create-account-rs.html' | ||
|
|
||
| ## Where to Go Next | ||
|
|
||
| Now that you can create accounts programmatically, explore related guides to fund accounts and send transactions. | ||
|
|
||
| <div class="grid cards" markdown> | ||
|
|
||
| - <span class="badge guide">Guide</span> __Send Transactions with SDKs__ | ||
|
|
||
| --- | ||
|
|
||
| Learn how to send signed transactions using your newly created accounts with Polkadot-API and Polkadot.js API libraries. | ||
|
|
||
| [:octicons-arrow-right-24: Get Started](/chain-interactions/send-transactions/with-sdks/) | ||
|
|
||
| - <span class="badge guide">Guide</span> __Calculate Transaction Fees__ | ||
|
|
||
| --- | ||
|
|
||
| Learn how to estimate transaction fees before sending transactions from your accounts. | ||
|
|
||
| [:octicons-arrow-right-24: Get Started](/chain-interactions/send-transactions/calculate-transaction-fees/) | ||
|
|
||
| - <span class="badge guide">Guide</span> __Query Chain Data__ | ||
|
|
||
| --- | ||
|
|
||
| Explore different methods for querying blockchain data including account balances and other chain state. | ||
|
|
||
| [:octicons-arrow-right-24: Get Started](/chain-interactions/query-data/query-sdks/) | ||
|
|
||
| </div> | ||
5 changes: 5 additions & 0 deletions
5
.snippets/code/chain-interactions/accounts/create-account/create-account-py.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <div class="termynal" data-termynal> | ||
| <span data-ty="input"><span class="file-path"></span>python create_account.py</span> | ||
| <span data-ty>Address: 15oF4uVJwmo4TdGW7VfQxNLavjCXviqxT9S1MgbjMNHr6Sp5</span> | ||
| <span data-ty>Mnemonic: cushion dog echo people vendor curve truck begin latin romance rebuild ...</span> | ||
| </div> |
5 changes: 5 additions & 0 deletions
5
.snippets/code/chain-interactions/accounts/create-account/create-account-rs.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <div class="termynal" data-termynal> | ||
| <span data-ty="input"><span class="file-path"></span>cargo run</span> | ||
| <span data-ty>Address: 15oF4uVJwmo4TdGW7VfQxNLavjCXviqxT9S1MgbjMNHr6Sp5</span> | ||
| <span data-ty>Mnemonic: cushion dog echo people vendor curve truck begin latin romance rebuild ...</span> | ||
| </div> |
6 changes: 6 additions & 0 deletions
6
.snippets/code/chain-interactions/accounts/create-account/create-account-ts.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <div class="termynal" data-termynal> | ||
| <span data-ty="input"><span class="file-path"></span>npx tsx create-account.ts</span> | ||
| <span data-ty="progress"></span> | ||
| <span data-ty>Address: 15oF4uVJwmo4TdGW7VfQxNLavjCXviqxT9S1MgbjMNHr6Sp5</span> | ||
| <span data-ty>Mnemonic: cushion dog echo people vendor curve truck begin latin romance rebuild ...</span> | ||
| </div> |
9 changes: 9 additions & 0 deletions
9
.snippets/code/chain-interactions/accounts/create-account/create-account.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| use sp_core::{crypto::Ss58Codec, Pair}; | ||
|
|
||
| fn main() { | ||
| let (pair, phrase, _) = sp_core::sr25519::Pair::generate_with_phrase(None); | ||
| let address = pair.public().to_ss58check(); | ||
|
|
||
| println!("Address: {}", address); | ||
nhussein11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| println!("Mnemonic: {}", phrase); | ||
| } | ||
15 changes: 15 additions & 0 deletions
15
.snippets/code/chain-interactions/accounts/create-account/create-account.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { cryptoWaitReady, mnemonicGenerate } from "@polkadot/util-crypto"; | ||
| import { Keyring } from "@polkadot/keyring"; | ||
|
|
||
| async function main() { | ||
| await cryptoWaitReady(); | ||
|
|
||
| const mnemonic = mnemonicGenerate(12); | ||
| const keyring = new Keyring({ type: "sr25519", ss58Format: 0 }); | ||
| const pair = keyring.addFromMnemonic(mnemonic); | ||
|
|
||
| console.log(`Address: ${pair.address}`); | ||
nhussein11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| console.log(`Mnemonic: ${mnemonic}`); | ||
| } | ||
|
|
||
| main().catch(console.error); | ||
7 changes: 7 additions & 0 deletions
7
.snippets/code/chain-interactions/accounts/create-account/create_account.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| from substrateinterface import Keypair | ||
|
|
||
| mnemonic = Keypair.generate_mnemonic() | ||
| keypair = Keypair.create_from_mnemonic(mnemonic) | ||
nhussein11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| print(f"Address: {keypair.ss58_address}") | ||
nhussein11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| print(f"Mnemonic: {mnemonic}") | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.