polyfill implementation

This commit is contained in:
Aravinth Manivannan 2021-12-07 15:35:10 +05:30
parent bdbcb95e05
commit 04845d7719
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
6 changed files with 97 additions and 82 deletions

1
.gitignore vendored
View file

@ -7,3 +7,4 @@ node_modules/
coverage
dist/
docs
tmp

View file

@ -21,7 +21,7 @@
"build": "webpack --mode production",
"start": "rimraf dist pkg && webpack-dev-server --open -d",
"test": "yarn build && yarn jest",
"doc": "yarn build && yarn typedoc js/index.ts"
"doc": "yarn typedoc src/index.ts"
},
"devDependencies": {
"@types/jest": "^27.0.2",
@ -37,7 +37,7 @@
"ts-jest": "^27.0.5",
"ts-loader": "^9.2.6",
"ts-node": "^10.3.0",
"typedoc": "^0.22.5",
"typedoc": "^0.22.10",
"typescript": "^4.4.4",
"webpack": "^5.58.2",
"webpack-cli": "^4.9.0",

View file

@ -1,12 +1,77 @@
/*
* mCaptcha is a PoW based DoS protection software.
* This is the frontend web component of the mCaptcha system
* Copyright © 2021 Aravinth Manivnanan <realaravinth@batsense.net>.
*
* Use of this source code is governed by Apache 2.0 or MIT license.
* You shoud have received a copy of MIT and Apache 2.0 along with
* this program. If not, see <https://spdx.org/licenses/MIT.html> for
* MIT or <http://www.apache.org/licenses/LICENSE-2.0> for Apache.
*/
const U128_MAX = 340282366920938463463374607431768211455n;
/**
* compute SHA-256 digest of a string
* @property {string} message - a string on which hash needs to be computed
**/
const digest = async (message: string) => {
const msgUint8 = new TextEncoder().encode(message); // encode as (utf-8) Uint8Array
const hashBuffer = await crypto.subtle.digest('SHA-256', msgUint8); // hash the message
const hashArray = Array.from(new Uint8Array(hashBuffer)); // convert buffer to byte array
// const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join(''); // convert bytes to hex string
// return hashHex;
const msgUint8 = new TextEncoder().encode(message);
const hashBuffer = await crypto.subtle.digest("SHA-256", msgUint8);
const hashArray = Array.from(new Uint8Array(hashBuffer));
return hashArray;
}
};
//const digestHex = await digestMessage(text);
/**
* calculate difficulty of a hash
*/
const score = (hash: number[]): BigInt => {
let sum = BigInt(0);
//digest("salt" + "msg" + 1).then(d => console.log(d));
for (let i = 15; i >= 0; i--) {
sum += 256n ** BigInt(16 - (i + 1)) * BigInt(hash[i]);
}
return sum;
};
/**
* Datatype describing a Proof-of-Work object
*
**/
export type WasmWork = {
/** proof solution's difficulty **/
result: string;
/** cryptographic nonce used in proof solution **/
nonce: number;
};
/**
* Generate Proof-of-Work(PoW) according to the algorithim used in mCaptcha
*
* @param {string} salt - salt used in PoW computation. Will be provided in PoW requirement
* @param {string} phrase - challenge phrase used in PoW computation. Will be provided in PoW requirement
* @param {number} difficulty - target difficulty for which PoW should be generated. Will be provided in PoW requirement
*
* @returns {Promise<WasmWork>} - proof-of-work
**/
export const generate_work = async (
salt: string,
phrase: string,
difficulty: number
): Promise<WasmWork> => {
let base = `${salt}${phrase}`;
let nonce = 0;
let result: BigInt = BigInt(0);
let difficulty_new = U128_MAX - U128_MAX / BigInt(difficulty);
while (result < difficulty_new) {
nonce += 1;
result = score(await digest(`${base}${nonce}`));
}
let work: WasmWork = {
result: result.toString(),
nonce,
};
return work;
};

View file

