tiagosiebler / binance

Node.js & JavaScript SDK for Binance REST APIs & WebSockets, with TypeScript & browser support, integration tests, beautification & more.
MIT License
747 stars 266 forks source link

Run in web worker #280

Closed mr-smit closed 1 year ago

mr-smit commented 1 year ago

When you run binanceapi.js in web worker , we get error

window is not defined

to fix this problem , just remove window.

from this code:

function signMessage(message, secret) {
    return __awaiter(this, void 0, void 0, function* () {
        const encoder = new TextEncoder();
        const key = yield window.crypto.subtle.importKey('raw', encoder.encode(secret), { name: 'HMAC', hash: { name: 'SHA-256' } }, false, ['sign']);
        const signature = yield window.crypto.subtle.sign('HMAC', key, encoder.encode(message));
        return Array.prototype.map.call(new Uint8Array(signature), (x) => ('00' + x.toString(16)).slice(-2)).join('');
    });
}
tiagosiebler commented 1 year ago

Thanks for letting me know! Would you mind making a PR? It's this file: https://github.com/tiagosiebler/binance/blob/master/src/util/browser-support.ts#L4-L12