UNPKG

@ethereumjs/mpt

Version:

Implementation of the modified merkle patricia tree as specified in Ethereum's yellow paper.

79 lines 1.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.bytesToNibbles = bytesToNibbles; exports.nibblesTypeToPackedBytes = nibblesTypeToPackedBytes; exports.nibblesCompare = nibblesCompare; exports.matchingNibbleLength = matchingNibbleLength; /** * Converts a bytes to a nibble array. * @private * @param key */ function bytesToNibbles(key) { const nibbles = []; for (let i = 0; i < key.length; i++) { let q = i * 2; nibbles[q] = key[i] >> 4; ++q; nibbles[q] = key[i] % 16; } return nibbles; } /** * Converts a nibble array into bytes. * @private * @param arr - Nibble array */ function nibblesTypeToPackedBytes(arr) { const buf = new Uint8Array(arr.length / 2); for (let i = 0; i < buf.length; i++) { let q = i * 2; buf[i] = (arr[q] << 4) + arr[++q]; } return buf; } /** * Compare two nibble array. * * `0` is returned if `n2` === `n1`. * * `1` is returned if `n2` > `n1`. * * `-1` is returned if `n2` < `n1`. * @param n1 - Nibble array * @param n2 - Nibble array */ function nibblesCompare(n1, n2) { const cmpLength = Math.min(n1.length, n2.length); let res = 0; for (let i = 0; i < cmpLength; i++) { if (n1[i] < n2[i]) { res = -1; break; } else if (n1[i] > n2[i]) { res = 1; break; } } if (res === 0) { if (n1.length < n2.length) { res = -1; } else if (n1.length > n2.length) { res = 1; } } return res; } /** * Returns the number of in order matching nibbles of two give nibble arrays. * @private * @param nib1 * @param nib2 */ function matchingNibbleLength(nib1, nib2) { let i = 0; while (nib1[i] === nib2[i] && nib1.length > i) { i++; } return i; } //# sourceMappingURL=nibbles.js.map