Skip to content

Commit

Permalink
Fix formatting and package details
Browse files Browse the repository at this point in the history
  • Loading branch information
lnbc1QWFyb24 committed Aug 6, 2022
1 parent 47dd740 commit e7e3bcc
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 144 deletions.
24 changes: 12 additions & 12 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended', 'prettier'],
plugins: ['svelte3', '@typescript-eslint'],
parserOptions: {
sourceType: 'module',
ecmaVersion: 2020
},
env: {
browser: true,
es2017: true
}
root: true,
parser: '@typescript-eslint/parser',
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended', 'prettier'],
plugins: ['svelte3', '@typescript-eslint'],
parserOptions: {
sourceType: 'module',
ecmaVersion: 2020
},
env: {
browser: true,
es2017: true
}
}
10 changes: 5 additions & 5 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"useTabs": true,
"semi": false,
"singleQuote": true,
"trailingComma": "none",
"printWidth": 100
"useTabs": false,
"semi": false,
"singleQuote": true,
"trailingComma": "none",
"printWidth": 100
}
32 changes: 17 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ A simple JavaScript libary for decoding [runes](https://github.com/rustyrussell/

Install directly from the repo:

**Yarn**
### Yarn

`yarn add https://github.com/clams-tech/rune-decoder`

**NPM**
### NPM

`npm i https://github.com/clams-tech/rune-decoder`

## Usage
Expand All @@ -20,24 +22,24 @@ Using the decode method you can get a similar result to that of passing a rune t
import { decode } from 'decode-rune'

const rune =
'aTEhoWOAllxYDgWSUyGPEKVeUwr-MG_Il1HXZis1MYs9NCZtZXRob2RebGlzdHxtZXRob2ReZ2V0fG1ldGhvZD1zdW1tYXJ5Jm1ldGhvZC9saXN0ZGF0YXN0b3Jl'
'aTEhoWOAllxYDgWSUyGPEKVeUwr-MG_Il1HXZis1MYs9NCZtZXRob2RebGlzdHxtZXRob2ReZ2V0fG1ldGhvZD1zdW1tYXJ5Jm1ldGhvZC9saXN0ZGF0YXN0b3Jl'

const decoded = decode(rune)

console.log(decoded)

// {
// id: '4',
// hash: '693121a16380965c580e059253218f10a55e530aff30ffc89751d7662b35318b',
// restrictions: [
// {
// alternatives: ['method^list', 'method^get', 'method=summary'],
// summary: 'method starts with list OR method starts with get OR method is equal to summary'
// },
// {
// alternatives: ['method/listdatastore'],
// summary: 'method is not equal to listdatastore'
// }
// ]
// id: '4',
// hash: '693121a16380965c580e059253218f10a55e530aff30ffc89751d7662b35318b',
// restrictions: [
// {
// alternatives: ['method^list', 'method^get', 'method=summary'],
// summary: 'method starts with list OR method starts with get OR method is equal to summary'
// },
// {
// alternatives: ['method/listdatastore'],
// summary: 'method is not equal to listdatastore'
// }
// ]
// }
```
41 changes: 21 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
{
"name": "rune-decoder",
"version": "0.0.1",
"description": "Decode runes",
"main": "dist/index.js",
"scripts": {
"build": "tsc",
"prepare": "npm run build"
},
"author": "Aaron Barnard",
"license": "MIT",
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.27.0",
"@typescript-eslint/parser": "^5.27.0",
"eslint": "^8.16.0",
"eslint-config-prettier": "^8.3.0",
"prettier": "^2.6.2"
},
"dependencies": {
"typescript": "^4.7.4"
}
"name": "rune-decoder",
"version": "0.0.1",
"description": "Decode runes",
"main": "dist/index.js",
"scripts": {
"build": "tsc",
"prepare": "npm run build"
},
"types": "dist/types",
"author": "Aaron Barnard",
"license": "MIT",
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.27.0",
"@typescript-eslint/parser": "^5.27.0",
"eslint": "^8.16.0",
"eslint-config-prettier": "^8.3.0",
"prettier": "^2.6.2"
},
"dependencies": {
"typescript": "^4.7.4"
}
}
66 changes: 33 additions & 33 deletions src/base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,48 +23,48 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

export const Base64Binary = {
_keyStr: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
_keyStr: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',

removePaddingChars: function (input: string) {
const lkey = this._keyStr.indexOf(input.charAt(input.length - 1))
removePaddingChars: function (input: string) {
const lkey = this._keyStr.indexOf(input.charAt(input.length - 1))

if (lkey == 64) {
return input.substring(0, input.length - 1)
}
if (lkey == 64) {
return input.substring(0, input.length - 1)
}

return input
},
return input
},

decode: function (input: string) {
//get last chars to see if are valid
input = this.removePaddingChars(input)
input = this.removePaddingChars(input)
decode: function (input: string) {
//get last chars to see if are valid
input = this.removePaddingChars(input)
input = this.removePaddingChars(input)

const bytes = parseInt(((input.length / 4) * 3).toString(), 10)
const bytes = parseInt(((input.length / 4) * 3).toString(), 10)

let chr1, chr2, chr3
let enc1, enc2, enc3, enc4
let i = 0
let j = 0
let chr1, chr2, chr3
let enc1, enc2, enc3, enc4
let i = 0
let j = 0

const uarray = new Uint8Array(bytes)
const uarray = new Uint8Array(bytes)

for (i = 0; i < bytes; i += 3) {
//get the 3 octects in 4 ascii chars
enc1 = this._keyStr.indexOf(input.charAt(j++))
enc2 = this._keyStr.indexOf(input.charAt(j++))
enc3 = this._keyStr.indexOf(input.charAt(j++))
enc4 = this._keyStr.indexOf(input.charAt(j++))
for (i = 0; i < bytes; i += 3) {
//get the 3 octects in 4 ascii chars
enc1 = this._keyStr.indexOf(input.charAt(j++))
enc2 = this._keyStr.indexOf(input.charAt(j++))
enc3 = this._keyStr.indexOf(input.charAt(j++))
enc4 = this._keyStr.indexOf(input.charAt(j++))

chr1 = (enc1 << 2) | (enc2 >> 4)
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2)
chr3 = ((enc3 & 3) << 6) | enc4
chr1 = (enc1 << 2) | (enc2 >> 4)
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2)
chr3 = ((enc3 & 3) << 6) | enc4

uarray[i] = chr1
if (enc3 != 64) uarray[i + 1] = chr2
if (enc4 != 64) uarray[i + 2] = chr3
}
uarray[i] = chr1
if (enc3 != 64) uarray[i + 1] = chr2
if (enc4 != 64) uarray[i + 2] = chr3
}

return uarray
}
return uarray
}
}
48 changes: 24 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,37 @@ import { binaryHashToHex, operatorToDescription, runeOperatorRegex } from './uti
export * from './types'

export const decode = (rune: string) => {
const runeBinary = Base64Binary.decode(rune)
const hashBinary = runeBinary.slice(0, 32)
const hash = binaryHashToHex(hashBinary)
const restBinary = runeBinary.slice(32)
const runeBinary = Base64Binary.decode(rune)
const hashBinary = runeBinary.slice(0, 32)
const hash = binaryHashToHex(hashBinary)
const restBinary = runeBinary.slice(32)

const [uniqueId, ...restrictionStrings] = new TextDecoder().decode(restBinary).split('&')
const [uniqueId, ...restrictionStrings] = new TextDecoder().decode(restBinary).split('&')

const id = uniqueId.split('=')[1]
const id = uniqueId.split('=')[1]

const restrictions = restrictionStrings.map((restriction) => {
const alternatives = restriction.split('|')
const restrictions = restrictionStrings.map((restriction) => {
const alternatives = restriction.split('|')

const summary = alternatives.reduce((str, alternative) => {
const [operator] = alternative.match(runeOperatorRegex) || []
const summary = alternatives.reduce((str, alternative) => {
const [operator] = alternative.match(runeOperatorRegex) || []

if (!operator) return str
if (!operator) return str

const [name, value] = alternative.split(operator)
const [name, value] = alternative.split(operator)

return `${str ? `${str} OR ` : ''}${name} ${operatorToDescription(operator)} ${value}`
}, '')
return `${str ? `${str} OR ` : ''}${name} ${operatorToDescription(operator)} ${value}`
}, '')

return {
alternatives,
summary
}
})
return {
alternatives,
summary
}
})

return {
id,
hash,
restrictions
}
return {
id,
hash,
restrictions
}
}
10 changes: 5 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export type Restriction = {
alternatives: string[]
summary: string
alternatives: string[]
summary: string
}

export type DecodedRune = {
id: string
hash: string
restrictions: Restriction[]
id: string
hash: string
restrictions: Restriction[]
}
60 changes: 30 additions & 30 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
function i2hex(i: number) {
return ('0' + i.toString(16)).slice(-2)
return ('0' + i.toString(16)).slice(-2)
}

export const binaryHashToHex = (hash: Uint8Array): string => {
return hash.reduce(function (memo, i) {
return memo + i2hex(i)
}, '')
return hash.reduce(function (memo, i) {
return memo + i2hex(i)
}, '')
}

export const runeOperatorRegex = /[=^$/~<>{}#!]/g

export const operatorToDescription = (operator: string): string => {
switch (operator) {
case '=':
return 'is equal to'
case '^':
return 'starts with'
case '$':
return 'ends with'
case '/':
return 'is not equal to'
case '~':
return 'contains'
case '<':
return 'is less than'
case '>':
return 'is greater than'
case '{':
return 'sorts before'
case '}':
return 'sorts after'
case '#':
return 'comment'
case '!':
return 'is missing'
default:
return ''
}
switch (operator) {
case '=':
return 'is equal to'
case '^':
return 'starts with'
case '$':
return 'ends with'
case '/':
return 'is not equal to'
case '~':
return 'contains'
case '<':
return 'is less than'
case '>':
return 'is greater than'
case '{':
return 'sorts before'
case '}':
return 'sorts after'
case '#':
return 'comment'
case '!':
return 'is missing'
default:
return ''
}
}

0 comments on commit e7e3bcc

Please sign in to comment.