@enbox/api
Version:
SDK for accessing the features and capabilities of Web5
109 lines • 5.09 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
import { AgentPermissionsApi, getRecordAuthor } from '@enbox/agent';
import { DwnInterface } from '@enbox/agent';
import { Convert } from '@enbox/common';
/**
* The `PermissionGrantRevocation` class encapsulates a permissions protocol `grant/revocation` record, providing a more
* developer-friendly interface for working with Decentralized Web Node (DWN) records.
*
* Methods are provided to manage the grant revocation's lifecycle, including writing to remote DWNs.
*
* @beta
*/
export class PermissionGrantRevocation {
constructor(permissions, options) {
this._permissions = permissions;
this._connectedDid = options.connectedDid;
// Store the message that represents the grant.
this._message = options.message;
}
/** The author of the underlying revocation message */
get author() {
return getRecordAuthor(this._message);
}
/** parses the grant revocation given am agent, connectedDid and data encoded records write message */
static parse({ connectedDid, agent, message }) {
return __awaiter(this, void 0, void 0, function* () {
const permissions = new AgentPermissionsApi({ agent });
return new PermissionGrantRevocation(permissions, { connectedDid, message });
});
}
/** The agent to use for this instantiation of the grant revocation */
get agent() {
return this._permissions.agent;
}
/** The raw `RecordsWrite` DWN message with encoded data that was used to instantiate this grant revocation */
get rawMessage() {
return this._message;
}
/**
* Send the current grant revocation to a remote DWN by specifying their DID
* If no DID is specified, the target is assumed to be the owner (connectedDID).
*
* @param target - the optional DID to send the grant revocation to, if none is set it is sent to the connectedDid
* @returns the status of the send grant revocation request
*
* @beta
*/
send(target) {
return __awaiter(this, void 0, void 0, function* () {
target !== null && target !== void 0 ? target : (target = this._connectedDid);
const _a = this._message, { encodedData } = _a, rawMessage = __rest(_a, ["encodedData"]);
const dataStream = new Blob([Convert.base64Url(encodedData).toUint8Array()]);
const sendRequestOptions = {
messageType: DwnInterface.RecordsWrite,
author: this._connectedDid,
target: target,
dataStream,
rawMessage,
};
// Send the current/latest state to the target.
const { reply } = yield this.agent.sendDwnRequest(sendRequestOptions);
return reply;
});
}
/**
* Stores the current grant revocation to the owner's DWN.
*
* @param importGrant - if true, the grant revocation will signed by the owner before storing it to the owner's DWN. Defaults to false.
* @returns the status of the store request
*
* @beta
*/
store(importRevocation) {
return __awaiter(this, void 0, void 0, function* () {
const _a = this.rawMessage, { encodedData } = _a, rawMessage = __rest(_a, ["encodedData"]);
const dataStream = new Blob([Convert.base64Url(encodedData).toUint8Array()]);
const { reply, message } = yield this.agent.processDwnRequest({
author: this._connectedDid,
target: this._connectedDid,
messageType: DwnInterface.RecordsWrite,
signAsOwner: importRevocation,
rawMessage,
dataStream,
});
this._message = Object.assign(Object.assign({}, message), { encodedData: encodedData });
return { status: reply.status };
});
}
}
//# sourceMappingURL=grant-revocation.js.map