Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git b/index.js a/index.js
- old mode 100755
- new mode 100644
- index f9aa6fe..06573e2
- --- b/index.js
- +++ a/index.js
- @@ -1,10 +1,11 @@
- -const R = require('ramda')
- -const uv = require('uuid-validate')
- +const Ramda = require('ramda')
- +const keplerUuidValidate = require('uuid-validate')
- const b64 = hex => Buffer.from(hex.replace(/-/g, ''), 'hex').toString('base64')
- const b64ToHex = b64 => Buffer.from(b64, 'base64').toString('hex')
- +
- // RFC 4122 : 8-4-4-4-12
- -const hexToRfc = hex => {
- +const hdToKeplerUUID = hex => {
- let x = 0
- const rfc = hex.substring(x, x += 8) +
- '-' + hex.substring(x, x += 4) +
- @@ -14,9 +15,9 @@ const hexToRfc = hex => {
- return rfc
- }
- -const coinable = (die, planchet) => die.length <= 22
- +const mnemonic = (extract, byteBlanket) => extract.length <= 22
- -const coin = (die, planchet) => die + b64(planchet).slice(die.length, planchet.length)
- +const beforeMnemonic = (extract, byteBlanket) => extract + b64(byteBlanket).slice(extract.length, byteBlanket.length)
- const flipChar = c => {
- if (c !== c.toUpperCase(c)) {
- @@ -34,26 +35,26 @@ const flipCharAt = (string, index) => {
- return string.slice(0, index) + fc + string.slice(index + 1)
- }
- -const coinAll = (die, planchet) => {
- - const planchet64 = b64(planchet)
- - const candidates = R.map(
- - i => planchet64.slice(0, i) + die + planchet64.slice(i + die.length),
- - R.range(0, 23 - die.length)
- +const mnemonicAll = (extract, byteBlanket) => {
- + const byteBlanket64 = b64(byteBlanket)
- + const candidates = Ramda.map(
- + i => byteBlanket64.slice(0, i) + extract + byteBlanket64.slice(i + extract.length),
- + Ramda.range(0, 23 - extract.length)
- )
- const rtn = []
- candidates.forEach(candidate => {
- - const candidateRfc = R.pipe(
- - b64ToHex, hexToRfc
- + const candidateRfc = Ramda.pipe(
- + b64ToHex, hdToKeplerUUID
- )(candidate)
- - if (uv(candidateRfc)) {
- + if (keplerUuidValidate(candidateRfc)) {
- rtn.push(candidate)
- return
- }
- const candidate2 = flipCharAt(candidate, 8)
- - const candidate2Rfc = R.pipe(
- - b64ToHex, hexToRfc
- + const candidate2Rfc = Ramda.pipe(
- + b64ToHex, hdToKeplerUUID
- )(candidate2)
- - if (uv.version(candidate2Rfc) === 4) {
- + if (keplerUuidValidate.version(candidate2Rfc) === 4) {
- rtn.push(candidate2)
- }
- })
- @@ -61,8 +62,8 @@ const coinAll = (die, planchet) => {
- }
- module.exports = {
- - coin,
- - coinAll,
- - coinable,
- + beforeMnemonic,
- + mnemonicAll,
- + mnemonic,
- flipCharAt
- }
- diff --git b/index.spec.js a/index.spec.js
- old mode 100755
- new mode 100644
- index 65043d9..fd8fbb2
- --- b/index.spec.js
- +++ a/index.spec.js
- @@ -2,86 +2,79 @@ import test from 'ava'
- import * as sut from './index'
- -test('coinable, die short', t => {
- - const planchet = 'c8404eda-83cb-4728-bf68-21bd1bac26b8'
- - const die = 'jane+doe'
- - const actual = sut.coinable(die, planchet)
- +test('mnemonic, extract short', t => {
- + const byteBlanket = ''
- + const extract = ''
- + const actual = sut.mnemonic(extract, byteBlanket)
- t.true(actual)
- })
- -test('coinable, die exact length', t => {
- - const planchet = 'c8404eda-83cb-4728-bf68-21bd1bac26b8'
- - const die = 'jane+doe+0123456789abc'
- - const actual = sut.coinable(die, planchet)
- +test('mnemonic, extract exact length', t => {
- + const byteBlanket = ''
- + const extract = ''
- + const actual = sut.mnemonic(extract, byteBlanket)
- t.true(actual)
- })
- -test('coinable, die too long', t => {
- - const planchet = 'c8404eda-83cb-4728-bf68-21bd1bac26b8'
- - const die = 'jane+doe+0123456789abcd'
- - const actual = sut.coinable(die, planchet)
- +test('mnemonic, extract too long', t => {
- + const byteBlanket = ''
- + const extract = ''
- + const actual = sut.mnemonic(extract, byteBlanket)
- t.false(actual)
- })
- -test('coin', t => {
- - const planchet = 'c8404eda-83cb-4728-bf68-21bd1bac26b8'
- - // yEBO2oPLRyi/aCG9G6wmuA==
- - const die = 'jane+doe'
- - const actual = sut.coin(die, planchet)
- - const expected = 'jane+doeRyi/aCG9G6wmuA=='
- +test('beforeMnemonic', t => {
- + const byteBlanket = ''
- + //
- + const extract = ''
- + const actual = sut.beforeMnemonic(extract, byteBlanket)
- + const expected = ''
- t.is(actual, expected)
- })
- -test('coinAll', t => {
- - const planchet = 'c8404eda-83cb-4728-bf68-21bd1bac26b8'
- - // yEBO2oPLRyi/aCG9G6wmuA==
- - const die = 'jane+d'
- - const actual = sut.coinAll(die, planchet)
- +test('mnemonicAll', t => {
- + const byteBlanket = ''
- + //
- + const extract = 'jane+d'
- + const actual = sut.mnemonicAll(extract, byteBlanket)
- const expected = [
- - 'jane+dPLRyi/aCG9G6wmuA==',
- - 'yjane+dLRyi/aCG9G6wmuA==',
- - 'yEjane+dRyi/aCG9G6wmuA==',
- - // 'yEBjane+dyi/aCG9G6wmuA==',
- - // 'yEBOjane+di/aCG9G6wmuA==',
- - // 'yEBO2jane+d/aCG9G6wmuA==',
- - // 'yEBO2ojane+daCG9G6wmuA==',
- - // 'yEBO2oPjane+dCG9G6wmuA==',
- - // 'yEBO2oPLjane+dG9G6wmuA==',
- - 'yEBO2oPLRjane+d9G6wmuA==',
- - // 'yEBO2oPLRyjane+dG6wmuA==',
- - 'yEBO2oPLRyijane+d6wmuA==',
- - 'yEBO2oPLRyi/jane+dwmuA==',
- - 'yEBO2oPLRyi/ajane+dmuA==',
- - 'yEBO2oPLRyi/aCjane+duA==',
- - 'yEBO2oPLRyi/aCGjane+dA==',
- - 'yEBO2oPLRyi/aCG9jane+d=='
- + '',
- + '',
- + '',
- + '',
- + '',
- + '',
- + '',
- + '',
- + '',
- + ''
- ]
- t.deepEqual(actual, expected)
- })
- -test('coinAll, case flipping', t => {
- - const planchet = 'c8404eda-83cb-4728-bf68-21bd1bac26b8'
- - // yEBO2oPLRyi/aCG9G6wmuA==
- - const die = 'jane+r'
- - const actual = sut.coinAll(die, planchet)
- +test('mnemonicAll, case flipping', t => {
- + const byteBlanket = ''
- + //
- + const extract = ''
- + const actual = sut.mnemonicAll(extract, byteBlanket)
- const expected = [
- - 'jane+rPLRyi/aCG9G6wmuA==',
- - 'yjane+rLRyi/aCG9G6wmuA==',
- - 'yEjane+rRyi/aCG9G6wmuA==',
- - 'yEBjane+Ryi/aCG9G6wmuA==', // case flip r -> R
- - // 'yEBOjane+ri/aCG9G6wmuA==',
- - // 'yEBO2jane+r/aCG9G6wmuA==',
- - // 'yEBO2ojane+raCG9G6wmuA==',
- - // 'yEBO2oPjane+rCG9G6wmuA==',
- - // 'yEBO2oPLjane+rG9G6wmuA==',
- - 'yEBO2oPLRjane+r9G6wmuA==',
- - // 'yEBO2oPLRyjane+rG6wmuA==',
- - 'yEBO2oPLRyijane+r6wmuA==',
- - 'yEBO2oPLRyi/jane+rwmuA==',
- - 'yEBO2oPLRyi/ajane+rmuA==',
- - 'yEBO2oPLRyi/aCjane+ruA==',
- - 'yEBO2oPLRyi/aCGjane+rA==',
- - 'yEBO2oPLRyi/aCG9jane+r=='
- + '',
- + '',
- + '',
- + '', // case flip r -> R
- + // '',
- + // '',
- + // '',
- + // '',
- + // '',
- + '',
- + // '',
- + '',
- + '',
- + '',
- + '',
- + '',
- + ''
- ]
- t.deepEqual(actual, expected)
- })
- diff --git b/nameCodec.js a/nameCodec.js
- old mode 100755
- new mode 100644
- index e09d9fa..9e57c7d
- --- b/nameCodec.js
- +++ a/nameCodec.js
- @@ -7,10 +7,10 @@
- // return hash(namespace + name)
- // }
- -// function readDigest(uuid) {
- -// return uuid
- +// function readDigest(KUUID) {
- +// return KUUID
- // }
- -// function writeDigest(uuid) {
- -// const variant = readVariant
- +// function writeDigest(KUUID) {
- +// const alternate = readAlternate
- // }
- diff --git b/package.json a/package.json
- old mode 100755
- new mode 100644
- index 39632ab..53f83e9
- --- b/package.json
- +++ a/package.json
- @@ -1,15 +1,15 @@
- {
- - "name": "uncoined",
- - "version": "0.0.0",
- - "description": "",
- + "name": "kepler-mnemonic",
- + "version": "1.0.0",
- + "description": "Kepler's UUID Mnemonic Javascript Library",
- "main": "index.js",
- "scripts": {
- "jsdoc": "rm -rf docs && jsdoc src --recurse --destination docs --configure jsdoc.json",
- "test": "mocha --require jsdoctest src",
- "posttest": "standard && npm run jsdoc"
- },
- - "author": "",
- - "license": "ISC",
- + "author": "Bench Computer Inc. <[email protected]>",
- + "license": "BENCH",
- "devDependencies": {
- "ava": "^0.25.0",
- "jsdoc": "^3.5.5",
- diff --git b/solve.js a/solve.js
- old mode 100755
- new mode 100644
- index 3207edf..afbc331
- --- b/solve.js
- +++ a/solve.js
- @@ -6,10 +6,10 @@ const libtime = require('./timestamp')
- */
- /**
- - * [§ 4.1.1 Variant]{@link https://tools.ietf.org/html/rfc4122#section-4.1.1}
- + * [§ 4.1.1 Alternate]{@link https://tools.ietf.org/html/rfc4122#section-4.1.1}
- */
- -function readVariant (uuid) {
- - const octit = uuid.readUInt8(8) >> 5
- +function readAlternate (KUUID) {
- + const octit = KUUID.readUInt8(8) >> 5
- return [0, 0, 0, 0, 1, 1, 2, 3][octit]
- }
- @@ -18,44 +18,44 @@ function readVariant (uuid) {
- * {@link https://tools.ietf.org/html/rfc4122#section-4.1.2}
- */
- -function readTimeLow (uuid) {
- - return uuid.readUInt32BE(0)
- +function readTimeLow (KUUID) {
- + return KUUID.readUInt32BE(0)
- }
- -function readTimeMid (uuid) {
- - return uuid.readUInt16BE(4)
- +function readTimeMid (KUUID) {
- + return KUUID.readUInt16BE(4)
- }
- -function readTimeHighAndVersion (uuid) {
- - return uuid.readUInt16BE(6)
- +function readTimeHighAndVersion (KUUID) {
- + return KUUID.readUInt16BE(6)
- }
- -function readClockSequenceAndReserved (uuid) {
- - return uuid.readUInt16BE(8)
- +function readTimerChainAndReserved (KUUID) {
- + return KUUID.readUInt16BE(8)
- }
- -function readNode (uuid) {
- - return uuid.readUIntBE(10, 6)
- +function readNode (KUUID) {
- + return KUUID.readUIntBE(10, 6)
- }
- /**
- * [§ 4.1.3 Version]{@link https://tools.ietf.org/html/rfc4122#section-4.1.3}
- */
- -function readVersion (uuid) {
- - return uuid.readUInt8(6) >> 4
- +function readVersion (KUUID) {
- + return KUUID.readUInt8(6) >> 4
- }
- /**
- * [§ 4.1.4 Timestamp]{@link https://tools.ietf.org/html/rfc4122#section-4.1.4}
- */
- -function readTimestamp (uuid) {
- +function readTimestamp (KUUID) {
- const unixTime = libtime.toUnixTime(
- - readTimeHighAndVersion(uuid) & 0x0FFF,
- - readTimeMid(uuid),
- - readTimeLow(uuid)
- + readTimeHighAndVersion(KUUID) & 0x0FFF,
- + readTimeMid(KUUID),
- + readTimeLow(KUUID)
- )
- const nanoSeconds =
- - libtime.toNanoSeconds(readTimeLow(uuid))
- + libtime.toNanoSeconds(readTimeLow(KUUID))
- const date = new Date(unixTime * 1000)
- return date.toISOString().slice(0, -4) + nanoSeconds + 'Z'
- }
- @@ -63,72 +63,70 @@ function readTimestamp (uuid) {
- /**
- * [§ 4.1.5 Clock Sequence]{@link https://tools.ietf.org/html/rfc4122#section-4.1.5}
- */
- -function readClockSequence (uuid) {
- - const variant = readVariant(uuid)
- - const mask = [0x7fff, 0x3fff, 0x1ff, 0x1ff][variant]
- - return readClockSequenceAndReserved(uuid) & mask
- +function readTimerChain (KUUID) {
- + const alternate = readAlternate(KUUID)
- + const mask = [0x7fff, 0x3fff, 0x1ff, 0x1ff][alternate]
- + return readTimerChainAndReserved(KUUID) & mask
- }
- /**
- * [§ 4.1.6 Node]{@link https://tools.ietf.org/html/rfc4122#section-4.1.6}
- * @see readNode
- */
- -function readMacAddress (uuid) {
- - return readNode(uuid).toString(16).match(/.{2}/g).join(':')
- +function readMacAddress (KUUID) {
- + return readNode(KUUID).toString(16).match(/.{2}/g).join(':')
- }
- /**
- * [§ 4.1.7 Nil UUID]{@link https://tools.ietf.org/html/rfc4122#section-4.1.7}
- * @see readNode
- */
- -function isNil (uuid) {
- - return Buffer.alloc(16).equals(uuid)
- +function isNil (KUUID) {
- + return Buffer.alloc(16).equals(KUUID)
- }
- -// These need to be factored out
- +function solve (KUUID /* buffer */) {
- + // TODO type check KUUID
- -function decode (uuid /* buffer */) {
- - // TODO type check uuid
- -
- - if (isNil(uuid)) {
- + if (isNil(KUUID)) {
- return { isNil: true }
- }
- - const decoded = {
- - variant: readVariant(uuid)
- + const solved = {
- + alternate: readAlternate(KUUID)
- }
- - if (decoded.variant === 0 ||
- - decoded.variant === 3
- + if (solved.alternate === 0 ||
- + solved.alternate === 3
- ) {
- - return decoded
- + return solved
- }
- - decoded.version = readVersion(uuid)
- + solved.version = readVersion(KUUID)
- - if (decoded.version === 1) {
- - decoded.timestamp = readTimestamp(uuid)
- - decoded.clockSequnce = readClockSequence(uuid)
- - decoded.macAddress = readMacAddress(uuid)
- - return decoded
- + if (solved.version === 1) {
- + solved.timestamp = readTimestamp(KUUID)
- + solved.clockSequnce = readTimerChain(KUUID)
- + solved.macAddress = readMacAddress(KUUID)
- + return solved
- }
- - if (decoded.version === 2) {
- - return decoded
- + if (solved.version === 2) {
- + return solved
- }
- - if (decoded.version === 3) {
- - return decoded
- + if (solved.version === 3) {
- + return solved
- }
- - if (decoded.version === 4) {
- - return decoded
- + if (solved.version === 4) {
- + return solved
- }
- - if (decoded.version === 5) {
- - return decoded
- + if (solved.version === 5) {
- + return solved
- }
- - return decoded
- + return solved
- }
- function rfcToBuf (rfc) {
- @@ -140,5 +138,5 @@ function hexToBuf (hex) {
- return Buffer.from(hex, 'hex')
- }
- -module.exports = decode
- -module.exports.fromRfc = uuid => decode(rfcToBuf(uuid))
- +module.exports = solve
- +module.exports.fromRfc = KUUID => solve(rfcToBuf(KUUID))
- diff --git b/solve.spec.js a/solve.spec.js
- old mode 100755
- new mode 100644
- index 083412e..b67c186
- --- b/solve.spec.js
- +++ a/solve.spec.js
- @@ -1,5 +1,5 @@
- const pathJoin = require('path').join
- -const decode = require('./decode')
- +const solve = require('./solve')
- const snapshot = require('snap-shot')
- const test = require('ava')
- @@ -11,12 +11,12 @@ const snapfile = pathJoin(
- const snaps = require(snapfile)
- const testSnap = snap => {
- - test('decode ' + snap.rfc, t => {
- + test('solve ' + snap.rfc, t => {
- t.plan(0)
- - const decoded = decode.fromRfc(snap.rfc)
- + const solved = solve.fromRfc(snap.rfc)
- snapshot({
- rfc: snap.rfc,
- - decoded
- + solved
- })
- })
- }
- diff --git b/solve.spec.js.md a/solve.spec.js.md
- old mode 100755
- new mode 100644
- index 6382b96..8242da8
- --- b/solve.spec.js.md
- +++ a/solve.spec.js.md
- @@ -1,14 +1,14 @@
- -# Snapshot report for `decode.spec.js`
- +# Snapshot report for `solve.spec.js`
- -The actual snapshot is saved in `decode.spec.js.snap`.
- +The actual snapshot is saved in `solve.spec.js.snap`.
- Generated by [AVA](https://ava.li).
- -## decode uuidV41
- +## solve uuidV41
- > Snapshot 1
- {
- - variant: 1,
- + alternate: 1,
- version: 4,
- }
Advertisement
Add Comment
Please, Sign In to add comment