@inkwell.ar/sdk
Version:
SDK for interacting with the Inkwell Blog CRUD AO process using aoconnect for deployment and interactions
136 lines • 5.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BlogRegistrySDK = void 0;
const aoconnect_1 = require("@permaweb/aoconnect");
const registry_1 = require("../config/registry");
const types_1 = require("../types");
const logger_1 = require("../utils/logger");
class BlogRegistrySDK {
constructor(ao, registryProcessId, logLevel = types_1.LogLevel.WARN) {
this.aoconnect = ao || (0, aoconnect_1.connect)({ MODE: 'legacy' });
this.registryProcessId = registryProcessId || registry_1.BLOG_REGISTRY_PROCESS_ID;
this.logger = new logger_1.Logger({ level: logLevel });
if (this.registryProcessId === 'YOUR_REGISTRY_PROCESS_ID_HERE') {
this.logger.error(logger_1.LogGroup.REGISTRY, 'Registry process ID not configured');
throw new Error('Registry process ID not configured. Please run the deployment script first: npm run deploy:registry');
}
this.logger.info(logger_1.LogGroup.REGISTRY, `Initialized BlogRegistrySDK with process ID: ${this.registryProcessId}`);
}
/**
* Note: Write operations (register, remove, update) are only available to blog processes
* for security reasons. The registry uses msg.From as the blog ID to ensure only
* the actual blog process can modify its own permissions.
*/
/**
* Get all blogs a wallet has permissions for
*/
async getWalletBlogs(wallet) {
this.logger.debug(logger_1.LogGroup.REGISTRY, `Getting blogs for wallet: ${wallet}`);
const result = await this.aoconnect.dryrun({
process: this.registryProcessId,
data: '',
tags: [
{ name: 'Action', value: 'Get-Wallet-Blogs' },
{ name: 'Wallet-Address', value: wallet },
],
});
const response = JSON.parse(result.Messages[0].Data);
if (!response.success) {
this.logger.error(logger_1.LogGroup.REGISTRY, `Failed to get wallet blogs: ${response.data}`);
throw new Error(response.data);
}
this.logger.debug(logger_1.LogGroup.REGISTRY, `Found ${response.data.length} blogs for wallet`);
return response.data;
}
/**
* Get all wallets with permissions for a specific blog
*/
async getBlogWallets(blogId) {
const result = await this.aoconnect.dryrun({
process: this.registryProcessId,
data: '',
tags: [
{ name: 'Action', value: 'Get-Blog-Wallets' },
{ name: 'Blog-ID', value: blogId },
],
});
const response = JSON.parse(result.Messages[0].Data);
if (!response.success) {
throw new Error(response.data);
}
return response.data;
}
/**
* Check if a wallet has a specific role for a blog
*/
async checkWalletRole(wallet, blogId, role) {
this.logger.debug(logger_1.LogGroup.AUTH, `Checking if wallet ${wallet} has role ${role} for blog ${blogId}`);
const result = await this.aoconnect.dryrun({
process: this.registryProcessId,
data: '',
tags: [
{ name: 'Action', value: 'Check-Wallet-Role' },
{ name: 'Wallet-Address', value: wallet },
{ name: 'Blog-ID', value: blogId },
{ name: 'Role', value: role },
],
});
const response = JSON.parse(result.Messages[0].Data);
if (!response.success) {
this.logger.error(logger_1.LogGroup.AUTH, `Role check failed: ${response.data.error}`);
throw new Error(response.data.error);
}
this.logger.debug(logger_1.LogGroup.AUTH, `Role check result: ${response.data.has_role}`);
return response.data.has_role;
}
/**
* Get registry statistics
*/
async getRegistryStats() {
const result = await this.aoconnect.dryrun({
process: this.registryProcessId,
data: '',
tags: [{ name: 'Action', value: 'Get-Registry-Stats' }],
});
const response = JSON.parse(result.Messages[0].Data);
if (!response.success) {
throw new Error(response.data);
}
return response.data;
}
/**
* Note: Bulk operations and sync operations are only available to blog processes
* for security reasons. The registry uses msg.From as the blog ID to ensure only
* the actual blog process can modify its own permissions.
*/
/**
* Get all blogs that a wallet can admin
*/
async getAdminBlogs(wallet) {
const allBlogs = await this.getWalletBlogs(wallet);
return allBlogs.filter((blog) => blog.roles.includes(types_1.Role.ADMIN));
}
/**
* Get all blogs that a wallet can edit
*/
async getEditableBlogs(wallet) {
const allBlogs = await this.getWalletBlogs(wallet);
return allBlogs.filter((blog) => blog.roles.includes(types_1.Role.EDITOR) || blog.roles.includes(types_1.Role.ADMIN));
}
/**
* Check if a wallet can admin a specific blog
*/
async canAdminBlog(wallet, blogId) {
return this.checkWalletRole(wallet, blogId, types_1.Role.ADMIN);
}
/**
* Check if a wallet can edit a specific blog
*/
async canEditBlog(wallet, blogId) {
const canEdit = await this.checkWalletRole(wallet, blogId, types_1.Role.EDITOR);
const canAdmin = await this.checkWalletRole(wallet, blogId, types_1.Role.ADMIN);
return canEdit || canAdmin;
}
}
exports.BlogRegistrySDK = BlogRegistrySDK;
//# sourceMappingURL=blog-registry-sdk.js.map