-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
How to use exported account and index files so they are visible in de…
…veloper console (#274) Co-authored-by: upalinski <[email protected]>
- Loading branch information
Showing
10 changed files
with
114 additions
and
5 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
examples/node/5-use-exported-account/6S5nrcLgUrhC2tLpaG83VLrMndnQ66DPTz7j6PoRnQjZKpvx.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
6 changes: 6 additions & 0 deletions
6
...e/6-developer-console-compatibility/6S5nrcLgUrhC2tLpaG83VLrMndnQ66DPTz7j6PoRnQjZKpvx.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters