switch to typescript

This commit is contained in:
overflowerror 2024-08-16 20:58:03 +02:00
parent 626888fbd9
commit 1877b1aa6e
6 changed files with 43 additions and 29 deletions

View file

@ -2,9 +2,9 @@
"name": "axowall",
"version": "0.0.1",
"description": "",
"main": "src/main.js",
"main": "src/main.ts",
"scripts": {
"build": "esbuild src/main.js --bundle --outfile=public/bundle.js",
"build": "esbuild src/main.ts --bundle --outfile=public/bundle.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {

View file

@ -6,7 +6,7 @@ const suffixCharset = [
].flatMap(set => set.split(""));
const suffixCharsetBits = 6;
export function makeSuffix(index) {
export function makeSuffix(index: number): string {
let result = "";
while (index > 0) {

View file

@ -1,23 +0,0 @@
const hexCharset = "0123456789abcdef".split("");
export const sha256 = "SHA-256";
Uint8Array.prototype.toHex = function() {
return [...this].map(c => hexCharset[c >> 4] + hexCharset[c & 15]).join("");
};
Uint8Array.prototype.hasPrefix = function(array, bits) {
for(let i = 0; i < array.length && i * 8 < bits; i++) {
const bitsForByte = Math.min(8, bits - i * 8);
const bitMask = ((1 << bitsForByte) - 1) << (8 - bitsForByte);
if ((this[i] & bitMask) != (array[i] & bitMask)) {
return false;
}
}
return true;
};
export async function digest(algo, input) {
return new Uint8Array(await crypto.subtle.digest(algo, new TextEncoder().encode(input)));
}

32
src/hash.ts Normal file
View file

@ -0,0 +1,32 @@
const hexCharset = "0123456789abcdef".split("");
export const sha256: AlgorithmIdentifier = "SHA-256";
declare global {
interface Uint8Array {
toHex(): string;
hasPrefix(prefix: Uint8Array, bits: number): boolean;
}
}
Uint8Array.prototype.toHex = function(): string {
return [...this].map(c => hexCharset[c >> 4] + hexCharset[c & 15]).join("");
};
Uint8Array.prototype.hasPrefix = function(prefix: Uint8Array, bits: number): boolean {
for(let i = 0; i < prefix.length && i * 8 < bits; i++) {
const bitsForByte = Math.min(8, bits - i * 8);
const bitMask = ((1 << bitsForByte) - 1) << (8 - bitsForByte);
const currentBits = this[i] & bitMask;
const prefixBits = prefix[i] & bitMask;
if (currentBits != prefixBits) {
return false;
}
}
return true;
};
export async function digest(algo: AlgorithmIdentifier, input: string): Promise<Uint8Array> {
return new Uint8Array(await crypto.subtle.digest(algo, new TextEncoder().encode(input)));
}

View file

@ -1,12 +1,12 @@
import {makeSuffix} from "./content";
import {digest, sha256} from "./hash";
async function findHashWithPrefix(hashPrefixBits, inputPrefix) {
const hashPrefix = new Uint8Array(Array(Math.ceil(hashPrefixBits / 8)).map(c => 0));
async function findHashWithPrefix(hashPrefixBits: number, inputPrefix: string): Promise<string> {
const hashPrefix = new Uint8Array(Array(Math.ceil(hashPrefixBits / 8)).map(_ => 0));
let iteration = 0;
do {
var message = inputPrefix + makeSuffix(iteration++)
var message = inputPrefix + makeSuffix(iteration++);
var hash = await digest(sha256, message);
} while (!hash.hasPrefix(hashPrefix, hashPrefixBits));

5
tsconfig.json Normal file
View file

@ -0,0 +1,5 @@
{
"compilerOptions": {
"target": "es2019"
}
}