@apillon/sdk
Version:
▶◀ Apillon SDK for NodeJS ▶◀
133 lines • 5.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ComputingContract = void 0;
const apillon_api_1 = require("../../lib/apillon-api");
const apillon_1 = require("../../lib/apillon");
const computing_1 = require("../../types/computing");
const common_1 = require("../../lib/common");
const apillon_logger_1 = require("../../lib/apillon-logger");
const storage_1 = require("../storage/storage");
class ComputingContract extends apillon_1.ApillonModel {
/**
* Constructor which should only be called via Computing class.
* @param uuid Unique identifier of the contract.
* @param data Data to populate computing contract with.
*/
constructor(uuid, data, config) {
super(uuid);
/**
* Name of the contract.
*/
this.name = null;
/**
* Contract description.
*/
this.description = null;
/**
* The bucket where files encrypted by this contract are stored
*/
this.bucketUuid = null;
/**
* The computing contract's type
*/
this.contractType = null;
/**
* The computing contract's status
*/
this.contractStatus = null;
/**
* The computing contract's on-chain address
*/
this.contractAddress = null;
/**
* The computing contract's on-chain deployer address
*/
this.deployerAddress = null;
/**
* The computing contract's deployment transaction hash
*/
this.transactionHash = null;
/**
* The computing contract's additional data
*/
this.data = null;
this.API_PREFIX = `/computing/contracts/${uuid}`;
this.populate(data);
this.config = config;
}
/**
* Gets list of transactions for this computing contract.
* @param {ITransactionListFilters} params Query filters.
* @returns {IComputingTransaction[]} List of transactions.
*/
async listTransactions(params) {
const url = (0, common_1.constructUrlWithQueryParams)(`${this.API_PREFIX}/transactions`, params);
return await apillon_api_1.ApillonApi.get(url);
}
/**
* Transfers ownership of the computing contract.
* @param {string} accountAddress The address of the new owner.
* @returns Success status
*/
async transferOwnership(accountAddress) {
const { success } = await apillon_api_1.ApillonApi.post(`${this.API_PREFIX}/transfer-ownership`, { accountAddress });
if (success) {
apillon_logger_1.ApillonLogger.log(`Ownership transferred successfully to ${accountAddress}`);
}
return success;
}
/**
* - Calls the encrypt method on the computing contract
* - Uploads the encrypted file to the bucket
* - Assigns the encrypted file's CID to the NFT used for decryption authentication
* @param {IEncryptData} data The data to use for encryption.
* @returns The uploaded encrypted file metadata
*/
async encryptFile(data) {
apillon_logger_1.ApillonLogger.log(`Encrypting file...`);
const { encryptedContent } = await apillon_api_1.ApillonApi.post(`${this.API_PREFIX}/encrypt`, Object.assign(Object.assign({}, data), { content: data.content.toString('base64') }));
if (!encryptedContent) {
throw new Error('Failed to encrypt file');
}
if (!this.bucketUuid) {
await this.get();
}
apillon_logger_1.ApillonLogger.log(`Uploading encrypted file to bucket...`);
const files = await new storage_1.Storage(this.config)
.bucket(this.bucketUuid)
.uploadFiles([
{
fileName: data.fileName,
content: Buffer.from(encryptedContent, 'utf-8'),
contentType: 'multipart/form-data',
},
], { awaitCid: true });
apillon_logger_1.ApillonLogger.log(`Assigning file CID to NFT ID...`);
await this.assignCidToNft({ cid: files[0].CID, nftId: data.nftId });
return files;
}
/**
* Assigns a CID to an NFT on the contract.
* @param data The payload for assigning a CID to an NFT
* @returns Success status
*/
async assignCidToNft(data) {
const { success } = await apillon_api_1.ApillonApi.post(`${this.API_PREFIX}/assign-cid-to-nft`, data);
if (success) {
apillon_logger_1.ApillonLogger.log(`Encrypted file CID assigned successfully to NFT with ID=${data.nftId}`);
}
return success;
}
serializeFilter(key, value) {
const serialized = super.serializeFilter(key, value);
const enums = {
contractType: computing_1.ComputingContractType[serialized],
contractStatus: computing_1.ComputingContractStatus[serialized],
transactionType: computing_1.ComputingTransactionType[serialized],
transactionStatus: computing_1.ComputingContractStatus[serialized],
};
return Object.keys(enums).includes(key) ? enums[key] : serialized;
}
}
exports.ComputingContract = ComputingContract;
//# sourceMappingURL=computing-contract.js.map