UNPKG

banani

Version:

JS/TS library for the Banano cryptocurrency in the style of bananopie

41 lines (40 loc) 1.8 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SlowJavascriptWorkProvider = exports.RPCWorkProvider = void 0; const blake2b_1 = __importDefault(require("blake2b")); const util_1 = require("./util"); const BANANO_WORK_THRESHOLD = "0xFFFFFE0000000000"; /** Request work from an RPC provider that supports the `work_generate` RPC call */ class RPCWorkProvider { constructor(rpc) { /** Extra json to send with the rpc payload. Needed for rpc.nano.to's work_generate, unfortunately */ this.extra_payload = {}; this.rpc = rpc; } async request_work(block_hash) { return (await this.rpc.call({ action: "work_generate", hash: block_hash, ...this.extra_payload, })).work; } } exports.RPCWorkProvider = RPCWorkProvider; /** Use Javascript to slowly generate work for blocks */ class SlowJavascriptWorkProvider { async request_work(block_hash) { let nonce = 0; while (true) { const nonce_bytes = (0, util_1.int_to_uint8array)(nonce, 8).reverse(); const work = (0, util_1.uint8array_to_hex)((0, blake2b_1.default)(8, undefined, undefined, undefined, true).update(nonce_bytes).update((0, util_1.hex_to_uint8array)(block_hash)).digest().reverse()); //noAssert = true so we can have byte of 8 (min for this library is 16 for some reason) if (BigInt(`0x${work}`) > BigInt(BANANO_WORK_THRESHOLD)) { return (0, util_1.uint8array_to_hex)(nonce_bytes.reverse()); } nonce += 1; } } } exports.SlowJavascriptWorkProvider = SlowJavascriptWorkProvider;