Skip to content

Commit a9cb35c

Browse files
feat(js-sdk): add public and private api endpoints (#67)
* feat(js-sdk): add private api endpoints * feat(js-sdk): add public api endpoints * feat(js-sdk): format and linter * refactor(js-sdk): move src * tests(js-sdk): public and private api integration testing * tests(js-sdk): update integration tests
1 parent d82ec00 commit a9cb35c

28 files changed

+2324
-105
lines changed

js/.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ packages/e2e-*/**
2727
**/playwright-report/
2828

2929
packages/**/dist/
30-
packages/**/src/
30+
packages/**/lib/
31+
32+
.env

js/package.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44
"description": "Bitwyre's Software Development Kit - Algorithmic Trading",
55
"private": true,
66
"scripts": {
7-
"lerna": "lerna"
7+
"lerna": "lerna",
8+
"bootstrap": "yarn --ignore-scripts --silent && lerna bootstrap -- --ignore-scripts && yarn build && lerna bootstrap -- --ignore-scripts",
9+
"build": "lerna run build",
10+
"lint": "lerna run lint",
11+
"test": "lerna run test",
12+
"format": "lerna run format",
13+
"check:all": "yarn test && yarn format && yarn lint && yarn bootstrap"
814
},
915
"repository": {
1016
"type": "git",

js/packages/private/.env.example

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
BASE_URL=https://api.bitwyre.com
2+
API_KEY=
3+
SECRET_KEY=

js/packages/private/.prettierrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"printWidth": 120,
3+
"trailingComma": "all",
4+
"singleQuote": true
5+
}

js/packages/private/authentication.ts

-40
This file was deleted.

js/packages/private/babel.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
presets: ['@babel/preset-env']
3+
}

js/packages/private/examples/main.ts

+13-14
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
import { spotBalance } from "../spot";
1+
import { getAccountBalance } from '../src';
2+
import * as dotenv from 'dotenv';
23

3-
async function main () {
4-
try{
5-
const res = await spotBalance(
6-
`https://api.bitwyre.com`,
7-
'',
8-
'',
9-
);
10-
return res.data;
11-
}
12-
catch(e: any) {
13-
console.log(e.response.data);
14-
}
4+
dotenv.config({ path: `${__dirname}/../.env` });
5+
6+
async function main() {
7+
const res = await getAccountBalance(
8+
process.env.BASE_URL ?? '',
9+
process.env.API_KEY ?? '',
10+
process.env.SECRET_KEY ?? '',
11+
);
12+
return res.data;
1513
}
1614

1715
main()
18-
.then();
16+
.then(console.log)
17+
.catch((e) => console.log(e.response.data));

js/packages/private/index.ts

-1
This file was deleted.

js/packages/private/package.json

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
{
2-
"name": "@bitwyre-sdk/private",
2+
"name": "@bitwyre/sdk",
33
"version": "1.0.0",
44
"description": "Bitwyre's Software Development Kit - Algorithmic Trading",
5-
"main": "index.js",
5+
"main": "lib/index.js",
6+
"types": "lib/index.d.ts",
7+
"files": [
8+
"lib/**/*"
9+
],
610
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
11+
"build": "tsc",
12+
"format": "prettier --write \"**/*.ts\"",
13+
"lint": "tslint -p tsconfig.json",
14+
"prepare": "yarn build",
15+
"prepublishOnly": "npm test && yarn lint && yarn build",
16+
"preversion": "yarn lint",
17+
"version": "yarn format && git add -A src",
18+
"postversion": "git push && git push --tags",
19+
"test": "jest --config ./test/jest.json --runInBand"
820
},
921
"repository": {
1022
"type": "git",
@@ -23,12 +35,24 @@
2335
},
2436
"homepage": "https://github.com/bitwyre/sdk#readme",
2537
"dependencies": {
38+
"@bitwyre/internal-public": "1.0.0",
2639
"@types/sha256": "^0.2.0",
2740
"axios": "^1.0.0",
2841
"crypto": "^1.0.1"
2942
},
3043
"devDependencies": {
44+
"@types/jest": "^29.1.2",
3145
"@types/node": "^18.8.2",
46+
"babel-jest": "^29.1.2",
47+
"dotenv": "^16.0.3",
48+
"jest": "^29.1.2",
49+
"prettier": "^2.7.1",
50+
"ts-jest": "^29.0.3",
51+
"tslint": "^6.1.3",
52+
"tslint-config-prettier": "^1.18.0",
3253
"typescript": "^4.8.4"
54+
},
55+
"directories": {
56+
"test": "test"
3357
}
3458
}

js/packages/private/spot.ts

-21
This file was deleted.
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import crypto from 'crypto';
2+
import { SignData } from './types';
3+
import { getServerTime } from '@bitwyre/internal-public';
4+
5+
export async function getNonce(baseUrl: string): Promise<number> {
6+
const res = await getServerTime(baseUrl);
7+
return res.data.result.unixtime;
8+
}
9+
10+
export function getChecksum(payload: string): string {
11+
const temp = JSON.stringify(payload);
12+
const temp2 = JSON.stringify(temp);
13+
return crypto.createHash('sha256').update(temp2).digest('hex');
14+
}
15+
16+
export function getNonceChecksum(nonce: string, checksum: string): string {
17+
return crypto.createHash('sha256').update(nonce.concat(checksum)).digest('hex');
18+
}
19+
20+
export async function sign(baseUrl: string, secretKey: string, uriPath: string, payload: string): Promise<SignData> {
21+
const nonce: number = await getNonce(baseUrl);
22+
23+
const checksum: string = getChecksum(payload);
24+
const nonceChecksum = getNonceChecksum(nonce.toString(), checksum);
25+
const data = uriPath.concat(nonceChecksum);
26+
const signature = crypto.createHmac('sha512', secretKey).update(data).digest('hex');
27+
28+
return {
29+
nonce,
30+
checksum,
31+
signature,
32+
};
33+
}

0 commit comments

Comments
 (0)