-
Notifications
You must be signed in to change notification settings - Fork 764
feat: add RSA and ed22519 support #927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
8054073
1e58469
55d795e
bdeb880
0789d52
96dfb2b
456f312
b7d2e73
8ea1941
bf29300
56aab92
6006a6d
51f77b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
|
|
||
| import WebSocket from 'ws'; | ||
| // import request from 'request'; | ||
| import crypto from 'crypto'; | ||
| import crypto, { createSign } from 'crypto'; | ||
| import file from 'fs'; | ||
| import url from 'url'; | ||
| import JSONbig from 'json-bigint'; | ||
|
|
@@ -11,6 +11,11 @@ import { HttpsProxyAgent } from 'https-proxy-agent'; | |
| import { SocksProxyAgent } from 'socks-proxy-agent'; | ||
| // @ts-ignore | ||
| import nodeFetch from 'node-fetch'; | ||
| // @ts-ignore | ||
| import { ed25519 } from '@noble/curves/ed25519'; | ||
| // @ts-ignore | ||
| import { base64 } from '@scure/base'; | ||
|
|
||
| // @ts-ignore | ||
| import zip from 'lodash.zipobject'; | ||
| import stringHash from 'string-hash'; | ||
|
|
@@ -503,7 +508,7 @@ export default class Binance { | |
| data.timestamp += this.timeOffset; | ||
| } | ||
| query = this.makeQueryString(data); | ||
| data.signature = crypto.createHmac('sha256', this.APISECRET).update(query).digest('hex'); // HMAC hash header | ||
| data.signature = this.generateSignature(query); | ||
| opt.url = `${url}?${query}&signature=${data.signature}`; | ||
| } | ||
| (opt as any).qs = data; | ||
|
|
@@ -615,7 +620,9 @@ export default class Binance { | |
|
|
||
| if (!data.recvWindow) data.recvWindow = this.Options.recvWindow; | ||
| const query = method === 'POST' && noDataInSignature ? '' : this.makeQueryString(data); | ||
| const signature = crypto.createHmac('sha256', this.Options.APISECRET).update(query).digest('hex'); // set the HMAC hash header | ||
|
|
||
| const signature = this.generateSignature(query); | ||
|
|
||
| if (method === 'POST') { | ||
| const opt = this.reqObjPOST( | ||
| url, | ||
|
|
@@ -638,6 +645,42 @@ export default class Binance { | |
| } | ||
| } | ||
|
|
||
| unarmorKey(a:string):number[] { | ||
| // eslint-disable-next-line no-useless-escape | ||
| const m = /-----BEGIN [^-]+-----\n([A-Za-z0-9+\/=\s]+)\n-----END [^-]+-----|begin-base64[^\n]+\n([A-Za-z0-9+\/=\s]+)====/.exec(a); | ||
| if (m) { | ||
| if (m[1]) { | ||
| a = m[1]; | ||
| } else if (m[2]) { | ||
| a = m[2]; | ||
| } else { | ||
| throw new Error("RegExp out of sync"); | ||
| } | ||
| } | ||
| return base64.decode(a); | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we shouldn't use this method at all, why do we need it? it is confusing (which might scare some users), does an exec, ... all red flags
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. btw most crypto libs already properly parse the private key headers
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. about this, I've just copy-pasted that as it's bad, we should also remove from ccxt
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ttodua if you check the latest implementation in this branch it does not use any of that |
||
|
|
||
| generateSignature(query: string) { | ||
| const APISECRET = this.Options.APISECRET || this.APISECRET; | ||
|
carlosmiei marked this conversation as resolved.
Outdated
|
||
| let signature = ''; | ||
| if (APISECRET.includes ('PRIVATE KEY')) { | ||
| // if less than the below length, then it can't be RSA key | ||
| if (APISECRET.length < 500) { | ||
|
carlosmiei marked this conversation as resolved.
Outdated
|
||
| const privateKey = new Uint8Array (this.unarmorKey (APISECRET).slice (16)); | ||
| const encodedQuery = new TextEncoder().encode(query); | ||
| const signatureInit = ed25519.sign (encodedQuery, privateKey); | ||
| signature = base64.encode (signatureInit); | ||
| } else { | ||
| const signed = createSign('RSA-SHA256').update(query); | ||
| signature = signed.sign(APISECRET, 'base64'); | ||
| signature = encodeURIComponent (signature); | ||
| } | ||
| } else { | ||
| signature = crypto.createHmac('sha256', this.Options.APISECRET).update(query).digest('hex'); // set the HMAC hash header | ||
|
carlosmiei marked this conversation as resolved.
|
||
| } | ||
| return signature; | ||
| } | ||
|
|
||
| // --- ENDPOINTS --- // | ||
|
|
||
| /** | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.