Skip to content

Commit

Permalink
How to use exported account and index files so they are visible in de…
Browse files Browse the repository at this point in the history
…veloper console (#274)

Co-authored-by: upalinski <[email protected]>
  • Loading branch information
upalinski and upalinski authored Nov 7, 2024
1 parent 1642c29 commit cb5d86f
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 5 deletions.
Binary file removed examples/node/2-store-read-file/2mb.jpg
Binary file not shown.
4 changes: 2 additions & 2 deletions examples/node/2-store-read-file/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ const dir = path.dirname(fileURLToPath(import.meta.url));
/**
* The file to be stored
*/
const inputFilePath = path.resolve(dir, '2mb.jpg');
const inputFilePath = path.resolve(dir, '../assets/nature.jpg');

/**
* The path to store the downloaded file
*/
const outputFilePath = path.resolve(dir, '2mb-downloaded.jpg');
const outputFilePath = path.resolve(dir, '../assets/nature-downloaded.jpg');

/**
* Create a DDC client instance and connect it to DDC TESTNET
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"encoded": "kBkc/6ghJVs4thxE7ZMSRhxyVtYi2EzjJz8gvI2HfKQAgAAAAQAAAAgAAAAs5o9q7l+BpKkYsTXvuroLsxdysjikaK8qRkm13kzbnCfG5t9Df2QnQfHxrYyboPp5SoGdKxhnz5HRLK4/Fq1wPZpk43XSaa/b4PxOphxLfFYdjG2MJt80QSIzzRrP6TNj6R/YfMsKSySGqYHgiOkVnJJyZq0Exc35+3W6CyESzciXzDIw2iYzNdV5CJWpiAtgnW7tHxmsldKhUmYf",
"encoding": { "content": ["pkcs8", "ed25519"], "type": ["scrypt", "xsalsa20-poly1305"], "version": "3" },
"address": "5EdskhqqsNCUABVFhsZkxXfG2p4sRtf9ChU9iC5pE9bhx1kP",
"meta": {}
}
31 changes: 31 additions & 0 deletions examples/node/5-use-exported-account/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { DdcClient, File, TESTNET, JsonSigner } from '@cere-ddc-sdk/ddc-client';
import * as fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

// The wallet should have enough CERE to pay for the transaction fees
const pathToAccount = './6S5nrcLgUrhC2tLpaG83VLrMndnQ66DPTz7j6PoRnQjZKpvx.json';
const accountPassphrase = '1234';

// The DDC bucket where the file will be stored
const bucketId = BigInt(448920);

// Detect the current directory
const dir = path.dirname(fileURLToPath(import.meta.url));

// Create DDC client instance using JSON account exported from Cere Wallet
const keyringPair = JSON.parse(fs.readFileSync(path.resolve(dir, pathToAccount)).toString());
const jsonSigner = new JsonSigner(keyringPair, { passphrase: accountPassphrase });
const client = await DdcClient.create(jsonSigner, TESTNET);

// Upload file
const pathToFileToUpload = path.resolve(dir, '../assets/nature.jpg');
const fileToUpload = new File(fs.createReadStream(pathToFileToUpload), { size: fs.statSync(pathToFileToUpload).size });
const uploadedFileUri = await client.store(bucketId, fileToUpload);
console.log(
'The file can be accessed by this URL',
`https://cdn.testnet.cere.network/${bucketId}/${uploadedFileUri.cid}`,
);
console.log('File stored into bucket', bucketId, 'with CID', uploadedFileUri.cid);

await client.disconnect();
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"encoded": "kBkc/6ghJVs4thxE7ZMSRhxyVtYi2EzjJz8gvI2HfKQAgAAAAQAAAAgAAAAs5o9q7l+BpKkYsTXvuroLsxdysjikaK8qRkm13kzbnCfG5t9Df2QnQfHxrYyboPp5SoGdKxhnz5HRLK4/Fq1wPZpk43XSaa/b4PxOphxLfFYdjG2MJt80QSIzzRrP6TNj6R/YfMsKSySGqYHgiOkVnJJyZq0Exc35+3W6CyESzciXzDIw2iYzNdV5CJWpiAtgnW7tHxmsldKhUmYf",
"encoding": { "content": ["pkcs8", "ed25519"], "type": ["scrypt", "xsalsa20-poly1305"], "version": "3" },
"address": "5EdskhqqsNCUABVFhsZkxXfG2p4sRtf9ChU9iC5pE9bhx1kP",
"meta": {}
}
8 changes: 8 additions & 0 deletions examples/node/6-developer-console-compatibility/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Developer console compatibility

You can use this guide if you want your files uploaded via SDK to be visible in Developer Console.

By default, files uploaded to DDC aren't indexed.
Once you uploaded file to DDC you receive it's CID a unique hash-based identifier so later you can access your file using bucket id and CID.

In order to have a folder structure (hierarchy) Developer Console uses DAG API. That API allows us to structure set of files if a folder hierarchy where each folder is a separate DAG node. So when client uploads a file or creates a folder the all DAG node above it are updated as well.
54 changes: 54 additions & 0 deletions examples/node/6-developer-console-compatibility/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { DdcClient, File, Link, DagNodeUri, DagNode, JsonSigner, TESTNET } from '@cere-ddc-sdk/ddc-client';
import * as fs from 'fs';
import { fileURLToPath } from 'url';
import path from 'path';

// The wallet should have enough CERE to pay for the transaction fees
const pathToAccount = './6S5nrcLgUrhC2tLpaG83VLrMndnQ66DPTz7j6PoRnQjZKpvx.json';
const accountPassphrase = '1234';

// The DDC bucket where the file will be stored
const bucketId = BigInt(448924);

// Detect the current directory
const dir = path.dirname(fileURLToPath(import.meta.url));

const pathToFileToUpload = path.resolve(dir, '../assets/nature.jpg');
const fileName = pathToFileToUpload.substring(pathToFileToUpload.lastIndexOf('/') + 1);

// Initialise client using JSON account exported from Cere Wallet
const keyringPair = JSON.parse(fs.readFileSync(path.resolve(dir, pathToAccount)).toString());
const jsonSigner = new JsonSigner(keyringPair, { passphrase: accountPassphrase });
const client = await DdcClient.create(jsonSigner, { ...TESTNET, logLevel: 'fatal' });

// Upload file
const fileSize = fs.statSync(pathToFileToUpload).size;
const fileToUpload = new File(fs.createReadStream(pathToFileToUpload), {
size: fileSize,
});
const uploadedFileUri = await client.store(bucketId, fileToUpload);
console.log('File stored into bucket', bucketId, 'with CID', uploadedFileUri.cid);
console.log(
'The file can be accessed by this URL',
`https://cdn.testnet.cere.network/${bucketId}/${uploadedFileUri.cid}`,
);

// Attach uploaded file to 'fs' DAG node (where 'fs' is a CNS name used by Developer Console to index files)
const filePathInDeveloperConsole = 'photos/nature/';
const rootDagNode = await client.read(new DagNodeUri(bucketId, 'fs'), { cacheControl: 'no-cache' }).catch((res) => {
if (res.message == 'Cannot resolve CNS name: "fs"') {
return new DagNode(null);
} else {
throw new Error("Failed to fetch 'fs' DAG node");
}
});
rootDagNode.links.push(new Link(uploadedFileUri.cid, fileSize, filePathInDeveloperConsole + fileName));
await client.store(bucketId, rootDagNode, { name: 'fs' });
console.log(
'The file can be found in developer console in bucket',
bucketId,
'and path is',
filePathInDeveloperConsole,
);

await client.disconnect();
6 changes: 4 additions & 2 deletions examples/node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ This examples directory contains several common DDC SDK use-cases for NodeJs app
- [How to store and read a file](./2-store-read-file/index.ts)
- [How to upload a website](./3-upload-website/index.ts)
- [How to stream events](./4-store-read-events/index.ts)
- [How to use exported account (Cere Wallet)](./5-use-exported-account/index.ts)
- [How to index files so that they are visible in Developer Console](./6-developer-console-compatibility/index.ts)

## Quick start

Expand All @@ -32,13 +34,13 @@ Run the following commands from the project root
4. Go to `examples` directory

```bash
cd ./examples
cd ./examples/node
```

5. Run an example

```bash
npm run example:1 # can be 1-4
npm run example:1 # can be 1-6
```


Expand Down
Binary file added examples/node/assets/nature.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion examples/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"example:1": "ts-node --esm ./1-create-bucket/index.ts",
"example:2": "ts-node --esm ./2-store-read-file/index.ts",
"example:3": "ts-node --esm ./3-upload-website/index.ts",
"example:4": "ts-node --esm ./4-store-read-events/index.ts"
"example:4": "ts-node --esm ./4-store-read-events/index.ts",
"example:5": "ts-node --esm ./5-use-exported-account/index.ts",
"example:6": "ts-node --esm ./6-developer-console-compatibility/index.ts"
},
"dependencies": {
"@cere-ddc-sdk/ddc-client": "2.13.0",
Expand Down

0 comments on commit cb5d86f

Please sign in to comment.