add vdk_colors and vdk_names libs

This commit is contained in:
2023-08-12 18:19:52 -06:00
parent 29defebc2e
commit d64045f0d7
13 changed files with 4425 additions and 1 deletions
+134 -1
View File
@@ -8,9 +8,15 @@ export {
// basic bytes functions // basic bytes functions
bytes_eq, bytes_eq,
bytes_concat, bytes_concat,
bytes_concat_arr,
strong_rand_bytes, strong_rand_bytes,
bytes_to_bigint, bytes_to_bigint,
bytes_to_bigint_little,
bigint_to_bytes, bigint_to_bytes,
bigint_to_bytes_little,
encode_utf8,
bytes_zeros,
bytes_to_hex_str,
// basic bits functions // basic bits functions
bits_null, bits_null,
bits_zeros, bits_zeros,
@@ -54,7 +60,7 @@ bytes_eq
/** /**
* Concatenate two arrays * Concatenate two byte arrays
*/ */
function function
bytes_concat bytes_concat
@@ -91,6 +97,22 @@ bytes_concat
/**
* Concatenate an arbitrary number of byte arrays
*
* Calling with the empty array as an argument returns an empty byte array
*/
function
bytes_concat_arr
(arrays : Array<Uint8Array>)
: Uint8Array
{
// reduce is what js calls foldl
return arrays.reduce(bytes_concat, new Uint8Array([]));
}
/** /**
* Cryptographically random bytes * Cryptographically random bytes
*/ */
@@ -110,6 +132,8 @@ strong_rand_bytes
* Convert a byte array to a bigint * Convert a byte array to a bigint
* *
* Equivalent to `binary:decode_unsigned/1` from Erlang * Equivalent to `binary:decode_unsigned/1` from Erlang
*
* Big endian byte order
*/ */
function function
bytes_to_bigint bytes_to_bigint
@@ -129,11 +153,32 @@ bytes_to_bigint
/**
* Convert a byte array to a bigint
*
* Equivalent to `binary:decode_unsigned/1` from Erlang
*
* Little endian byte order
*/
function
bytes_to_bigint_little
(bytes: Uint8Array)
: bigint
{
// reverse bytes and then call bytes_to_bigint
bytes.reverse();
return bytes_to_bigint(bytes);
}
/** /**
* Convert a bigint to a byte array * Convert a bigint to a byte array
* *
* Equivalent to `binary:encode_unsigned/1` from Erlang * Equivalent to `binary:encode_unsigned/1` from Erlang
* *
* Big endian byte order
*
* Requires input to be positive * Requires input to be positive
*/ */
function function
@@ -151,12 +196,100 @@ bigint_to_bytes
q /= 256n; q /= 256n;
arr_reverse.push(r); arr_reverse.push(r);
} }
// i wrote the big endian implementation first
// so leaving as is
// but if we wanted to be more l33t we would refactor this as calling
// `bigint_to_bytes_little` and reversing the result
arr_reverse.reverse(); arr_reverse.reverse();
return new Uint8Array(arr_reverse); return new Uint8Array(arr_reverse);
} }
/**
* Same as `bigint_to_bytes` but result array is reversed (i.e. little endian
* byte order)
*/
function
bigint_to_bytes_little
(q: bigint)
: Uint8Array
{
// copied from big endian implementation
if (q < 0n) {
throw new Error('q < 0n: ' + q);
}
let arr_reverse = [];
while (q > 0n) {
let r = Number(q % 256n);
q /= 256n;
arr_reverse.push(r);
}
// ah: BIG endian notation reverses the array
// we will just not do that
// arr_reverse.reverse();
return new Uint8Array(arr_reverse);
}
/**
* Encode a string in utf8
*/
function
encode_utf8
(str : string)
: Uint8Array
{
return (new TextEncoder()).encode(str);
}
/**
* Given number of zero bytes
*/
function
bytes_zeros
(byte_length: number)
: Uint8Array
{
let result : Uint8Array = new Uint8Array(byte_length);
for (let i0=0; i0<byte_length; i0++) {
result[i0] = 0;
}
return result;
}
/**
* Encode a byte string in hexadecimal
*/
function
bytes_to_hex_str
(bytes: Uint8Array)
: string
{
// make sure each byte is a 2 digit string
function byte_to_hex2(n: number): string {
if (n < 16)
return '0' + n.toString(16);
else
return n.toString(16);
}
let result: string = '';
for (let n of bytes) {
result += byte_to_hex2(n);
}
return result;
}
/** /**
* Oh no, bitstrings in a language that only has bytestrings * Oh no, bitstrings in a language that only has bytestrings
* *
+10
View File
@@ -0,0 +1,10 @@
dist
jex_mindist
jex_include
*.beam
*.swp
*.swo
test/testgen/b58_cases_2.eterms
test/testgen/b58_cases_3.eterms
__pycache__
docs/
+4
View File
@@ -0,0 +1,4 @@
# vdk_names
This is a library for taking a bytestring and giving it a human name. There are
4096 names. The use case is giving public keys human names in JR.
+5
View File
@@ -0,0 +1,5 @@
{type, library}.
{realm, local}.
{name, vdk_colors}.
{version, "0.1.0"}.
{deps, []}.
+24
View File
@@ -0,0 +1,24 @@
done:
- base64/base58 encoding/decoding
- rlp
today:
- pull apart data
tomorrow:
- basic easy serialization/deserialization
- serialize/deserialize examples
- (maybe) pull in awcp/sidekick
- sidekick: sign/propagate and sign/noprop
later:
- general node querying with parasite (think about autogeneration)
- simple aetto/ae/etc unit converter
- ghetto sophia ide
- general "send money to someone" page
GUI:
- simple testnet explorer
- contract ide
+38
View File
@@ -0,0 +1,38 @@
/**
* Miscellaneous helper functions for RGB/HSV color arithmetic
*
* Reference: https://en.wikipedia.org/wiki/HSL_and_HSV
*
* @module
*/
export {
};
export type {
rgb
}
/**
* standard red-green-blue color representation
*
* attributes `r`, `g` and `b` each are integers ranging within `0..255` inclusive
*/
type rgb
= {r : number,
g : number,
b : number};
/**
* hue-saturation-value color representation
*
- * `h` is an inte
*/
type hsv
= {h : number,
s : number,
v : number};
+16
View File
@@ -0,0 +1,16 @@
{"compilerOptions" : {"target" : "es2022",
"strict" : true,
"esModuleInterop" : true,
"skipLibCheck" : true,
"forceConsistentCasingInFileNames" : true,
"noImplicitAny" : true,
"strictNullChecks" : true,
"strictPropertyInitialization" : true,
"sourceMap" : true,
"outDir" : "dist",
"declaration" : true},
"$schema" : "https://json.schemastore.org/tsconfig",
"display" : "Recommended",
"include" : ["src/**/*"],
"exclude" : ["src/jex_include"],
"composite" : true}
+10
View File
@@ -0,0 +1,10 @@
dist
jex_mindist
jex_include
*.beam
*.swp
*.swo
test/testgen/b58_cases_2.eterms
test/testgen/b58_cases_3.eterms
__pycache__
docs/
+4
View File
@@ -0,0 +1,4 @@
# vdk_names
This is a library for taking a bytestring and giving it a human name. There are
4096 names. The use case is giving public keys human names in JR.
+5
View File
@@ -0,0 +1,5 @@
{type, library}.
{realm, local}.
{name, vdk_names}.
{version, "0.1.0"}.
{deps, []}.
+24
View File
@@ -0,0 +1,24 @@
done:
- base64/base58 encoding/decoding
- rlp
today:
- pull apart data
tomorrow:
- basic easy serialization/deserialization
- serialize/deserialize examples
- (maybe) pull in awcp/sidekick
- sidekick: sign/propagate and sign/noprop
later:
- general node querying with parasite (think about autogeneration)
- simple aetto/ae/etc unit converter
- ghetto sophia ide
- general "send money to someone" page
GUI:
- simple testnet explorer
- contract ide
File diff suppressed because it is too large Load Diff
+16
View File
@@ -0,0 +1,16 @@
{"compilerOptions" : {"target" : "es2022",
"strict" : true,
"esModuleInterop" : true,
"skipLibCheck" : true,
"forceConsistentCasingInFileNames" : true,
"noImplicitAny" : true,
"strictNullChecks" : true,
"strictPropertyInitialization" : true,
"sourceMap" : true,
"outDir" : "dist",
"declaration" : true},
"$schema" : "https://json.schemastore.org/tsconfig",
"display" : "Recommended",
"include" : ["src/**/*"],
"exclude" : ["src/jex_include"],
"composite" : true}