mirror of
https://github.com/sigmasternchen/axowall
synced 2025-03-15 00:28:54 +00:00
switch to typescript
This commit is contained in:
parent
626888fbd9
commit
1877b1aa6e
6 changed files with 43 additions and 29 deletions
|
@ -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": {
|
||||
|
|
|
@ -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) {
|
23
src/hash.js
23
src/hash.js
|
@ -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
32
src/hash.ts
Normal 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)));
|
||||
}
|
|
@ -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
5
tsconfig.json
Normal file
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "es2019"
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue