UNPKG

gateio-api

Version:

Complete & Robust Node.js SDK for Gate.com's REST APIs, WebSockets & WebSocket APIs, with TypeScript declarations.

64 lines (63 loc) 2.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.hashMessage = hashMessage; exports.signMessage = signMessage; exports.checkWebCryptoAPISupported = checkWebCryptoAPISupported; const misc_util_js_1 = require("./misc-util.js"); function bufferToB64(buffer) { let binary = ''; const bytes = new Uint8Array(buffer); const len = bytes.byteLength; for (let i = 0; i < len; i++) { binary += String.fromCharCode(bytes[i]); } return globalThis.btoa(binary); } /** * Similar to node crypto's `createHash()` function */ async function hashMessage(message, method, algorithm) { const encoder = new TextEncoder(); const buffer = await globalThis.crypto.subtle.digest(algorithm, encoder.encode(message)); switch (method) { case 'hex': { return Array.from(new Uint8Array(buffer)) .map((byte) => byte.toString(16).padStart(2, '0')) .join(''); } case 'base64': { return bufferToB64(buffer); } default: { throw (0, misc_util_js_1.neverGuard)(method, `Unhandled sign method: "${method}"`); } } } /** * Sign a message, with a secret, using the Web Crypto API */ async function signMessage(message, secret, method, algorithm) { const encoder = new TextEncoder(); const key = await globalThis.crypto.subtle.importKey('raw', encoder.encode(secret), { name: 'HMAC', hash: algorithm }, false, ['sign']); const buffer = await globalThis.crypto.subtle.sign('HMAC', key, encoder.encode(message)); switch (method) { case 'hex': { return Array.from(new Uint8Array(buffer)) .map((byte) => byte.toString(16).padStart(2, '0')) .join(''); } case 'base64': { return bufferToB64(buffer); } default: { throw (0, misc_util_js_1.neverGuard)(method, `Unhandled sign method: "${method}"`); } } } function checkWebCryptoAPISupported() { if (!globalThis.crypto) { throw new Error(`Web Crypto API unavailable. Authentication will not work. Are you using an old Node.js release? Refer to the current Node.js LTS version. Node.js v18 reached end of life in April 2025! You should be using Node LTS or newer (v22 or above)!`); } } //# sourceMappingURL=webCryptoAPI.js.map