Skip to content

Commit 377900a

Browse files
more documentation
1 parent 27aa696 commit 377900a

File tree

5 files changed

+77
-12
lines changed

5 files changed

+77
-12
lines changed

README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,37 @@
44
[MerkleHash](https://github.com/LucasArmstrong/merkle-ts/blob/main/src/merkle/MerkleHash.ts),
55
and [MerkleChain](https://github.com/LucasArmstrong/merkle-ts/blob/main/src/merkle/MerkleChain.ts)
66
in TypeScript
7-
- Created by: Lucas Armstrong - Lucas@throneit.com - github.com/LucasArmstrong
7+
- Created by: Lucas Armstrong - Lucas@throneit.com - [github.com/LucasArmstrong](https://github.com/LucasArmstrong)
88
- License: `MIT`
99

1010
## Install in your project with NPM and use it
1111
- In your project run:
12-
- >`npm i https://github.com/LucasArmstrong/merkle-ts`
12+
- >npm i https://github.com/LucasArmstrong/merkle-ts
1313
- For TypeScript projects you can import the module MerkleTree
14-
- For JavaScript projects you can build with `npm run build`
14+
- For JavaScript projects you can build with:
15+
- >npm run build
1516
- - Then require in your code like:
16-
- - >`const {MerkleTree} = require('./node_modules/merkle-ts/dist/src/merkle/MerkleTree.js');`
17+
- - >const {MerkleTree} = require('./node_modules/merkle-ts/dist/src/merkle/MerkleTree.js');
1718
- - Example of using a merkle root in a jest test:
18-
- - >`let tree = new MerkleTree([1]); expect(tree.root).toBe('6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b');`
19+
- - >let tree = new MerkleTree([1]); expect(tree.root).toBe('6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b');
1920
2021
## Calculate a Merkle Root from a directory of files recursively
2122
- First set or export environment variable `ASSET_DIRECTORY_PATH` to the directory you want to process.
2223
- Then run:
23-
- >`npm run process`
24+
- >npm run process
2425
- Digests and calculates a Merkle Root for the entire specified directory and contents
2526

26-
### Test
27+
## Test
2728
- Execute tests with:
28-
- >`npm run test`
29+
- >npm run test
2930
- Runs a variety of tests proving that the MerkleTree class correctly calculates the merkle root for a set of data
3031

3132
## Merkle Tree Info.
3233
In cryptography and computer science, a hash tree or Merkle tree is a tree in which every "leaf" (node) is labelled with the cryptographic hash of a data block, and every node that is not a leaf (called a branch, inner node, or inode) is labelled with the cryptographic hash of the labels of its child nodes. A hash tree allows efficient and secure verification of the contents of a large data structure. A hash tree is a generalization of a hash list and a hash chain.
3334
- Read more at https://en.wikipedia.org/wiki/Merkle_tree
3435

3536
## Available Hash Algorithms
36-
>'RSA-MD4',
37+
`'RSA-MD4',
3738
'RSA-MD5',
3839
'RSA-MDC2',
3940
'RSA-RIPEMD160',
@@ -91,4 +92,4 @@ In cryptography and computer science, a hash tree or Merkle tree is a tree in wh
9192
'sm3WithRSAEncryption',
9293
'ssl3-md5',
9394
'ssl3-sha1',
94-
'whirlpool'
95+
'whirlpool'`

src/merkle/MerkleChain.ts

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/**
2-
* @module MerkleChain - list based data structure seeded with a gensis timestamp and
2+
* @module MerkleChain - list based data structure seeded with a genesis timestamp and
33
* maintains a merkle root, optimized to quickly traverse and search nodes
44
* @module MerkleChainNode - type for the nodes in MerkleChain
5+
*
56
* @author Lucas Armstrong - Lucas@throneit.com - github.com/LucasArmstrong
67
*/
78

@@ -18,8 +19,15 @@ export type MerkleChainNodeNullable = MerkleChainNode | null;
1819

1920
/**
2021
* @class MerkleChainNode
22+
*
23+
* @param genesisAt {number}
24+
* @param dataArray {MerkleDataType[]}
2125
*/
2226
export class MerkleChainNode {
27+
28+
/**
29+
* @var next
30+
*/
2331
private _next: MerkleChainNodeNullable = null;
2432
set next(nextNode: MerkleChainNodeNullable) {
2533
this._next = nextNode;
@@ -33,11 +41,17 @@ export class MerkleChainNode {
3341
return this._next;
3442
}
3543

44+
/**
45+
* @var nextRoot
46+
*/
3647
private _nextRoot: string = '';
3748
get nextRoot(): string {
3849
return this._nextRoot;
3950
}
4051

52+
/**
53+
* @var prev
54+
*/
4155
private _prev: MerkleChainNodeNullable = null;
4256
set prev(prevNode: MerkleChainNodeNullable) {
4357
this._prev = prevNode;
@@ -51,12 +65,22 @@ export class MerkleChainNode {
5165
return this._prev;
5266
}
5367

68+
/**
69+
* @var prevRoot
70+
*/
5471
private _prevRoot: string = '';
5572
get prevRoot(): string {
5673
return this._prevRoot;
5774
}
5875

76+
/**
77+
* @var merkleRoot
78+
*/
5979
merkleRoot: string = '';
80+
81+
/**
82+
* @var dataArray
83+
*/
6084
dataArray: MerkleDataType[];
6185

6286
constructor (genesisAt: number, dataArray: MerkleDataType[] = []) {
@@ -71,13 +95,28 @@ export class MerkleChainNode {
7195

7296
/**
7397
* @class MerkleChain
98+
* @param dataArray {MerkleDataType[]}
7499
*/
75100
export class MerkleChain {
101+
102+
/**
103+
* @var head - pointer for the node at the start of the list
104+
*/
76105
head: MerkleChainNodeNullable = null;
106+
107+
/**
108+
* @var tail - pointer for the node at the end of the list
109+
*/
77110
tail: MerkleChainNodeNullable = null;
78111

112+
/**
113+
* @var genesisAt - timestamp used to seed the list
114+
*/
79115
genesisAt: number = 0;
80116

117+
/**
118+
* @var merkleRoot
119+
*/
81120
private _merkleRoot: string = '';
82121
get merkleRoot(): string {
83122
return this._merkleRoot;
@@ -88,11 +127,22 @@ export class MerkleChain {
88127
this.genesisAt = new Date().getTime();
89128
}
90129

91-
addNode(dataArray: MerkleDataType[] = []) {
130+
/**
131+
* @method addNode - creates a new node from a MerkleDataType array
132+
*
133+
* @param dataArray {MerkleDataType[]} - array of MerkleDataType data that gets inserted into the list as a new node
134+
* @returns {void}
135+
*/
136+
addNode(dataArray: MerkleDataType[] = []): void {
92137
this.newNode(new MerkleChainNode(this.genesisAt, dataArray));
93138
this._merkleRoot = new MerkleTree(this.toRootArray()).root;
94139
}
95140

141+
/**
142+
* @method toRootArray - returns an array of the merkle roots from all nodes in the list
143+
*
144+
* @returns {string[]}
145+
*/
96146
toRootArray(): string[] {
97147
const values: string[] = [];
98148
let current: MerkleChainNodeNullable = this.head;
@@ -103,6 +153,12 @@ export class MerkleChain {
103153
return values;
104154
}
105155

156+
/**
157+
* @method newNode - manages inserting a new node to the list
158+
*
159+
* @param node {MerkleChainNode} - node to be added to the list
160+
* @returns {MerkleChainNode}
161+
*/
106162
private newNode(node: MerkleChainNode) : MerkleChainNode {
107163
if (!this.head) {
108164
this.head = node;
@@ -119,4 +175,5 @@ export class MerkleChain {
119175

120176
return node;
121177
}
178+
122179
}

src/merkle/MerkleFileProcessor.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* @module MerkleFileProcessor - utilities to find the Merkle Root of a file or directory
3+
*
34
* @author Lucas Armstrong - Lucas@throneit.com - github.com/LucasArmstrong
45
*/
56

@@ -70,4 +71,5 @@ export class MerkleFileProcessor {
7071
throw new Error(`processDirectory Error: ${error}`);
7172
}
7273
}
74+
7375
}

src/merkle/MerkleHash.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* @module MerkleHash - hash utlities for MerkleTree
3+
*
34
* @author Lucas Armstrong - Lucas@throneit.com - github.com/LucasArmstrong
45
*/
56

@@ -95,6 +96,7 @@ export class MerkleHash {
9596

9697
/**
9798
* @method createHashFromFile - Static method to be used as utility in order to create a hash from a file of any size
99+
*
98100
* @param filePath
99101
* @returns {Promise<string>}
100102
*/
@@ -133,4 +135,5 @@ export class MerkleHash {
133135
return dataType;
134136
}
135137
}
138+
136139
}

src/merkle/MerkleTree.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* @module MerkleTree - generates a MerkleTree from a list of data
3+
*
34
* @author Lucas Armstrong - Lucas@throneit.com - github.com/LucasArmstrong
45
*/
56

@@ -157,4 +158,5 @@ export class MerkleTree implements IMerkleTree {
157158
// one hash means the root has been found
158159
return hashed[0];
159160
}
161+
160162
}

0 commit comments

Comments
 (0)