Skip to content
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
69 changes: 57 additions & 12 deletions src/ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ let ProjectId = "";
let ProjectSecret = "";

let EstuaryInstance;
let NumbersProtocolInstance;
let NumbersProtocolCaptureToken = ""; // Store the Capture Token
let NumbersProtocolIpfsGatewayLambda = "https://mjacdgxzla.execute-api.us-east-1.amazonaws.com/default/ipfs-add-with-payment";

export async function initInfura(projectId, projectSecret) {
ProjectId = projectId;
Expand Down Expand Up @@ -110,25 +111,69 @@ export async function estuaryAdd(bytes) {
}
}

export async function initNumbersProtocol(apiKey) {
NumbersProtocolInstance = new Estuary(apiKey);
// Update this function to accept and store the Capture Token
export async function initNumbersProtocol(captureToken) {
NumbersProtocolCaptureToken = captureToken;
}

export async function numbersProtocolIpfsAddBytes(bytes) {
let cid;
try {
cid = await NumbersProtocolInstance.addFromBuffer(bytes);
return cid;
// Create form data with the file
const fileReadStream = stream.Readable.from(bytes);
const formData = new FormData();
formData.append('file', fileReadStream);

// Use Numbers Protocol IPFS add API endpoint with Capture Token in header
const url = NumbersProtocolIpfsGatewayLambda;
const headers = {
"Authorization": `token ${NumbersProtocolCaptureToken}`,
...formData.getHeaders(),
};

const httpResponse = await http.post(url, formData, headers);
const assetCid = httpResponse.data.cid;
return assetCid;
} catch(error) {
console.error(error);
console.error("Error adding to Numbers Protocol IPFS:", error);
throw error;
}
}

export async function numbersProtocolIpfsCat(cid) {
const url = `https://${cid}.ipfs.numbersprotocol.io`;
const requestConfig = {
timeout: { request: 30000 },
try {
// Use Numbers Protocol IPFS cat API endpoint with Capture Token
const url = NumbersProtocolIpfsGatewayLambda;
const requestConfig = {
headers: {
"Authorization": `token ${NumbersProtocolCaptureToken}`
},
timeout: { request: 30000 },
};

const r = await got.get(url, requestConfig);
return r.rawBody;
} catch(error) {
console.error(`Failed to download content of CID ${cid} from Numbers Protocol IPFS:`, error);
throw error;
}
}

// Add new function to unpin content from Numbers Protocol IPFS with Capture Token
export async function numbersProtocolIpfsUnpin(cid) {
try {
// Use Numbers Protocol IPFS unpin API endpoint with Capture Token
const url = NumbersProtocolIpfsGatewayLambda;
const requestConfig = {
headers: {
"Authorization": `token ${NumbersProtocolCaptureToken}`
},
timeout: { request: 30000 },
};

const r = await got.delete(url, requestConfig);
return r.statusCode === 200;
} catch(error) {
console.error(`Failed to unpin CID ${cid} from Numbers Protocol IPFS:`, error);
return false;
}
const r = await got.get(url, requestConfig);
return r.rawBody;
}
4 changes: 4 additions & 0 deletions src/nit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export const nitconfigTemplate = {
"projectId": "a".repeat(infuraSecretLength),
"projectSecret": "a".repeat(infuraSecretLength)
},
/* Numbers Protocol IPFS settings */
"numbersProtocol": {
"captureToken": "" // Capture Token for Numbers Protocol IPFS API
},
"commitDatabase": {
"updateUrl": "",
"commitUrl": "",
Expand Down
5 changes: 5 additions & 0 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,11 @@ async function main() {
const blockchain = await nit.loadBlockchain(config);

await ipfs.initInfura(config.infura.projectId, config.infura.projectSecret);

// Initialize Numbers Protocol IPFS with Capture Token if available
if (config.numbersProtocol && config.numbersProtocol.captureToken) {
await ipfs.initNumbersProtocol(config.numbersProtocol.captureToken);
}

if (args.command === "ipfsadd") {
const contentBytes = fs.readFileSync(args.params.fileapth);
Expand Down