Skip to content

Commit

Permalink
v3.0.0-rc.2 release (#134)
Browse files Browse the repository at this point in the history
* v3.0.0-rc.2 release

* update change log date
  • Loading branch information
2pd authored May 3, 2023
1 parent 995b23d commit 1f678bc
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 40 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 3.0.0-rc.2 - 2023-05-03

### Added
- Support Ed25519 key signature

## v3.0.0-rc1 - 2023-04-20

### Added
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,24 @@ client.newOrder('BNBUSDT', 'BUY', 'LIMIT', {

Please find `examples` folder to check for more endpoints.

## RSA Key based Authentication
## Key Pair Based Authentication

```javascript
const { Spot } = require('@binance/connector')
const { Spot, PrivateKeyAlgo } = require('@binance/connector')

const apiKey = ''
const apiSecret = '' // has no effect when RSA private key is provided

// load private key
const privateKey = fs.readFileSync('/Users/john/ssl/private_key_encrypted.pem')
const privateKeyPassphrase = 'password'
const privateKeyAlgo = PrivateKeyAlgo.RSA // for RSA key
const privateKeyAlgo = PrivateKeyAlgo.ED25519 // for Ed25519 key

const client = new Spot(apiKey, apiSecret, {
privateKey,
privateKeyPassphrase // only used for encrypted key
privateKeyAlgo
})

// Get account information
Expand Down
33 changes: 4 additions & 29 deletions examples/spot/trade/account.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,17 @@
'use strict'

const fs = require('fs')
const Spot = require('../../../src/spot')

let apiKey = ''
let apiSecret = ''
let client
const apiKey = ''
const apiSecret = ''

// baseURL is used to set which market you wish to send the request to.
// Please make sure the api key/secret is consistent with the baseURL, It won't work if you send request to production site with testnet api key.
// Please make sure the api key/secret is consistent with the baseURL. It won't work if you send request to production site with testnet api key.
// https://api.binance.com is for production.
// https://testnet.binance.vision is the spot testnet base url

// timeout is another optional value in milliseconds, that how long the request is allowed to executed.

client = new Spot(apiKey, apiSecret, { baseURL: 'https://testnet.binance.vision', timeout: 1000 })
const client = new Spot(apiKey, apiSecret, { baseURL: 'https://testnet.binance.vision', timeout: 1000 })

client.account()
.then(response => client.logger.log(response.data))
.catch(error => client.logger.error(error.message))

// Below example shows how to sign the request with RSA key. You will have to a few steps:
// 1. Have your RSA keys ready.
// 2. Login to Binance.com to register your public key.
// 3. Save the API Key that is generated from UI, then you are ready to go.
apiKey = 'the_api_key'
apiSecret = '' // Not required for RSA signature.

// load private key
const privateKey = fs.readFileSync('/Users/liangshi/ssl/private_key_encrypted.pem')

client = new Spot(apiKey, apiSecret, {
baseURL: 'https://testnet.binance.vision', // This URL is for testnet; Remove it for production.
privateKey,
privateKeyPassphrase: 'private_key_password', // only used for encrypted key
timeout: 1000
})

client.account()
.then(response => client.logger.log(response.data))
.catch(error => client.logger.error(error))
29 changes: 29 additions & 0 deletions examples/spot/trade/account_ed25519.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict'

const fs = require('fs')
const Spot = require('../../../src/spot')
const PrivateKeyAlgo = require('../../../src/helpers/privateKeyAlgo')

// Please make sure the api key/secret is consistent with the baseURL. It won't work if you send request to production site with testnet api key.
// https://api.binance.com is for production.
// https://testnet.binance.vision is the spot testnet base url

// Below example shows how to sign the request with key. You will have to a few steps:
// 1. Have your Ed25519 key ready.
// 2. Login to Binance.com to register your public key.
// 3. Save the API Key that is generated from UI, then you are ready to go.
const apiKey = 'the api key'

// load private key
const privateKey = fs.readFileSync('/Users/john/ed25519.pem')

const client = new Spot(apiKey, null, {
baseURL: 'https://testnet.binance.vision', // This URL is for testnet; Remove it for production.
privateKey,
privateKeyAlgo: PrivateKeyAlgo.ED25519, // specific the key algo
timeout: 1000
})

client.account()
.then(response => client.logger.log(response.data))
.catch(error => client.logger.error(error))
29 changes: 29 additions & 0 deletions examples/spot/trade/account_rsa.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict'

const fs = require('fs')
const Spot = require('../../../src/spot')

// Please make sure the api key/secret is consistent with the baseURL. It won't work if you send request to production site with testnet api key.
// https://api.binance.com is for production.
// https://testnet.binance.vision is the spot testnet base url

// Below example shows how to sign the request with RSA key. You will have to a few steps:
// 1. Have your RSA keys ready.
// 2. Login to Binance.com to register your public key.
// 3. Save the API Key that is generated from UI, then you are ready to go.
const apiKey = 'the api key'
const apiSecret = '' // Not required for RSA signature.

// load private key
const privateKey = fs.readFileSync('/Users/john/private_key_encrypted.pem')

const client = new Spot(apiKey, apiSecret, {
baseURL: 'https://testnet.binance.vision', // This URL is for testnet; Remove it for production.
privateKey,
privateKeyPassphrase: 'password', // only used for encrypted key
timeout: 1000
})

client.account()
.then(response => client.logger.log(response.data))
.catch(error => client.logger.error(error))
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@binance/connector",
"version": "3.0.0-rc.1",
"version": "3.0.0-rc.2",
"description": "This is a lightweight library that works as a connector to the Binance public API.",
"main": "src/index.js",
"scripts": {
Expand Down
22 changes: 16 additions & 6 deletions src/APIBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

const crypto = require('crypto')
const { removeEmptyValue, buildQueryString, createRequest, defaultLogger } = require('./helpers/utils')
const ConnectorClientError = require('./error/connectorClientError')
const PrivateKeyAlgo = require('./helpers/privateKeyAlgo')

class APIBase {
constructor (options) {
const { apiKey, apiSecret, baseURL, logger, timeout, proxy, httpsAgent, privateKey, privateKeyPassphrase, wsURL } = options
const { apiKey, apiSecret, baseURL, logger, timeout, proxy, httpsAgent, privateKey, privateKeyPassphrase, privateKeyAlgo, wsURL } = options

this.apiKey = apiKey
this.apiSecret = apiSecret
Expand All @@ -17,6 +19,7 @@ class APIBase {
this.logger = logger || defaultLogger
this.privateKey = privateKey || ''
this.privateKeyPassphrase = privateKeyPassphrase || ''
this.privateKeyAlgo = privateKeyAlgo || PrivateKeyAlgo.RSA
this.wsURL = wsURL
}

Expand Down Expand Up @@ -49,13 +52,20 @@ class APIBase {
.update(queryString)
.digest('hex')
} else {
signature = crypto
.createSign('RSA-SHA256')
.update(queryString)
.sign({
if (this.privateKeyAlgo === PrivateKeyAlgo.RSA) {
signature = crypto.sign('RSA-SHA256', Buffer.from(queryString), {
key: this.privateKey,
passphrase: this.privateKeyPassphrase
}, 'base64')
}).toString('base64')
} else if (this.privateKeyAlgo === PrivateKeyAlgo.ED25519) {
signature = crypto.sign(null, Buffer.from(queryString), {
key: this.privateKey,
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
saltLength: crypto.constants.RSA_PSS_SALTLEN_DIGEST
}).toString('base64')
} else {
throw new ConnectorClientError("privateKeyAlgo must be either 'RSA' or 'ED25519'")
}
signature = encodeURIComponent(signature)
}

Expand Down
12 changes: 12 additions & 0 deletions src/error/connectorClientError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'

const Error = require('../error/error')

class ConnectorClientError extends Error {
constructor (errorMessage) {
super(errorMessage)
this.name = 'ConnectorClientError'
}
}

module.exports = ConnectorClientError
6 changes: 6 additions & 0 deletions src/helpers/privateKeyAlgo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const PrivateKeyAlgo = {
RSA: 'RSA',
ED25519: 'Ed25519'
}

module.exports = PrivateKeyAlgo
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

module.exports.PrivateKeyAlgo = require('./helpers/privateKeyAlgo')
module.exports.Spot = require('./spot')
module.exports.WebsocketStream = require('./websocketStream')
module.exports.WebsocketAPI = require('./websocketAPI')

0 comments on commit 1f678bc

Please sign in to comment.