diff --git a/libs/vdk_base64/src/b58.ts b/libs/vdk_base64/src/b58.ts deleted file mode 100644 index 1fd30b8..0000000 --- a/libs/vdk_base64/src/b58.ts +++ /dev/null @@ -1,418 +0,0 @@ -/** - * Base58 encoding/decoding - */ - -export { - encode, - decode -} - - - -//============================================================================= -// ENCODING -//============================================================================= - -/** - * Encode a Uint8Array into base58 - */ -function -encode - (binary : Uint8Array) - : string -{ - let num_leading_zeros : number = nlz(binary); - let rest : Uint8Array = binary.slice(num_leading_zeros); - let ones : string = encode_zeros(num_leading_zeros); - let rest_b58 : string = encode_rest(rest); - let result : string = ones + rest_b58; - return result; -} - - - -/** - * count the number of leading zeros in a uint8array - * - * @internal - */ -function -nlz - (bytes: Uint8Array) - : number -{ - let n = 0; - for (let this_byte of bytes) - { - if (0 === this_byte) { n++; } - else { break; } - } - return n; -} - - - -/** - * Generate a bunch of '1's for however many leading zeros there are - * - * @internal - */ -function -encode_zeros - (how_many : number) - : string -{ - let ones : string = ''; - for (let i = 1; - i <= how_many; - i++) - { - ones += '1'; - } - - return ones; -} - - - -/** - * Encode a Uint8Array that has no leading zeros - * - * @internal - */ -function -encode_rest - (bytes : Uint8Array) - : string -{ - let bytes_bignum : bigint = bytes_to_bigint(bytes); - let result : string = bignum_to_base58(bytes_bignum); - return result; -} - - - -/** - * Convert a bytestring to a bignum - * - * @internal - */ -function -bytes_to_bigint - (bytes: Uint8Array) - : bigint -{ - let acc_bigint : bigint = 0n; - for(let this_byte of bytes) - { - acc_bigint <<= 8n; - acc_bigint += BigInt(this_byte); - } - return acc_bigint; -} - - - -/** - * Convert a BigInt to Base58 - * - * @internal - */ -function -bignum_to_base58 - (q: bigint) - : string -{ - let s = ''; - while (q !== 0n) - { - let this_n : bigint = q % 58n; - q /= 58n; - - let this_b58_char : string = bigint_to_char(this_n); - s = this_b58_char + s; - } - return s; -} - - - -//============================================================================= -// DECODING -//============================================================================= - -/** - * Decode a Base58 string into a Uint8Array - */ -function -decode - (base58: string) - : Uint8Array -{ - let num_leading_ones : number = nlo(base58); - let rest : string = base58.slice(num_leading_ones); - let zeros : Array = decode_ones(num_leading_ones); - let rest_arr : Array = decode_rest(rest); - let pre_result : Array = zeros.concat(rest_arr); - return new Uint8Array(pre_result); -} - - - -/** - * count the number of leading 1 characters in a uint8array - * - * @internal - */ -function -nlo - (base58: string) - : number -{ - let n = 0; - for (let this_char of base58) - { - if ('1' === this_char) { n++; } - else { break; } - } - return n; -} - - - -/** - * Generate a bunch of '0's for however many leading ones there are - * - * @internal - */ -function -decode_ones - (how_many : number) - : Array -{ - let zeros : Array = []; - for (let i = 1; - i <= how_many; - i++) - { - zeros.push(0); - } - - return zeros; -} - - - -/** - * Decode a string that has no leading 1s - * - * @internal - */ -function -decode_rest - (base58: string) - : Array -{ - let result_bignum : bigint = base58_to_bigint(base58); - let result : Array = bigint_to_base256(result_bignum); - return result; -} - - - -/** - * Convert a base58 string to a bignum - * - * @internal - */ -function -base58_to_bigint - (base58: string) - : bigint -{ - let acc_bigint : bigint = 0n; - for(let this_char of base58) - { - acc_bigint *= 58n; - acc_bigint += char_to_bigint(this_char); - } - return acc_bigint; -} - - -/** - * convert a bignum into a byte array - * - * @end - */ -function -bigint_to_base256 - (q: bigint) - : Array -{ - let arr_reverse = []; - while(q !== 0n) - { - let r: number = Number(q % 256n); - q /= 256n; - arr_reverse.push(r); - } - arr_reverse.reverse(); - return arr_reverse; -} - - -//============================================================================= -// TRANSLATION TABLES -//============================================================================= - - -/** - * Base58 integer -> character conversion table - * - * @internal - */ -function -bigint_to_char - (n: bigint) - : string -{ - switch(n) { - case 0n: return '1'; - case 1n: return '2'; - case 2n: return '3'; - case 3n: return '4'; - case 4n: return '5'; - case 5n: return '6'; - case 6n: return '7'; - case 7n: return '8'; - case 8n: return '9'; - case 9n: return 'A'; - case 10n: return 'B'; - case 11n: return 'C'; - case 12n: return 'D'; - case 13n: return 'E'; - case 14n: return 'F'; - case 15n: return 'G'; - case 16n: return 'H'; - case 17n: return 'J'; - case 18n: return 'K'; - case 19n: return 'L'; - case 20n: return 'M'; - case 21n: return 'N'; - case 22n: return 'P'; - case 23n: return 'Q'; - case 24n: return 'R'; - case 25n: return 'S'; - case 26n: return 'T'; - case 27n: return 'U'; - case 28n: return 'V'; - case 29n: return 'W'; - case 30n: return 'X'; - case 31n: return 'Y'; - case 32n: return 'Z'; - case 33n: return 'a'; - case 34n: return 'b'; - case 35n: return 'c'; - case 36n: return 'd'; - case 37n: return 'e'; - case 38n: return 'f'; - case 39n: return 'g'; - case 40n: return 'h'; - case 41n: return 'i'; - case 42n: return 'j'; - case 43n: return 'k'; - case 44n: return 'm'; - case 45n: return 'n'; - case 46n: return 'o'; - case 47n: return 'p'; - case 48n: return 'q'; - case 49n: return 'r'; - case 50n: return 's'; - case 51n: return 't'; - case 52n: return 'u'; - case 53n: return 'v'; - case 54n: return 'w'; - case 55n: return 'x'; - case 56n: return 'y'; - case 57n: return 'z'; - default: - throw new Error('invalid base58 bigint: ' + n); - } -} - - - -/** - * Base58 character -> integer conversion table - * - * @internal - */ -function -char_to_bigint - (s: string) - : bigint -{ - switch(s) { - case '1': return 0n; - case '2': return 1n; - case '3': return 2n; - case '4': return 3n; - case '5': return 4n; - case '6': return 5n; - case '7': return 6n; - case '8': return 7n; - case '9': return 8n; - case 'A': return 9n; - case 'B': return 10n; - case 'C': return 11n; - case 'D': return 12n; - case 'E': return 13n; - case 'F': return 14n; - case 'G': return 15n; - case 'H': return 16n; - case 'J': return 17n; - case 'K': return 18n; - case 'L': return 19n; - case 'M': return 20n; - case 'N': return 21n; - case 'P': return 22n; - case 'Q': return 23n; - case 'R': return 24n; - case 'S': return 25n; - case 'T': return 26n; - case 'U': return 27n; - case 'V': return 28n; - case 'W': return 29n; - case 'X': return 30n; - case 'Y': return 31n; - case 'Z': return 32n; - case 'a': return 33n; - case 'b': return 34n; - case 'c': return 35n; - case 'd': return 36n; - case 'e': return 37n; - case 'f': return 38n; - case 'g': return 39n; - case 'h': return 40n; - case 'i': return 41n; - case 'j': return 42n; - case 'k': return 43n; - case 'm': return 44n; - case 'n': return 45n; - case 'o': return 46n; - case 'p': return 47n; - case 'q': return 48n; - case 'r': return 49n; - case 's': return 50n; - case 't': return 51n; - case 'u': return 52n; - case 'v': return 53n; - case 'w': return 54n; - case 'x': return 55n; - case 'y': return 56n; - case 'z': return 57n; - default: - throw new Error('invalid base58 char: ' + s); - } -} diff --git a/libs/vdk_base64/src/bin.ts b/libs/vdk_base64/src/bin.ts deleted file mode 100644 index 958aa2a..0000000 --- a/libs/vdk_base64/src/bin.ts +++ /dev/null @@ -1,377 +0,0 @@ -/** - * Miscellaneous binary utility functions - * - * @module - */ - -export { - bytes_to_bigint, - bigint_to_bytes, - concat, - strong_rand_bytes -}; - - - -/** - * Concatenate two arrays - */ -function -concat - (arr1 : Uint8Array, - arr2 : Uint8Array) - : Uint8Array -{ - let len1 : number = arr1.length; - let len2 : number = arr2.length; - let arr1_idx0_offset : number = 0; - let arr2_idx0_offset : number = len1; - let result_len : number = len1 + len2; - let result : Uint8Array = new Uint8Array(result_len); - // copy first array into result - for (let arr1_idx0 = 0; - arr1_idx0 < len1; - arr1_idx0++) - { - // no offset here - let result_idx0 : number = arr1_idx0 + arr1_idx0_offset; - result[result_idx0] = arr1[arr1_idx0]; - } - // copy second array into result - for (let arr2_idx0 = 0; - arr2_idx0 < len2; - arr2_idx0++) - { - // offset by the length of the first array - let result_idx0 : number = arr2_idx0 + arr2_idx0_offset; - result[result_idx0] = arr2[arr2_idx0]; - } - return result; -} - - - -/** - * Cryptographically random bytes - */ -function -strong_rand_bytes - (how_many : number) - : Uint8Array -{ - let arr = new Uint8Array(how_many); - (new Crypto()).getRandomValues(arr); - return arr; -} - - - -/** - * Convert a byte array to a bigint - * - * Equivalent to `binary:decode_unsigned/1` from Erlang - */ -function -bytes_to_bigint - (bytes: Uint8Array) - : bigint -{ - let n : bigint = 0n; - for (let b of bytes) { - // move first, then add - // otherwise it ends on a move - // imperative languages are for losers - n <<= 8n; - n += BigInt(b); - } - return n; -} - - - -/** - * Convert a bigint to a byte array - * - * Equivalent to `binary:encode_unsigned/1` from Erlang - * - * Requires input to be positive - */ -function -bigint_to_bytes - (q: bigint) - : Uint8Array -{ - 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); - } - arr_reverse.reverse(); - return new Uint8Array(arr_reverse); -} - - - -/** - * Oh no, bitstrings in a language that only has bytestrings - * - * By convention these are `Uint8Array`s with byte length `ceil(bit_length / - * 8)`, and all trailing bits are zero. - */ -type bits = - {bit_length : number, - bytes : Uint8Array}; - - - -/** - * Get an uninitialized bitstring - * - * @internal - */ -function -bits_null - (bit_length : number) - : bits -{ - let byte_length : number = Math.ceil(bit_length / 8); - let result : Uint8Array = new Uint8Array(byte_length); - return {bit_length : bit_length, - bytes : result}; -} - - - -/** - * Get a bitstring of a given length where every value is 0. - */ -function -bits_zeros - (bit_length : number) - : bits -{ - let byte_length : number = Math.ceil(bit_length / 8); - let result : Uint8Array = new Uint8Array(byte_length); - for (let i0 = 0; - i0 < byte_length; - i0++) - { - result[i0] = 0; - } - return {bit_length : bit_length, - bytes : result}; -} - - - -/** - * Get a bitstring of a given length where every value is 1. - */ -function -bits_ones - (bit_length : number) - : bits -{ - let byte_length : number = Math.ceil(bit_length / 8); - let result : Uint8Array = new Uint8Array(byte_length); - - // fill everything except the last byte with 255s - for (let i0 = 0; - i0 < (byte_length - 1); - i0++) - { - result[i0] = 255; - } - - // alright so the last byte - // ok so the number of leading 0s is - // 8 - (bit_length % 8) - let num_trailing_zero_bits : number = 8 - (bit_length % 8); - // the trailing byte is 255 << that - // e.g. 3 trailing 0s - // 1111_1111 -> 1111_1000 - let last_byte : number = 255 << num_trailing_zero_bits; - let last_byte_idx0 : number = byte_length - 1; - result[last_byte_idx0] = last_byte; - - return {bit_length : bit_length, - bytes : result}; -} - - - -/** - * Get the bit at a given 0-index - */ -function -bits_i0th - (bit_idx0 : number, - bits : bits) - : number -{ - // first task is figuring out what byte we're at - // for instance if we want bit 27 - // 3*8 = 24 =< 27 < 4*8 - // so it's Math.floor(bit_idx0 / 8) - let byte_idx0 : number = Math.floor(bit_idx0 / 8); - // let's fetch the byte and work with that - let the_byte : number = bits.bytes[byte_idx0]; - - // ok so let's go with 27 again - // 27 = 3 mod 8 - // so we bitshift right by (8 - 3) - // and then take the remainder dividing by 2 - // --B-_---- -> ----_---B -> 0000_000B - let bsr : number = 8 - (bit_idx0 % 8); - return (the_byte >> bsr) % 2; -} - - - -/** - * Concatenate two bitstrings - */ -function -bits_concat - (bits1 : bits, - bits2 : bits) - : bits -{ - let result_bit_length : number = bits1.bit_length + bits2.bit_length; - let bytes1 : Uint8Array = bits1.bytes; - let bytes2 : Uint8Array = bits2.bytes; - // using zeros here because of our xor trick in a minute - let result_bits : bits = bits_null(result_bit_length); - let result_bytes : Uint8Array = result_bits.bytes; - - // go along each byte in result, and compute the byte boundary - for (let i = 0; - i < result_bytes.length; - i++) - { - // ABCD_EFGH _ - // 0123_4567 8 - // this is the bit index of the leftmost bit in this byte - let start_bit_bi0 : number = i * 8; - let next_start_bit_bi0 : number = start_bit_bi0 + 8; - // does the bit at the beginning of this byte correspond to the first array? - // strict comparison: - // suppose i = 0, - // suppose bit_length1 is 0 - // then this says no, go to second array - // suppose bl1 = 1, - // this says start at first array - let start_bit_is_of_first_array : boolean = start_bit_bi0 < bits1.bit_length; - // weak comparison: - // suppose bit_length1 = 8 - // ABCD_EFGH _ - // 0123_4567 8 - // ^ - // start_bit_bi0 ^ next_start_bit_bi0 - let stop_bit_is_of_first_array : boolean = next_start_bit_bi0 <= bits1.bit_length; - // is this a bytes1 byte - let is_bytes1_byte : boolean = start_bit_is_of_first_array && stop_bit_is_of_first_array; - let is_boundary_byte : boolean = start_bit_is_of_first_array && !stop_bit_is_of_first_array; - - // need to work out the ping_pong bs up here because js is dumb and I - // can't put lets between elseifs - // alright, now we're in the case of only copying from the second array - // we have two cases: - // ping-pong: - // ABCD_EFGH 1234_5678 - // - ---- --- - // copying these bits - // ping: - // ABCD_EFGH - // - ---- 000 - // - // how to distinguish between these two?? - // - // we're in the ping case when the start_bit_bi0 corresponds to the - // final byte of the second array - // - // ok - // - // we need to compute the bit address in the second array that - // corresponds to the bit address at the beginning of this byte in the - // result array - let bits2_addr_bi0 : number = start_bit_bi0 - bits1.bit_length; - // I think the variable is - // bits_left_to_copy = bits2.bit_length - bit_addr2_bi0 - // no + 1 because the current bit is uncopied, - // so if bit_addr2_bi0 = 7 and bits2.bit_length is 8, it means we - // have the last bit to copy - // cases: - // bits_left_to_copy <= 0 -> - // this would mean we have more bits to copy, but are out of - // source bits. should be impossible if bits_null is correct - // bits_left_to_copy <= 8 -> - // this would mean we are going to fill this last byte in the - // result array with the correct bits from array2, but how we - // do this will depend on how those are arranged in bytes2 - // (whether we grab one or two bytes) - // - // this is the tricky case - // - // I think what matters here is the byte address in the second array - // if we're on the last byte - // 8 < bits_left_to_copy -> - // this means we can safely grab two bytes from bytes2, and do - // our bitshifting to make it correct - // - // FIXME - let bits_left_to_copy : number = bits2.bit_length - bits2_addr_bi0; - // no the variable that matters is which byte we're on in the result - let this_bytes2_addr_i0 : number = Math.floor(bits2_addr_bi0 / 8); - let next_bytes2_addr_i0 : number = this_bytes2_addr_i0 + 1; - let bitshift_amt : number = bits1.bit_length % 8; - - let ping : boolean = next_bytes2_addr_i0 === bytes2.length; - - - // simple case: this is a byte from the first array - // just copy it - if (is_bytes1_byte) - result_bytes[i] = bytes1[i]; - // this is a boundary byte - // further cases: - // second bit length is 0 -> - // ABCD_EF-- - // ^ starting here - // just copy first byte and move along - else if (is_boundary_byte && (bits2.bit_length === 0)) - result_bytes[i] = bytes1[i]; - // boundary byte, and there is at least one byte in the second array - else if (is_boundary_byte) - { - // copy over the first byte - result_bytes[i] = bytes1[i]; - // take the first byte from the second array - let first_byte_of_second_array : number = bytes2[0]; - // bytes1: - // ABCD_EF00 - // 6 - // bytes2: - // 1234_5678 - // 0000_0012 - // and bitshift it right by that amount - let bitshift_amt : number = bits1.bit_length % 8; - result_bytes[i] ^= first_byte_of_second_array >> bitshift_amt; - } - // last byte of second array - else if (ping) - result_bytes[i] = (bytes2[this_bytes2_addr_i0] << bitshift_amt); - // ping-pong: not last byte of second array - else - result_bytes[i] = (bytes2[this_bytes2_addr_i0] << bitshift_amt) ^ (bytes2[next_bytes2_addr_i0] << bitshift_amt); - } - - return {bit_length : result_bit_length, - bytes : result_bytes}; - -} diff --git a/libs/vdk_base64/src/faert.ts b/libs/vdk_base64/src/faert.ts deleted file mode 100644 index 16c4c43..0000000 --- a/libs/vdk_base64/src/faert.ts +++ /dev/null @@ -1,399 +0,0 @@ -/** - * FÆRT: Fast Æternity Recovery Text - * - * Reference: https://gitlab.com/zxq9/passgas/-/blob/83607fedb08be5dfd03210e331f9c76125bb3467/fullofbeans - * - * @module - */ - -export { - encode, - decode, - byte_of_word, - check_word, - check_byte, - words -} - -import * as safe from './safe.js'; - - - -/** - * Encode a bytestring into FÆRT - */ -function -encode - (bytes : Uint8Array) - : string -{ - // initial accumulator - let words_acc : Array = []; - - // go along each byte and look up the word - // add it to the accumulator - for (let this_byte of bytes) - { - let this_word = words[this_byte]; - words_acc.push(this_word); - } - - // prepend check word to phrase - // yes craig it would be faster to do the check byte inline - // the usage case here is 64 byte strings - // double pass is not a big deal - return check_word(bytes) + ' ' + words_acc.join(' '); -} - - - -/** - * Decode a FAERT string into bytes - */ -function -decode - (faert : string) - : safe.Safe -{ - let all_words : Array = faert.split(' '); - let input_check_word : string = all_words[0]; - let input_words : Array = all_words.slice(1) - - // accumulator - let computed_bytes : Array = []; - - // loop over words and figure out accumulator - for (let this_input_word of input_words) - { - let maybe_this_byte : safe.Safe = byte_of_word(this_input_word); - // if the word is an allowable word, add it to the accumulator - if (maybe_this_byte.ok) - { - let this_byte : number = maybe_this_byte.result; - computed_bytes.push(this_byte); - } - // error case, propagate the error up the call chain - else - return maybe_this_byte; - } - - // at this point, we can assume we correctly decoded all words - // compute the check byte - let computed_bytes_u8s : Uint8Array = new Uint8Array(computed_bytes); - let computed_check_word : string = check_word(computed_bytes_u8s); - - // check if it is correct - // if so, return the computed bytes - if (computed_check_word === input_check_word) - return safe.ok(computed_bytes_u8s); - // otherwise, return an error - else - return safe.error('checksum failure! computed check word: ' + computed_check_word + '; input check word: ' + input_check_word); -} - - - -/** - * given a word, find its index - */ -function -byte_of_word - (word: string) - : safe.Safe -{ - for (let i=0; i<=255; i++) - { - if (word === words[i]) - return safe.ok(i); - } - return safe.error('invalid word: ' + word); -} - - - -/** - * compute the check word of an array - */ -function -check_word - (bytes: Uint8Array) - : string -{ - return words[check_byte(bytes)]; -} - - -/** - * given an array, compute the xor of all the bytes in the array - */ -function -check_byte - (bytes: Uint8Array) - : number -{ - let check_byte = 0; - for (let this_byte of bytes) - check_byte ^= this_byte; - return check_byte; -} - - - -let words = [ - "able", - "abuse", - "acquire", - "adjust", - "agent", - "air", - "alert", - "alpha", - "anger", - "answer", - "any", - "argue", - "around", - "assume", - "asthma", - "aunt", - "awkward", - "balcony", - "barrel", - "because", - "behave", - "bicycle", - "bike", - "blind", - "blouse", - "bonus", - "bottom", - "breeze", - "brother", - "bubble", - "burger", - "butter", - "can", - "cannon", - "cargo", - "catalog", - "caught", - "century", - "chaos", - "chicken", - "churn", - "cinnamon", - "clever", - "clock", - "clown", - "collect", - "conduct", - "convince", - "correct", - "cradle", - "crane", - "crime", - "cross", - "crystal", - "curve", - "daughter", - "debris", - "decrease", - "deny", - "deputy", - "despair", - "diesel", - "dinner", - "distance", - "document", - "donate", - "drama", - "drink", - "dutch", - "earth", - "educate", - "elbow", - "employ", - "endless", - "enlist", - "enter", - "equal", - "eternal", - "example", - "exhibit", - "exotic", - "faculty", - "famous", - "fault", - "feature", - "fiber", - "filter", - "firm", - "flame", - "flush", - "follow", - "forward", - "frame", - "fringe", - "future", - "game", - "gate", - "gift", - "glare", - "glow", - "goat", - "grab", - "grief", - "guilt", - "hand", - "hawk", - "health", - "hen", - "hire", - "honey", - "hover", - "hurry", - "hybrid", - "impose", - "inch", - "inherit", - "injury", - "install", - "iron", - "jazz", - "joke", - "just", - "kit", - "kitchen", - "language", - "laundry", - "leader", - "lend", - "leopard", - "license", - "live", - "lobster", - "lounge", - "magnet", - "mail", - "mansion", - "mass", - "math", - "media", - "message", - "metal", - "misery", - "mix", - "more", - "mountain", - "museum", - "mutual", - "narrow", - "nerve", - "next", - "note", - "obey", - "obtain", - "offer", - "once", - "orange", - "ostrich", - "over", - "owner", - "palace", - "patch", - "pave", - "pen", - "phrase", - "piece", - "pizza", - "plunge", - "polar", - "pool", - "power", - "pretty", - "private", - "protect", - "pulp", - "purity", - "quit", - "quote", - "raise", - "razor", - "recall", - "region", - "relief", - "rent", - "rescue", - "review", - "ride", - "risk", - "roof", - "rough", - "saddle", - "salmon", - "sand", - "scene", - "scrub", - "season", - "seek", - "series", - "shed", - "shoe", - "sick", - "silent", - "situate", - "skill", - "slide", - "slush", - "snack", - "solar", - "soul", - "special", - "sphere", - "spot", - "spy", - "stairs", - "stereo", - "strategy", - "stuff", - "success", - "sunny", - "surge", - "swear", - "symptom", - "tail", - "ten", - "tent", - "theme", - "thumb", - "tiny", - "toe", - "tooth", - "topic", - "trade", - "trash", - "trophy", - "truly", - "tumble", - "typical", - "uncle", - "unfair", - "until", - "upper", - "useless", - "van", - "venue", - "video", - "visa", - "vocal", - "walk", - "waste", - "wedding", - "weekend", - "wide", - "winner", - "wise", - "wood", - "wrist", - "zero" -] diff --git a/libs/vdk_base64/src/rlp.ts b/libs/vdk_base64/src/rlp.ts deleted file mode 100644 index 8c48b75..0000000 --- a/libs/vdk_base64/src/rlp.ts +++ /dev/null @@ -1,521 +0,0 @@ -/** - * Ethereum Recursive-Length Prefix Encoding implementation - * - * Two reference implementations: - * - * 1. Ethereum Python implementation - * 2. My Erlang implementation (agrees with Ethereum Python in randomized - * tests) - * - * This work can be found in ../test/testgen/ - * - * FIXME: need to have Safe assertions for "i am decoding a list", "i am - * decoding a binary", "I am decoding something with no remainder", etc - * - * @module - */ - -export { - decoded_data, - decode_result, - decode, - encode -} - - -//============================================================================= -//============================================================================= -// DECODING -//============================================================================= -//============================================================================= - - -/** - * Data that RLP can encode. Also the result of decoding. - */ -type decoded_data - = Uint8Array - | Array; - - - -/** - * Decoding can have a remainder - */ -type decode_result - = {decoded_data : decoded_data, - remainder : Uint8Array}; - - - -/** - * Decode an RLP-encoded bytestring into a `decode_result` (decoded data + - * whatever wasn't consumed) - */ -function -decode - (bytes: Uint8Array) - : decode_result -{ - // check the first byte - let first_byte: number = bytes[0]; - let rest : Uint8Array = bytes.slice(1); - // if the first byte is between 0 and 127, that is the data - if - (first_byte <= 127) { - return dr(new Uint8Array([first_byte]), rest); - } - // if the first byte is between 128 and 183 = 128 + 55, it is a bytestring - // and the length is Byte - 128 - else if - (first_byte <= 183) { - let payload_byte_length : number = first_byte - 128; - let payload : Uint8Array = rest.slice(0, payload_byte_length); - let rest2 : Uint8Array = rest.slice(payload_byte_length); - return dr(payload, rest2); - } - // if the first byte is between 184 = 183 + 1 and 191 = 183 + 8, it is a - // bytestring. the byte length of bytestring is FirstByte - 183. Then pull - // out the actual data - else if - (first_byte <= 191) { - let byte_length_of_byte_length : number = first_byte - 183; - let bytes_of_byte_length : Uint8Array = rest.slice(0, byte_length_of_byte_length); - let byte_length : number = bytes_to_number(bytes_of_byte_length); - let bytes : Uint8Array = rest.slice(byte_length_of_byte_length, - byte_length + byte_length_of_byte_length); - let rest2 : Uint8Array = rest.slice(byte_length + byte_length_of_byte_length); - return dr(bytes, rest2); - } - // If the first byte is between 192 and 247 = 192 + 55, it is a list. The - // byte length of the list-payload is FirstByte - 192. Then the list - // payload, which needs to be decoded on its own. - else if - (first_byte <= 247) { - let byte_length_of_list : number = first_byte - 192; - let list_payload : Uint8Array = rest.slice(0, byte_length_of_list); - let list : Array = decode_list(list_payload); - let rest2 : Uint8Array = rest.slice(byte_length_of_list); - return dr(list, rest2); - } - // If the first byte is between 248 = 247 + 1 and 255 = 247 + 8, it is a - // list. The byte length of the byte length of the list-payload is - // FirstByte - 247. Then the byte length of the list. Then the list - // payload, which needs to be decoded on its own. - else { - let byte_length_of_byte_length : number = first_byte - 247; - let bytes_of_byte_length : Uint8Array = rest.slice(0, byte_length_of_byte_length); - let byte_length : number = bytes_to_number(bytes_of_byte_length); - let list_bytes : Uint8Array = rest.slice(byte_length_of_byte_length, - byte_length + byte_length_of_byte_length); - let list : Array = decode_list(list_bytes); - let rest2 : Uint8Array = rest.slice(byte_length + byte_length_of_byte_length); - return dr(list, rest2); - } -} - - - -/** - * Decode a list payload (non-prefixed) into an `Array`. - * - * Repeatedly decodes an element off the list until remainder is empty. - * - * @internal - */ -function -decode_list - (bytes: Uint8Array) - : Array -{ - let arr : Array = []; - while (bytes.length > 0) { - // grab an item off the bytes - let {decoded_data, remainder} = decode(bytes); - // push it - arr.push(decoded_data); - // update bytes - bytes = remainder; - } - return arr; -} - - - -/** - * Convert bytestring to number - * - * @internal - */ -function -bytes_to_number - (bytes: Uint8Array) - : number -{ - let n : number = 0; - for (let b of bytes) - { - n <<= 8; - n += b; - } - return n; -} - - - -/** - * Make a `decode_result` containing the two arguments - * - * @internal - */ -function -dr - (x : decoded_data, - y : Uint8Array) - : decode_result -{ - return {decoded_data : x, - remainder : y}; -} - - - -//============================================================================= -//============================================================================= -// ENCODING -//============================================================================= -//============================================================================= - -/** - * Encode some decoded data - */ -function -encode - (data: decoded_data) - : Uint8Array -{ - // is it an array or data - if - (is_binary(data)) { - return encode_binary(data as Uint8Array); - } - else if - (is_list(data)) { - return encode_list(data as Array); - } - else { - throw new Error('encode told to encode something that is not an array or a binary: ' + data); - } -} - - - -/** - * Encode a binary into RLP - * - * @internal - */ -function -encode_binary - (bytes: Uint8Array) - : Uint8Array -{ - let len: number = bytes.length; - // single byte case when the byte is between 0..127 - // result is the bytestring containing the byte itself - if - ((len === 1) && - (bytes[0] <= 127)){ - return bytes; - } - // if the bytestring is 0..55 bytes long, the first byte in the result is - // 128 + Length, the rest of the result bytestring is the input string - else if - (len <= 55) { - // construct the result - // <<128 + Len, Bytes/binary>> - let result : Uint8Array = new Uint8Array(len + 1); - // first byte is 128 + length - result[0] = 128 + len; - // copy input bytes into result - for (let input_idx0 = 0; - input_idx0 < len; - input_idx0++) - { - let result_idx0 : number = input_idx0 + 1; - result[result_idx0] = bytes[input_idx0]; - } - return result; - } - // if the bytestring is more than 55 bytes long, the first byte is 183 + - // ByteLengthOfByteLength, followed by the byte length, followed by the - // bytes - else { - let len_bytes : Uint8Array = encode_unsigned(len); - let len_bytes_length : number = len_bytes.length; - // total array is - // <<183 + len_bytes_length, len_bytes, Bytes>> - // 1 byte len_bytes_length bytes len bytes - let result : Uint8Array = new Uint8Array(1 + len_bytes_length + len); - // <<183 + len_bytes_length, len_bytes, Bytes>> - // 1 byte len_bytes_length bytes len bytes - // - // ^ YOU ARE HERE - result[0] = 183 + len_bytes_length; - // copy len_bytes into result - for (let len_bytes_idx0 = 0; - len_bytes_idx0 < len_bytes_length; - len_bytes_idx0++) - { - // <<183 + len_bytes_length, len_bytes, Bytes>> - // 1 byte len_bytes_length bytes len bytes - // - // ^ YOU ARE HERE - let result_idx0: number = len_bytes_idx0 + 1; - result[result_idx0] = len_bytes[len_bytes_idx0]; - } - // copy original byte array into result - for (let input_idx0 = 0; - input_idx0 < len; - input_idx0++) - { - // <<183 + len_bytes_length, len_bytes, Bytes>> - // 1 byte len_bytes_length bytes len bytes - // - // ^ YOU ARE HERE - let result_idx0: number = input_idx0 + (1 + len_bytes_length); - result[result_idx0] = bytes[input_idx0]; - } - // finally, return the result - return result; - } -} - - - -/** - * Encode a list - * - * @internal - */ -function -encode_list - (list: Array) - : Uint8Array -{ - // first encode every element in the list, then branch - let payloads : Array = list.map(encode); - let payload : Uint8Array = uint8arr_concat(payloads); - let payload_size : number = payload.length; - // if the payload size is in 0..55, then the first byte is 192 + payload - // size, followed by the payload - if - (payload_size <= 55) { - // result: <<(192 + Payload_Size), Payload/binary >>; - // 1 byte payload_size bytes - let result : Uint8Array = new Uint8Array(1 + payload_size); - // result: <<(192 + Payload_Size), Payload/binary >>; - // 1 byte payload_size bytes - // - // ^ YOU ARE HERE - result[0] = 192 + payload_size; - // copy the rest of the payload into result - for (let payload_idx0 = 0; - payload_idx0 < payload_size; - payload_idx0++) - { - // result: <<(192 + Payload_Size), Payload/binary >>; - // 1 byte payload_size bytes - // - // ^ YOU ARE HERE - let result_idx0: number = payload_idx0 + 1; - result[result_idx0] = payload[payload_idx0]; - } - return result; - } - // if the payload size is greater than 55, the first byte is 247 + - // size_of_payload_size, followed by the payload size, followed by the - // payload - else { - // compute the payload size size - let payload_size_bytes : Uint8Array = encode_unsigned(payload_size); - let payload_size_size : number = payload_size_bytes.length; - // result = <<(247 + payload_size_size), payload_size_bytes/binary, payload/binary >> - // 1 byte payload_size_size bytes payload_size bytes - let result: Uint8Array = new Uint8Array(1 + payload_size_size + payload_size); - // result = <<(247 + payload_size_size), payload_size_bytes/binary, payload/binary >> - // 1 byte payload_size_size bytes payload_size bytes - // - // ^ YOU ARE HERE - // first byte is 247 + payload_size_size - result[0] = 247 + payload_size_size; - // copy the payload_size_bytes into result - for (let psb_idx0 = 0; - psb_idx0 < payload_size_size; - psb_idx0++) - { - // result = <<(247 + payload_size_size), payload_size_bytes/binary, payload/binary >> - // 1 byte payload_size_size bytes payload_size bytes - // - // ^ YOU ARE HERE - // offset is 1 byte for this - let result_idx0 : number = psb_idx0 + 1; - result[result_idx0] = payload_size_bytes[psb_idx0]; - } - // copy the payload into result - for (let pb_idx0 = 0; - pb_idx0 < payload_size; - pb_idx0++) - { - // result = <<(247 + payload_size_size), payload_size_bytes/binary, payload/binary >> - // 1 byte payload_size_size bytes payload_size bytes - // - // ^ YOU ARE HERE - // offset is 1 + payload_size_size bytes for this - let result_idx0 : number = pb_idx0 + (1 + payload_size_size); - result[result_idx0] = payload[pb_idx0]; - } - // finally, return result - return result; - } -} - - - -/** - * Encode a number as a bytestring - * - * Not for general use, this assumes the input number is greater than 55 - * - * @internal - */ -function -encode_unsigned - (n: number) - : Uint8Array -{ - // can assume that n initially is greater than 55 - // have to encode it in reverse order - let arr : Array = []; - // repeated division by 256 with remainder - // - // example: suppose bign was 1234 and we were dividing by 10 - // - // iteration 0: - // bign = 1234 - // arr = [] - // --- - // bign / 10 = 123 - // bign % 10 = 4 - // --- - // bign = 123 - // arr = [4] - // - // iteration 1: - // bign = 123 - // arr = [4] - // --- - // bign / 10 = 12 - // bign % 10 = 3 - // --- - // bign = 12 - // arr = [4, 3] - // - // iteration 2: - // bign = 12 - // arr = [4, 3] - // --- - // bign / 10 = 1 - // bign % 10 = 2 - // --- - // bign = 1 - // arr = [4, 3, 2] - // - // iteration 3: - // bign = 1 - // arr = [4, 3, 2] - // --- - // bign / 10 = 0 - // bign % 10 = 1 - // --- - // bign = 0 - // arr = [4, 3, 2, 1] - // - // iteration 4: - // bign = 0 - // arr = [4, 3, 2, 1] - // --- - // DONE - while (n > 0) { - arr.push(n % 256); - n >>= 8; - } - // reverse and make into Uint8Array - arr.reverse(); - return new Uint8Array(arr); -} - - - -/** - * concatenate an array of `Uint8Array`s into a single Uint8Array - * - * @internal - */ -function -uint8arr_concat - (arrs: Array) - : Uint8Array -{ - // total length - let total_len = arrs.reduce(// fold - function (acc_len: number, this_uint8array: Uint8Array): number { - return acc_len + this_uint8array.length; - }, - // initial accumulator - 0); - // start up result - let result : Uint8Array = new Uint8Array(total_len); - let result_idx0 : number = 0; - // concatenate - for (let this_uint8arr of arrs) { - // bytewise copy of this uint8array - for (let this_uint8arr_idx0 = 0; - this_uint8arr_idx0 < this_uint8arr.length; - this_uint8arr_idx0++) - { - // copy byte and increment result idx0 - result[result_idx0] = this_uint8arr[this_uint8arr_idx0]; - result_idx0++; - } - } - return result; -} - -/** - * returns true if input is `instanceof Uint8Array` - * - * @internal - */ -function -is_binary - (x: any) - : boolean -{ - return (x instanceof Uint8Array); -} - - - -/** - * returns true if input is `instanceof Array` - * - * @internal - */ -function -is_list - (x: any) - : boolean -{ - return (x instanceof Array); -} diff --git a/libs/vdk_base64/src/safe.ts b/libs/vdk_base64/src/safe.ts deleted file mode 100644 index b3e4c0c..0000000 --- a/libs/vdk_base64/src/safe.ts +++ /dev/null @@ -1,112 +0,0 @@ -/** - * "Safe" error handling - * - * The idea here is that there are situations where it is known - * - * ```typescript - * type Safe - * = Ok - * | Error; - * - * type Ok - * = {ok : true, - * result : ok_t}; - * - * type Error - * = {ok : false, - * error : err_t}; - * ``` - * - * 1. a given function call is likely to fail - * 2. the likely errors can be enumerated - * - * These are called "positive errors". An example would be a page script - * asking a browser wallet extension to sign a transaction. The following - * errors, among others, are likely: - * - * - the user does not have a wallet installed - * - the user has a wallet but does not have the correct signing key - * - the user rejects the transaction - * - the sign request timed out - * - * These errors should not generate exceptions, as these behaviors are to some - * degree "expected". - */ - -export { - Safe, - Ok, - Error, - ok, - error, - unsafe -} - - -/** - * Type that catches positive errors - */ -type Safe - = Ok - | Error; - - -/** - * Ok type - */ -type Ok - = {ok : true, - result : ok_t}; - - -/** - * Error type - */ -type Error - = {ok : false, - error : err_t}; - - -/** - * Constructs an `Ok` value from a pure value - */ -function -ok - - (x : ok_t) - : Ok -{ - return {ok: true, result: x}; -} - - - -/** - * Constructs an `Error` value from a pure value - */ -function -error - - (x: err_t) - : Error -{ - return {ok: false, error: x}; -} - - - -/** - * Takes a `Safe` value, if `ok`, returns the `ok_t`, or if an error throws the - * `err_t` - */ -function -unsafe - - (x: Safe) - : ok_t -{ - if (x.ok) - return x.result; - else - throw x.error; -} diff --git a/libs/vdk_base64/src/b64.ts b/libs/vdk_base64/src/vdk_base64.ts similarity index 100% rename from libs/vdk_base64/src/b64.ts rename to libs/vdk_base64/src/vdk_base64.ts diff --git a/libs/vdk_binary/src/bin.ts b/libs/vdk_binary/src/vdk_binary.ts similarity index 100% rename from libs/vdk_binary/src/bin.ts rename to libs/vdk_binary/src/vdk_binary.ts