MCPcopy Create free account
hub / github.com/ccxt/node-binance-api / generateSignature

Method generateSignature

src/node-binance-api.ts:786–822  ·  view source on GitHub ↗
(query: string, encode = true)

Source from the content-addressed store, hash-verified

784 }
785
786 generateSignature(query: string, encode = true) {
787 const secret = this.APISECRET || this.PRIVATEKEY;
788 let signature = '';
789 if (secret.includes('PRIVATE KEY')) {
790 // if less than the below length, then it can't be RSA key
791 let keyObject: crypto.KeyObject;
792 try {
793 const privateKeyObj: crypto.PrivateKeyInput = { key: secret };
794
795 if (this.PRIVATEKEYPASSWORD) {
796 privateKeyObj.passphrase = this.PRIVATEKEYPASSWORD;
797 }
798
799 keyObject = crypto.createPrivateKey(privateKeyObj);
800
801 } catch (e) {
802 throw new Error(
803 'Invalid private key. Please provide a valid RSA or ED25519 private key. ' + e.toString()
804 );
805 }
806
807 if (secret.length > 120) {
808 // RSA key
809 signature = crypto
810 .sign('RSA-SHA256', Buffer.from(query), keyObject)
811 .toString('base64');
812 if (encode) signature = encodeURIComponent(signature);
813 return signature;
814 } else {
815 // Ed25519 key
816 signature = crypto.sign(null, Buffer.from(query), keyObject).toString('base64');
817 }
818 } else {
819 signature = crypto.createHmac('sha256', this.Options.APISECRET).update(query).digest('hex'); // set the HMAC hash header
820 }
821 return signature;
822 }
823
824 // --- ENDPOINTS --- //
825

Callers 4

futuresRequestMethod · 0.95
signedRequestMethod · 0.95
userDataMethod · 0.95
crypto.test.tsFile · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected