Swap to using eslint (#435)
* WIP * ... and more * yet more... * and examples (more-or-less) fixed * Only problematic promisify left... * Last batch * rm config header * Update * Use Record * Bumps
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
/* eslint-disable @typescript-eslint/no-var-requires */
|
||||
// Import Keyring class and utility function
|
||||
const Keyring = require('@polkadot/keyring').default;
|
||||
const stringToU8a = require('@polkadot/util/string/toU8a').default;
|
||||
@@ -12,8 +13,7 @@ async function main () {
|
||||
// Create pair and add Alice to keyring pair dictionary (with account seed)
|
||||
const pairAlice = keyring.addFromSeed(stringToU8a(ALICE_SEED));
|
||||
|
||||
console.log(`Created keyring pair for Alice with address: ` +
|
||||
`${keyring.getPair(pairAlice.address).address}`);
|
||||
console.log(`Created keyring pair for Alice with address: ${keyring.getPair(pairAlice.address).address}`);
|
||||
}
|
||||
|
||||
main().catch(console.error).finally(_ => process.exit());
|
||||
main().catch(console.error).finally(() => process.exit());
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
/* eslint-disable @typescript-eslint/no-var-requires */
|
||||
// Import Keyring class and utility function
|
||||
const Keyring = require('@polkadot/keyring').default;
|
||||
const stringToU8a = require('@polkadot/util/string/toU8a').default;
|
||||
@@ -23,9 +24,8 @@ async function main () {
|
||||
.forEach((pair, index) => {
|
||||
const { address } = pair;
|
||||
|
||||
console.log(`\nAccount with index #${index} has json: `,
|
||||
JSON.stringify(keyring.toJson(address), null, 2));
|
||||
console.log(`\nAccount with index #${index} has json: `, JSON.stringify(keyring.toJson(address), null, 2));
|
||||
});
|
||||
}
|
||||
|
||||
main().catch(console.error).finally(_ => process.exit());
|
||||
main().catch(console.error).finally(() => process.exit());
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
/* eslint-disable @typescript-eslint/no-var-requires */
|
||||
const { encodeAddress, decodeAddress } = require('@polkadot/keyring');
|
||||
const { isHex, hexToU8a } = require('@polkadot/util');
|
||||
|
||||
@@ -13,4 +14,5 @@ const isValidAddressPolkadotAddress = () => {
|
||||
};
|
||||
|
||||
const isValid = isValidAddressPolkadotAddress();
|
||||
|
||||
console.log(isValid);
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
# Create Patricia Trie
|
||||
|
||||
The following example shows how to create a Patricia Trie.
|
||||
|
||||
[include](index.js)
|
||||
@@ -1,13 +0,0 @@
|
||||
const Trie = require('@polkadot/trie-db').default;
|
||||
|
||||
async function main () {
|
||||
const trie = new Trie();
|
||||
|
||||
// Open the database for each of the trie instances
|
||||
trie.open();
|
||||
|
||||
// Initial root value is valid
|
||||
console.log(`Initial root Uint8Array value: ${trie.getRoot()}`);
|
||||
}
|
||||
|
||||
main().catch(console.error).finally(_ => process.exit());
|
||||
@@ -1,19 +0,0 @@
|
||||
{
|
||||
"name": "01_create_patricia_trie",
|
||||
"version": "0.1.0",
|
||||
"description": "Example showing how to create a Patricia Trie",
|
||||
"main": "index.js",
|
||||
"author": "ltfschoen",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"clean": "rimraf node_modules",
|
||||
"start": "node index.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@polkadot/trie-db": "^0.32.10",
|
||||
"@polkadot/db": "^0.32.10"
|
||||
},
|
||||
"devDependencies": {
|
||||
"rimraf": "^2.6.2"
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
# Get & Set & Delete Trie Nodes
|
||||
|
||||
The following example shows how to get, set, and delete nodes of a Patricia Trie.
|
||||
|
||||
[include](index.js)
|
||||
@@ -1,48 +0,0 @@
|
||||
const toU8a = require('@polkadot/util/u8a/toU8a').default;
|
||||
const Trie = require('@polkadot/trie-db').default;
|
||||
|
||||
async function main () {
|
||||
const trie = new Trie();
|
||||
|
||||
// Open the database for each of the trie instances
|
||||
trie.open();
|
||||
|
||||
// Initial root value is valid
|
||||
console.log(`Initial root Uint8Array value: ${trie.getRoot()}`);
|
||||
|
||||
// Create a branch under the trie root with a key of 'test' and a value of 'one'
|
||||
trie.put(toU8a('test'), toU8a('one'));
|
||||
|
||||
// Store the root hash of the trie root node in a variable
|
||||
const trieRootNodeHash = trie.getRoot();
|
||||
|
||||
// Store the root node object in a variable
|
||||
const trieRootNode = trie.getNode(trieRootNodeHash);
|
||||
|
||||
// Show the node object of the root node hash
|
||||
console.log(`Root node object: ${trieRootNode}`);
|
||||
|
||||
// Show the updated root hash value of the trie root node
|
||||
console.log(`Updated root hash value: ${trie.getRoot()}`);
|
||||
|
||||
const branch = trie.get(toU8a('test'));
|
||||
// Retrieve the value of a given key named 'test' in the trie
|
||||
console.log(`Current value of key 'test': ${branch}`);
|
||||
|
||||
// Deletes the value of a given key named 'test' in the trie branch
|
||||
trie.del(toU8a('test'));
|
||||
|
||||
// Retrieve the value of a given key named 'test' in the trie
|
||||
console.log(`Updated value of key 'test' after deleting it: ${trie.get(toU8a('test'))}`);
|
||||
|
||||
// Set the root hash value of the trie root node (if necessary)
|
||||
trie.setRoot(trieRootNodeHash);
|
||||
|
||||
// Show the node object of the root node hash
|
||||
console.log(`Root node object: ${trie.getRoot()}`);
|
||||
|
||||
// Retrieve the value of a given key named 'test' in the trie
|
||||
console.log(`Restored value of key 'test' after restoring root node: ${branch}`);
|
||||
}
|
||||
|
||||
main().catch(console.error).finally(_ => process.exit());
|
||||
@@ -1,19 +0,0 @@
|
||||
{
|
||||
"name": "02_get_set_del_trie_nodes",
|
||||
"version": "0.1.0",
|
||||
"description": "Example showing how to get, set, and delete nodes of a Patricia Trie",
|
||||
"main": "index.js",
|
||||
"author": "ltfschoen",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"clean": "rimraf node_modules",
|
||||
"start": "node index.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@polkadot/trie-db": "^0.32.10",
|
||||
"@polkadot/db": "^0.32.10"
|
||||
},
|
||||
"devDependencies": {
|
||||
"rimraf": "^2.6.2"
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
# Duplicate Patricia Trie with Snapshot
|
||||
|
||||
The following example shows how to duplicate a Patricia Trie by taking a snapshot.
|
||||
|
||||
[include](index.js)
|
||||
@@ -1,24 +0,0 @@
|
||||
const Trie = require('@polkadot/trie-db').default;
|
||||
|
||||
async function main () {
|
||||
const trie = new Trie();
|
||||
|
||||
// Use `back` to later store a snapshot of the `trie`
|
||||
const back = new Trie();
|
||||
|
||||
// Open the database for each of the trie instances
|
||||
trie.open();
|
||||
back.open();
|
||||
|
||||
// Initial root value is valid
|
||||
console.log(`Initial root Uint8Array value: ${trie.getRoot()}`);
|
||||
|
||||
// Create a snapshot of the `trie` data into `back`
|
||||
trie.snapshot(back, () => {});
|
||||
|
||||
// Show value of snapshot's root is same as original trie's root
|
||||
console.log(`Snapshot root value matches original trie root: ` +
|
||||
`${back.getRoot() === trie.getRoot()}`);
|
||||
}
|
||||
|
||||
main().catch(console.error).finally(_ => process.exit());
|
||||
@@ -1,19 +0,0 @@
|
||||
{
|
||||
"name": "03_duplicate_trie_with_snapshot",
|
||||
"version": "0.1.0",
|
||||
"description": "Example showing how to duplicate Patricia Trie nodes with a snapshot",
|
||||
"main": "index.js",
|
||||
"author": "ltfschoen",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"clean": "rimraf node_modules",
|
||||
"start": "node index.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@polkadot/trie-db": "^0.32.10",
|
||||
"@polkadot/db": "^0.32.10"
|
||||
},
|
||||
"devDependencies": {
|
||||
"rimraf": "^2.6.2"
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
# Show Size and Node Info of a Patricia Trie
|
||||
|
||||
The following example shows how to show the size and node information of a Patricia Trie.
|
||||
|
||||
[include](index.js)
|
||||
@@ -1,65 +0,0 @@
|
||||
const toU8a = require('@polkadot/util/u8a/toU8a').default;
|
||||
const Trie = require('@polkadot/trie-db').default;
|
||||
const {
|
||||
isEmptyNode,
|
||||
isKvNode,
|
||||
isExtensionNode,
|
||||
isLeafNode,
|
||||
isBranchNode
|
||||
} = require('@polkadot/trie-db/util/is');
|
||||
|
||||
async function main () {
|
||||
const trie = new Trie();
|
||||
|
||||
// Use `back` to later store a snapshot of the `trie`
|
||||
const back = new Trie();
|
||||
|
||||
// Open the database for each of the trie instances
|
||||
trie.open();
|
||||
back.open();
|
||||
|
||||
// Initial root value is valid
|
||||
console.log(`Initial root Uint8Array value: ${trie.getRoot()}`);
|
||||
|
||||
// Create two branches under the trie root with
|
||||
// key 'branch1' and 'branch2' and a values of 'one' and 'two'
|
||||
trie.put(toU8a('test'), toU8a('one'));
|
||||
trie.put(toU8a('test'), toU8a('two'));
|
||||
|
||||
// Store the root hash of the trie root node in a variable
|
||||
const trieRootNodeHash = trie.getRoot();
|
||||
|
||||
// Store the root node object in a variable
|
||||
const trieRootNode = trie.getNode(trieRootNodeHash);
|
||||
|
||||
// Show the node object of the root node hash
|
||||
console.log(`Root node object: ${trieRootNode}`);
|
||||
|
||||
// Show information about the trie node
|
||||
console.log(`Is root node object empty? : ${isEmptyNode(trieRootNode)}`);
|
||||
console.log(`Is root node object a KV node? : ${isKvNode(trieRootNode)}`);
|
||||
|
||||
// Show information about the trie that is Merkle Patricia Trie specific
|
||||
// Reference: https://github.com/ethereum/wiki/wiki/Patricia-Tree#optimization
|
||||
console.log(`Is root node object an Extension node? : ${isExtensionNode(trieRootNode)}`);
|
||||
console.log(`Is root node object an Leaf node? : ${isLeafNode(trieRootNode)}`);
|
||||
console.log(`Is root node object an Branch node? : ${isBranchNode(trieRootNode)}`);
|
||||
|
||||
// Retrieve the value of a given key named 'test' in the trie
|
||||
console.log(`Current value of key 'test': ${trie.get(toU8a('test'))}`);
|
||||
|
||||
// Create a snapshot of the `trie` data into `back`
|
||||
trie.snapshot(back, () => {});
|
||||
|
||||
// Show that value of the snapshot's root is the same as the original trie's root
|
||||
console.log(`Snapshot root value matches original trie root: ${back.getRoot() === trie.getRoot()}`);
|
||||
|
||||
// Deletes the value of a given key named 'test' in the trie branch
|
||||
trie.del(toU8a('test'));
|
||||
|
||||
// Show size of the database for each of the trie instances
|
||||
console.log(`Size of 'trie' database: ${trie.size()}`);
|
||||
console.log(`Size of 'back' database: ${back.size()}`);
|
||||
}
|
||||
|
||||
main().catch(console.error).finally(_ => process.exit());
|
||||
@@ -1,19 +0,0 @@
|
||||
{
|
||||
"name": "04_show_size_trie_node_info",
|
||||
"version": "0.1.0",
|
||||
"description": "Example showing how to show size and node info of a Patricia Trie",
|
||||
"main": "index.js",
|
||||
"author": "ltfschoen",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"clean": "rimraf node_modules",
|
||||
"start": "node index.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@polkadot/trie-db": "^0.32.10",
|
||||
"@polkadot/db": "^0.32.10"
|
||||
},
|
||||
"devDependencies": {
|
||||
"rimraf": "^2.6.2"
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
# Empty & Close & Drop a Patricia Trie
|
||||
|
||||
The following example shows how to empty, close, and drop a Patricia Trie.
|
||||
|
||||
[include](index.js)
|
||||
@@ -1,55 +0,0 @@
|
||||
const toU8a = require('@polkadot/util/u8a/toU8a').default;
|
||||
const Trie = require('@polkadot/trie-db').default;
|
||||
const {
|
||||
isEmptyNode,
|
||||
isKvNode
|
||||
} = require('@polkadot/trie-db/util/is');
|
||||
|
||||
async function main () {
|
||||
const trie = new Trie();
|
||||
|
||||
// Open the database for each of the trie instances
|
||||
trie.open();
|
||||
|
||||
// Initial root value is valid
|
||||
console.log(`Initial root Uint8Array value: ${trie.getRoot()}`);
|
||||
|
||||
// Create a branch under the trie root with a key of 'branch1' and 'branch2'
|
||||
// and a value of 'one' and 'two'
|
||||
trie.put(toU8a('branch1'), toU8a('one'));
|
||||
trie.put(toU8a('branch2'), toU8a('two'));
|
||||
|
||||
// Retrieve the value of a given key named 'branch1' and 'branch2' in the trie
|
||||
console.log(`Stored value of key 'branch1': ${trie.get(toU8a('branch1'))}`);
|
||||
console.log(`Stored value of key 'branch2': ${trie.get(toU8a('branch2'))}`);
|
||||
|
||||
// Store the root hash of the trie root node in a variable
|
||||
const trieRootNodeHash = trie.getRoot();
|
||||
|
||||
// Store the root node object in a variable
|
||||
const trieRootNode = trie.getNode(trieRootNodeHash);
|
||||
|
||||
// Show information about the trie node
|
||||
console.log(`Is root node object empty? : ${isEmptyNode(trieRootNode)}`);
|
||||
console.log(`Is root node object a KV node? : ${isKvNode(trieRootNode)}`);
|
||||
|
||||
// Deletes the value of a given key named 'branch1' in the trie branch
|
||||
trie.del(toU8a('branch1'));
|
||||
|
||||
// Retrieve the value of a given key named 'test' in the trie
|
||||
console.log(`Stored value of key 'branch1': ${trie.get(toU8a('branch1'))}`);
|
||||
|
||||
// Empty, close, and drop the database for each of the trie instances
|
||||
trie.empty();
|
||||
|
||||
// Retrieve the value of a given key named 'test' in the trie
|
||||
console.log(`Stored value of key 'branch2': ${trie.get(toU8a('branch2'))}`);
|
||||
|
||||
// Close the trie
|
||||
trie.close();
|
||||
|
||||
// Drop the trie
|
||||
trie.drop();
|
||||
}
|
||||
|
||||
main().catch(console.error).finally(_ => process.exit());
|
||||
@@ -1,19 +0,0 @@
|
||||
{
|
||||
"name": "05_empty_close_drop_trie",
|
||||
"version": "0.1.0",
|
||||
"description": "Example showing how to empty, close, and drop a Patricia Trie",
|
||||
"main": "index.js",
|
||||
"author": "ltfschoen",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"clean": "rimraf node_modules",
|
||||
"start": "node index.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@polkadot/trie-db": "^0.32.10",
|
||||
"@polkadot/db": "^0.32.10"
|
||||
},
|
||||
"devDependencies": {
|
||||
"rimraf": "^2.6.2"
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
# Trie-DB Examples
|
||||
|
||||
Here you will find a list of examples that takes you through the basics of creating a Patricia Trie, creating key/value pair branches, retrieving the value of a branch with its key, deleting branches, saving the root node hash to a variable and then later setting the root node hash to restore the trie to its previous state, duplicating the trie with a snapshot, showing the size of the trie and other node information, and emptying, closing, and dropping the trie.
|
||||
|
||||
## Running the examples
|
||||
|
||||
From within each folder run the following to install required dependencies and to execute the example:
|
||||
```shell
|
||||
yarn;
|
||||
yarn start
|
||||
```
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
/* eslint-disable @typescript-eslint/no-var-requires */
|
||||
const { naclEncrypt, naclDecrypt } = require('@polkadot/util-crypto');
|
||||
|
||||
async function main () {
|
||||
@@ -20,4 +21,4 @@ async function main () {
|
||||
console.log(`Does the decrypted message match the original message? ${isMatch}`);
|
||||
}
|
||||
|
||||
main().catch(console.error).finally(_ => process.exit());
|
||||
main().catch(console.error).finally(() => process.exit());
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
/* eslint-disable @typescript-eslint/no-var-requires */
|
||||
const { stringToU8a, u8aToHex } = require('@polkadot/util');
|
||||
const { naclEncrypt, naclKeypairFromSeed, naclSign, naclVerify, randomAsU8a } = require('@polkadot/util-crypto');
|
||||
|
||||
@@ -13,7 +14,7 @@ async function main () {
|
||||
const messagePreEncryption = stringToU8a('please send me DOTs');
|
||||
const noncePreEncryption = randomAsU8a(24);
|
||||
|
||||
const { encrypted, nonce } = naclEncrypt(messagePreEncryption, secret, noncePreEncryption);
|
||||
const { encrypted } = naclEncrypt(messagePreEncryption, secret, noncePreEncryption);
|
||||
|
||||
// Sign the message with a valid signature
|
||||
const messageSignature = naclSign(encrypted, secretKey);
|
||||
@@ -26,4 +27,4 @@ async function main () {
|
||||
console.log(`Was the message correctly signed? ${isValidSignature}`);
|
||||
}
|
||||
|
||||
main().catch(console.error).finally(_ => process.exit());
|
||||
main().catch(console.error).finally(() => process.exit());
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
/* eslint-disable @typescript-eslint/no-var-requires,@typescript-eslint/no-unused-vars */
|
||||
const { mnemonicGenerate, mnemonicToSeed, mnemonicValidate, naclKeypairFromSeed } = require('@polkadot/util-crypto');
|
||||
|
||||
async function main () {
|
||||
@@ -21,4 +22,4 @@ async function main () {
|
||||
// Encrypt, Sign and Validate the message. See Example 'Sign & Verify Message'
|
||||
}
|
||||
|
||||
main().catch(console.error).finally(_ => process.exit());
|
||||
main().catch(console.error).finally(() => process.exit());
|
||||
|
||||
Reference in New Issue
Block a user