SOURCE

_message =

    onmessage = async (_message) => {
        const getRand = () => {
            const arr = new Uint8Array(8);
            for (let i = 0; i < 8; i++) {
                const rand = Math.floor(Math.random() * 255);
                arr[i] = rand;
            }
            return arr;
        };

        const toHex = (buffer) => {
            return [...new Uint8Array(buffer)]
                .map(b => b.toString(16).padStart(2, "0"))
                .join("");
        };

        // console.log('in worker')
        let { mining_account, account, account_str, difficulty, last_mine_tx, last_mine_arr, sb } = _message.data;
        account = account.slice(0, 8);

        const is_wam = account_str.substr(-4) === '.wam';

        let good = false, itr = 0, rand = 0, hash, hex_digest, rand_arr, last;

        console.log(`Performing work with difficulty ${difficulty}, last tx is ${last_mine_tx}...`);
        /*if (is_wam){
            console.log(`Using WAM account`);
        }*/

        const start = (new Date()).getTime();

        while (!good) {
            rand_arr = getRand();

            // console.log('combining', account, last_mine_arr, rand_arr);
            const combined = new Uint8Array(account.length + last_mine_arr.length + rand_arr.length);
            combined.set(account);
            combined.set(last_mine_arr, account.length);
            combined.set(rand_arr, account.length + last_mine_arr.length);

            // hash = crypto.createHash("sha256");
            // hash.update(combined.slice(0, 24));
            // hex_digest = hash.digest('hex');
            // console.log('combined slice', combined.slice(0, 24))
            hash = await crypto.subtle.digest('SHA-256', combined.slice(0, 24));
            // console.log(hash);
            hex_digest = toHex(hash);
            good = hex_digest.substr(0, 4) === '0000';
            // console.log(hex_digest);
            /*if (is_wam){
                // easier for .wam accounts
            }
            else {
                // console.log(`non-wam account, mining is harder`)
                good = hex_digest.substr(0, 6) === '000000';
            }*/

            if (good) {
                last = parseInt(hex_digest.substr(4, 1), 16);
                /*if (is_wam){
                }
                else {
                    last = parseInt(hex_digest.substr(6, 1), 16);
                }*/
                good &= (last <= difficulty);
                // console.log(hex_digest, good);
            }
            itr++;

            if (itr % 1000000 === 0) {
                console.log(`Still mining - tried ${itr} iterations`);
            }

            if (!good) {
                hash = null;
            }

        }
        const end = (new Date()).getTime();

        // console.log(sb.array.slice(0, 20));
        // const rand_str = Buffer.from(sb.array.slice(16, 24)).toString('hex');
        const rand_str = toHex(rand_arr);

        console.log(`Found hash in ${itr} iterations with ${account} ${rand_str}, last = ${last}, hex_digest ${hex_digest} taking ${(end - start) / 1000}s`)
        const mine_work = { account: account_str, rand_str, hex_digest };

        this.postMessage(mine_work);

        return mine_work;

    }
console 命令行工具 X clear

                    
>
console