Skip to content

Commit

Permalink
Merge pull request #92 from DIG-Network/release/v0.0.1-alpha.102
Browse files Browse the repository at this point in the history
Release/v0.0.1 alpha.102
  • Loading branch information
MichaelTaylor3D authored Sep 26, 2024
2 parents 545d16b + 72237ae commit 7430241
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 24 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [0.0.1-alpha.102](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.101...v0.0.1-alpha.102) (2024-09-26)


### Features

* support ip6 on content server, propagation server and incentive server ([90ac77a](https://github.com/DIG-Network/dig-chia-sdk/commit/90ac77a6e5a62737de1d0f58ef77bece3fe02af9))

### [0.0.1-alpha.101](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.100...v0.0.1-alpha.101) (2024-09-26)


Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dignetwork/dig-sdk",
"version": "0.0.1-alpha.101",
"version": "0.0.1-alpha.102",
"description": "",
"type": "commonjs",
"main": "./dist/index.js",
Expand Down
17 changes: 9 additions & 8 deletions src/DigNetwork/ContentServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import http from "http";
import { URL } from "url";
import { Readable } from "stream";
import { getOrCreateSSLCerts } from "../utils/ssl";
import { formatHost } from "../utils/network";

export class ContentServer {
private ipAddress: string;
Expand All @@ -29,7 +30,7 @@ export class ContentServer {
challengeHex?: string
): Promise<string> {
// Construct the base URL
let url = `https://${this.ipAddress}:${ContentServer.port}/chia.${this.storeId}.${rootHash}/${key}`;
let url = `https://${formatHost(this.ipAddress)}:${ContentServer.port}/chia.${this.storeId}.${rootHash}/${key}`;

// If a challenge is provided, append it as a query parameter
if (challengeHex) {
Expand All @@ -56,19 +57,19 @@ export class ContentServer {

// Method to get the .well-known information
public async getWellKnown(): Promise<any> {
const url = `https://${this.ipAddress}:${ContentServer.port}/.well-known`;
const url = `https://${formatHost(this.ipAddress)}:${ContentServer.port}/.well-known`;
return this.fetchJson(url);
}

// Method to get the list of known stores
public async getKnownStores(): Promise<any> {
const url = `https://${this.ipAddress}:${ContentServer.port}/.well-known/stores`;
const url = `https://${formatHost(this.ipAddress)}:${ContentServer.port}/.well-known/stores`;
return this.fetchJson(url);
}

// Method to get the index of all stores
public async getStoresIndex(): Promise<any> {
const url = `https://${this.ipAddress}:${ContentServer.port}/`;
const url = `https://${formatHost(this.ipAddress)}:${ContentServer.port}/`;
return this.fetchJson(url);
}

Expand All @@ -80,7 +81,7 @@ export class ContentServer {
udi += `.${rootHash}`;
}

const url = `https://${this.ipAddress}:${ContentServer.port}/${udi}`;
const url = `https://${formatHost(this.ipAddress)}:${ContentServer.port}/${udi}`;
return this.fetchJson(url);
}

Expand All @@ -95,7 +96,7 @@ export class ContentServer {
udi += `.${rootHash}`;
}

const url = `https://${this.ipAddress}:${ContentServer.port}/${udi}/${key}`;
const url = `https://${formatHost(this.ipAddress)}:${ContentServer.port}/${udi}/${key}`;
return this.head(url);
}

Expand All @@ -104,7 +105,7 @@ export class ContentServer {
success: boolean;
headers?: http.IncomingHttpHeaders;
}> {
let url = `https://${this.ipAddress}:${ContentServer.port}/chia.${this.storeId}`;
let url = `https://${formatHost(this.ipAddress)}:${ContentServer.port}/chia.${this.storeId}`;

if (options?.hasRootHash) {
url += `?hasRootHash=${options.hasRootHash}`;
Expand All @@ -131,7 +132,7 @@ export class ContentServer {
}

return new Promise((resolve, reject) => {
const url = `https://${this.ipAddress}:${ContentServer.port}/${udi}/${key}`;
const url = `https://${formatHost(this.ipAddress)}:${ContentServer.port}/${udi}/${key}`;
const urlObj = new URL(url);

const requestOptions = {
Expand Down
11 changes: 6 additions & 5 deletions src/DigNetwork/IncentiveServer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import https from "https";
import { URL } from "url";
import { IncentiveProgramData } from "../types";
import { formatHost } from "../utils/network";

export class IncentiveServer {
private ipAddress: string;
Expand All @@ -12,35 +13,35 @@ export class IncentiveServer {

// Method to create a new incentive program
public async createIncentiveProgram(data: IncentiveProgramData): Promise<void> {
const url = `https://${this.ipAddress}:${this.port}/incentive`;
const url = `https://${formatHost(this.ipAddress)}:${this.port}/incentive`;

await this.makeRequest(url, "POST", data);
}

// Method to update an existing incentive program
public async updateIncentiveProgram(data: IncentiveProgramData): Promise<void> {
const url = `https://${this.ipAddress}:${this.port}/incentive`;
const url = `https://${formatHost(this.ipAddress)}:${this.port}/incentive`;

await this.makeRequest(url, "PUT", data);
}

// Method to delete an incentive program by storeId
public async deleteIncentiveProgram(storeId: string): Promise<void> {
const url = `https://${this.ipAddress}:${this.port}/incentive`;
const url = `https://${formatHost(this.ipAddress)}:${this.port}/incentive`;

await this.makeRequest(url, "DELETE", { storeId });
}

// Method to get all incentive programs
public async getAllIncentivePrograms(): Promise<IncentiveProgramData[]> {
const url = `https://${this.ipAddress}:${this.port}/incentive`;
const url = `https://${formatHost(this.ipAddress)}:${this.port}/incentive`;

return this.makeRequest(url, "GET");
}

// Method to get a specific incentive program by storeId
public async getIncentiveProgram(storeId: string): Promise<IncentiveProgramData> {
const url = `https://${this.ipAddress}:${this.port}/incentive/${storeId}`;
const url = `https://${formatHost(this.ipAddress)}:${this.port}/incentive/${storeId}`;

return this.makeRequest(url, "GET");
}
Expand Down
17 changes: 9 additions & 8 deletions src/DigNetwork/PropagationServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { PassThrough } from "stream";
import { promptCredentials } from "../utils/credentialsUtils";
import { STORE_PATH } from "../utils/config";
import { Wallet, DataStore } from "../blockchain";
import { formatHost } from '../utils/network';

// Helper function to trim long filenames with ellipsis and ensure consistent padding
function formatFilename(filename: string | undefined, maxLength = 30): string {
Expand Down Expand Up @@ -120,7 +121,7 @@ export class PropagationServer {
httpsAgent: this.createHttpsAgent(),
};

let url = `https://${this.ipAddress}:${PropagationServer.port}/${this.storeId}`;
let url = `https://${formatHost(this.ipAddress)}:${PropagationServer.port}/${this.storeId}`;
if (rootHash) {
url += `?hasRootHash=${rootHash}`;
}
Expand Down Expand Up @@ -193,7 +194,7 @@ export class PropagationServer {
};
}

const url = `https://${this.ipAddress}:${PropagationServer.port}/upload/${this.storeId}?roothash=${rootHash}`;
const url = `https://${formatHost(this.ipAddress)}:${PropagationServer.port}/upload/${this.storeId}?roothash=${rootHash}`;
const response = await axios.post(url, formData, config);

this.sessionId = response.data.sessionId;
Expand Down Expand Up @@ -222,7 +223,7 @@ export class PropagationServer {
httpsAgent: this.createHttpsAgent(),
};

const url = `https://${this.ipAddress}:${PropagationServer.port}/upload/${this.storeId}/${this.sessionId}/${filename}`;
const url = `https://${formatHost(this.ipAddress)}:${PropagationServer.port}/upload/${this.storeId}/${this.sessionId}/${filename}`;
const response = await axios.head(url, config);

// Check for 'x-file-exists' header
Expand Down Expand Up @@ -326,7 +327,7 @@ export class PropagationServer {
maxBodyLength: Infinity,
};

const url = `https://${this.ipAddress}:${PropagationServer.port}/upload/${this.storeId}/${this.sessionId}/${dataPath}`;
const url = `https://${formatHost(this.ipAddress)}:${PropagationServer.port}/upload/${this.storeId}/${this.sessionId}/${dataPath}`;

// Create a promise that resolves when the progress stream ends
const progressPromise = new Promise<void>((resolve, reject) => {
Expand Down Expand Up @@ -368,7 +369,7 @@ export class PropagationServer {
: undefined,
};

const url = `https://${this.ipAddress}:${PropagationServer.port}/commit/${this.storeId}/${this.sessionId}`;
const url = `https://${formatHost(this.ipAddress)}:${PropagationServer.port}/commit/${this.storeId}/${this.sessionId}`;
const response = await axios.post(url, {}, config);

spinner.success({
Expand Down Expand Up @@ -453,7 +454,7 @@ export class PropagationServer {
* Logs progress using a local cli-progress bar.
*/
async fetchFile(dataPath: string): Promise<Buffer> {
const url = `https://${this.ipAddress}:${PropagationServer.port}/fetch/${this.storeId}/${dataPath}`;
const url = `https://${formatHost(this.ipAddress)}:${PropagationServer.port}/fetch/${this.storeId}/${dataPath}`;
const config: AxiosRequestConfig = {
responseType: "stream",
httpsAgent: this.createHttpsAgent(),
Expand Down Expand Up @@ -544,7 +545,7 @@ export class PropagationServer {
httpsAgent: this.createHttpsAgent(),
};

const url = `https://${this.ipAddress}:${PropagationServer.port}/store/${this.storeId}/${rootHash}/${dataPath}`;
const url = `https://${formatHost(this.ipAddress)}:${PropagationServer.port}/store/${this.storeId}/${rootHash}/${dataPath}`;
const response = await axios.head(url, config);

// Check the headers for file existence and size
Expand Down Expand Up @@ -574,7 +575,7 @@ export class PropagationServer {
rootHash: string,
baseDir: string
) {
const url = `https://${this.ipAddress}:${PropagationServer.port}/fetch/${this.storeId}/${dataPath}`;
const url = `https://${formatHost(this.ipAddress)}:${PropagationServer.port}/fetch/${this.storeId}/${dataPath}`;
let downloadPath = path.join(baseDir, dataPath);

// Ensure that the directory for the file exists
Expand Down
13 changes: 13 additions & 0 deletions src/utils/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,16 @@ export const getPublicIpAddress = async (): Promise<string | undefined> => {
}
}
};

// Helper function to wrap IPv6 addresses in brackets
export const formatHost = (host: string): string => {
const ipv6Pattern = /^[a-fA-F0-9:]+$/; // Simple regex to match raw IPv6 addresses (without brackets)
const hasBrackets = /^\[.*\]$/; // Regex to check if the address already has brackets

// If it's an IPv6 address without brackets, add them
if (ipv6Pattern.test(host) && !hasBrackets.test(host)) {
return `[${host}]`;
}

return host; // Return the host as is (IPv4, hostname, or already bracketed IPv6)
};

0 comments on commit 7430241

Please sign in to comment.