Skip to content

Commit 21b5551

Browse files
committed
feat: add Numbers' IPFS API support
Signed-off-by: Bofu Chen <bofu@numbersprotocol.io>
1 parent e62c827 commit 21b5551

File tree

3 files changed

+65
-12
lines changed

3 files changed

+65
-12
lines changed

src/ipfs.ts

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ let ProjectId = "";
1010
let ProjectSecret = "";
1111

1212
let EstuaryInstance;
13-
let NumbersProtocolInstance;
13+
let NumbersProtocolCaptureToken = ""; // Store the Capture Token
1414

1515
export async function initInfura(projectId, projectSecret) {
1616
ProjectId = projectId;
@@ -110,25 +110,69 @@ export async function estuaryAdd(bytes) {
110110
}
111111
}
112112

113-
export async function initNumbersProtocol(apiKey) {
114-
NumbersProtocolInstance = new Estuary(apiKey);
113+
// Update this function to accept and store the Capture Token
114+
export async function initNumbersProtocol(captureToken) {
115+
NumbersProtocolCaptureToken = captureToken;
115116
}
116117

117118
export async function numbersProtocolIpfsAddBytes(bytes) {
118-
let cid;
119119
try {
120-
cid = await NumbersProtocolInstance.addFromBuffer(bytes);
121-
return cid;
120+
// Create form data with the file
121+
const fileReadStream = stream.Readable.from(bytes);
122+
const formData = new FormData();
123+
formData.append('file', fileReadStream);
124+
125+
// Use Numbers Protocol IPFS add API endpoint with Capture Token in header
126+
const url = "https://eow75n0xni8ruiy.m.pipedream.net";
127+
const headers = {
128+
"Authorization": `token ${NumbersProtocolCaptureToken}`,
129+
...formData.getHeaders(),
130+
};
131+
132+
const httpResponse = await http.post(url, formData, headers);
133+
const assetCid = httpResponse.data.cid;
134+
return assetCid;
122135
} catch(error) {
123-
console.error(error);
136+
console.error("Error adding to Numbers Protocol IPFS:", error);
137+
throw error;
124138
}
125139
}
126140

127141
export async function numbersProtocolIpfsCat(cid) {
128-
const url = `https://${cid}.ipfs.numbersprotocol.io`;
129-
const requestConfig = {
130-
timeout: { request: 30000 },
142+
try {
143+
// Use Numbers Protocol IPFS cat API endpoint with Capture Token
144+
const url = `https://eobf91xa1ra7i2n.m.pipedream.net`;
145+
const requestConfig = {
146+
headers: {
147+
"Authorization": `token ${NumbersProtocolCaptureToken}`
148+
},
149+
timeout: { request: 30000 },
150+
};
151+
152+
const r = await got.get(url, requestConfig);
153+
return r.rawBody;
154+
} catch(error) {
155+
console.error(`Failed to download content of CID ${cid} from Numbers Protocol IPFS:`, error);
156+
throw error;
157+
}
158+
}
159+
160+
// Add new function to unpin content from Numbers Protocol IPFS with Capture Token
161+
export async function numbersProtocolIpfsUnpin(cid) {
162+
try {
163+
// Use Numbers Protocol IPFS unpin API endpoint with Capture Token
164+
const url = `https://eo3wcvdjj73vq4x.m.pipedream.net`;
165+
const requestConfig = {
166+
headers: {
167+
"Authorization": `token ${NumbersProtocolCaptureToken}`
168+
},
169+
timeout: { request: 30000 },
170+
};
171+
172+
const r = await got.delete(url, requestConfig);
173+
return r.statusCode === 200;
174+
} catch(error) {
175+
console.error(`Failed to unpin CID ${cid} from Numbers Protocol IPFS:`, error);
176+
return false;
131177
}
132-
const r = await got.get(url, requestConfig);
133-
return r.rawBody;
134178
}

src/nit.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ export const nitconfigTemplate = {
7676
"projectId": "a".repeat(infuraSecretLength),
7777
"projectSecret": "a".repeat(infuraSecretLength)
7878
},
79+
/* Numbers Protocol IPFS settings */
80+
"numbersProtocol": {
81+
"captureToken": "" // Capture Token for Numbers Protocol IPFS API
82+
},
7983
"commitDatabase": {
8084
"updateUrl": "",
8185
"commitUrl": "",

src/run.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,11 @@ async function main() {
454454
const blockchain = await nit.loadBlockchain(config);
455455

456456
await ipfs.initInfura(config.infura.projectId, config.infura.projectSecret);
457+
458+
// Initialize Numbers Protocol IPFS with Capture Token if available
459+
if (config.numbersProtocol && config.numbersProtocol.captureToken) {
460+
await ipfs.initNumbersProtocol(config.numbersProtocol.captureToken);
461+
}
457462

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

0 commit comments

Comments
 (0)