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

fix: Update SDK token if signer address changed #262

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions packages/ddc/src/auth/sdkToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export const getSdkSigner = (signer: Signer, address: string) => {
return getRegestry(signer).get(address);
};

export const isValidSdkToken = (signer: Signer, token: AuthToken) => {
return signer.address === token.signature?.signer;
};

export const createSdkToken = async (signer: Signer) => {
if (!isWeb3Signer(signer)) {
return AuthToken.fullAccess().sign(signer);
Expand Down
17 changes: 14 additions & 3 deletions packages/ddc/src/routing/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { RouterNode, RouterOperation, RoutingStrategy } from './RoutingStrategy'
import { BlockchainStrategy, BlockchainStrategyConfig } from './BlockchainStrategy';
import { StaticStrategy, StaticStrategyConfig } from './StaticStrategy';
import { Logger, LoggerOptions, createLogger } from '../logger';
import { AuthToken, createSdkToken } from '../auth';
import { AuthToken, createSdkToken, isValidSdkToken } from '../auth';

export type RouterConfig = (StaticStrategyConfig | BlockchainStrategyConfig) &
LoggerOptions & {
Expand Down Expand Up @@ -46,6 +46,17 @@ export class Router {
}
}

/**
* Returns an SDK token for the current signer
*/
private getSdkToken() {
this.sdkTokenPromise = Promise.resolve(this.sdkTokenPromise).then((token) =>
token && isValidSdkToken(this.signer, token) ? token : createSdkToken(this.signer),
);

return this.sdkTokenPromise;
}

/**
* Retrieves a node for a specific operation in a specific bucket, excluding certain nodes.
*
Expand All @@ -60,7 +71,7 @@ export class Router {
async getNode(operation: RouterOperation, bucketId: BucketId, exclude: string[] = []) {
this.logger.info('Getting node for operation "%s" in bucket %s', operation, bucketId);

this.sdkTokenPromise ||= createSdkToken(this.signer); // Request the token only once
const sdkTokenPromise = this.getSdkToken();
await this.strategy.isReady();

const allNodes = await this.strategy.getNodes(bucketId);
Expand All @@ -75,7 +86,7 @@ export class Router {
const storageNode = new StorageNode(this.signer, {
...node,
logger: this.logger,
authToken: await this.sdkTokenPromise,
authToken: await sdkTokenPromise,
nodeId: node.nodeId || node.grpcUrl,
});

Expand Down
Loading