forked from Joystream/joystream
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,28 @@ | ||
// @ts-check | ||
import { cryptoWaitReady, mnemonicGenerate } from '@polkadot/util-crypto' | ||
import { Keyring } from '@polkadot/keyring' | ||
|
||
async function main() { | ||
await cryptoWaitReady() | ||
|
||
// https://polkadot.js.org/docs/keyring/start/create | ||
const keyring = new Keyring({ | ||
type: 'sr25519', | ||
ss58Format: 126, | ||
}) | ||
|
||
// Generate a new mnemonic | ||
const mnemonic = mnemonicGenerate() | ||
|
||
const keyringPair = keyring.addFromMnemonic( | ||
mnemonic, | ||
{ | ||
name: 'User 1', | ||
}, | ||
'sr25519' | ||
) | ||
|
||
console.log(keyringPair.address) | ||
} | ||
|
||
main().catch(console.error) |
This file contains 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,38 @@ | ||
import { ApiPromise, WsProvider } from '@polkadot/api' | ||
import { cryptoWaitReady } from '@polkadot/util-crypto' | ||
import { Keyring } from '@polkadot/keyring' | ||
import { BN } from '@polkadot/util' | ||
|
||
async function main() { | ||
await cryptoWaitReady() | ||
|
||
// Initialise the provider to connect to the local node | ||
const provider = new WsProvider('wss://rpc.joystream.org') | ||
|
||
// Create the API and wait until ready | ||
const api = await ApiPromise.create({ provider }) | ||
|
||
const keyring = new Keyring({ type: 'sr25519', ss58Format: 126 }) | ||
|
||
const keyringPair = keyring.addFromMnemonic('your mnemonic phrase', {}, 'sr25519') | ||
|
||
// Create a transfer transaction | ||
const OneJoy = new BN(10000000000) | ||
const tx = api.tx.balances.transfer('j4W34M9gBApzi8Sj9nXSSHJmv3UHnGKecyTFjLGnmxtJDi98L', OneJoy) | ||
|
||
// Get the next account nonce | ||
const nonce = await api.rpc.system.accountNextIndex(keyringPair.address) | ||
|
||
// We do not need the provider anymore, close it. | ||
await api.disconnect() | ||
|
||
// Sign the transaction | ||
const signedTx = tx.sign(keyringPair, { nonce }) | ||
|
||
// Paste the hex here to decode it. | ||
// https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Frpc.joystream.org#/extrinsics/decode | ||
// hex is the signed transaction which can be submitted to the network | ||
console.log(signedTx.toHex()) | ||
} | ||
|
||
main().catch(console.error) |