Skip to content

Commit

Permalink
chore(): tidy hmac handling for international vs exchange APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagosiebler committed Sep 19, 2024
1 parent 62d176f commit 4c45cee
Showing 1 changed file with 15 additions and 55 deletions.
70 changes: 15 additions & 55 deletions src/lib/BaseRestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ export abstract class BaseRestClient {

const apiKey = this.apiKey;
const apiSecret = this.apiSecret;
const apiPassphrase = this.apiPassphrase;
const jwtExpiresSeconds = this.options.jwtExpiresSeconds || 120;

if (!apiKey) {
Expand Down Expand Up @@ -449,8 +450,11 @@ export abstract class BaseRestClient {
// See: https://github.com/tiagosiebler/coinbase-api/issues/24
}

case REST_CLIENT_TYPE_ENUM.exchange: {
// Docs: https://docs.cdp.coinbase.com/exchange/docs/rest-auth
// Docs: https://docs.cdp.coinbase.com/exchange/docs/rest-auth
case REST_CLIENT_TYPE_ENUM.exchange:

// Docs: https://docs.cdp.coinbase.com/intx/docs/rest-auth
case REST_CLIENT_TYPE_ENUM.international: {
const timestampInSeconds = timestampInMs / 1000; // decimals are OK

const signInput =
Expand All @@ -460,7 +464,7 @@ export abstract class BaseRestClient {
throw new Error(`No API secret provided, cannot sign request.`);
}

if (!this.apiPassphrase) {
if (!apiPassphrase) {
throw new Error(`No API passphrase provided, cannot sign request.`);
}

Expand All @@ -476,7 +480,7 @@ export abstract class BaseRestClient {
'CB-ACCESS-KEY': apiKey,
'CB-ACCESS-SIGN': sign,
'CB-ACCESS-TIMESTAMP': timestampInSeconds,
'CB-ACCESS-PASSPHRASE': this.apiPassphrase,
'CB-ACCESS-PASSPHRASE': apiPassphrase,
};

return {
Expand All @@ -488,56 +492,16 @@ export abstract class BaseRestClient {
},
};

// TODO: is there demand for FIX
// For CB Exchange, is there demand for FIX
// Docs, FIX: https://docs.cdp.coinbase.com/exchange/docs/fix-connectivity
}

// Docs: https://docs.cdp.coinbase.com/intx/docs/rest-auth
case REST_CLIENT_TYPE_ENUM.international: {
const timestampInSeconds = String(Math.floor(timestampInMs / 1000));

const signInput =
timestampInSeconds + method + endpoint + requestBodyString;

if (!apiSecret) {
throw new Error(`No API secret provided, cannot sign request.`);
}

if (!this.apiPassphrase) {
throw new Error(`No API passphrase provided, cannot sign request.`);
}

const sign = await signMessage(
signInput,
apiSecret,
'base64',
'SHA-256',
'base64:web',
);

const headers = {
'CB-ACCESS-TIMESTAMP': timestampInSeconds,
'CB-ACCESS-SIGN': sign,
'CB-ACCESS-PASSPHRASE': this.apiPassphrase,
'CB-ACCESS-KEY': apiKey,
};

// For CB International, is there demand for FIX
// Docs, FIX: https://docs.cdp.coinbase.com/intx/docs/fix-overview

return {
...res,
sign: sign,
queryParamsWithSign: signRequestParams,
headers: {
...headers,
},
};
}

// Docs: https://docs.cdp.coinbase.com/prime/docs/rest-authentication
case REST_CLIENT_TYPE_ENUM.prime: {
const timestampInSeconds = String(Math.floor(timestampInMs / 1000));
const timestampInSeconds = Math.floor(timestampInMs / 1000); // decimal not allowed

const signInput =
timestampInSeconds + method + endpoint + requestBodyString;
Expand All @@ -546,7 +510,7 @@ export abstract class BaseRestClient {
throw new Error(`No API secret provided, cannot sign request.`);
}

if (!this.apiPassphrase) {
if (!apiPassphrase) {
throw new Error(`No API passphrase provided, cannot sign request.`);
}

Expand All @@ -555,18 +519,14 @@ export abstract class BaseRestClient {
apiSecret,
'base64',
'SHA-256',
'utf',
'base64:web',
);

const headers = {
// 'CB-ACCESS-TIMESTAMP': timestampInSeconds,
// 'CB-ACCESS-SIGN': sign,
// 'CB-ACCESS-PASSPHRASE': this.apiPassphrase,
// 'CB-ACCESS-KEY': apiKey,
'X-CB-ACCESS-TIMESTAMP': timestampInSeconds,
'X-CB-ACCESS-SIGNATURE': sign,
'X-CB-ACCESS-PASSPHRASE': this.apiPassphrase,
'X-CB-ACCESS-KEY': apiKey,
'X-CB-ACCESS-PASSPHRASE': apiPassphrase,
'X-CB-ACCESS-SIGNATURE': sign,
'X-CB-ACCESS-TIMESTAMP': timestampInSeconds,
};

// For CB Prime, is there demand for FIX
Expand Down

0 comments on commit 4c45cee

Please sign in to comment.