added 07 Example showing how to make a transfer with allowed block permissions folder in docs (#1062)

* added 07 folder in docs

* spelling and formatting
This commit is contained in:
JesseAbram
2019-07-01 15:09:14 +02:00
committed by Amaury Martiny
parent 9763883393
commit ffe1c71172
3 changed files with 69 additions and 0 deletions
@@ -0,0 +1,5 @@
# Make a Transfer with allowed block permissions
This transactions shows you how to add a nonce and a valid length of blocks a transaction is valid for.
This is important because as an account drops below the exsistential limit (0.1 dots) it gets pruned from the state tree. If it is added back its nonce starts at 0 again and is now vulnerable to replay attacks.
@@ -0,0 +1,45 @@
// @ts-check
// Import the API, Keyring and some utility functions
const { ApiPromise } = require('@polkadot/api');
const { Keyring } = require('@polkadot/keyring');
const { ExtrinsicEra } = require('@polkadot/types/type');
const BOB = '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty';
async function main () {
// Instantiate the API
const api = await ApiPromise.create(); //remove provider
// Constuct the keying after the API (crypto has an async init)
const keyring = new Keyring({ type: 'sr25519' });
// Add alice to our keyring with a hard-derived path (empty phrase, so uses dev)
const alice = keyring.addFromUri('//Alice');
// Get nonce for account
const nonce = await api.query.system.accountNonce(alice.address);
// get current block
const signedBlock = await api.rpc.chain.getBlock();
// get current block height
const currentHeight = signedBlock.block.header.number;
const exERA = new ExtrinsicEra({ current: currentHeight, period: 10 });
// get the beggining of the current era
const eraBirth = exERA.asMortalEra.birth(currentHeight.toNumber());
// get the hash of the beginning of the current era
const eraHash = await api.rpc.chain.getBlockHash(eraBirth);
// Create an extrinsic, transferring 12345 units to Bob
const transfer = api.tx.balances.transfer(BOB, 12345);
// Sign and send the transaction using our account with a nonce and the length of blocks the transaction is valid for
const hash = await transfer.signAndSend(alice, { blockHash: eraHash, era: exERA, nonce });
console.log('Transfer sent with hash', hash.toHex());
}
main().catch(console.error).finally(() => process.exit());
@@ -0,0 +1,19 @@
{
"name": "07_make_transfer_with_allowed_block_permissions_only",
"version": "0.1.0",
"description": "Example showing how to make a transfer with allowed block permissions",
"main": "index.js",
"author": "Jesse",
"license": "MIT",
"scripts": {
"clean": "rimraf node_modules",
"start": "node index.js"
},
"dependencies": {
"@polkadot/api": "0.80.1",
"@polkadot/keyring": "^0.93.1"
},
"devDependencies": {
"rimraf": "^2.6.2"
}
}