mirror of
https://github.com/jackyzha0/quartz.git
synced 2025-12-24 13:24:05 -06:00
209 lines
5.6 KiB
JavaScript
209 lines
5.6 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.Base = void 0;
|
|
const crypto_js_1 = __importDefault(require("crypto-js"));
|
|
class Base {
|
|
/**
|
|
* print
|
|
* @desc Prints out a visual representation of the merkle tree.
|
|
* @example
|
|
*```js
|
|
*tree.print()
|
|
*```
|
|
*/
|
|
print() {
|
|
Base.print(this);
|
|
}
|
|
/**
|
|
* bufferIndexOf
|
|
* @desc Returns the first index of which given buffer is found in array.
|
|
* @param {Buffer[]} haystack - Array of buffers.
|
|
* @param {Buffer} needle - Buffer to find.
|
|
* @return {Number} - Index number
|
|
*
|
|
* @example
|
|
* ```js
|
|
*const index = tree.bufferIndexOf(haystack, needle)
|
|
*```
|
|
*/
|
|
_bufferIndexOf(array, element) {
|
|
for (let i = 0; i < array.length; i++) {
|
|
if (element.equals(array[i])) {
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
/**
|
|
* bufferify
|
|
* @desc Returns a buffer type for the given value.
|
|
* @param {String|Number|Object|Buffer} value
|
|
* @return {Buffer}
|
|
*
|
|
* @example
|
|
* ```js
|
|
*const buf = MerkleTree.bufferify('0x1234')
|
|
*```
|
|
*/
|
|
static bufferify(value) {
|
|
if (!Buffer.isBuffer(value)) {
|
|
// crypto-js support
|
|
if (typeof value === 'object' && value.words) {
|
|
return Buffer.from(value.toString(crypto_js_1.default.enc.Hex), 'hex');
|
|
}
|
|
else if (Base.isHexString(value)) {
|
|
return Buffer.from(value.replace(/^0x/, ''), 'hex');
|
|
}
|
|
else if (typeof value === 'string') {
|
|
return Buffer.from(value);
|
|
}
|
|
else if (typeof value === 'number') {
|
|
let s = value.toString();
|
|
if (s.length % 2) {
|
|
s = `0${s}`;
|
|
}
|
|
return Buffer.from(s, 'hex');
|
|
}
|
|
}
|
|
return value;
|
|
}
|
|
/**
|
|
* isHexString
|
|
* @desc Returns true if value is a hex string.
|
|
* @param {String} value
|
|
* @return {Boolean}
|
|
*
|
|
* @example
|
|
* ```js
|
|
*console.log(MerkleTree.isHexString('0x1234'))
|
|
*```
|
|
*/
|
|
static isHexString(v) {
|
|
return (typeof v === 'string' && /^(0x)?[0-9A-Fa-f]*$/.test(v));
|
|
}
|
|
/**
|
|
* print
|
|
* @desc Prints out a visual representation of the given merkle tree.
|
|
* @param {Object} tree - Merkle tree instance.
|
|
* @return {String}
|
|
* @example
|
|
*```js
|
|
*MerkleTree.print(tree)
|
|
*```
|
|
*/
|
|
static print(tree) {
|
|
console.log(tree.toString());
|
|
}
|
|
/**
|
|
* bufferToHex
|
|
* @desc Returns a hex string with 0x prefix for given buffer.
|
|
* @param {Buffer} value
|
|
* @return {String}
|
|
* @example
|
|
*```js
|
|
*const hexStr = tree.bufferToHex(Buffer.from('A'))
|
|
*```
|
|
*/
|
|
bufferToHex(value, withPrefix = true) {
|
|
return Base.bufferToHex(value, withPrefix);
|
|
}
|
|
/**
|
|
* bufferToHex
|
|
* @desc Returns a hex string with 0x prefix for given buffer.
|
|
* @param {Buffer} value
|
|
* @return {String}
|
|
* @example
|
|
*```js
|
|
*const hexStr = MerkleTree.bufferToHex(Buffer.from('A'))
|
|
*```
|
|
*/
|
|
static bufferToHex(value, withPrefix = true) {
|
|
return `${withPrefix ? '0x' : ''}${(value || Buffer.alloc(0)).toString('hex')}`;
|
|
}
|
|
/**
|
|
* bufferify
|
|
* @desc Returns a buffer type for the given value.
|
|
* @param {String|Number|Object|Buffer} value
|
|
* @return {Buffer}
|
|
*
|
|
* @example
|
|
* ```js
|
|
*const buf = tree.bufferify('0x1234')
|
|
*```
|
|
*/
|
|
bufferify(value) {
|
|
return Base.bufferify(value);
|
|
}
|
|
/**
|
|
* bufferifyFn
|
|
* @desc Returns a function that will bufferify the return value.
|
|
* @param {Function}
|
|
* @return {Function}
|
|
*
|
|
* @example
|
|
* ```js
|
|
*const fn = tree.bufferifyFn((value) => sha256(value))
|
|
*```
|
|
*/
|
|
bufferifyFn(f) {
|
|
return (value) => {
|
|
const v = f(value);
|
|
if (Buffer.isBuffer(v)) {
|
|
return v;
|
|
}
|
|
if (this._isHexString(v)) {
|
|
return Buffer.from(v.replace('0x', ''), 'hex');
|
|
}
|
|
if (typeof v === 'string') {
|
|
return Buffer.from(v);
|
|
}
|
|
// crypto-js support
|
|
return Buffer.from(f(crypto_js_1.default.enc.Hex.parse(value.toString('hex'))).toString(crypto_js_1.default.enc.Hex), 'hex');
|
|
};
|
|
}
|
|
/**
|
|
* isHexString
|
|
* @desc Returns true if value is a hex string.
|
|
* @param {String} value
|
|
* @return {Boolean}
|
|
*
|
|
* @example
|
|
* ```js
|
|
*console.log(MerkleTree.isHexString('0x1234'))
|
|
*```
|
|
*/
|
|
_isHexString(value) {
|
|
return Base.isHexString(value);
|
|
}
|
|
/**
|
|
* log2
|
|
* @desc Returns the log2 of number.
|
|
* @param {Number} value
|
|
* @return {Number}
|
|
*/
|
|
_log2(n) {
|
|
return n === 1 ? 0 : 1 + this._log2((n / 2) | 0);
|
|
}
|
|
/**
|
|
* zip
|
|
* @desc Returns true if value is a hex string.
|
|
* @param {String[]|Number[]|Buffer[]} a - first array
|
|
* @param {String[]|Number[]|Buffer[]} b - second array
|
|
* @return {String[][]|Number[][]|Buffer[][]}
|
|
*
|
|
* @example
|
|
* ```js
|
|
*const zipped = tree.zip(['a', 'b'],['A', 'B'])
|
|
*console.log(zipped) // [ [ 'a', 'A' ], [ 'b', 'B' ] ]
|
|
*```
|
|
*/
|
|
_zip(a, b) {
|
|
return a.map((e, i) => [e, b[i]]);
|
|
}
|
|
}
|
|
exports.Base = Base;
|
|
exports.default = Base;
|