-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathnative_ipfs_dag_pb_chunked_data.js
More file actions
101 lines (84 loc) · 4.76 KB
/
native_ipfs_dag_pb_chunked_data.js
File metadata and controls
101 lines (84 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { createClient } from 'polkadot-api';
import { getWsProvider } from 'polkadot-api/ws';
import { cryptoWaitReady } from '@polkadot/util-crypto';
import { cidFromBytes, buildUnixFSDagPB, convertCid } from './cid_dag_metadata.js';
import { generateTextImage, fileToDisk, filesAreEqual, newSigner, waitForBlockProduction, DEFAULT_IPFS_GATEWAY_URL } from './common.js';
import { authorizeAccount, store, storeChunkedFile, fetchCid, TX_MODE_FINALIZED_BLOCK } from './api.js';
import { bulletin } from './.papi/descriptors/dist/index.js';
import assert from "assert";
import fs from 'fs'
import os from 'os'
import path from 'path'
import * as dagPB from "@ipld/dag-pb";
// Command line arguments: [ws_url] [seed] [ipfs_api_url]
const args = process.argv.slice(2);
const NODE_WS = args[0] || 'ws://localhost:10000';
const SEED = args[1] || '//Alice';
const HTTP_IPFS_API = args[2] || DEFAULT_IPFS_GATEWAY_URL;
// ---- CONFIG ----
const CHUNK_SIZE = 6 * 1024 // 6 KB
// -----------------
async function main() {
await cryptoWaitReady()
let client, resultCode;
try {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "bulletinimggen-"));
const filePath = path.join(tmpDir, "image.jpeg");
const downloadedFilePath = path.join(tmpDir, "downloaded.jpeg");
generateTextImage(filePath, "Hello, Bulletin dag - " + new Date().toString());
// Create PAPI client with WebSocket provider
client = createClient(getWsProvider(NODE_WS));
// Get typed API with generated descriptors
const typedApi = client.getTypedApi(bulletin);
await waitForBlockProduction(typedApi);
// Create signers
const { signer: authorizationSigner } = newSigner(SEED);
const { signer: whoSigner, address: whoAddress } = newSigner('//Nativeipfsdagsigner');
console.log('✅ Connected to Bulletin node')
console.log(`💳 Using account: ${whoAddress}`)
// Make sure an account can store data.
await authorizeAccount(typedApi, authorizationSigner, whoAddress, 128, BigInt(64 * 1024 * 1024), TX_MODE_FINALIZED_BLOCK);
// Read the file, chunk it, store in Bulletin and return CIDs.
let { chunks } = await storeChunkedFile(typedApi, whoSigner, filePath, CHUNK_SIZE);
////////////////////////////////////////////////////////////////////////////////////
// Example download picture by rootCID with IPFS DAG feature and HTTP gateway.
// Demonstrates how to download chunked content by one root CID.
// Basically, just take the `metadataJson` with already stored chunks and convert it to the DAG-PB format.
const { rootCid: expectedRootCid, dagBytes } = await buildUnixFSDagPB(chunks, 0x12);
let calculatedRootCid = await cidFromBytes(dagBytes, 0x70, 0x12);
assert.deepStrictEqual(expectedRootCid, calculatedRootCid);
// Store DAG file directly to the Bulletin. with DAG-PB / SHA2_256 content_hash.
// !!! (No IPFS magic needed: ipfs.dag.put or ipfs.block.put(dagBytes, { format: 'dag-pb', mhtype: 'sha2-256'}))
let { cid: rootCid } = await store(typedApi, whoSigner, dagBytes, 0x70, 0x12);
assert.deepStrictEqual(expectedRootCid, rootCid);
// Read by rootCID directly over IPFS gateway, which handles download all the chunks.
// (Other words Bulletin is compatible)
console.log('🧱 DAG stored on Bulletin with CID:', rootCid.toString())
console.log('\n🌐 Try opening in browser:')
console.log(` ${HTTP_IPFS_API}/ipfs/${rootCid.toString()}`)
console.log(" (You'll see binary content since this is an image)")
console.log('')
console.log(` ${HTTP_IPFS_API}/ipfs/${convertCid(rootCid, 0x55)}`)
console.log(" (You'll see the DAG file itself)")
// Download the content from IPFS HTTP gateway.
const fullBuffer = await fetchCid(HTTP_IPFS_API, rootCid);
console.log(`✅ Reconstructed file size: ${fullBuffer.length} bytes`);
await fileToDisk(downloadedFilePath, fullBuffer);
filesAreEqual(filePath, downloadedFilePath);
// Derive CID for DAG content from rootCID (change codec from 0x70 -> 0x55)
const rootCidAsRaw = convertCid(rootCid, 0x55);
const storedDagNode = dagPB.decode(await fetchCid(HTTP_IPFS_API, rootCidAsRaw));
const decodedDagNode = dagPB.decode(Buffer.from(dagBytes));
console.log("✅ Reconstructed DAG file: ", storedDagNode);
assert.deepStrictEqual(storedDagNode, decodedDagNode);
console.log(`\n\n\n✅✅✅ Passed all tests ✅✅✅`);
resultCode = 0;
} catch (error) {
console.error("❌ Error:", error);
resultCode = 1;
} finally {
if (client) client.destroy();
process.exit(resultCode);
}
}
await main();