UNPKG

@helium/crypto

Version:

Cryptography utilities including mnemonics, keypairs and base58-check encoding

54 lines 2.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.verify = exports.deriveChecksumBits = exports.binaryToByte = exports.bytesToBinary = exports.lpad = exports.sha256 = exports.randomBytes = void 0; /* eslint-disable arrow-body-style */ /* eslint-disable no-bitwise */ const sha2_1 = require("@noble/hashes/sha2"); const ed25519_1 = require("@noble/curves/ed25519"); const utils_1 = require("@noble/hashes/utils"); const randomBytes = (n) => { return Promise.resolve(Buffer.from((0, utils_1.randomBytes)(n))); }; exports.randomBytes = randomBytes; const sha256 = (buffer) => { const input = typeof buffer === 'string' ? Buffer.from(buffer) : buffer; return Buffer.from((0, sha2_1.sha256)(input)); }; exports.sha256 = sha256; const lpad = (str, padString, length) => { let strOut = str; while (strOut.length < length) strOut = padString + strOut; return strOut; }; exports.lpad = lpad; const bytesToBinary = (bytes) => bytes .map((x) => (0, exports.lpad)(x.toString(2), '0', 8)) .join(''); exports.bytesToBinary = bytesToBinary; const binaryToByte = (bin) => parseInt(bin, 2); exports.binaryToByte = binaryToByte; const deriveChecksumBits = (entropyBuffer) => { const ENT = entropyBuffer.length * 8; const CS = ENT / 32; const hash = (0, exports.sha256)(entropyBuffer); return (0, exports.bytesToBinary)([].slice.call(hash)).slice(0, CS); }; exports.deriveChecksumBits = deriveChecksumBits; const verify = async (signature, message, publicKey) => { try { const messageBytes = typeof message === 'string' ? Buffer.from(message) : message; const result = ed25519_1.ed25519.verify(signature, messageBytes, publicKey); return result; } catch (error) { if (error instanceof Error) { if (error.message.includes('invalid') || error.message.includes('Invalid')) { throw error; } } return false; } }; exports.verify = verify; //# sourceMappingURL=utils.js.map