Last active
February 15, 2022 10:27
-
-
Save ttodua/862a31e2dde2fd53cf4adc1eca58e09c to your computer and use it in GitHub Desktop.
temp-dydx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sign (path, api = 'private', method = 'GET', params = {}, headers = undefined, body = undefined) { | |
let request = '/' + this.version + '/' + this.implodeParams (path, params); | |
params = this.omit (params, this.extractParams (path)); | |
if (method === 'GET' || method === 'DELETE') { | |
if (Object.keys (params).length) { | |
request += '?' + this.urlencode (params); | |
} | |
} | |
const url = this.urls['api'][api] + request; | |
if (api === 'private') { | |
let isApiKeyBased = false; | |
let isPrivateKeyBased = false; | |
if (path === 'onboarding') { // it's only POST | |
isPrivateKeyBased = true; | |
} else { | |
isApiKeyBased = true; | |
} | |
// | |
// | |
if (isApiKeyBased) { | |
const timestamp = this.iso8601 (this.milliseconds ()); | |
let auth = timestamp + method + request; | |
if (method !== 'GET') { | |
body = this.json (params); | |
auth += body; | |
} | |
const secret = this.base64ToBinary (this.secret); | |
const signature = this.hmac (this.encode (auth), secret, 'sha256', 'base64'); | |
headers = { | |
'DYDX-TIMESTAMP': timestamp, | |
'DYDX-API-KEY': this.apiKey, | |
'DYDX-PASSPHRASE': this.password, | |
'DYDX-SIGNATURE': signature, | |
}; | |
if (method !== 'GET') { | |
headers['Content-type'] = 'application/json'; | |
} | |
} else if (isPrivateKeyBased) { | |
const isoTimestamp = this.iso8601 (this.milliseconds ()); // new Date ().toISOString (); | |
headers['DYDX-TIMESTAMP'] = isoTimestamp; | |
// ref: https://github.com/dydxprotocol/v3-client/blob/master/src/modules/eth-private.ts#L42 | |
headers['DYDX-ETHEREUM-ADDRESS'] = this.walletAddress; | |
body['action'] = 'DYDX-ONBOARDING'; | |
body['onlySignOn'] = 'https://trade.dydx.exchange'; | |
// const obj = method + path + isoTimestamp + paramsEncoded; | |
// const auth = this.walletAddress + 'sha256' + isoTimestamp + this.json (obj); | |
const signature = this.createDydxSign ({ | |
'requestPath': '/' + this.version + '/' + path, | |
'method': method, | |
'isoTimestamp': isoTimestamp, | |
// 'data': { 'market': 'BTC-USD', 'side': 'BUY', 'type': 'LIMIT', 'size': 0.0001, 'price': 3000, }, | |
'data': params, | |
}, this.secret); | |
headers['DYDX-SIGNATURE'] = signature; | |
} | |
} | |
return { 'url': url, 'method': method, 'body': body, 'headers': headers }; | |
} | |
async apiKeyHelper (generateOrCreate = false) { | |
const { DydxClient } = require ('@dydxprotocol/v3-client'); | |
const Web3 = require ('web3'); | |
const web3 = new Web3 (); | |
web3.eth.accounts.wallet.add (this.privateKey); | |
const client = new DydxClient ('https://api.dydx.exchange', { web3 }); | |
const apiCreds = generateOrCreate ? await client.onboarding.recoverDefaultApiCredentials (this.walletAddress) : await client.ethPrivate.createApiKey (this.walletAddress); | |
return apiCreds; | |
} | |
async generateApiKey () { | |
return this.apiKeyHelper (true); // https://docs.dydx.exchange/#recover-default-api-credentials | |
} | |
async createApiKey () { | |
return this.apiKeyHelper (false); | |
} | |
async createApiKeyByOnboard (params = undefined) { // https://docs.dydx.exchange/#onboarding | |
return {}; | |
} | |
createDydxSign ({ | |
requestPath, | |
method, | |
isoTimestamp, | |
data, | |
}, apiSecret) { | |
const messageString = isoTimestamp + method + requestPath + JSON.stringify (data); | |
const crypto = require ('crypto'); | |
return crypto.createHmac ('sha256', Buffer.from (apiSecret, 'base64')).update (messageString).digest ('base64'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment