This repository has been archived by the owner on May 24, 2023. It is now read-only.
forked from koii-network/task-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlinktree_task.js
67 lines (48 loc) · 1.96 KB
/
linktree_task.js
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
const { namespaceWrapper } = require('./namespaceWrapper');
const createFile = require('./helpers/createFile.js');
const deleteFile = require('./helpers/deleteFile');
const fs = require('fs');
const { Web3Storage, getFilesFromPath } = require('web3.storage');
const storageClient = new Web3Storage({
token: process.env.SECRET_WEB3_STORAGE_KEY,
});
const bs58 = require('bs58');
const nacl = require('tweetnacl');
const db = require('./db_model');
const { Keypair } = require('@solana/web3.js'); // TEST For local testing
const main = async () => {
console.log('******/ IN Linktree Task FUNCTION /******');
// Load node's keypair from the JSON file
const keypair = await namespaceWrapper.getSubmitterAccount();
// TEST For local testing, hardcode the keypair
// const keypair = Keypair.generate();
// Get linktree list fron localdb
const proofs_list_object = await db.getAllProofs();
// Use the node's keypair to sign the linktree list
const messageUint8Array = new Uint8Array(
Buffer.from(JSON.stringify(proofs_list_object)),
);
const signedMessage = nacl.sign(messageUint8Array, keypair.secretKey);
const signature = signedMessage.slice(0, nacl.sign.signatureLength);
const submission_value = {
proofs: proofs_list_object,
node_publicKey: keypair.publicKey,
node_signature: bs58.encode(signature),
};
// upload the proofs of the linktree on web3.storage
const path = `./Linktree/proofs.json`;
if (!fs.existsSync('./Linktree')) fs.mkdirSync('./Linktree');
console.log('PATH', path);
await createFile(path, submission_value);
if (storageClient) {
const file = await getFilesFromPath(path);
const proof_cid = await storageClient.put(file);
console.log('User Linktrees proof uploaded to IPFS: ', proof_cid);
// deleting the file from fs once it is uploaded to IPFS
await deleteFile(path);
return proof_cid;
} else {
console.log('NODE DO NOT HAVE ACCESS TO WEB3.STORAGE');
}
};
module.exports = main;