Skip to content

Commit 59e5eb5

Browse files
committed
Add authentication_token
1 parent 1eadea5 commit 59e5eb5

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

example.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const stopBlockNum = "+10";
1010
const substreams = new Substreams(outputModule, {
1111
startBlockNum,
1212
stopBlockNum,
13-
authorization: process.env.SUBSTREAMS_API_TOKEN
13+
authorization: "<STREAMINGFAST_KEY or SUBSTREAMS_API_TOKEN>"
1414
});
1515

1616
(async () => {

examples/subtivity.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const stopBlockNum = "+10";
1010
const substreams = new Substreams(outputModule, {
1111
startBlockNum,
1212
stopBlockNum,
13-
authorization: process.env.SUBSTREAMS_API_TOKEN
13+
authorization: process.env.STREAMINGFAST_KEY
1414
});
1515

1616
(async () => {

src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export * from "./generated/sf/substreams/v1/substreams"
2020
export * from "./utils";
2121

2222
// Utils
23-
import { parseBlockData, parseStopBlock } from './utils';
23+
import { parseAuthorization, parseBlockData, parseStopBlock } from './utils';
2424

2525
interface MapOutput extends ModuleOutput {
2626
data: {
@@ -88,7 +88,9 @@ export class Substreams extends (EventEmitter as new () => TypedEmitter<MessageE
8888

8989
// Credentials
9090
const metadata = new Metadata();
91-
if ( options.authorization ) metadata.add('authorization', options.authorization);
91+
if ( options.authorization ) parseAuthorization(options.authorization).then( token => {
92+
metadata.add('authorization', token);
93+
})
9294
const creds = credentials.combineChannelCredentials(
9395
credentials.createSsl(),
9496
credentials.createFromMetadataGenerator((_, callback) => callback(null, metadata)),

src/utils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,28 @@ export function getSeconds( clock?: Clock ) {
2929
return Number(clock?.timestamp?.seconds);
3030
}
3131

32+
interface Token {
33+
token: string;
34+
expires_at: number;
35+
}
36+
37+
export async function authentication_token(api_key: string) {
38+
const url = 'https://auth.streamingfast.io/v1/auth/issue';
39+
const response = await fetch(url, {
40+
method: 'POST',
41+
headers: { 'Content-Type': 'application/json' },
42+
body: JSON.stringify({api_key})
43+
})
44+
return response.json() as Promise<Token>;
45+
}
46+
47+
export async function parseAuthorization(authorization: string ) {
48+
if ( authorization.includes("server_") ) {
49+
return (await authentication_token(authorization)).token;
50+
}
51+
return authorization;
52+
}
53+
3254
export const isIpfs = ( str: string ) => /^Qm[1-9A-Za-z]{44}$/.test(str);
3355

3456
export function parseStopBlock( startBlock: string, stopBlock?: string ) {

0 commit comments

Comments
 (0)