to hex methods

This commit is contained in:
Miguel Mota 2019-06-07 17:41:48 -07:00
parent 4d0accf644
commit cd0eb7506a
No known key found for this signature in database
GPG Key ID: 67EC1161588A00F9
4 changed files with 28 additions and 1 deletions

2
dist/index.d.ts vendored
View File

@ -77,6 +77,7 @@ export declare class MerkleTree {
*``` *```
*/ */
getRoot(): any; getRoot(): any;
getHexRoot(): string;
/** /**
* getProof * getProof
* @desc Returns the proof for a target leaf. * @desc Returns the proof for a target leaf.
@ -98,6 +99,7 @@ export declare class MerkleTree {
*``` *```
*/ */
getProof(leaf: any, index?: any): any[]; getProof(leaf: any, index?: any): any[];
getHexProof(leaf: any, index?: any): string[];
/** /**
* verify * verify
* @desc Returns true if the proof path (array of hashes) can connect the target node * @desc Returns true if the proof path (array of hashes) can connect the target node

11
dist/index.js vendored
View File

@ -135,6 +135,10 @@ var MerkleTree = /** @class */ (function () {
MerkleTree.prototype.getRoot = function () { MerkleTree.prototype.getRoot = function () {
return this.layers[this.layers.length - 1][0] || Buffer.from([]); return this.layers[this.layers.length - 1][0] || Buffer.from([]);
}; };
// TODO: documentation
MerkleTree.prototype.getHexRoot = function () {
return bufferToHex(this.getRoot());
};
/** /**
* getProof * getProof
* @desc Returns the proof for a target leaf. * @desc Returns the proof for a target leaf.
@ -204,6 +208,10 @@ var MerkleTree = /** @class */ (function () {
return proof; return proof;
} }
}; };
// TODO: documentation
MerkleTree.prototype.getHexProof = function (leaf, index) {
return this.getProof(leaf, index).map(function (x) { return bufferToHex(x.data); });
};
/** /**
* verify * verify
* @desc Returns true if the proof path (array of hashes) can connect the target node * @desc Returns true if the proof path (array of hashes) can connect the target node
@ -309,6 +317,9 @@ var MerkleTree = /** @class */ (function () {
return MerkleTree; return MerkleTree;
}()); }());
exports.MerkleTree = MerkleTree; exports.MerkleTree = MerkleTree;
function bufferToHex(value) {
return '0x' + value.toString('hex');
}
function bufferify(x) { function bufferify(x) {
if (!Buffer.isBuffer(x)) { if (!Buffer.isBuffer(x)) {
// crypto-js support // crypto-js support

View File

@ -176,6 +176,11 @@ export class MerkleTree {
return this.layers[this.layers.length-1][0] || Buffer.from([]) return this.layers[this.layers.length-1][0] || Buffer.from([])
} }
// TODO: documentation
getHexRoot() {
return bufferToHex(this.getRoot())
}
/** /**
* getProof * getProof
* @desc Returns the proof for a target leaf. * @desc Returns the proof for a target leaf.
@ -259,6 +264,11 @@ export class MerkleTree {
} }
} }
// TODO: documentation
getHexProof(leaf, index?) {
return this.getProof(leaf, index).map(x => bufferToHex(x.data))
}
/** /**
* verify * verify
* @desc Returns true if the proof path (array of hashes) can connect the target node * @desc Returns true if the proof path (array of hashes) can connect the target node
@ -375,6 +385,10 @@ export class MerkleTree {
} }
} }
function bufferToHex(value:Buffer) {
return '0x'+value.toString('hex')
}
function bufferify(x) { function bufferify(x) {
if (!Buffer.isBuffer(x)) { if (!Buffer.isBuffer(x)) {
// crypto-js support // crypto-js support

View File

@ -1,6 +1,6 @@
{ {
"name": "merkletreejs", "name": "merkletreejs",
"version": "0.1.2", "version": "0.1.3",
"description": "Construct Merkle Trees and verify proofs", "description": "Construct Merkle Trees and verify proofs",
"main": "dist/index.js", "main": "dist/index.js",
"types": "typings/merkletreejs/*", "types": "typings/merkletreejs/*",