Skip to content

Commit b65b80b

Browse files
- Added command line utility for generating private keys
- Updated documentation
1 parent bef0c31 commit b65b80b

File tree

5 files changed

+552
-18
lines changed

5 files changed

+552
-18
lines changed

Diff for: .prettierrc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"plugins": ["prettier-plugin-organize-imports"],
3+
"useTabs": true
4+
}

Diff for: README.md

+22-14
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,42 @@ npm install --save @chronicleprotocol/api-auth
88

99
## Usage
1010

11-
Generating authentication tokens:
11+
Generating authentication tokens programmatically:
1212

1313
```js
1414
import { signAuthToken } from "@chronicleprotocol/api-auth";
1515

1616
const { token, message } = signAuthToken({
17-
// private key is 0x prefixed 32 byte hex string
18-
privateKey: "0xabc...",
17+
// private key is 0x prefixed 32 byte hex string
18+
privateKey: "0xabc...",
1919
});
2020

2121
// `token` is hex string to be included as Authorization header (see below)
2222
// `message` is object containing decoded data within `token`. Optional,
2323
// but can be useful for programmatic token handling
24+
```
25+
26+
To generate a token via the command line, use:
2427

28+
```bash
29+
# Please do not put your private key directly in the command and have it show up in your shell history :-(
30+
npx @chronicleprotocol/api-auth --privateKey=$PRIVATE_KEY
2531
```
2632

27-
> Your public signing address must be allow-listed on our servers before your tokens will be valid.
33+
> NOTE: Your public signing address must be allow-listed on our servers before your tokens will be valid.
2834
29-
Using an auth token to fetch an API endpoint:
35+
Using an auth token to fetch an API endpoint programmatically:
3036

3137
```js
32-
// token is received from the server
33-
fetch(
34-
"https://chroniclelabs.org/api/pairs",
35-
{
36-
headers: {
37-
Authorization: `Bearer ${token}`,
38-
},
39-
},
40-
);
38+
fetch("https://chroniclelabs.org/api/authTest", {
39+
headers: {
40+
Authorization: `Bearer ${token}`,
41+
},
42+
});
43+
```
44+
45+
or via command line:
46+
47+
```bash
48+
curl --header "Authorization: Bearer $AUTH_TOKEN" https://chroniclelabs.org/api/authTest
4149
```

Diff for: bin/signAuthToken.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env npx tsx
2+
3+
import { Hex } from "viem";
4+
import { signAuthToken } from "../src/index";
5+
6+
let privateKey = (process.argv[2] || "").replace("--privateKey=", "");
7+
8+
if (!privateKey) {
9+
console.error("Usage: signAuthToken --privateKey=<privateKey>");
10+
process.exit(1);
11+
}
12+
13+
if (!privateKey.startsWith("0x")) {
14+
privateKey = "0x" + privateKey;
15+
}
16+
17+
if (privateKey.length !== 66) {
18+
console.error(
19+
"Invalid private key. Must be a 32 byte (64 character) hex string, with or without leading 0x",
20+
);
21+
process.exit(1);
22+
}
23+
24+
signAuthToken({ privateKey: privateKey as Hex }).then((result) => {
25+
console.log(result.token);
26+
});

0 commit comments

Comments
 (0)