ff19

benchlab-kepleruuid-mnemonic

Jul 7th, 2018
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 14.15 KB | None | 0 0
  1. diff --git b/index.js a/index.js
  2. old mode 100755
  3. new mode 100644
  4. index f9aa6fe..06573e2
  5. --- b/index.js
  6. +++ a/index.js
  7. @@ -1,10 +1,11 @@
  8. -const R = require('ramda')
  9. -const uv = require('uuid-validate')
  10. +const Ramda = require('ramda')
  11. +const keplerUuidValidate = require('uuid-validate')
  12.  
  13.  const b64 = hex => Buffer.from(hex.replace(/-/g, ''), 'hex').toString('base64')
  14.  const b64ToHex = b64 => Buffer.from(b64, 'base64').toString('hex')
  15. +
  16.  // RFC 4122 : 8-4-4-4-12
  17. -const hexToRfc = hex => {
  18. +const hdToKeplerUUID = hex => {
  19.    let x = 0
  20.    const rfc = hex.substring(x, x += 8) +
  21.     '-' + hex.substring(x, x += 4) +
  22. @@ -14,9 +15,9 @@ const hexToRfc = hex => {
  23.    return rfc
  24.  }
  25.  
  26. -const coinable = (die, planchet) => die.length <= 22
  27. +const mnemonic = (extract, byteBlanket) => extract.length <= 22
  28.  
  29. -const coin = (die, planchet) => die + b64(planchet).slice(die.length, planchet.length)
  30. +const beforeMnemonic = (extract, byteBlanket) => extract + b64(byteBlanket).slice(extract.length, byteBlanket.length)
  31.  
  32.  const flipChar = c => {
  33.    if (c !== c.toUpperCase(c)) {
  34. @@ -34,26 +35,26 @@ const flipCharAt = (string, index) => {
  35.    return string.slice(0, index) + fc + string.slice(index + 1)
  36.  }
  37.  
  38. -const coinAll = (die, planchet) => {
  39. -  const planchet64 = b64(planchet)
  40. -  const candidates = R.map(
  41. -    i => planchet64.slice(0, i) + die + planchet64.slice(i + die.length),
  42. -    R.range(0, 23 - die.length)
  43. +const mnemonicAll = (extract, byteBlanket) => {
  44. +  const byteBlanket64 = b64(byteBlanket)
  45. +  const candidates = Ramda.map(
  46. +    i => byteBlanket64.slice(0, i) + extract + byteBlanket64.slice(i + extract.length),
  47. +    Ramda.range(0, 23 - extract.length)
  48.    )
  49.    const rtn = []
  50.    candidates.forEach(candidate => {
  51. -    const candidateRfc = R.pipe(
  52. -      b64ToHex, hexToRfc
  53. +    const candidateRfc = Ramda.pipe(
  54. +      b64ToHex, hdToKeplerUUID
  55.      )(candidate)
  56. -    if (uv(candidateRfc)) {
  57. +    if (keplerUuidValidate(candidateRfc)) {
  58.        rtn.push(candidate)
  59.        return
  60.      }
  61.      const candidate2 = flipCharAt(candidate, 8)
  62. -    const candidate2Rfc = R.pipe(
  63. -      b64ToHex, hexToRfc
  64. +    const candidate2Rfc = Ramda.pipe(
  65. +      b64ToHex, hdToKeplerUUID
  66.      )(candidate2)
  67. -    if (uv.version(candidate2Rfc) === 4) {
  68. +    if (keplerUuidValidate.version(candidate2Rfc) === 4) {
  69.        rtn.push(candidate2)
  70.      }
  71.    })
  72. @@ -61,8 +62,8 @@ const coinAll = (die, planchet) => {
  73.  }
  74.  
  75.  module.exports = {
  76. -  coin,
  77. -  coinAll,
  78. -  coinable,
  79. +  beforeMnemonic,
  80. +  mnemonicAll,
  81. +  mnemonic,
  82.    flipCharAt
  83.  }
  84. diff --git b/index.spec.js a/index.spec.js
  85. old mode 100755
  86. new mode 100644
  87. index 65043d9..fd8fbb2
  88. --- b/index.spec.js
  89. +++ a/index.spec.js
  90. @@ -2,86 +2,79 @@ import test from 'ava'
  91.  
  92.  import * as sut from './index'
  93.  
  94. -test('coinable, die short', t => {
  95. -  const planchet = 'c8404eda-83cb-4728-bf68-21bd1bac26b8'
  96. -  const die = 'jane+doe'
  97. -  const actual = sut.coinable(die, planchet)
  98. +test('mnemonic, extract short', t => {
  99. +  const byteBlanket = ''
  100. +  const extract = ''
  101. +  const actual = sut.mnemonic(extract, byteBlanket)
  102.    t.true(actual)
  103.  })
  104.  
  105. -test('coinable, die exact length', t => {
  106. -  const planchet = 'c8404eda-83cb-4728-bf68-21bd1bac26b8'
  107. -  const die = 'jane+doe+0123456789abc'
  108. -  const actual = sut.coinable(die, planchet)
  109. +test('mnemonic, extract exact length', t => {
  110. +  const byteBlanket = ''
  111. +  const extract = ''
  112. +  const actual = sut.mnemonic(extract, byteBlanket)
  113.    t.true(actual)
  114.  })
  115.  
  116. -test('coinable, die too long', t => {
  117. -  const planchet = 'c8404eda-83cb-4728-bf68-21bd1bac26b8'
  118. -  const die = 'jane+doe+0123456789abcd'
  119. -  const actual = sut.coinable(die, planchet)
  120. +test('mnemonic, extract too long', t => {
  121. +  const byteBlanket = ''
  122. +  const extract = ''
  123. +  const actual = sut.mnemonic(extract, byteBlanket)
  124.    t.false(actual)
  125.  })
  126.  
  127. -test('coin', t => {
  128. -  const planchet = 'c8404eda-83cb-4728-bf68-21bd1bac26b8'
  129. -  // yEBO2oPLRyi/aCG9G6wmuA==
  130. -  const die = 'jane+doe'
  131. -  const actual = sut.coin(die, planchet)
  132. -  const expected = 'jane+doeRyi/aCG9G6wmuA=='
  133. +test('beforeMnemonic', t => {
  134. +  const byteBlanket = ''
  135. +  //
  136. +  const extract = ''
  137. +  const actual = sut.beforeMnemonic(extract, byteBlanket)
  138. +  const expected = ''
  139.    t.is(actual, expected)
  140.  })
  141.  
  142. -test('coinAll', t => {
  143. -  const planchet = 'c8404eda-83cb-4728-bf68-21bd1bac26b8'
  144. -  // yEBO2oPLRyi/aCG9G6wmuA==
  145. -  const die = 'jane+d'
  146. -  const actual = sut.coinAll(die, planchet)
  147. +test('mnemonicAll', t => {
  148. +  const byteBlanket = ''
  149. +  //
  150. +  const extract = 'jane+d'
  151. +  const actual = sut.mnemonicAll(extract, byteBlanket)
  152.    const expected = [
  153. -    'jane+dPLRyi/aCG9G6wmuA==',
  154. -    'yjane+dLRyi/aCG9G6wmuA==',
  155. -    'yEjane+dRyi/aCG9G6wmuA==',
  156. -    // 'yEBjane+dyi/aCG9G6wmuA==',
  157. -    // 'yEBOjane+di/aCG9G6wmuA==',
  158. -    // 'yEBO2jane+d/aCG9G6wmuA==',
  159. -    // 'yEBO2ojane+daCG9G6wmuA==',
  160. -    // 'yEBO2oPjane+dCG9G6wmuA==',
  161. -    // 'yEBO2oPLjane+dG9G6wmuA==',
  162. -    'yEBO2oPLRjane+d9G6wmuA==',
  163. -    // 'yEBO2oPLRyjane+dG6wmuA==',
  164. -    'yEBO2oPLRyijane+d6wmuA==',
  165. -    'yEBO2oPLRyi/jane+dwmuA==',
  166. -    'yEBO2oPLRyi/ajane+dmuA==',
  167. -    'yEBO2oPLRyi/aCjane+duA==',
  168. -    'yEBO2oPLRyi/aCGjane+dA==',
  169. -    'yEBO2oPLRyi/aCG9jane+d=='
  170. +    '',
  171. +    '',
  172. +    '',
  173. +    '',
  174. +    '',
  175. +    '',
  176. +    '',
  177. +    '',
  178. +    '',
  179. +    ''
  180.    ]
  181.    t.deepEqual(actual, expected)
  182.  })
  183.  
  184. -test('coinAll, case flipping', t => {
  185. -  const planchet = 'c8404eda-83cb-4728-bf68-21bd1bac26b8'
  186. -  // yEBO2oPLRyi/aCG9G6wmuA==
  187. -  const die = 'jane+r'
  188. -  const actual = sut.coinAll(die, planchet)
  189. +test('mnemonicAll, case flipping', t => {
  190. +  const byteBlanket = ''
  191. +  //
  192. +  const extract = ''
  193. +  const actual = sut.mnemonicAll(extract, byteBlanket)
  194.    const expected = [
  195. -    'jane+rPLRyi/aCG9G6wmuA==',
  196. -    'yjane+rLRyi/aCG9G6wmuA==',
  197. -    'yEjane+rRyi/aCG9G6wmuA==',
  198. -    'yEBjane+Ryi/aCG9G6wmuA==', // case flip r -> R
  199. -    // 'yEBOjane+ri/aCG9G6wmuA==',
  200. -    // 'yEBO2jane+r/aCG9G6wmuA==',
  201. -    // 'yEBO2ojane+raCG9G6wmuA==',
  202. -    // 'yEBO2oPjane+rCG9G6wmuA==',
  203. -    // 'yEBO2oPLjane+rG9G6wmuA==',
  204. -    'yEBO2oPLRjane+r9G6wmuA==',
  205. -    // 'yEBO2oPLRyjane+rG6wmuA==',
  206. -    'yEBO2oPLRyijane+r6wmuA==',
  207. -    'yEBO2oPLRyi/jane+rwmuA==',
  208. -    'yEBO2oPLRyi/ajane+rmuA==',
  209. -    'yEBO2oPLRyi/aCjane+ruA==',
  210. -    'yEBO2oPLRyi/aCGjane+rA==',
  211. -    'yEBO2oPLRyi/aCG9jane+r=='
  212. +    '',
  213. +    '',
  214. +    '',
  215. +    '', // case flip r -> R
  216. +    // '',
  217. +    // '',
  218. +    // '',
  219. +    // '',
  220. +    // '',
  221. +    '',
  222. +    // '',
  223. +    '',
  224. +    '',
  225. +    '',
  226. +    '',
  227. +    '',
  228. +    ''
  229.    ]
  230.    t.deepEqual(actual, expected)
  231.  })
  232. diff --git b/nameCodec.js a/nameCodec.js
  233. old mode 100755
  234. new mode 100644
  235. index e09d9fa..9e57c7d
  236. --- b/nameCodec.js
  237. +++ a/nameCodec.js
  238. @@ -7,10 +7,10 @@
  239.  //   return hash(namespace + name)
  240.  // }
  241.  
  242. -// function readDigest(uuid) {
  243. -//   return uuid
  244. +// function readDigest(KUUID) {
  245. +//   return KUUID
  246.  // }
  247.  
  248. -// function writeDigest(uuid) {
  249. -//   const variant = readVariant
  250. +// function writeDigest(KUUID) {
  251. +//   const alternate = readAlternate
  252.  // }
  253. diff --git b/package.json a/package.json
  254. old mode 100755
  255. new mode 100644
  256. index 39632ab..53f83e9
  257. --- b/package.json
  258. +++ a/package.json
  259. @@ -1,15 +1,15 @@
  260.  {
  261. -  "name": "uncoined",
  262. -  "version": "0.0.0",
  263. -  "description": "",
  264. +  "name": "kepler-mnemonic",
  265. +  "version": "1.0.0",
  266. +  "description": "Kepler's UUID Mnemonic Javascript Library",
  267.    "main": "index.js",
  268.    "scripts": {
  269.      "jsdoc": "rm -rf docs && jsdoc src --recurse --destination docs --configure jsdoc.json",
  270.      "test": "mocha --require jsdoctest src",
  271.      "posttest": "standard && npm run jsdoc"
  272.    },
  273. -  "author": "",
  274. -  "license": "ISC",
  275. +  "author": "Bench Computer Inc. <[email protected]>",
  276. +  "license": "BENCH",
  277.    "devDependencies": {
  278.      "ava": "^0.25.0",
  279.      "jsdoc": "^3.5.5",
  280. diff --git b/solve.js a/solve.js
  281. old mode 100755
  282. new mode 100644
  283. index 3207edf..afbc331
  284. --- b/solve.js
  285. +++ a/solve.js
  286. @@ -6,10 +6,10 @@ const libtime = require('./timestamp')
  287.   */
  288.  
  289.  /**
  290. - * [§ 4.1.1 Variant]{@link https://tools.ietf.org/html/rfc4122#section-4.1.1}
  291. + * [§ 4.1.1 Alternate]{@link https://tools.ietf.org/html/rfc4122#section-4.1.1}
  292.   */
  293. -function readVariant (uuid) {
  294. -  const octit = uuid.readUInt8(8) >> 5
  295. +function readAlternate (KUUID) {
  296. +  const octit = KUUID.readUInt8(8) >> 5
  297.    return [0, 0, 0, 0, 1, 1, 2, 3][octit]
  298.  }
  299.  
  300. @@ -18,44 +18,44 @@ function readVariant (uuid) {
  301.   *   {@link https://tools.ietf.org/html/rfc4122#section-4.1.2}
  302.   */
  303.  
  304. -function readTimeLow (uuid) {
  305. -  return uuid.readUInt32BE(0)
  306. +function readTimeLow (KUUID) {
  307. +  return KUUID.readUInt32BE(0)
  308.  }
  309.  
  310. -function readTimeMid (uuid) {
  311. -  return uuid.readUInt16BE(4)
  312. +function readTimeMid (KUUID) {
  313. +  return KUUID.readUInt16BE(4)
  314.  }
  315.  
  316. -function readTimeHighAndVersion (uuid) {
  317. -  return uuid.readUInt16BE(6)
  318. +function readTimeHighAndVersion (KUUID) {
  319. +  return KUUID.readUInt16BE(6)
  320.  }
  321.  
  322. -function readClockSequenceAndReserved (uuid) {
  323. -  return uuid.readUInt16BE(8)
  324. +function readTimerChainAndReserved (KUUID) {
  325. +  return KUUID.readUInt16BE(8)
  326.  }
  327.  
  328. -function readNode (uuid) {
  329. -  return uuid.readUIntBE(10, 6)
  330. +function readNode (KUUID) {
  331. +  return KUUID.readUIntBE(10, 6)
  332.  }
  333.  
  334.  /**
  335.   * [§ 4.1.3 Version]{@link https://tools.ietf.org/html/rfc4122#section-4.1.3}
  336.   */
  337. -function readVersion (uuid) {
  338. -  return uuid.readUInt8(6) >> 4
  339. +function readVersion (KUUID) {
  340. +  return KUUID.readUInt8(6) >> 4
  341.  }
  342.  
  343.  /**
  344.   * [§ 4.1.4 Timestamp]{@link https://tools.ietf.org/html/rfc4122#section-4.1.4}
  345.   */
  346. -function readTimestamp (uuid) {
  347. +function readTimestamp (KUUID) {
  348.    const unixTime = libtime.toUnixTime(
  349. -    readTimeHighAndVersion(uuid) & 0x0FFF,
  350. -    readTimeMid(uuid),
  351. -    readTimeLow(uuid)
  352. +    readTimeHighAndVersion(KUUID) & 0x0FFF,
  353. +    readTimeMid(KUUID),
  354. +    readTimeLow(KUUID)
  355.    )
  356.    const nanoSeconds =
  357. -    libtime.toNanoSeconds(readTimeLow(uuid))
  358. +    libtime.toNanoSeconds(readTimeLow(KUUID))
  359.    const date = new Date(unixTime * 1000)
  360.    return date.toISOString().slice(0, -4) + nanoSeconds + 'Z'
  361.  }
  362. @@ -63,72 +63,70 @@ function readTimestamp (uuid) {
  363.  /**
  364.   * [§ 4.1.5 Clock Sequence]{@link https://tools.ietf.org/html/rfc4122#section-4.1.5}
  365.   */
  366. -function readClockSequence (uuid) {
  367. -  const variant = readVariant(uuid)
  368. -  const mask = [0x7fff, 0x3fff, 0x1ff, 0x1ff][variant]
  369. -  return readClockSequenceAndReserved(uuid) & mask
  370. +function readTimerChain (KUUID) {
  371. +  const alternate = readAlternate(KUUID)
  372. +  const mask = [0x7fff, 0x3fff, 0x1ff, 0x1ff][alternate]
  373. +  return readTimerChainAndReserved(KUUID) & mask
  374.  }
  375.  
  376.  /**
  377.   * [§ 4.1.6 Node]{@link https://tools.ietf.org/html/rfc4122#section-4.1.6}
  378.   * @see readNode
  379.   */
  380. -function readMacAddress (uuid) {
  381. -  return readNode(uuid).toString(16).match(/.{2}/g).join(':')
  382. +function readMacAddress (KUUID) {
  383. +  return readNode(KUUID).toString(16).match(/.{2}/g).join(':')
  384.  }
  385.  
  386.  /**
  387.   * [§ 4.1.7 Nil UUID]{@link https://tools.ietf.org/html/rfc4122#section-4.1.7}
  388.   * @see readNode
  389.   */
  390. -function isNil (uuid) {
  391. -  return Buffer.alloc(16).equals(uuid)
  392. +function isNil (KUUID) {
  393. +  return Buffer.alloc(16).equals(KUUID)
  394.  }
  395.  
  396. -// These need to be factored out
  397. +function solve (KUUID /* buffer */) {
  398. +  // TODO type check KUUID
  399.  
  400. -function decode (uuid /* buffer */) {
  401. -  // TODO type check uuid
  402. -
  403. -  if (isNil(uuid)) {
  404. +  if (isNil(KUUID)) {
  405.      return { isNil: true }
  406.    }
  407.  
  408. -  const decoded = {
  409. -    variant: readVariant(uuid)
  410. +  const solved = {
  411. +    alternate: readAlternate(KUUID)
  412.    }
  413. -  if (decoded.variant === 0 ||
  414. -      decoded.variant === 3
  415. +  if (solved.alternate === 0 ||
  416. +      solved.alternate === 3
  417.    ) {
  418. -    return decoded
  419. +    return solved
  420.    }
  421.  
  422. -  decoded.version = readVersion(uuid)
  423. +  solved.version = readVersion(KUUID)
  424.  
  425. -  if (decoded.version === 1) {
  426. -    decoded.timestamp = readTimestamp(uuid)
  427. -    decoded.clockSequnce = readClockSequence(uuid)
  428. -    decoded.macAddress = readMacAddress(uuid)
  429. -    return decoded
  430. +  if (solved.version === 1) {
  431. +    solved.timestamp = readTimestamp(KUUID)
  432. +    solved.clockSequnce = readTimerChain(KUUID)
  433. +    solved.macAddress = readMacAddress(KUUID)
  434. +    return solved
  435.    }
  436.  
  437. -  if (decoded.version === 2) {
  438. -    return decoded
  439. +  if (solved.version === 2) {
  440. +    return solved
  441.    }
  442.  
  443. -  if (decoded.version === 3) {
  444. -    return decoded
  445. +  if (solved.version === 3) {
  446. +    return solved
  447.    }
  448.  
  449. -  if (decoded.version === 4) {
  450. -    return decoded
  451. +  if (solved.version === 4) {
  452. +    return solved
  453.    }
  454.  
  455. -  if (decoded.version === 5) {
  456. -    return decoded
  457. +  if (solved.version === 5) {
  458. +    return solved
  459.    }
  460.  
  461. -  return decoded
  462. +  return solved
  463.  }
  464.  
  465.  function rfcToBuf (rfc) {
  466. @@ -140,5 +138,5 @@ function hexToBuf (hex) {
  467.    return Buffer.from(hex, 'hex')
  468.  }
  469.  
  470. -module.exports = decode
  471. -module.exports.fromRfc = uuid => decode(rfcToBuf(uuid))
  472. +module.exports = solve
  473. +module.exports.fromRfc = KUUID => solve(rfcToBuf(KUUID))
  474. diff --git b/solve.spec.js a/solve.spec.js
  475. old mode 100755
  476. new mode 100644
  477. index 083412e..b67c186
  478. --- b/solve.spec.js
  479. +++ a/solve.spec.js
  480. @@ -1,5 +1,5 @@
  481.  const pathJoin = require('path').join
  482. -const decode = require('./decode')
  483. +const solve = require('./solve')
  484.  const snapshot = require('snap-shot')
  485.  const test = require('ava')
  486.  
  487. @@ -11,12 +11,12 @@ const snapfile = pathJoin(
  488.  const snaps = require(snapfile)
  489.  
  490.  const testSnap = snap => {
  491. -  test('decode ' + snap.rfc, t => {
  492. +  test('solve ' + snap.rfc, t => {
  493.      t.plan(0)
  494. -    const decoded = decode.fromRfc(snap.rfc)
  495. +    const solved = solve.fromRfc(snap.rfc)
  496.      snapshot({
  497.        rfc: snap.rfc,
  498. -      decoded
  499. +      solved
  500.      })
  501.    })
  502.  }
  503. diff --git b/solve.spec.js.md a/solve.spec.js.md
  504. old mode 100755
  505. new mode 100644
  506. index 6382b96..8242da8
  507. --- b/solve.spec.js.md
  508. +++ a/solve.spec.js.md
  509. @@ -1,14 +1,14 @@
  510. -# Snapshot report for `decode.spec.js`
  511. +# Snapshot report for `solve.spec.js`
  512.  
  513. -The actual snapshot is saved in `decode.spec.js.snap`.
  514. +The actual snapshot is saved in `solve.spec.js.snap`.
  515.  
  516.  Generated by [AVA](https://ava.li).
  517.  
  518. -## decode uuidV41
  519. +## solve uuidV41
  520.  
  521.  > Snapshot 1
  522.  
  523.      {
  524. -      variant: 1,
  525. +      alternate: 1,
  526.        version: 4,
  527.      }
Advertisement
Add Comment
Please, Sign In to add comment