Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(node-client): allow xtoken for http connections #387

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 65 additions & 33 deletions yellowstone-grpc-client-nodejs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export {

export default class Client {
_client: GeyserClient;

_insecureXToken: string | undefined;
constructor(
endpoint: string,
xToken: string | undefined,
Expand All @@ -79,13 +79,24 @@ export default class Client {
);
} else {
creds = ChannelCredentials.createInsecure();
if (xToken !== undefined) {
this._insecureXToken = xToken;
}
}

this._client = new GeyserClient(endpointURL.host, creds, channelOptions);
}

private _getInsecureMetadata(): Metadata {
const metadata = new Metadata();
if (this._insecureXToken) {
metadata.add("x-token", this._insecureXToken);
}
return metadata;
}

async subscribe() {
return await this._client.subscribe();
return await this._client.subscribe(this._getInsecureMetadata());
}

async subscribeOnce(
Expand All @@ -99,7 +110,7 @@ export default class Client {
commitment: CommitmentLevel | undefined,
accountsDataSlice: SubscribeRequestAccountsDataSlice[]
) {
const stream = await this._client.subscribe();
const stream = await this._client.subscribe(this._getInsecureMetadata());

await new Promise<void>((resolve, reject) => {
stream.write(
Expand Down Expand Up @@ -129,51 +140,67 @@ export default class Client {

async ping(count: number): Promise<number> {
return await new Promise<number>((resolve, reject) => {
this._client.ping({ count }, (err, response) => {
if (err === null || err === undefined) {
resolve(response.count);
} else {
reject(err);
this._client.ping(
{ count },
this._getInsecureMetadata(),
(err, response) => {
if (err === null || err === undefined) {
resolve(response.count);
} else {
reject(err);
}
}
});
);
});
}

async getLatestBlockhash(
commitment?: CommitmentLevel
): Promise<GetLatestBlockhashResponse> {
return await new Promise<GetLatestBlockhashResponse>((resolve, reject) => {
this._client.getLatestBlockhash({ commitment }, (err, response) => {
if (err === null || err === undefined) {
resolve(response);
} else {
reject(err);
this._client.getLatestBlockhash(
{ commitment },
this._getInsecureMetadata(),
(err, response) => {
if (err === null || err === undefined) {
resolve(response);
} else {
reject(err);
}
}
});
);
});
}

async getBlockHeight(commitment?: CommitmentLevel): Promise<string> {
return await new Promise<string>((resolve, reject) => {
this._client.getBlockHeight({ commitment }, (err, response) => {
if (err === null || err === undefined) {
resolve(response.blockHeight);
} else {
reject(err);
this._client.getBlockHeight(
{ commitment },
this._getInsecureMetadata(),
(err, response) => {
if (err === null || err === undefined) {
resolve(response.blockHeight);
} else {
reject(err);
}
}
});
);
});
}

async getSlot(commitment?: CommitmentLevel): Promise<string> {
return await new Promise<string>((resolve, reject) => {
this._client.getSlot({ commitment }, (err, response) => {
if (err === null || err === undefined) {
resolve(response.slot);
} else {
reject(err);
this._client.getSlot(
{ commitment },
this._getInsecureMetadata(),
(err, response) => {
if (err === null || err === undefined) {
resolve(response.slot);
} else {
reject(err);
}
}
});
);
});
}

Expand All @@ -184,6 +211,7 @@ export default class Client {
return await new Promise<IsBlockhashValidResponse>((resolve, reject) => {
this._client.isBlockhashValid(
{ blockhash, commitment },
this._getInsecureMetadata(),
(err, response) => {
if (err === null || err === undefined) {
resolve(response);
Expand All @@ -197,13 +225,17 @@ export default class Client {

async getVersion(): Promise<string> {
return await new Promise<string>((resolve, reject) => {
this._client.getVersion({}, (err, response) => {
if (err === null || err === undefined) {
resolve(response.version);
} else {
reject(err);
this._client.getVersion(
{},
this._getInsecureMetadata(),
(err, response) => {
if (err === null || err === undefined) {
resolve(response.version);
} else {
reject(err);
}
}
});
);
});
}
}