tiagosiebler / kucoin-api

Node.js & JavaScript SDK for Kucoin REST APIs & WebSockets, with TypeScript & browser support.
https://www.npmjs.com/package/kucoin-api
7 stars 3 forks source link

Cannot read properties of undefined (reading 'subtle') #38

Open amirtaherkhani opened 1 week ago

amirtaherkhani commented 1 week ago

When I attempt to test a function, such as getAccountSummary, I encounter the error: "Cannot read properties of undefined (reading 'subtle')." However, when I test the API endpoints directly using Axios, I do not experience this error. I Tested that on Node.js version 18 and Ubuntu 20.04.

tiagosiebler commented 1 week ago

Ah my newer SDKs use the Web Crypto API for the hmac/sign functionality used to authenticate requests. It should have support as of Node 15: https://developer.mozilla.org/en-US/docs/Web/API/Crypto/subtle

Edit: here it is, the Web crypto API as a whole came in Node v19, even though it seems the subtle portion was available earlier somehow: https://developer.mozilla.org/en-US/docs/Web/API/Crypto#browser_compatibility

So I'm not sure why it's undefined for you... If you can, I'd recommend using the active Node LTS (v22.11.0). Node 18 is in maintenance mode with end-of-life coming in early next year: https://nodejs.org/en/about/previous-releases

With tools such as NVM, you can easily switch node version (although it's not the only way): https://github.com/nvm-sh/nvm

If you have your reasons and want to continue using Node 18 for now, you can pass through a custom sign function to use Node's own createHmac function (does not work in browsers). Here's an example - it's for my bitmart SDK but will work here too: https://github.com/tiagosiebler/bitmart-api/blob/master/examples/fasterHmacSign.ts#L21-L23

amirtaherkhani commented 1 week ago

Thank you for your answer. I plan to switch to Node.js 22 and move the KuCoin side of the app to another external service. This will allow me to use the SDK and KuCoin API services more effectively