All files / urkel/lib/radix common.js

87.85% Statements 94/107
56% Branches 14/25
88.23% Functions 15/17
87.61% Lines 92/105

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225                1x           1x 1x 1x 1x 1x             1634546x 1634546x 1634546x       44879x 44879x 44879x       76178x 60722x   15456x   15456x   15456x 15456x 15456x 15456x 15456x 15456x   15456x       40741x       40408x 40408x       84x   84x 16x   68x   68x 680x   680x     680x 680x   680x       68x       107x   107x   107x 948x   107x       1x 1x 1x 1x       1x   1x 1x   1x 1x         1x       1519165x                   1519165x             192151x 192151x 192151x                         176696x 176696x 176696x 176696x 176696x 176696x 176696x 176696x       5x   5x             5x 160x   5x       22x                           22x     22x   22x             1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
/*!
 * common.js - tree common functions
 * Copyright (c) 2018, Christopher Jeffrey (MIT License).
 * https://github.com/handshake-org/urkel
 */
 
'use strict';
 
const assert = require('bsert');
 
/*
 * Constants
 */
 
const SKIP_PREFIX = Buffer.from([0x02]);
const INTERNAL_PREFIX = Buffer.from([0x01]);
const LEAF_PREFIX = Buffer.from([0x00]);
const EMPTY = Buffer.alloc(0);
const size = Buffer.alloc(2);
 
/*
 * Common
 */
 
function hasBit(key, index) {
  const oct = index >>> 3;
  const bit = index & 7;
  return (key[oct] >>> (7 - bit)) & 1;
}
 
function setBit(key, index, b) {
  const oct = index >>> 3;
  const bit = index & 7;
  key[oct] |= b << (7 - bit);
}
 
function hashInternal(hash, prefix, left, right) {
  if (prefix.size === 0)
    return hash.multi(INTERNAL_PREFIX, left, right);
 
  const ctx = hash.ctx;
 
  writeU16(size, prefix.size, 0);
 
  ctx.init();
  ctx.update(SKIP_PREFIX);
  ctx.update(size);
  ctx.update(prefix.data);
  ctx.update(left);
  ctx.update(right);
 
  return ctx.final();
}
 
function hashLeaf(hash, key, valueHash) {
  return hash.multi(LEAF_PREFIX, key, valueHash);
}
 
function hashValue(hash, key, value) {
  const valueHash = hash.digest(value);
  return hashLeaf(hash, key, valueHash);
}
 
function parseU32(name) {
  assert(typeof name === 'string');
 
  if (name.length !== 10)
    return -1;
 
  let num = 0;
 
  for (let i = 0; i < 10; i++) {
    const ch = name.charCodeAt(i);
 
    Iif (ch < 0x30 || ch > 0x39)
      return -1;
 
    num *= 10;
    num += ch - 0x30;
 
    Iif (num > 0xffffffff)
      return -1;
  }
 
  return num;
}
 
function serializeU32(num) {
  assert((num >>> 0) === num);
 
  let str = num.toString(10);
 
  while (str.length < 10)
    str = '0' + str;
 
  return str;
}
 
function randomString() {
  const m = Number.MAX_SAFE_INTEGER;
  const n = Math.random() * m;
  const s = Math.floor(n);
  return s.toString(32);
}
 
function randomPath(path) {
  assert(typeof path === 'string');
 
  while (path.length > 1) {
    const ch = path[path.length - 1];
 
    Eif (ch !== '/' && ch !== '\\')
      break;
 
    path = path.slice(0, -1);
  }
 
  return `${path}.${randomString()}~`;
}
 
function readU16(data, off) {
  return data[off++] + data[off] * 0x100;
}
 
function readU24(data, off) {
  return (data[off++]
    + data[off++] * 0x100
    + data[off] * 0x10000);
}
 
function readU32(data, off) {
  return (data[off++]
    + data[off++] * 0x100
    + data[off++] * 0x10000
    + data[off] * 0x1000000);
}
 
function writeU16(dst, num, off) {
  dst[off++] = num;
  dst[off++] = num >>> 8;
  return off;
}
 
function writeU24(dst, num, off) {
  dst[off++] = num;
  num >>>= 8;
  dst[off++] = num;
  num >>>= 8;
  dst[off++] = num;
  return off;
}
 
function writeU32(dst, num, off) {
  dst[off++] = num;
  num >>>= 8;
  dst[off++] = num;
  num >>>= 8;
  dst[off++] = num;
  num >>>= 8;
  dst[off++] = num;
  return off;
}
 
function randomBytes(size) {
  assert((size & 0xffff) === size);
 
  const bytes = Buffer.allocUnsafe(size);
 
  // Does not need to be cryptographically
  // strong, just needs to be _different_
  // from everyone else to make an attack
  // not worth trying. Predicting one user's
  // key does nothing to help an attacker.
  for (let i = 0; i < bytes.length; i++)
    bytes[i] = (Math.random() * 0x100) >>> 0;
 
  return bytes;
}
 
function checksum(hash, data, key) {
  switch (hash.name) {
    case 'BLAKE2b160':
    case 'BLAKE2b256':
    case 'BLAKE2b384':
    case 'BLAKE2b512':
    case 'BLAKE2s160':
    case 'BLAKE2s224':
    case 'BLAKE2s256':
      // Hack.
      hash = hash.__proto__;
      break;
  }
 
  // Special case for blake2.
  Iif (hash.name === 'BLAKE2b' || hash.name === 'BLAKE2s')
    return hash.digest(data, 20, key);
 
  assert(hash.size >= 20);
 
  return hash.multi(data, key).slice(0, 20);
}
 
/*
 * Expose
 */
 
exports.EMPTY = EMPTY;
exports.hasBit = hasBit;
exports.setBit = setBit;
exports.hashInternal = hashInternal;
exports.hashLeaf = hashLeaf;
exports.hashValue = hashValue;
exports.parseU32 = parseU32;
exports.serializeU32 = serializeU32;
exports.randomString = randomString;
exports.randomPath = randomPath;
exports.readU16 = readU16;
exports.readU24 = readU24;
exports.readU32 = readU32;
exports.writeU16 = writeU16;
exports.writeU24 = writeU24;
exports.writeU32 = writeU32;
exports.randomBytes = randomBytes;
exports.checksum = checksum;