Skip to content

Commit d82ec00

Browse files
feat(js-sdk): create private auth and order api (#66)
1 parent d68844e commit d82ec00

File tree

9 files changed

+4565
-0
lines changed

9 files changed

+4565
-0
lines changed

js/.gitignore

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
node_modules
2+
dist
3+
*.log
4+
.vscode/**
5+
!.vscode/extensions.json
6+
.idea
7+
coverage
8+
.DS_Store
9+
*.tsbuildinfo
10+
.cache
11+
fiddle/build/
12+
packages/core/mitosis-build
13+
.history
14+
.pnp.*
15+
.yarn
16+
**/.yarn/*
17+
!**/.yarn/patches
18+
!**/.yarn/plugins
19+
!**/.yarn/releases
20+
!**/.yarn/sdks
21+
!**/.yarn/versions
22+
23+
examples/**/output
24+
e2e/**/output
25+
packages/e2e-*/**
26+
**/test-results/
27+
**/playwright-report/
28+
29+
packages/**/dist/
30+
packages/**/src/

js/lerna.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"packages": [
3+
"packages/*"
4+
],
5+
"npmClient": "yarn",
6+
"useWorkspaces": true,
7+
"version": "1.0.0"
8+
}

js/package.json

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "root",
3+
"version": "1.0.0",
4+
"description": "Bitwyre's Software Development Kit - Algorithmic Trading",
5+
"private": true,
6+
"scripts": {
7+
"lerna": "lerna"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/bitwyre/sdk.git"
12+
},
13+
"keywords": [
14+
"sdk",
15+
"algorithmic",
16+
"trading",
17+
"cryptocurrency"
18+
],
19+
"author": "Agustinus Theodorus",
20+
"license": "MIT",
21+
"bugs": {
22+
"url": "https://github.com/bitwyre/sdk/issues"
23+
},
24+
"workspaces": [
25+
"packages/*"
26+
],
27+
"homepage": "https://github.com/bitwyre/sdk#readme",
28+
"dependencies": {
29+
"lerna": "^5.5.4"
30+
}
31+
}

js/packages/private/authentication.ts

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

js/packages/private/examples/main.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { spotBalance } from "../spot";
2+
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+
}
15+
}
16+
17+
main()
18+
.then();

js/packages/private/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./authentication";

js/packages/private/package.json

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "@bitwyre-sdk/private",
3+
"version": "1.0.0",
4+
"description": "Bitwyre's Software Development Kit - Algorithmic Trading",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/bitwyre/sdk.git"
12+
},
13+
"keywords": [
14+
"sdk",
15+
"algorithmic",
16+
"trading",
17+
"cryptocurrency"
18+
],
19+
"author": "Agustinus Theodorus",
20+
"license": "MIT",
21+
"bugs": {
22+
"url": "https://github.com/bitwyre/sdk/issues"
23+
},
24+
"homepage": "https://github.com/bitwyre/sdk#readme",
25+
"dependencies": {
26+
"@types/sha256": "^0.2.0",
27+
"axios": "^1.0.0",
28+
"crypto": "^1.0.1"
29+
},
30+
"devDependencies": {
31+
"@types/node": "^18.8.2",
32+
"typescript": "^4.8.4"
33+
}
34+
}

js/packages/private/spot.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import axios from 'axios';
2+
import { sign, SignData } from './authentication';
3+
4+
export async function spotBalance(baseUrl: string, apiKey: string, secretKey: string, page?: number, perPage?: number, usePagination: boolean = false, asset: string = "") {
5+
const payload = "";
6+
const uriPath: string = "/private/account/spotbalance";
7+
const signData: SignData = await sign(secretKey, uriPath, payload);
8+
9+
return await axios.get(uriPath, {
10+
baseURL: baseUrl,
11+
params: {
12+
"nonce": signData.nonce,
13+
"checksum": signData.checksum,
14+
"payload": payload
15+
},
16+
headers: {
17+
"API-Key": apiKey,
18+
"API-Sign": signData.signature
19+
}
20+
});
21+
}

0 commit comments

Comments
 (0)