Skip to content

Commit

Permalink
Add authentication_token
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed Feb 1, 2023
1 parent 1eadea5 commit 59e5eb5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion example.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const stopBlockNum = "+10";
const substreams = new Substreams(outputModule, {
startBlockNum,
stopBlockNum,
authorization: process.env.SUBSTREAMS_API_TOKEN
authorization: "<STREAMINGFAST_KEY or SUBSTREAMS_API_TOKEN>"
});

(async () => {
Expand Down
2 changes: 1 addition & 1 deletion examples/subtivity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const stopBlockNum = "+10";
const substreams = new Substreams(outputModule, {
startBlockNum,
stopBlockNum,
authorization: process.env.SUBSTREAMS_API_TOKEN
authorization: process.env.STREAMINGFAST_KEY
});

(async () => {
Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export * from "./generated/sf/substreams/v1/substreams"
export * from "./utils";

// Utils
import { parseBlockData, parseStopBlock } from './utils';
import { parseAuthorization, parseBlockData, parseStopBlock } from './utils';

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

// Credentials
const metadata = new Metadata();
if ( options.authorization ) metadata.add('authorization', options.authorization);
if ( options.authorization ) parseAuthorization(options.authorization).then( token => {
metadata.add('authorization', token);
})
const creds = credentials.combineChannelCredentials(
credentials.createSsl(),
credentials.createFromMetadataGenerator((_, callback) => callback(null, metadata)),
Expand Down
22 changes: 22 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,28 @@ export function getSeconds( clock?: Clock ) {
return Number(clock?.timestamp?.seconds);
}

interface Token {
token: string;
expires_at: number;
}

export async function authentication_token(api_key: string) {
const url = 'https://auth.streamingfast.io/v1/auth/issue';
const response = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({api_key})
})
return response.json() as Promise<Token>;
}

export async function parseAuthorization(authorization: string ) {
if ( authorization.includes("server_") ) {
return (await authentication_token(authorization)).token;
}
return authorization;
}

export const isIpfs = ( str: string ) => /^Qm[1-9A-Za-z]{44}$/.test(str);

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

0 comments on commit 59e5eb5

Please sign in to comment.