Skip to content

fix for local anchors #16

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

Merged
merged 2 commits into from
Jul 16, 2025
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
62 changes: 58 additions & 4 deletions anchor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ interface Anchor {
};
}

interface BasicAuthConfig {
username: string;
password: string;
}

interface UpdateOptions {
localPath?: string; // Local file
remoteUrls?: string[]; // Remote endpoints to fetch anchor file
staticAnchors?: Anchor[]; // Use the following static anchors
checkIntervalMs?: number; // Periodic refresh
basicAuth?: BasicAuthConfig;
}

export class AnchorStore {
Expand Down Expand Up @@ -198,12 +204,59 @@ export class AnchorStore {
return null;
}

private extractAuthFromUrl(url: string): { username: string; password: string } | null {
try {
const urlObj = new URL(url);
if (urlObj.username && urlObj.password) {
return {
username: decodeURIComponent(urlObj.username),
password: decodeURIComponent(urlObj.password)
};
}
} catch (err) {
log(`Invalid URL format: ${url}`);
}
return null;
}

private cleanUrl(url: string): string {
try {
const urlObj = new URL(url);
urlObj.username = '';
urlObj.password = '';
return urlObj.toString();
} catch (err) {
log(`Invalid URL format: ${url}`);
return url;
}
}

private createAuthHeaders(url: string): HeadersInit {
const headers: HeadersInit = {};
const authFromUrl = this.extractAuthFromUrl(url);
if (authFromUrl) {
const credentials = Buffer.from(`${authFromUrl.username}:${authFromUrl.password}`).toString('base64');
headers['Authorization'] = `Basic ${credentials}`;
} else if (this.options.basicAuth) {
const { username, password } = this.options.basicAuth;
const credentials = Buffer.from(`${username}:${password}`).toString('base64');
headers['Authorization'] = `Basic ${credentials}`;
}

return headers;
}

private async fetchAnchorsFromRemotes(remoteUrls: string[]): Promise<Anchor[]> {
const responses = await Promise.all(
remoteUrls.map(async url => {
log(`Fetching anchors from: ${url}`);
try {
const res = await fetch(url);
const authHeaders = this.createAuthHeaders(url);
const cleanUrl = this.cleanUrl(url);

const res = await fetch(cleanUrl, {
headers: authHeaders
});
if (!res.ok) {
throw new Error(`Status: ${res.status}`);
}
Expand Down Expand Up @@ -238,9 +291,9 @@ export class AnchorStore {
for (const group of groups.values()) {
if (
!chosen ||
group.count > chosen.count ||
(group.count === chosen.count &&
group.anchors[0].block.height > chosen.anchors[0].block.height)
group.count > chosen.count ||
(group.count === chosen.count &&
group.anchors[0].block.height > chosen.anchors[0].block.height)
) {
chosen = group;
}
Expand All @@ -251,6 +304,7 @@ export class AnchorStore {
return chosen.anchors;
}


public isStale(version: number): boolean {
return version < this.staleThreshold;
}
Expand Down
21 changes: 12 additions & 9 deletions bin/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,19 @@ export async function nodeOpts(opts: MainOptions): Promise<FabricOptions> {

export async function anchorFromOpts(opts: MainOptions): Promise<AnchorStore> {
const localAnchors = opts.localAnchors || process.env.FABRIC_LOCAL_ANCHORS;
const remoteAnchors =
opts.remoteAnchors ||
(process.env.FABRIC_REMOTE_ANCHORS
? process.env.FABRIC_REMOTE_ANCHORS.split(',')
: ['http://127.0.0.1:7225/root-anchors.json']);
const remoteAnchors = opts.remoteAnchors || (process.env.FABRIC_REMOTE_ANCHORS ? process.env.FABRIC_REMOTE_ANCHORS.split(',') : undefined);

return AnchorStore.create({
localPath: localAnchors,
remoteUrls: remoteAnchors,
});
const updateOptions: any = {};

if (localAnchors) {
updateOptions.localPath = localAnchors;
} else if (remoteAnchors) {
updateOptions.remoteUrls = remoteAnchors;
} else {
updateOptions.remoteUrls = ['http://127.0.0.1:7225/root-anchors.json'];
}

return AnchorStore.create(updateOptions);
}

export function joinHostPort(address: Address | null): string {
Expand Down