Compare commits

...

2 Commits

Author SHA1 Message Date
Bit Borealis e55a845642
finish parser script 2022-08-12 01:22:55 +00:00
Bit Borealis fc97b12562
initialize ts project 2022-08-11 15:02:51 +00:00
5 changed files with 99 additions and 5 deletions

40
src/parse.js Normal file
View File

@ -0,0 +1,40 @@
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) {
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));

43
src/parse.ts Normal file
View File

@ -0,0 +1,43 @@
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));

11
todo.md Normal file
View File

@ -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===`

View File

@ -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

5
tsconfig.json Normal file
View File

@ -0,0 +1,5 @@
{
"files": [
"src/parse.ts"
]
}