From e55a84564259410a5ddbe9881931f701d3812148 Mon Sep 17 00:00:00 2001 From: Bit Borealis Date: Fri, 12 Aug 2022 01:22:55 +0000 Subject: [PATCH] finish parser script --- src/parse.js | 43 ++++++++++++++++++++++++++++++++++++++----- src/parse.ts | 48 ++++++++++++++++++++++++++++++++++++++++++------ todo.md | 11 +++++++++++ todo.txt | 5 ----- tsconfig.json | 4 +++- 5 files changed, 94 insertions(+), 17 deletions(-) create mode 100644 todo.md delete mode 100644 todo.txt diff --git a/src/parse.js b/src/parse.js index 2002254..afdb106 100644 --- a/src/parse.js +++ b/src/parse.js @@ -1,7 +1,40 @@ -// number will be 0 if valid -// 1 if no id ( creator ) -// 2 if encryption key is missing / malformed / wrong length +var baseUrl = "https://0x0.st"; +var id = "a0eiaje"; // < unicode > +var key = "LhBRttf+oI/PNHyTBrPa7HGDK/uC9fcMkGGgz63zT4Y="; // 32 bytes b64 +var completeUrl = baseUrl + "/" + id + "#" + key; +console.log(completeUrl); +var urlObj = new URL(completeUrl); +/** will return a tuple of the key and id if valid + * 1 if no id ( creator ) + * 2 if encryption key is missing / wrong length + * 3 if key is malformed + */ function validUrl(url) { - // do stuff - return 0; + var key; + try { + key = atob(url.hash.substring(1)); + } + catch (e) { + return 3; + } + var id = url.pathname.substring(1); + if (id === "/") { + return 1; + } + var binKey = binaryToArray(key); + if (key.length === 32) { + return [id, binKey]; + } + else { + return 2; + } } +function binaryToArray(raw) { + var rawLength = raw.length; + var array = new Uint8Array(new ArrayBuffer(rawLength)); + for (var i = 0; i < rawLength; i++) { + array[i] = raw.charCodeAt(i); + } + return array; +} +console.log(validUrl(urlObj)); diff --git a/src/parse.ts b/src/parse.ts index d9d4a25..dfa1150 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -1,7 +1,43 @@ -// number will be 0 if valid -// 1 if no id ( creator ) -// 2 if encryption key is missing / malformed / wrong length -function validUrl(url: string): number { - // do stuff - return 0 +const baseUrl: string = "https://0x0.st" +const id: string = "a0eiaje" // < unicode > +const key: string = "LhBRttf+oI/PNHyTBrPa7HGDK/uC9fcMkGGgz63zT4Y=" // 32 bytes b64 +const completeUrl: string = baseUrl + "/" + id + "#" + key +console.log(completeUrl); +const urlObj:URL = new URL(completeUrl); + +/** will return a tuple of the key and id if valid + * 1 if no id ( creator ) + * 2 if encryption key is missing / wrong length + * 3 if key is malformed + */ +function validUrl(url: URL): [string,Uint8Array] | number { + var key: string + try { + key = atob(url.hash.substring(1)) + } catch(e) { + return 3 + } + const id: string = url.pathname.substring(1); + if ( id === "/" ) { + return 1 + } + const binKey = binaryToArray(key) + if ( key.length === 32 ) { + return [id,binKey] + } + else { + return 2 + } } + +function binaryToArray(raw: string): Uint8Array { + var rawLength = raw.length; + var array = new Uint8Array(new ArrayBuffer(rawLength)); + + for(var i = 0; i < rawLength; i++) { + array[i] = raw.charCodeAt(i); + } + return array; +} + +console.log(validUrl(urlObj)); diff --git a/todo.md b/todo.md new file mode 100644 index 0000000..b3be9ea --- /dev/null +++ b/todo.md @@ -0,0 +1,11 @@ +- [x] baseurl / id # < 32 bytes of b64 ( key ) > + - parse handling happypath and stormypath + - no id < no creator > + - < key missing / malformed / wrong length > + - if malformed re-request the url + - test : + Valid: `LhBRttf+oI/PNHyTBrPa7HGDK/uC9fcMkGGgz63zT4Y=` + Short: `bi83sPUmUNknOITgXStHuCfimOjpELmizvVnW8LL2Q==` + Long: `Yj3ryVkyOzDg6UbEHkWiNkNZcbbR5wTwtm6TZ6F7OLCI` + Invalid: `bZLMOC157NAlKtFP3wHXkXj0eLAu6E/Bv6rGL4HLQ===` + diff --git a/todo.txt b/todo.txt deleted file mode 100644 index d050361..0000000 --- a/todo.txt +++ /dev/null @@ -1,5 +0,0 @@ -- baseurl / id # < 32 bytes of b64 ( key ) > - - parse handling happypath and stormypath - - no id < no creator > - - < key missing / malformed / wrong length > - - if malformed re-request the url diff --git a/tsconfig.json b/tsconfig.json index d46fd1e..751bfc5 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,3 +1,5 @@ { - "include": ["src/**/*", "tests/**/*"] + "files": [ + "src/parse.ts" + ] }