From 48008e2cc8a613ee7ae2ac7928a92fdd2d9642d8 Mon Sep 17 00:00:00 2001 From: Luke Schoen Date: Wed, 14 Nov 2018 13:26:53 +0100 Subject: [PATCH] refactor: Extension of #231. Destructuring to combine imports (#237) * refactor: Destructure imports together from same source in example util-crypto 01 * refactor: Destructure imports together example util-crypto 02. Avoid using default import. Show data to user * refactor: Destructure imports together example util-crypto 03. Show data to user * refactor: Split import over two lines looks cleaner * fix: Address review comments. Update dependencies in examples --- docs/examples/keyring/01_create_account/package.json | 4 ++-- docs/examples/keyring/02_load_accounts/index.js | 2 +- docs/examples/keyring/02_load_accounts/package.json | 4 ++-- .../trie-db/01_create_patricia_trie/package.json | 4 ++-- .../trie-db/02_get_set_del_trie_nodes/package.json | 4 ++-- .../03_duplicate_trie_with_snapshot/package.json | 4 ++-- .../trie-db/04_show_size_trie_node_info/package.json | 4 ++-- .../trie-db/05_empty_close_drop_trie/package.json | 4 ++-- .../01_encrypt_decrypt_message_nacl/index.js | 3 +-- .../01_encrypt_decrypt_message_nacl/package.json | 2 +- .../util-crypto/02_sign_verify_message_nacl/index.js | 11 ++++------- .../02_sign_verify_message_nacl/package.json | 2 +- .../03_mnemonic_generate_validate_bip39/index.js | 8 +++----- .../03_mnemonic_generate_validate_bip39/package.json | 2 +- 14 files changed, 26 insertions(+), 32 deletions(-) diff --git a/docs/examples/keyring/01_create_account/package.json b/docs/examples/keyring/01_create_account/package.json index 6e485ff23..ec676826c 100644 --- a/docs/examples/keyring/01_create_account/package.json +++ b/docs/examples/keyring/01_create_account/package.json @@ -10,8 +10,8 @@ "start": "node index.js" }, "dependencies": { - "@polkadot/util": "^0.32.2", - "@polkadot/keyring": "^0.32.1" + "@polkadot/util": "^0.32.10", + "@polkadot/keyring": "^0.32.10" }, "devDependencies": { "rimraf": "^2.6.2" diff --git a/docs/examples/keyring/02_load_accounts/index.js b/docs/examples/keyring/02_load_accounts/index.js index 04acdbb6f..664321640 100755 --- a/docs/examples/keyring/02_load_accounts/index.js +++ b/docs/examples/keyring/02_load_accounts/index.js @@ -23,7 +23,7 @@ async function main () { .forEach((pair, index) => { const address = pair.address(); - console.log(`${index} account has json: `, + console.log(`\nAccount with index #${index} has json: `, JSON.stringify(keyring.toJson(address), null, 2)); }); } diff --git a/docs/examples/keyring/02_load_accounts/package.json b/docs/examples/keyring/02_load_accounts/package.json index f334a8dc4..2d2eeccef 100644 --- a/docs/examples/keyring/02_load_accounts/package.json +++ b/docs/examples/keyring/02_load_accounts/package.json @@ -10,8 +10,8 @@ "start": "node index.js" }, "dependencies": { - "@polkadot/util": "^0.32.2", - "@polkadot/keyring": "^0.32.1" + "@polkadot/util": "^0.32.10", + "@polkadot/keyring": "^0.32.10" }, "devDependencies": { "rimraf": "^2.6.2" diff --git a/docs/examples/trie-db/01_create_patricia_trie/package.json b/docs/examples/trie-db/01_create_patricia_trie/package.json index 17952a2b6..ae0e0a187 100644 --- a/docs/examples/trie-db/01_create_patricia_trie/package.json +++ b/docs/examples/trie-db/01_create_patricia_trie/package.json @@ -10,8 +10,8 @@ "start": "node index.js" }, "dependencies": { - "@polkadot/trie-db": "^0.32.1", - "@polkadot/db": "^0.32.2" + "@polkadot/trie-db": "^0.32.10", + "@polkadot/db": "^0.32.10" }, "devDependencies": { "rimraf": "^2.6.2" diff --git a/docs/examples/trie-db/02_get_set_del_trie_nodes/package.json b/docs/examples/trie-db/02_get_set_del_trie_nodes/package.json index 68f0c8f27..65034a3a7 100644 --- a/docs/examples/trie-db/02_get_set_del_trie_nodes/package.json +++ b/docs/examples/trie-db/02_get_set_del_trie_nodes/package.json @@ -10,8 +10,8 @@ "start": "node index.js" }, "dependencies": { - "@polkadot/trie-db": "^0.32.1", - "@polkadot/db": "^0.32.2" + "@polkadot/trie-db": "^0.32.10", + "@polkadot/db": "^0.32.10" }, "devDependencies": { "rimraf": "^2.6.2" diff --git a/docs/examples/trie-db/03_duplicate_trie_with_snapshot/package.json b/docs/examples/trie-db/03_duplicate_trie_with_snapshot/package.json index f17309761..dcff9ff96 100644 --- a/docs/examples/trie-db/03_duplicate_trie_with_snapshot/package.json +++ b/docs/examples/trie-db/03_duplicate_trie_with_snapshot/package.json @@ -10,8 +10,8 @@ "start": "node index.js" }, "dependencies": { - "@polkadot/trie-db": "^0.32.1", - "@polkadot/db": "^0.32.2" + "@polkadot/trie-db": "^0.32.10", + "@polkadot/db": "^0.32.10" }, "devDependencies": { "rimraf": "^2.6.2" diff --git a/docs/examples/trie-db/04_show_size_trie_node_info/package.json b/docs/examples/trie-db/04_show_size_trie_node_info/package.json index 6115f5635..00486e921 100644 --- a/docs/examples/trie-db/04_show_size_trie_node_info/package.json +++ b/docs/examples/trie-db/04_show_size_trie_node_info/package.json @@ -10,8 +10,8 @@ "start": "node index.js" }, "dependencies": { - "@polkadot/trie-db": "^0.32.1", - "@polkadot/db": "^0.32.2" + "@polkadot/trie-db": "^0.32.10", + "@polkadot/db": "^0.32.10" }, "devDependencies": { "rimraf": "^2.6.2" diff --git a/docs/examples/trie-db/05_empty_close_drop_trie/package.json b/docs/examples/trie-db/05_empty_close_drop_trie/package.json index b36ca1086..f53c6f0d0 100644 --- a/docs/examples/trie-db/05_empty_close_drop_trie/package.json +++ b/docs/examples/trie-db/05_empty_close_drop_trie/package.json @@ -10,8 +10,8 @@ "start": "node index.js" }, "dependencies": { - "@polkadot/trie-db": "^0.32.1", - "@polkadot/db": "^0.32.2" + "@polkadot/trie-db": "^0.32.10", + "@polkadot/db": "^0.32.10" }, "devDependencies": { "rimraf": "^2.6.2" diff --git a/docs/examples/util-crypto/01_encrypt_decrypt_message_nacl/index.js b/docs/examples/util-crypto/01_encrypt_decrypt_message_nacl/index.js index 13afb75e6..e6606e7e2 100755 --- a/docs/examples/util-crypto/01_encrypt_decrypt_message_nacl/index.js +++ b/docs/examples/util-crypto/01_encrypt_decrypt_message_nacl/index.js @@ -1,5 +1,4 @@ -const { naclEncrypt } = require('@polkadot/util-crypto'); -const { naclDecrypt } = require('@polkadot/util-crypto'); +const { naclEncrypt, naclDecrypt } = require('@polkadot/util-crypto'); async function main () { const secret = new Uint8Array(32); diff --git a/docs/examples/util-crypto/01_encrypt_decrypt_message_nacl/package.json b/docs/examples/util-crypto/01_encrypt_decrypt_message_nacl/package.json index 146a5c14a..e77697e65 100644 --- a/docs/examples/util-crypto/01_encrypt_decrypt_message_nacl/package.json +++ b/docs/examples/util-crypto/01_encrypt_decrypt_message_nacl/package.json @@ -10,7 +10,7 @@ "start": "node index.js" }, "dependencies": { - "@polkadot/util-crypto": "^0.32.2" + "@polkadot/util-crypto": "^0.32.10" }, "devDependencies": { "rimraf": "^2.6.2" diff --git a/docs/examples/util-crypto/02_sign_verify_message_nacl/index.js b/docs/examples/util-crypto/02_sign_verify_message_nacl/index.js index fb5763ed2..a12c67c87 100755 --- a/docs/examples/util-crypto/02_sign_verify_message_nacl/index.js +++ b/docs/examples/util-crypto/02_sign_verify_message_nacl/index.js @@ -1,10 +1,5 @@ -const stringToU8a = require('@polkadot/util/string/toU8a').default; -const { randomAsU8a } = require('@polkadot/util-crypto'); - -const { naclKeypairFromSeed } = require('@polkadot/util-crypto'); -const { naclEncrypt } = require('@polkadot/util-crypto'); -const { naclSign } = require('@polkadot/util-crypto'); -const { naclVerify } = require('@polkadot/util-crypto'); +const { stringToU8a, u8aToHex } = require('@polkadot/util'); +const { naclEncrypt, naclKeypairFromSeed, naclSign, naclVerify, randomAsU8a } = require('@polkadot/util-crypto'); async function main () { // Create account seed for Alice as fallback if generated mnemonic not valid @@ -23,6 +18,8 @@ async function main () { // Sign the message with a valid signature const messageSignature = naclSign(encrypted, secretKey); + console.log(`Message signature: ${u8aToHex(messageSignature)}`); + // Validate that the message was correctly signed const isValidSignature = naclVerify(encrypted, messageSignature, publicKey); diff --git a/docs/examples/util-crypto/02_sign_verify_message_nacl/package.json b/docs/examples/util-crypto/02_sign_verify_message_nacl/package.json index 67e7bbc33..956608339 100644 --- a/docs/examples/util-crypto/02_sign_verify_message_nacl/package.json +++ b/docs/examples/util-crypto/02_sign_verify_message_nacl/package.json @@ -10,7 +10,7 @@ "start": "node index.js" }, "dependencies": { - "@polkadot/util-crypto": "^0.32.2" + "@polkadot/util-crypto": "^0.32.10" }, "devDependencies": { "rimraf": "^2.6.2" diff --git a/docs/examples/util-crypto/03_mnemonic_generate_validate_bip39/index.js b/docs/examples/util-crypto/03_mnemonic_generate_validate_bip39/index.js index 57cb9a411..332db61f4 100755 --- a/docs/examples/util-crypto/03_mnemonic_generate_validate_bip39/index.js +++ b/docs/examples/util-crypto/03_mnemonic_generate_validate_bip39/index.js @@ -1,13 +1,11 @@ -const { mnemonicGenerate } = require('@polkadot/util-crypto'); -const { mnemonicToSeed } = require('@polkadot/util-crypto'); -const { mnemonicValidate } = require('@polkadot/util-crypto'); -const { toSecret } = require('@polkadot/util-crypto'); -const { naclKeypairFromSeed } = require('@polkadot/util-crypto'); +const { mnemonicGenerate, mnemonicToSeed, mnemonicValidate, naclKeypairFromSeed } = require('@polkadot/util-crypto'); async function main () { // Create mnemonic string for Alice using BIP39 const mnemonicAlice = mnemonicGenerate(); + console.log(`Generated mnemonic: ${mnemonicAlice}`); + // Validate the mnemic string that was generated const isValidMnemonic = mnemonicValidate(mnemonicAlice); diff --git a/docs/examples/util-crypto/03_mnemonic_generate_validate_bip39/package.json b/docs/examples/util-crypto/03_mnemonic_generate_validate_bip39/package.json index 67e7bbc33..956608339 100644 --- a/docs/examples/util-crypto/03_mnemonic_generate_validate_bip39/package.json +++ b/docs/examples/util-crypto/03_mnemonic_generate_validate_bip39/package.json @@ -10,7 +10,7 @@ "start": "node index.js" }, "dependencies": { - "@polkadot/util-crypto": "^0.32.2" + "@polkadot/util-crypto": "^0.32.10" }, "devDependencies": { "rimraf": "^2.6.2"