All files cash.js

72.41% Statements 84/116
88.23% Branches 15/17
100% Functions 4/4
72.41% Lines 84/116

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 1171x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x                                                             2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 5x 1x 1x 4x 5x 2x 1x 1x 2x 2x 2x 2x 6x 6x 4x 4x 4x 2x 2x 2x 2x     2x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x  
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Cash = void 0;
const crypto_1 = require("crypto");
class Cash {
    constructor(data, bits) {
        this.data = data;
        this.bits = bits;
        this.proof = null;
        this.nonce = null;
    }
    toString() {
        return `Cash(${this.bits})`;
    }
    mine() {
        this.nonce = Math.floor(Math.random() * 100000000);
        let cycle = 0;
        while (true) {
            cycle += 1;
            const inputData = Buffer.concat([
                Buffer.from('FC:'),
                Buffer.from(this.bits.toString()),
                Buffer.from(':'),
                this.data,
                Buffer.from(':'),
                Buffer.from(this.nonce.toString()),
            ]);
            const hash = (0, crypto_1.createHash)('sha256');
            const digest = hash.update(inputData).digest();
            let foundBits = 0;
            for (const c of digest) {
                if (c & 0b10000000) {
                    break;
                }
                if (c & 0b01000000) {
                    foundBits += 1;
                    break;
                }
                if (c & 0b00100000) {
                    foundBits += 2;
                    break;
                }
                if (c & 0b00010000) {
                    foundBits += 3;
                    break;
                }
                if (c & 0b00001000) {
                    foundBits += 4;
                    break;
                }
                if (c & 0b00000100) {
                    foundBits += 5;
                    break;
                }
                if (c & 0b00000010) {
                    foundBits += 6;
                    break;
                }
                if (c & 0b00000001) {
                    foundBits += 7;
                    break;
                }
                if (c === 0) {
                    foundBits += 8;
                }
                if (foundBits >= this.bits) {
                    break;
                }
            }
            if (foundBits >= this.bits) {
                this.proof = digest.toString('hex');
                break;
            }
            this.nonce += 1;
        }
        return cycle;
    }
    verify(proof, nonce) {
        if (proof.length !== 64) {
            return false;
        }
        const full_bytes = this.bits % 4 === 0;
        if (full_bytes) {
            if (!proof.startsWith('0'.repeat(this.bits / 4))) {
                return false;
            }
        }
        else {
            let foundBits = 0;
            for (const c of Buffer.from(proof, 'hex')) {
                const pos = c.toString(2).padStart(8, '0').indexOf('1');
                if (pos === -1) {
                    foundBits += 8;
                    continue;
                }
                foundBits += pos;
                break;
            }
            if (foundBits < this.bits) {
                return false;
            }
        }
        const inputData = Buffer.concat([
            Buffer.from('FC:'),
            Buffer.from(this.bits.toString()),
            Buffer.from(':'),
            this.data,
            Buffer.from(':'),
            Buffer.from(nonce.toString()),
        ]);
        const hash = (0, crypto_1.createHash)('sha256');
        const digest1 = hash.update(inputData.toString()).digest().toString('hex');
        return digest1 === proof;
    }
}
exports.Cash = Cash;