@ -1,37 +0,0 @@
const decodeExp = (arr: number[]) => {
let res = BigInt(0);
let pos = arr.length - 1;
arr.forEach(v => {
if (pos == 0) {
res += BigInt(v);
console.log(`val: ${v} res: ${res}`);
} else if (pos == arr.length - 1) {
console.log('first run');
res = BigInt(v << (pos * 8));
console.log(res);
console.log(`pos: ${pos} val: ${v} res: ${res}`);
} else {
//res += BigInt(v << (pos * 8));
res += BigInt(v) << BigInt(pos * 8);
console.log(v << (pos * 8));
console.log(`pos: ${pos} val: ${v} res: ${res}`);
}
pos -= 1;
});
console.log(res);
};
const encodeExp = () => {
var myVal = 20000;
// var myVal = n58918699885758813231285507404327079076;
var bytes = [];
bytes[0] = (myVal & 0xff00) >> 8;
bytes[1] = myVal & 0x00ff;
console.log(bytes);
decodeExp(bytes);
// console.log((bytes[0] << 8) + bytes[1]);
// console.log((bytes[0] << 8));// + bytes[1]);
};
//encodeExp();
//decodeExp(a);

View file

@ -1,7 +1,7 @@
{
"compilerOptions": {
"incremental": true,
"target": "es5",
"target": "es2020",
"module": "es2020",
"allowJs": false,
"sourceMap": true,

View file

@ -3107,13 +3107,6 @@ lodash@4.x, lodash@^4.17.14, lodash@^4.7.0:
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
lru-cache@^5.1.1:
version "5.1.1"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-5.1.1.tgz#1da27e6710271947695daf6848e847f01d84b920"
integrity sha512-KpNARQA3Iwv+jTA0utUVVbrh+Jlrr1Fv0e56GGzAFOXN7dk/FviaDW8LHmK52DlcH4WP2n6gI8vN1aesBFgo9w==
dependencies:
yallist "^3.0.2"
lru-cache@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-6.0.0.tgz#6d6fe6570ebd96aaf90fcad1dafa3b2566db3a94"
@ -3145,10 +3138,10 @@ makeerror@1.0.x:
dependencies:
tmpl "1.0.x"
marked@^3.0.4:
version "3.0.7"
resolved "https://registry.yarnpkg.com/marked/-/marked-3.0.7.tgz#343aad9e91b96249b495c99c512ea09cfe06de1e"
integrity sha512-ctKqbnLuNbsHbI26cfMyOlKgXGfl1orOv1AvWWDX7AkgfMOwCWvmuYc+mVLeWhQ9W6hdWVBynOs96VkcscKo0Q==
marked@^3.0.8:
version "3.0.8"
resolved "https://registry.yarnpkg.com/marked/-/marked-3.0.8.tgz#2785f0dc79cbdc6034be4bb4f0f0a396bd3f8aeb"
integrity sha512-0gVrAjo5m0VZSJb4rpL59K1unJAMb/hm8HRXqasD8VeC8m91ytDPMritgFSlKonfdt+rRYYpP/JfLxgIX8yoSw==
media-typer@0.3.0:
version "0.3.0"
@ -3376,13 +3369,6 @@ onetime@^5.1.2:
dependencies:
mimic-fn "^2.1.0"
onigasm@^2.2.5:
version "2.2.5"
resolved "https://registry.yarnpkg.com/onigasm/-/onigasm-2.2.5.tgz#cc4d2a79a0fa0b64caec1f4c7ea367585a676892"
integrity sha512-F+th54mPc0l1lp1ZcFMyL/jTs2Tlq4SqIHKIXGZOR/VkHkF9A7Fr5rRr5+ZG/lWeRsyrClLYRq7s/yFQ/XhWCA==
dependencies:
lru-cache "^5.1.1"
open@^8.0.9:
version "8.3.0"
resolved "https://registry.yarnpkg.com/open/-/open-8.3.0.tgz#fdef1cdfe405e60dec8ebd18889e7e812f39c59f"
@ -3868,13 +3854,13 @@ shebang-regex@^3.0.0:
resolved "https://registry.yarnpkg.com/shebang-regex/-/shebang-regex-3.0.0.tgz#ae16f1644d873ecad843b0307b143362d4c42172"
integrity sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==
shiki@^0.9.11:
version "0.9.12"
resolved "https://registry.yarnpkg.com/shiki/-/shiki-0.9.12.tgz#70cbc8c1bb78ff7b356f84a7eecdb040efddd247"
integrity sha512-VXcROdldv0/Qu0w2XvzU4IrvTeBNs/Kj/FCmtcEXGz7Tic/veQzliJj6tEiAgoKianhQstpYmbPDStHU5Opqcw==
shiki@^0.9.12:
version "0.9.14"
resolved "https://registry.yarnpkg.com/shiki/-/shiki-0.9.14.tgz#6b3e369edf76049ae7ad7c2b0498c35c200b8dd7"
integrity sha512-uLHjjyJdNsMzF9GOF8vlOuZ8BwigiYPraMN5yjC826k8K7Xu90JQcC5GUNrzRibLgT2EOk9597I1IX+jRdA8nw==
dependencies:
jsonc-parser "^3.0.0"
onigasm "^2.2.5"
vscode-oniguruma "^1.6.1"
vscode-textmate "5.2.0"
signal-exit@^3.0.2, signal-exit@^3.0.3:
@ -4237,16 +4223,16 @@ typedarray-to-buffer@^3.1.5:
dependencies:
is-typedarray "^1.0.0"
typedoc@^0.22.5:
version "0.22.5"
resolved "https://registry.yarnpkg.com/typedoc/-/typedoc-0.22.5.tgz#c1a7c33fcdc808f57c766584a6afb47762944229"
integrity sha512-KFrWGU1iKiTGw0RcyjLNYDmhd7uICU14HgBNPmFKY/sT4Pm/fraaLyWyisst9vGTUAKxqibqoDITR7+ZcAkhHg==
typedoc@^0.22.10:
version "0.22.10"
resolved "https://registry.yarnpkg.com/typedoc/-/typedoc-0.22.10.tgz#221e1a2b17bcb71817ef027dc4c4969d572e7620"
integrity sha512-hQYZ4WtoMZ61wDC6w10kxA42+jclWngdmztNZsDvIz7BMJg7F2xnT+uYsUa7OluyKossdFj9E9Ye4QOZKTy8SA==
dependencies:
glob "^7.2.0"
lunr "^2.3.9"
marked "^3.0.4"
marked "^3.0.8"
minimatch "^3.0.4"
shiki "^0.9.11"
shiki "^0.9.12"
typescript@^4.4.4:
version "4.4.4"
@ -4312,6 +4298,11 @@ vary@~1.1.2:
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"
integrity sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=
vscode-oniguruma@^1.6.1:
version "1.6.1"
resolved "https://registry.yarnpkg.com/vscode-oniguruma/-/vscode-oniguruma-1.6.1.tgz#2bf4dfcfe3dd2e56eb549a3068c8ee39e6c30ce5"
integrity sha512-vc4WhSIaVpgJ0jJIejjYxPvURJavX6QG41vu0mGhqywMkQqulezEqEQ3cO3gc8GvcOpX6ycmKGqRoROEMBNXTQ==
vscode-textmate@5.2.0:
version "5.2.0"
resolved "https://registry.yarnpkg.com/vscode-textmate/-/vscode-textmate-5.2.0.tgz#01f01760a391e8222fe4f33fbccbd1ad71aed74e"
@ -4605,11 +4596,6 @@ y18n@^5.0.5:
resolved "https://registry.yarnpkg.com/y18n/-/y18n-5.0.8.tgz#7f4934d0f7ca8c56f95314939ddcd2dd91ce1d55"
integrity sha512-0pfFzegeDWJHJIAmTLRP2DwHjdF5s7jo9tuztdQxAhINCdvS+3nGINqPd00AphqJR/0LhANUS6/+7SCb98YOfA==
yallist@^3.0.2:
version "3.1.1"
resolved "https://registry.yarnpkg.com/yallist/-/yallist-3.1.1.tgz#dbb7daf9bfd8bac9ab45ebf602b8cbad0d5d08fd"
integrity sha512-a4UGQaWPH59mOXUYnAG2ewncQS4i4F43Tv3JoAM+s2VDAmS9NsK8GpDMLrCHPksFT7h3K6TOoUNn2pb7RoXx4g==
yallist@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72"