bitmart-api
Version:
Complete & robust Node.js SDK for BitMart's REST APIs and WebSockets, with TypeScript declarations.
35 lines • 1.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.signMessage = signMessage;
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);
}
/**
* Sign a message, with a secret, using the Web Crypto API
*/
async function signMessage(message, secret, method) {
const encoder = new TextEncoder();
const key = await globalThis.crypto.subtle.importKey('raw', encoder.encode(secret), { name: 'HMAC', hash: 'SHA-256' }, 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}"`);
}
}
}
//# sourceMappingURL=webCryptoAPI.js.map