Mint Your DAT
End-to-End Example: Contributing Privacy Data and Claiming Rewards
Project Setup
mkdir LazAI-contribution
cd LazAI-contribution
npm init -y
Install Alith and Dependencies
npm install alithnpm install --save-dev @types/node-rsa
Create index.ts
index.ts
Create a file named index.ts
and paste the following script:
import { Client } from "alith/lazai";
import { PinataIPFS } from "alith/data/storage";
import { encrypt } from "alith/data/crypto";
import NodeRSA from "node-rsa";
import axios, { AxiosResponse } from "axios";
async function main() {
const client = new Client();
const ipfs = new PinataIPFS();
// 1. Prepare your privacy data and encrypt it
const dataFileName = "your_encrypted_data.txt";
const privacyData = "Your Privacy Data";
const encryptionSeed = "Sign to retrieve your encryption key";
const password = client.getWallet().sign(encryptionSeed).signature;
const encryptedData = await encrypt(Uint8Array.from(privacyData), password);
// 2. Upload the privacy data to IPFS and get the shared url
const token = process.env.IPFS_JWT || "";
const fileMeta = await ipfs.upload({
name: dataFileName,
data: Buffer.from(encryptedData),
token: token,
});
const url = await ipfs.getShareLink({ token: token, id: fileMeta.id });
// 3. Upload the privacy url to LazAI
let fileId = await client.getFileIdByUrl(url);
if (fileId == BigInt(0)) {
fileId = await client.addFile(url);
}
// 4. Request proof in the verified computing node
await client.requestProof(fileId, BigInt(100));
const jobIds = await client.fileJobIds(fileId);
const jobId = jobIds[jobIds.length - 1];
const job = await client.getJob(jobId);
const nodeInfo = await client.getNode(job.nodeAddress);
const nodeUrl = nodeInfo.url;
const pubKey = nodeInfo.publicKey;
const rsa = new NodeRSA(pubKey, "pkcs1-public-pem");
const encryptedKey = rsa.encrypt(password, "hex");
const proofRequest = {
job_id: Number(jobId),
file_id: Number(fileId),
file_url: url,
encryption_key: encryptedKey,
encryption_seed: encryptionSeed,
nonce: null,
proof_url: null,
};
const response: AxiosResponse = await axios.post(
`${nodeUrl}/proof`,
proofRequest,
{
headers: { "Content-Type": "application/json" },
},
);
if (response.status === 200) {
console.log("Proof request sent successfully");
} else {
console.log("Failed to send proof request:", response.data);
}
// 5. Request DAT reward
await client.requestReward(fileId);
console.log("Reward requested for file id", fileId);
}
await main();
Update package.json
Scripts
package.json
ScriptsAdd the following to your package.json
:
"scripts": { "start": "node --loader ts-node/esm index.ts"}
Add tsconfig.json
tsconfig.json
Create a file named tsconfig.json
with the following content:
{ "compilerOptions": { "target": "ES2022", "module": "ES2022", "moduleResolution": "node", "strict": true, "esModuleInterop": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true, "outDir": "./dist", "allowJs": true, "resolveJsonModule": true }, "ts-node": { "esm": true, "experimentalSpecifierResolution": "node" }, "include": ["*.ts"], "exclude": ["node_modules"]}
Set Environment Variables
Create a .env
file or export the following in your shell:
export PRIVATE_KEY=<your wallet private key>export IPFS_JWT=<your pinata ipfs jwt>
Note: Your
PRIVATE_KEY
should start with0x
.
Run the Script
npm run start
You should see output similar to:
Proof request sent successfullyReward requested for file id <file_id>