Skip to content

Commit

Permalink
perf: signer accepts string as a paylod
Browse files Browse the repository at this point in the history
  • Loading branch information
vansergen committed Dec 25, 2020
1 parent c05b5cf commit 3cc424b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 13 deletions.
10 changes: 4 additions & 6 deletions src/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createHmac } from "crypto";
export type SignerOptions = {
key: string;
secret: string;
body: Record<string, unknown>;
payload: string;
};

export type SignedHeaders = {
Expand All @@ -12,13 +12,11 @@ export type SignedHeaders = {
"X-BFX-SIGNATURE": string;
};

export function Signer({ key, secret, body }: SignerOptions): SignedHeaders {
const payload = Buffer.from(JSON.stringify(body)).toString("base64");
export function Signer({ key, secret, payload }: SignerOptions): SignedHeaders {
const signature = createHmac("sha384", secret).update(payload).digest("hex");
return {
"X-BFX-APIKEY": key,
"X-BFX-PAYLOAD": payload,
"X-BFX-SIGNATURE": createHmac("sha384", secret)
.update(payload)
.digest("hex"),
"X-BFX-SIGNATURE": signature,
};
}
8 changes: 1 addition & 7 deletions test/signer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,11 @@ suite("Signer", () => {
test("Correct signature", () => {
const key = "17a455a3b5ee1cc6df5ab9a5287c0c9c6e6a86b5b9";
const secret = "ddc64733b9a786f9ca45ddbc2f370ecdab2ed7acbb";
const body = {
request: "/v1/deposit/new",
method: "bitcoin",
wallet_name: "trading",
nonce: "1574959951447",
};
const payload =
"eyJyZXF1ZXN0IjoiL3YxL2RlcG9zaXQvbmV3IiwibWV0aG9kIjoiYml0Y29pbiIsIndhbGxldF9uYW1lIjoidHJhZGluZyIsIm5vbmNlIjoiMTU3NDk1OTk1MTQ0NyJ9";
const signature =
"49e35e58d8dc083cc245bf9933a54565c6b6202dc427574b75c13c49e8c9e5c8666185df8a6e4b44d63f2fe7175b79e9";
const headers = Signer({ key, secret, body });
const headers = Signer({ key, secret, payload });
const expectedHeaders: SignedHeaders = {
"X-BFX-APIKEY": key,
"X-BFX-PAYLOAD": payload,
Expand Down

0 comments on commit 3cc424b

Please sign in to comment.