From ffe1c71172755374ba82bcc30216bae41da4a9c1 Mon Sep 17 00:00:00 2001 From: JesseAbram <33698952+JesseAbram@users.noreply.github.com> Date: Mon, 1 Jul 2019 09:09:14 -0400 Subject: [PATCH] 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 --- .../README.md | 5 +++ .../index.js | 45 +++++++++++++++++++ .../package.json | 19 ++++++++ 3 files changed, 69 insertions(+) create mode 100644 docs/examples/promise/07_make_transfer_with_allowed_block_permissions_only/README.md create mode 100644 docs/examples/promise/07_make_transfer_with_allowed_block_permissions_only/index.js create mode 100644 docs/examples/promise/07_make_transfer_with_allowed_block_permissions_only/package.json diff --git a/docs/examples/promise/07_make_transfer_with_allowed_block_permissions_only/README.md b/docs/examples/promise/07_make_transfer_with_allowed_block_permissions_only/README.md new file mode 100644 index 0000000000..0ffdd5e752 --- /dev/null +++ b/docs/examples/promise/07_make_transfer_with_allowed_block_permissions_only/README.md @@ -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. diff --git a/docs/examples/promise/07_make_transfer_with_allowed_block_permissions_only/index.js b/docs/examples/promise/07_make_transfer_with_allowed_block_permissions_only/index.js new file mode 100644 index 0000000000..3170939941 --- /dev/null +++ b/docs/examples/promise/07_make_transfer_with_allowed_block_permissions_only/index.js @@ -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()); diff --git a/docs/examples/promise/07_make_transfer_with_allowed_block_permissions_only/package.json b/docs/examples/promise/07_make_transfer_with_allowed_block_permissions_only/package.json new file mode 100644 index 0000000000..7da13b42a3 --- /dev/null +++ b/docs/examples/promise/07_make_transfer_with_allowed_block_permissions_only/package.json @@ -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" + } +}