@enbox/api 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +585 -0
- package/dist/browser.js +2226 -0
- package/dist/browser.js.map +7 -0
- package/dist/browser.mjs +2226 -0
- package/dist/browser.mjs.map +7 -0
- package/dist/cjs/did-api.js +126 -0
- package/dist/cjs/did-api.js.map +1 -0
- package/dist/cjs/dwn-api.js +804 -0
- package/dist/cjs/dwn-api.js.map +1 -0
- package/dist/cjs/grant-revocation.js +183 -0
- package/dist/cjs/grant-revocation.js.map +1 -0
- package/dist/cjs/index.js +63 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/permission-grant.js +365 -0
- package/dist/cjs/permission-grant.js.map +1 -0
- package/dist/cjs/permission-request.js +272 -0
- package/dist/cjs/permission-request.js.map +1 -0
- package/dist/cjs/protocol.js +110 -0
- package/dist/cjs/protocol.js.map +1 -0
- package/dist/cjs/record.js +1127 -0
- package/dist/cjs/record.js.map +1 -0
- package/dist/cjs/subscription-util.js +86 -0
- package/dist/cjs/subscription-util.js.map +1 -0
- package/dist/cjs/utils.js +127 -0
- package/dist/cjs/utils.js.map +1 -0
- package/dist/cjs/vc-api.js +64 -0
- package/dist/cjs/vc-api.js.map +1 -0
- package/dist/cjs/web5.js +471 -0
- package/dist/cjs/web5.js.map +1 -0
- package/dist/esm/did-api.js +69 -0
- package/dist/esm/did-api.js.map +1 -0
- package/dist/esm/dwn-api.js +573 -0
- package/dist/esm/dwn-api.js.map +1 -0
- package/dist/esm/grant-revocation.js +109 -0
- package/dist/esm/grant-revocation.js.map +1 -0
- package/dist/esm/index.js +34 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/permission-grant.js +233 -0
- package/dist/esm/permission-grant.js.map +1 -0
- package/dist/esm/permission-request.js +166 -0
- package/dist/esm/permission-request.js.map +1 -0
- package/dist/esm/protocol.js +67 -0
- package/dist/esm/protocol.js.map +1 -0
- package/dist/esm/record.js +814 -0
- package/dist/esm/record.js.map +1 -0
- package/dist/esm/subscription-util.js +35 -0
- package/dist/esm/subscription-util.js.map +1 -0
- package/dist/esm/utils.js +120 -0
- package/dist/esm/utils.js.map +1 -0
- package/dist/esm/vc-api.js +30 -0
- package/dist/esm/vc-api.js.map +1 -0
- package/dist/esm/web5.js +281 -0
- package/dist/esm/web5.js.map +1 -0
- package/dist/types/did-api.d.ts +66 -0
- package/dist/types/did-api.d.ts.map +1 -0
- package/dist/types/dwn-api.d.ts +336 -0
- package/dist/types/dwn-api.d.ts.map +1 -0
- package/dist/types/grant-revocation.d.ts +66 -0
- package/dist/types/grant-revocation.d.ts.map +1 -0
- package/dist/types/index.d.ts +34 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/permission-grant.d.ts +157 -0
- package/dist/types/permission-grant.d.ts.map +1 -0
- package/dist/types/permission-request.d.ts +108 -0
- package/dist/types/permission-request.d.ts.map +1 -0
- package/dist/types/protocol.d.ts +59 -0
- package/dist/types/protocol.d.ts.map +1 -0
- package/dist/types/record.d.ts +441 -0
- package/dist/types/record.d.ts.map +1 -0
- package/dist/types/subscription-util.d.ts +19 -0
- package/dist/types/subscription-util.d.ts.map +1 -0
- package/dist/types/utils.d.ts +85 -0
- package/dist/types/utils.d.ts.map +1 -0
- package/dist/types/vc-api.d.ts +24 -0
- package/dist/types/vc-api.d.ts.map +1 -0
- package/dist/types/web5.d.ts +219 -0
- package/dist/types/web5.d.ts.map +1 -0
- package/package.json +111 -0
- package/src/did-api.ts +90 -0
- package/src/dwn-api.ts +952 -0
- package/src/grant-revocation.ts +124 -0
- package/src/index.ts +35 -0
- package/src/permission-grant.ts +327 -0
- package/src/permission-request.ts +214 -0
- package/src/protocol.ts +87 -0
- package/src/record.ts +1125 -0
- package/src/subscription-util.ts +42 -0
- package/src/utils.ts +128 -0
- package/src/vc-api.ts +30 -0
- package/src/web5.ts +516 -0
|
@@ -0,0 +1,804 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* NOTE: Added reference types here to avoid a `pnpm` bug during build.
|
|
4
|
+
* https://github.com/TBD54566975/web5-js/pull/507
|
|
5
|
+
*/
|
|
6
|
+
/// <reference types="@enbox/dwn-sdk-js" />
|
|
7
|
+
var __assign = (this && this.__assign) || function () {
|
|
8
|
+
__assign = Object.assign || function(t) {
|
|
9
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
10
|
+
s = arguments[i];
|
|
11
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
12
|
+
t[p] = s[p];
|
|
13
|
+
}
|
|
14
|
+
return t;
|
|
15
|
+
};
|
|
16
|
+
return __assign.apply(this, arguments);
|
|
17
|
+
};
|
|
18
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
19
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
20
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
21
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
22
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
23
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
24
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
28
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
29
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
30
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
31
|
+
function step(op) {
|
|
32
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
33
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
34
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
35
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
36
|
+
switch (op[0]) {
|
|
37
|
+
case 0: case 1: t = op; break;
|
|
38
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
39
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
40
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
41
|
+
default:
|
|
42
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
43
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
44
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
45
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
46
|
+
if (t[2]) _.ops.pop();
|
|
47
|
+
_.trys.pop(); continue;
|
|
48
|
+
}
|
|
49
|
+
op = body.call(thisArg, _);
|
|
50
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
51
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
55
|
+
var t = {};
|
|
56
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
57
|
+
t[p] = s[p];
|
|
58
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
59
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
60
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
61
|
+
t[p[i]] = s[p[i]];
|
|
62
|
+
}
|
|
63
|
+
return t;
|
|
64
|
+
};
|
|
65
|
+
var __values = (this && this.__values) || function(o) {
|
|
66
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
67
|
+
if (m) return m.call(o);
|
|
68
|
+
if (o && typeof o.length === "number") return {
|
|
69
|
+
next: function () {
|
|
70
|
+
if (o && i >= o.length) o = void 0;
|
|
71
|
+
return { value: o && o[i++], done: !o };
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
75
|
+
};
|
|
76
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
77
|
+
exports.DwnApi = void 0;
|
|
78
|
+
var agent_1 = require("@enbox/agent");
|
|
79
|
+
var common_1 = require("@enbox/common");
|
|
80
|
+
var agent_2 = require("@enbox/agent");
|
|
81
|
+
var record_js_1 = require("./record.js");
|
|
82
|
+
var utils_js_1 = require("./utils.js");
|
|
83
|
+
var protocol_js_1 = require("./protocol.js");
|
|
84
|
+
var permission_grant_js_1 = require("./permission-grant.js");
|
|
85
|
+
var permission_request_js_1 = require("./permission-request.js");
|
|
86
|
+
var subscription_util_js_1 = require("./subscription-util.js");
|
|
87
|
+
/**
|
|
88
|
+
* Interface to interact with DWN Records and Protocols
|
|
89
|
+
*/
|
|
90
|
+
var DwnApi = /** @class */ (function () {
|
|
91
|
+
function DwnApi(options) {
|
|
92
|
+
this.agent = options.agent;
|
|
93
|
+
this.connectedDid = options.connectedDid;
|
|
94
|
+
this.delegateDid = options.delegateDid;
|
|
95
|
+
this.permissionsApi = new agent_1.AgentPermissionsApi({ agent: this.agent });
|
|
96
|
+
}
|
|
97
|
+
Object.defineProperty(DwnApi.prototype, "permissions", {
|
|
98
|
+
/**
|
|
99
|
+
* API to interact with Grants
|
|
100
|
+
*
|
|
101
|
+
* NOTE: This is an EXPERIMENTAL API that will change behavior.
|
|
102
|
+
*
|
|
103
|
+
* Currently only supports issuing requests, grants, revokes and queries on behalf without permissions or impersonation.
|
|
104
|
+
* If the agent is connected to a delegateDid, the delegateDid will be used to sign/author the underlying records.
|
|
105
|
+
* If the agent is not connected to a delegateDid, the connectedDid will be used to sign/author the underlying records.
|
|
106
|
+
*
|
|
107
|
+
* @beta
|
|
108
|
+
*/
|
|
109
|
+
get: function () {
|
|
110
|
+
var _this = this;
|
|
111
|
+
return {
|
|
112
|
+
/**
|
|
113
|
+
* Request permission for a specific scope.
|
|
114
|
+
*/
|
|
115
|
+
request: function (request) { return __awaiter(_this, void 0, void 0, function () {
|
|
116
|
+
var message, requestParams;
|
|
117
|
+
var _a, _b;
|
|
118
|
+
return __generator(this, function (_c) {
|
|
119
|
+
switch (_c.label) {
|
|
120
|
+
case 0: return [4 /*yield*/, this.permissionsApi.createRequest(__assign(__assign({}, request), { author: (_a = this.delegateDid) !== null && _a !== void 0 ? _a : this.connectedDid }))];
|
|
121
|
+
case 1:
|
|
122
|
+
message = (_c.sent()).message;
|
|
123
|
+
requestParams = {
|
|
124
|
+
connectedDid: (_b = this.delegateDid) !== null && _b !== void 0 ? _b : this.connectedDid,
|
|
125
|
+
agent: this.agent,
|
|
126
|
+
message: message,
|
|
127
|
+
};
|
|
128
|
+
return [4 /*yield*/, permission_request_js_1.PermissionRequest.parse(requestParams)];
|
|
129
|
+
case 2: return [2 /*return*/, _c.sent()];
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}); },
|
|
133
|
+
/**
|
|
134
|
+
* Grant permission for a specific scope to a grantee DID.
|
|
135
|
+
*/
|
|
136
|
+
grant: function (request) { return __awaiter(_this, void 0, void 0, function () {
|
|
137
|
+
var message, grantParams;
|
|
138
|
+
var _a, _b;
|
|
139
|
+
return __generator(this, function (_c) {
|
|
140
|
+
switch (_c.label) {
|
|
141
|
+
case 0: return [4 /*yield*/, this.permissionsApi.createGrant(__assign(__assign({}, request), { author: (_a = this.delegateDid) !== null && _a !== void 0 ? _a : this.connectedDid }))];
|
|
142
|
+
case 1:
|
|
143
|
+
message = (_c.sent()).message;
|
|
144
|
+
grantParams = {
|
|
145
|
+
connectedDid: (_b = this.delegateDid) !== null && _b !== void 0 ? _b : this.connectedDid,
|
|
146
|
+
agent: this.agent,
|
|
147
|
+
message: message,
|
|
148
|
+
};
|
|
149
|
+
return [4 /*yield*/, permission_grant_js_1.PermissionGrant.parse(grantParams)];
|
|
150
|
+
case 2: return [2 /*return*/, _c.sent()];
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
}); },
|
|
154
|
+
/**
|
|
155
|
+
* Query permission requests. You can filter by protocol and specify if you want to query a remote DWN.
|
|
156
|
+
*/
|
|
157
|
+
queryRequests: function (request) {
|
|
158
|
+
if (request === void 0) { request = {}; }
|
|
159
|
+
return __awaiter(_this, void 0, void 0, function () {
|
|
160
|
+
var from, params, fetchResponse, requests, fetchResponse_1, fetchResponse_1_1, permission, requestParams, _a, _b, e_1_1;
|
|
161
|
+
var e_1, _c;
|
|
162
|
+
var _d, _e, _f;
|
|
163
|
+
return __generator(this, function (_g) {
|
|
164
|
+
switch (_g.label) {
|
|
165
|
+
case 0:
|
|
166
|
+
from = request.from, params = __rest(request, ["from"]);
|
|
167
|
+
return [4 /*yield*/, this.permissionsApi.fetchRequests(__assign(__assign({}, params), { author: (_d = this.delegateDid) !== null && _d !== void 0 ? _d : this.connectedDid, target: (_e = from !== null && from !== void 0 ? from : this.delegateDid) !== null && _e !== void 0 ? _e : this.connectedDid, remote: from !== undefined }))];
|
|
168
|
+
case 1:
|
|
169
|
+
fetchResponse = _g.sent();
|
|
170
|
+
requests = [];
|
|
171
|
+
_g.label = 2;
|
|
172
|
+
case 2:
|
|
173
|
+
_g.trys.push([2, 7, 8, 9]);
|
|
174
|
+
fetchResponse_1 = __values(fetchResponse), fetchResponse_1_1 = fetchResponse_1.next();
|
|
175
|
+
_g.label = 3;
|
|
176
|
+
case 3:
|
|
177
|
+
if (!!fetchResponse_1_1.done) return [3 /*break*/, 6];
|
|
178
|
+
permission = fetchResponse_1_1.value;
|
|
179
|
+
requestParams = {
|
|
180
|
+
connectedDid: (_f = this.delegateDid) !== null && _f !== void 0 ? _f : this.connectedDid,
|
|
181
|
+
agent: this.agent,
|
|
182
|
+
message: permission.message,
|
|
183
|
+
};
|
|
184
|
+
_b = (_a = requests).push;
|
|
185
|
+
return [4 /*yield*/, permission_request_js_1.PermissionRequest.parse(requestParams)];
|
|
186
|
+
case 4:
|
|
187
|
+
_b.apply(_a, [_g.sent()]);
|
|
188
|
+
_g.label = 5;
|
|
189
|
+
case 5:
|
|
190
|
+
fetchResponse_1_1 = fetchResponse_1.next();
|
|
191
|
+
return [3 /*break*/, 3];
|
|
192
|
+
case 6: return [3 /*break*/, 9];
|
|
193
|
+
case 7:
|
|
194
|
+
e_1_1 = _g.sent();
|
|
195
|
+
e_1 = { error: e_1_1 };
|
|
196
|
+
return [3 /*break*/, 9];
|
|
197
|
+
case 8:
|
|
198
|
+
try {
|
|
199
|
+
if (fetchResponse_1_1 && !fetchResponse_1_1.done && (_c = fetchResponse_1.return)) _c.call(fetchResponse_1);
|
|
200
|
+
}
|
|
201
|
+
finally { if (e_1) throw e_1.error; }
|
|
202
|
+
return [7 /*endfinally*/];
|
|
203
|
+
case 9: return [2 /*return*/, requests];
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
},
|
|
208
|
+
/**
|
|
209
|
+
* Query permission grants. You can filter by grantee, grantor, protocol and specify if you want to query a remote DWN.
|
|
210
|
+
*/
|
|
211
|
+
queryGrants: function (request) {
|
|
212
|
+
if (request === void 0) { request = {}; }
|
|
213
|
+
return __awaiter(_this, void 0, void 0, function () {
|
|
214
|
+
var checkRevoked, from, params, remote, author, target, fetchResponse, grants, fetchResponse_2, fetchResponse_2_1, permission, grantParams, grantRecordId, _a, _b, e_2_1;
|
|
215
|
+
var e_2, _c;
|
|
216
|
+
var _d, _e, _f;
|
|
217
|
+
return __generator(this, function (_g) {
|
|
218
|
+
switch (_g.label) {
|
|
219
|
+
case 0:
|
|
220
|
+
checkRevoked = request.checkRevoked, from = request.from, params = __rest(request, ["checkRevoked", "from"]);
|
|
221
|
+
remote = from !== undefined;
|
|
222
|
+
author = (_d = this.delegateDid) !== null && _d !== void 0 ? _d : this.connectedDid;
|
|
223
|
+
target = (_e = from !== null && from !== void 0 ? from : this.delegateDid) !== null && _e !== void 0 ? _e : this.connectedDid;
|
|
224
|
+
return [4 /*yield*/, this.permissionsApi.fetchGrants(__assign(__assign({}, params), { author: author, target: target, remote: remote }))];
|
|
225
|
+
case 1:
|
|
226
|
+
fetchResponse = _g.sent();
|
|
227
|
+
grants = [];
|
|
228
|
+
_g.label = 2;
|
|
229
|
+
case 2:
|
|
230
|
+
_g.trys.push([2, 9, 10, 11]);
|
|
231
|
+
fetchResponse_2 = __values(fetchResponse), fetchResponse_2_1 = fetchResponse_2.next();
|
|
232
|
+
_g.label = 3;
|
|
233
|
+
case 3:
|
|
234
|
+
if (!!fetchResponse_2_1.done) return [3 /*break*/, 8];
|
|
235
|
+
permission = fetchResponse_2_1.value;
|
|
236
|
+
grantParams = {
|
|
237
|
+
connectedDid: (_f = this.delegateDid) !== null && _f !== void 0 ? _f : this.connectedDid,
|
|
238
|
+
agent: this.agent,
|
|
239
|
+
message: permission.message,
|
|
240
|
+
};
|
|
241
|
+
if (!checkRevoked) return [3 /*break*/, 5];
|
|
242
|
+
grantRecordId = permission.grant.id;
|
|
243
|
+
return [4 /*yield*/, this.permissionsApi.isGrantRevoked({ author: author, target: target, grantRecordId: grantRecordId, remote: remote })];
|
|
244
|
+
case 4:
|
|
245
|
+
if (_g.sent()) {
|
|
246
|
+
return [3 /*break*/, 7];
|
|
247
|
+
}
|
|
248
|
+
_g.label = 5;
|
|
249
|
+
case 5:
|
|
250
|
+
_b = (_a = grants).push;
|
|
251
|
+
return [4 /*yield*/, permission_grant_js_1.PermissionGrant.parse(grantParams)];
|
|
252
|
+
case 6:
|
|
253
|
+
_b.apply(_a, [_g.sent()]);
|
|
254
|
+
_g.label = 7;
|
|
255
|
+
case 7:
|
|
256
|
+
fetchResponse_2_1 = fetchResponse_2.next();
|
|
257
|
+
return [3 /*break*/, 3];
|
|
258
|
+
case 8: return [3 /*break*/, 11];
|
|
259
|
+
case 9:
|
|
260
|
+
e_2_1 = _g.sent();
|
|
261
|
+
e_2 = { error: e_2_1 };
|
|
262
|
+
return [3 /*break*/, 11];
|
|
263
|
+
case 10:
|
|
264
|
+
try {
|
|
265
|
+
if (fetchResponse_2_1 && !fetchResponse_2_1.done && (_c = fetchResponse_2.return)) _c.call(fetchResponse_2);
|
|
266
|
+
}
|
|
267
|
+
finally { if (e_2) throw e_2.error; }
|
|
268
|
+
return [7 /*endfinally*/];
|
|
269
|
+
case 11: return [2 /*return*/, grants];
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
},
|
|
276
|
+
enumerable: false,
|
|
277
|
+
configurable: true
|
|
278
|
+
});
|
|
279
|
+
Object.defineProperty(DwnApi.prototype, "protocols", {
|
|
280
|
+
/**
|
|
281
|
+
* API to interact with DWN protocols (e.g., `dwn.protocols.configure()`).
|
|
282
|
+
*/
|
|
283
|
+
get: function () {
|
|
284
|
+
var _this = this;
|
|
285
|
+
return {
|
|
286
|
+
/**
|
|
287
|
+
* Configure method, used to setup a new protocol (or update) with the passed definitions
|
|
288
|
+
*/
|
|
289
|
+
configure: function (request) { return __awaiter(_this, void 0, void 0, function () {
|
|
290
|
+
var agentRequest, delegatedGrant, agentResponse, message, messageCid, status, response, metadata;
|
|
291
|
+
return __generator(this, function (_a) {
|
|
292
|
+
switch (_a.label) {
|
|
293
|
+
case 0:
|
|
294
|
+
agentRequest = {
|
|
295
|
+
author: this.connectedDid,
|
|
296
|
+
messageParams: request.message,
|
|
297
|
+
messageType: agent_2.DwnInterface.ProtocolsConfigure,
|
|
298
|
+
target: this.connectedDid
|
|
299
|
+
};
|
|
300
|
+
if (!this.delegateDid) return [3 /*break*/, 2];
|
|
301
|
+
return [4 /*yield*/, this.permissionsApi.getPermissionForRequest({
|
|
302
|
+
connectedDid: this.connectedDid,
|
|
303
|
+
delegateDid: this.delegateDid,
|
|
304
|
+
protocol: request.message.definition.protocol,
|
|
305
|
+
delegate: true,
|
|
306
|
+
cached: true,
|
|
307
|
+
messageType: agentRequest.messageType
|
|
308
|
+
})];
|
|
309
|
+
case 1:
|
|
310
|
+
delegatedGrant = (_a.sent()).message;
|
|
311
|
+
agentRequest.messageParams = __assign(__assign({}, agentRequest.messageParams), { delegatedGrant: delegatedGrant });
|
|
312
|
+
agentRequest.granteeDid = this.delegateDid;
|
|
313
|
+
_a.label = 2;
|
|
314
|
+
case 2: return [4 /*yield*/, this.agent.processDwnRequest(agentRequest)];
|
|
315
|
+
case 3:
|
|
316
|
+
agentResponse = _a.sent();
|
|
317
|
+
message = agentResponse.message, messageCid = agentResponse.messageCid, status = agentResponse.reply.status;
|
|
318
|
+
response = { status: status };
|
|
319
|
+
if (status.code < 300) {
|
|
320
|
+
metadata = { author: this.connectedDid, messageCid: messageCid };
|
|
321
|
+
response.protocol = new protocol_js_1.Protocol(this.agent, message, metadata);
|
|
322
|
+
}
|
|
323
|
+
return [2 /*return*/, response];
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
}); },
|
|
327
|
+
/**
|
|
328
|
+
* Query the available protocols
|
|
329
|
+
*/
|
|
330
|
+
query: function (request) { return __awaiter(_this, void 0, void 0, function () {
|
|
331
|
+
var agentRequest, permissionGrantId, _error_1, agentResponse, reply, _a, entries, status, protocols;
|
|
332
|
+
var _this = this;
|
|
333
|
+
return __generator(this, function (_b) {
|
|
334
|
+
switch (_b.label) {
|
|
335
|
+
case 0:
|
|
336
|
+
agentRequest = {
|
|
337
|
+
author: this.connectedDid,
|
|
338
|
+
messageParams: request.message,
|
|
339
|
+
messageType: agent_2.DwnInterface.ProtocolsQuery,
|
|
340
|
+
target: request.from || this.connectedDid
|
|
341
|
+
};
|
|
342
|
+
if (!this.delegateDid) return [3 /*break*/, 4];
|
|
343
|
+
_b.label = 1;
|
|
344
|
+
case 1:
|
|
345
|
+
_b.trys.push([1, 3, , 4]);
|
|
346
|
+
return [4 /*yield*/, this.permissionsApi.getPermissionForRequest({
|
|
347
|
+
connectedDid: this.connectedDid,
|
|
348
|
+
delegateDid: this.delegateDid,
|
|
349
|
+
protocol: request.message.filter.protocol,
|
|
350
|
+
cached: true,
|
|
351
|
+
messageType: agentRequest.messageType
|
|
352
|
+
})];
|
|
353
|
+
case 2:
|
|
354
|
+
permissionGrantId = (_b.sent()).grant.id;
|
|
355
|
+
agentRequest.messageParams = __assign(__assign({}, agentRequest.messageParams), { permissionGrantId: permissionGrantId });
|
|
356
|
+
agentRequest.granteeDid = this.delegateDid;
|
|
357
|
+
return [3 /*break*/, 4];
|
|
358
|
+
case 3:
|
|
359
|
+
_error_1 = _b.sent();
|
|
360
|
+
// if a grant is not found, we should author the request as the delegated DID to get public protocols
|
|
361
|
+
agentRequest.author = this.delegateDid;
|
|
362
|
+
return [3 /*break*/, 4];
|
|
363
|
+
case 4:
|
|
364
|
+
if (!request.from) return [3 /*break*/, 6];
|
|
365
|
+
return [4 /*yield*/, this.agent.sendDwnRequest(agentRequest)];
|
|
366
|
+
case 5:
|
|
367
|
+
agentResponse = _b.sent();
|
|
368
|
+
return [3 /*break*/, 8];
|
|
369
|
+
case 6: return [4 /*yield*/, this.agent.processDwnRequest(agentRequest)];
|
|
370
|
+
case 7:
|
|
371
|
+
agentResponse = _b.sent();
|
|
372
|
+
_b.label = 8;
|
|
373
|
+
case 8:
|
|
374
|
+
reply = agentResponse.reply;
|
|
375
|
+
_a = reply.entries, entries = _a === void 0 ? [] : _a, status = reply.status;
|
|
376
|
+
protocols = entries.map(function (entry) {
|
|
377
|
+
var metadata = { author: _this.connectedDid };
|
|
378
|
+
return new protocol_js_1.Protocol(_this.agent, entry, metadata);
|
|
379
|
+
});
|
|
380
|
+
return [2 /*return*/, { protocols: protocols, status: status }];
|
|
381
|
+
}
|
|
382
|
+
});
|
|
383
|
+
}); }
|
|
384
|
+
};
|
|
385
|
+
},
|
|
386
|
+
enumerable: false,
|
|
387
|
+
configurable: true
|
|
388
|
+
});
|
|
389
|
+
Object.defineProperty(DwnApi.prototype, "records", {
|
|
390
|
+
/**
|
|
391
|
+
* API to interact with DWN records (e.g., `dwn.records.create()`).
|
|
392
|
+
*/
|
|
393
|
+
get: function () {
|
|
394
|
+
var _this = this;
|
|
395
|
+
return {
|
|
396
|
+
/**
|
|
397
|
+
* Alias for the `write` method
|
|
398
|
+
*/
|
|
399
|
+
create: function (request) { return __awaiter(_this, void 0, void 0, function () {
|
|
400
|
+
return __generator(this, function (_a) {
|
|
401
|
+
return [2 /*return*/, this.records.write(request)];
|
|
402
|
+
});
|
|
403
|
+
}); },
|
|
404
|
+
/**
|
|
405
|
+
* Write a record based on an existing one (useful for updating an existing record)
|
|
406
|
+
*/
|
|
407
|
+
createFrom: function (request) { return __awaiter(_this, void 0, void 0, function () {
|
|
408
|
+
var _a, inheritedAuthor, inheritedProperties;
|
|
409
|
+
var _b;
|
|
410
|
+
return __generator(this, function (_c) {
|
|
411
|
+
_a = request.record.toJSON(), inheritedAuthor = _a.author, inheritedProperties = __rest(_a, ["author"]);
|
|
412
|
+
// If `data` is being updated then `dataCid` and `dataSize` must not be present.
|
|
413
|
+
if (request.data !== undefined) {
|
|
414
|
+
delete inheritedProperties.dataCid;
|
|
415
|
+
delete inheritedProperties.dataSize;
|
|
416
|
+
}
|
|
417
|
+
// If `published` is set to false, ensure that `datePublished` is undefined. Otherwise, DWN SDK's schema validation
|
|
418
|
+
// will throw an error if `published` is false but `datePublished` is set.
|
|
419
|
+
if (((_b = request.message) === null || _b === void 0 ? void 0 : _b.published) === false && inheritedProperties.datePublished !== undefined) {
|
|
420
|
+
delete inheritedProperties.datePublished;
|
|
421
|
+
delete inheritedProperties.published;
|
|
422
|
+
}
|
|
423
|
+
// If the request changes the `author` or message `descriptor` then the deterministic `recordId` will change.
|
|
424
|
+
// As a result, we will discard the `recordId` if either of these changes occur.
|
|
425
|
+
if (!(0, common_1.isEmptyObject)(request.message) || (request.author && request.author !== inheritedAuthor)) {
|
|
426
|
+
delete inheritedProperties.recordId;
|
|
427
|
+
}
|
|
428
|
+
return [2 /*return*/, this.records.write({
|
|
429
|
+
data: request.data,
|
|
430
|
+
message: __assign(__assign({}, inheritedProperties), request.message),
|
|
431
|
+
})];
|
|
432
|
+
});
|
|
433
|
+
}); },
|
|
434
|
+
/**
|
|
435
|
+
* Delete a record
|
|
436
|
+
*/
|
|
437
|
+
delete: function (request) { return __awaiter(_this, void 0, void 0, function () {
|
|
438
|
+
var agentRequest, delegatedGrant, agentResponse, status;
|
|
439
|
+
return __generator(this, function (_a) {
|
|
440
|
+
switch (_a.label) {
|
|
441
|
+
case 0:
|
|
442
|
+
agentRequest = {
|
|
443
|
+
/**
|
|
444
|
+
* The `author` is the DID that will sign the message and must be the DID the Web5 app is
|
|
445
|
+
* connected with and is authorized to access the signing private key of.
|
|
446
|
+
*/
|
|
447
|
+
author: this.connectedDid,
|
|
448
|
+
messageParams: request.message,
|
|
449
|
+
messageType: agent_2.DwnInterface.RecordsDelete,
|
|
450
|
+
/**
|
|
451
|
+
* The `target` is the DID of the DWN tenant under which the delete will be executed.
|
|
452
|
+
* If `from` is provided, the delete operation will be executed on a remote DWN.
|
|
453
|
+
* Otherwise, the record will be deleted on the local DWN.
|
|
454
|
+
*/
|
|
455
|
+
target: request.from || this.connectedDid
|
|
456
|
+
};
|
|
457
|
+
if (!this.delegateDid) return [3 /*break*/, 2];
|
|
458
|
+
return [4 /*yield*/, this.permissionsApi.getPermissionForRequest({
|
|
459
|
+
connectedDid: this.connectedDid,
|
|
460
|
+
delegateDid: this.delegateDid,
|
|
461
|
+
protocol: request.protocol,
|
|
462
|
+
delegate: true,
|
|
463
|
+
cached: true,
|
|
464
|
+
messageType: agentRequest.messageType
|
|
465
|
+
})];
|
|
466
|
+
case 1:
|
|
467
|
+
delegatedGrant = (_a.sent()).message;
|
|
468
|
+
agentRequest.messageParams = __assign(__assign({}, agentRequest.messageParams), { delegatedGrant: delegatedGrant });
|
|
469
|
+
agentRequest.granteeDid = this.delegateDid;
|
|
470
|
+
_a.label = 2;
|
|
471
|
+
case 2:
|
|
472
|
+
if (!request.from) return [3 /*break*/, 4];
|
|
473
|
+
return [4 /*yield*/, this.agent.sendDwnRequest(agentRequest)];
|
|
474
|
+
case 3:
|
|
475
|
+
agentResponse = _a.sent();
|
|
476
|
+
return [3 /*break*/, 6];
|
|
477
|
+
case 4: return [4 /*yield*/, this.agent.processDwnRequest(agentRequest)];
|
|
478
|
+
case 5:
|
|
479
|
+
agentResponse = _a.sent();
|
|
480
|
+
_a.label = 6;
|
|
481
|
+
case 6:
|
|
482
|
+
status = agentResponse.reply.status;
|
|
483
|
+
return [2 /*return*/, { status: status }];
|
|
484
|
+
}
|
|
485
|
+
});
|
|
486
|
+
}); },
|
|
487
|
+
/**
|
|
488
|
+
* Query a single or multiple records based on the given filter
|
|
489
|
+
*/
|
|
490
|
+
query: function (request) { return __awaiter(_this, void 0, void 0, function () {
|
|
491
|
+
var agentRequest, delegatedGrant, _error_2, agentResponse, reply, _a, entries, status, cursor, records;
|
|
492
|
+
var _this = this;
|
|
493
|
+
return __generator(this, function (_b) {
|
|
494
|
+
switch (_b.label) {
|
|
495
|
+
case 0:
|
|
496
|
+
agentRequest = {
|
|
497
|
+
/**
|
|
498
|
+
* The `author` is the DID that will sign the message and must be the DID the Web5 app is
|
|
499
|
+
* connected with and is authorized to access the signing private key of.
|
|
500
|
+
*/
|
|
501
|
+
author: this.connectedDid,
|
|
502
|
+
messageParams: request.message,
|
|
503
|
+
messageType: agent_2.DwnInterface.RecordsQuery,
|
|
504
|
+
/**
|
|
505
|
+
* The `target` is the DID of the DWN tenant under which the query will be executed.
|
|
506
|
+
* If `from` is provided, the query operation will be executed on a remote DWN.
|
|
507
|
+
* Otherwise, the local DWN will be queried.
|
|
508
|
+
*/
|
|
509
|
+
target: request.from || this.connectedDid
|
|
510
|
+
};
|
|
511
|
+
if (!this.delegateDid) return [3 /*break*/, 4];
|
|
512
|
+
_b.label = 1;
|
|
513
|
+
case 1:
|
|
514
|
+
_b.trys.push([1, 3, , 4]);
|
|
515
|
+
return [4 /*yield*/, this.permissionsApi.getPermissionForRequest({
|
|
516
|
+
connectedDid: this.connectedDid,
|
|
517
|
+
delegateDid: this.delegateDid,
|
|
518
|
+
protocol: request.protocol,
|
|
519
|
+
delegate: true,
|
|
520
|
+
cached: true,
|
|
521
|
+
messageType: agentRequest.messageType
|
|
522
|
+
})];
|
|
523
|
+
case 2:
|
|
524
|
+
delegatedGrant = (_b.sent()).message;
|
|
525
|
+
agentRequest.messageParams = __assign(__assign({}, agentRequest.messageParams), { delegatedGrant: delegatedGrant });
|
|
526
|
+
agentRequest.granteeDid = this.delegateDid;
|
|
527
|
+
return [3 /*break*/, 4];
|
|
528
|
+
case 3:
|
|
529
|
+
_error_2 = _b.sent();
|
|
530
|
+
// if a grant is not found, we should author the request as the delegated DID to get public records
|
|
531
|
+
agentRequest.author = this.delegateDid;
|
|
532
|
+
return [3 /*break*/, 4];
|
|
533
|
+
case 4:
|
|
534
|
+
if (!request.from) return [3 /*break*/, 6];
|
|
535
|
+
return [4 /*yield*/, this.agent.sendDwnRequest(agentRequest)];
|
|
536
|
+
case 5:
|
|
537
|
+
agentResponse = _b.sent();
|
|
538
|
+
return [3 /*break*/, 8];
|
|
539
|
+
case 6: return [4 /*yield*/, this.agent.processDwnRequest(agentRequest)];
|
|
540
|
+
case 7:
|
|
541
|
+
agentResponse = _b.sent();
|
|
542
|
+
_b.label = 8;
|
|
543
|
+
case 8:
|
|
544
|
+
reply = agentResponse.reply;
|
|
545
|
+
_a = reply.entries, entries = _a === void 0 ? [] : _a, status = reply.status, cursor = reply.cursor;
|
|
546
|
+
records = entries.map(function (entry) {
|
|
547
|
+
var recordOptions = __assign({
|
|
548
|
+
/**
|
|
549
|
+
* Extract the `author` DID from the record entry since records may be signed by the
|
|
550
|
+
* tenant owner or any other entity.
|
|
551
|
+
*/
|
|
552
|
+
author: (0, agent_2.getRecordAuthor)(entry),
|
|
553
|
+
/**
|
|
554
|
+
* Set the `connectedDid` to currently connected DID so that subsequent calls to
|
|
555
|
+
* {@link Record} instance methods, such as `record.update()` are executed on the
|
|
556
|
+
* local DWN even if the record was returned by a query of a remote DWN.
|
|
557
|
+
*/
|
|
558
|
+
connectedDid: _this.connectedDid,
|
|
559
|
+
/**
|
|
560
|
+
* If the record was returned by a query of a remote DWN, set the `remoteOrigin` to
|
|
561
|
+
* the DID of the DWN that returned the record. The `remoteOrigin` property will be used
|
|
562
|
+
* to determine which DWN to send subsequent read requests to in the event the data
|
|
563
|
+
* payload exceeds the threshold for being returned with queries.
|
|
564
|
+
*/
|
|
565
|
+
remoteOrigin: request.from, delegateDid: _this.delegateDid, protocolRole: agentRequest.messageParams.protocolRole }, entry);
|
|
566
|
+
var record = new record_js_1.Record(_this.agent, recordOptions, _this.permissionsApi);
|
|
567
|
+
return record;
|
|
568
|
+
});
|
|
569
|
+
return [2 /*return*/, { records: records, status: status, cursor: cursor }];
|
|
570
|
+
}
|
|
571
|
+
});
|
|
572
|
+
}); },
|
|
573
|
+
/**
|
|
574
|
+
* Read a single record based on the given filter
|
|
575
|
+
*/
|
|
576
|
+
read: function (request) { return __awaiter(_this, void 0, void 0, function () {
|
|
577
|
+
var agentRequest, delegatedGrant, _error_3, agentResponse, _a, entry, status, record, recordOptions;
|
|
578
|
+
return __generator(this, function (_b) {
|
|
579
|
+
switch (_b.label) {
|
|
580
|
+
case 0:
|
|
581
|
+
agentRequest = {
|
|
582
|
+
/**
|
|
583
|
+
* The `author` is the DID that will sign the message and must be the DID the Web5 app is
|
|
584
|
+
* connected with and is authorized to access the signing private key of.
|
|
585
|
+
*/
|
|
586
|
+
author: this.connectedDid,
|
|
587
|
+
messageParams: request.message,
|
|
588
|
+
messageType: agent_2.DwnInterface.RecordsRead,
|
|
589
|
+
/**
|
|
590
|
+
* The `target` is the DID of the DWN tenant under which the read will be executed.
|
|
591
|
+
* If `from` is provided, the read operation will be executed on a remote DWN.
|
|
592
|
+
* Otherwise, the read will occur on the local DWN.
|
|
593
|
+
*/
|
|
594
|
+
target: request.from || this.connectedDid
|
|
595
|
+
};
|
|
596
|
+
if (!this.delegateDid) return [3 /*break*/, 4];
|
|
597
|
+
_b.label = 1;
|
|
598
|
+
case 1:
|
|
599
|
+
_b.trys.push([1, 3, , 4]);
|
|
600
|
+
return [4 /*yield*/, this.permissionsApi.getPermissionForRequest({
|
|
601
|
+
connectedDid: this.connectedDid,
|
|
602
|
+
delegateDid: this.delegateDid,
|
|
603
|
+
protocol: request.protocol,
|
|
604
|
+
delegate: true,
|
|
605
|
+
cached: true,
|
|
606
|
+
messageType: agentRequest.messageType
|
|
607
|
+
})];
|
|
608
|
+
case 2:
|
|
609
|
+
delegatedGrant = (_b.sent()).message;
|
|
610
|
+
agentRequest.messageParams = __assign(__assign({}, agentRequest.messageParams), { delegatedGrant: delegatedGrant });
|
|
611
|
+
agentRequest.granteeDid = this.delegateDid;
|
|
612
|
+
return [3 /*break*/, 4];
|
|
613
|
+
case 3:
|
|
614
|
+
_error_3 = _b.sent();
|
|
615
|
+
// if a grant is not found, we should author the request as the delegated DID to get public records
|
|
616
|
+
agentRequest.author = this.delegateDid;
|
|
617
|
+
return [3 /*break*/, 4];
|
|
618
|
+
case 4:
|
|
619
|
+
if (!request.from) return [3 /*break*/, 6];
|
|
620
|
+
return [4 /*yield*/, this.agent.sendDwnRequest(agentRequest)];
|
|
621
|
+
case 5:
|
|
622
|
+
agentResponse = _b.sent();
|
|
623
|
+
return [3 /*break*/, 8];
|
|
624
|
+
case 6: return [4 /*yield*/, this.agent.processDwnRequest(agentRequest)];
|
|
625
|
+
case 7:
|
|
626
|
+
agentResponse = _b.sent();
|
|
627
|
+
_b.label = 8;
|
|
628
|
+
case 8:
|
|
629
|
+
_a = agentResponse.reply, entry = _a.entry, status = _a.status;
|
|
630
|
+
if (200 <= status.code && status.code <= 299) {
|
|
631
|
+
recordOptions = __assign({
|
|
632
|
+
/**
|
|
633
|
+
* Extract the `author` DID from the record since records may be signed by the
|
|
634
|
+
* tenant owner or any other entity.
|
|
635
|
+
*/
|
|
636
|
+
author: (0, agent_2.getRecordAuthor)(entry.recordsWrite),
|
|
637
|
+
/**
|
|
638
|
+
* Set the `connectedDid` to currently connected DID so that subsequent calls to
|
|
639
|
+
* {@link Record} instance methods, such as `record.update()` are executed on the
|
|
640
|
+
* local DWN even if the record was read from a remote DWN.
|
|
641
|
+
*/
|
|
642
|
+
connectedDid: this.connectedDid,
|
|
643
|
+
/**
|
|
644
|
+
* If the record was returned by reading from a remote DWN, set the `remoteOrigin` to
|
|
645
|
+
* the DID of the DWN that returned the record. The `remoteOrigin` property will be used
|
|
646
|
+
* to determine which DWN to send subsequent read requests to in the event the data
|
|
647
|
+
* payload must be read again (e.g., if the data stream is consumed).
|
|
648
|
+
*/
|
|
649
|
+
remoteOrigin: request.from, delegateDid: this.delegateDid, data: entry.data, initialWrite: entry.initialWrite }, entry.recordsWrite);
|
|
650
|
+
record = new record_js_1.Record(this.agent, recordOptions, this.permissionsApi);
|
|
651
|
+
}
|
|
652
|
+
return [2 /*return*/, { record: record, status: status }];
|
|
653
|
+
}
|
|
654
|
+
});
|
|
655
|
+
}); },
|
|
656
|
+
/**
|
|
657
|
+
* Subscribes to records based on the given filter and emits events to the `subscriptionHandler`.
|
|
658
|
+
*
|
|
659
|
+
* @param request must include the `message` with the subscription filter and the `subscriptionHandler` to process the events.
|
|
660
|
+
* @returns the subscription status and the subscription object used to close the subscription.
|
|
661
|
+
*/
|
|
662
|
+
subscribe: function (request) { return __awaiter(_this, void 0, void 0, function () {
|
|
663
|
+
var agentRequest, delegatedGrant, _error_4, agentResponse, reply, status, subscription;
|
|
664
|
+
return __generator(this, function (_a) {
|
|
665
|
+
switch (_a.label) {
|
|
666
|
+
case 0:
|
|
667
|
+
agentRequest = {
|
|
668
|
+
/**
|
|
669
|
+
* The `author` is the DID that will sign the message and must be the DID the Web5 app is
|
|
670
|
+
* connected with and is authorized to access the signing private key of.
|
|
671
|
+
*/
|
|
672
|
+
author: this.connectedDid,
|
|
673
|
+
messageParams: request.message,
|
|
674
|
+
messageType: agent_2.DwnInterface.RecordsSubscribe,
|
|
675
|
+
/**
|
|
676
|
+
* The `target` is the DID of the DWN tenant under which the subscribe operation will be executed.
|
|
677
|
+
* If `from` is provided, the subscribe operation will be executed on a remote DWN.
|
|
678
|
+
* Otherwise, the local DWN will execute the subscribe operation.
|
|
679
|
+
*/
|
|
680
|
+
target: request.from || this.connectedDid,
|
|
681
|
+
/**
|
|
682
|
+
* The handler to process the subscription events.
|
|
683
|
+
*/
|
|
684
|
+
subscriptionHandler: subscription_util_js_1.SubscriptionUtil.recordSubscriptionHandler({
|
|
685
|
+
agent: this.agent,
|
|
686
|
+
connectedDid: this.connectedDid,
|
|
687
|
+
delegateDid: this.delegateDid,
|
|
688
|
+
permissionsApi: this.permissionsApi,
|
|
689
|
+
protocolRole: request.message.protocolRole,
|
|
690
|
+
request: request
|
|
691
|
+
})
|
|
692
|
+
};
|
|
693
|
+
if (!this.delegateDid) return [3 /*break*/, 4];
|
|
694
|
+
_a.label = 1;
|
|
695
|
+
case 1:
|
|
696
|
+
_a.trys.push([1, 3, , 4]);
|
|
697
|
+
return [4 /*yield*/, this.permissionsApi.getPermissionForRequest({
|
|
698
|
+
connectedDid: this.connectedDid,
|
|
699
|
+
delegateDid: this.delegateDid,
|
|
700
|
+
protocol: request.protocol,
|
|
701
|
+
delegate: true,
|
|
702
|
+
cached: true,
|
|
703
|
+
messageType: agentRequest.messageType
|
|
704
|
+
})];
|
|
705
|
+
case 2:
|
|
706
|
+
delegatedGrant = (_a.sent()).message;
|
|
707
|
+
agentRequest.messageParams = __assign(__assign({}, agentRequest.messageParams), { delegatedGrant: delegatedGrant });
|
|
708
|
+
agentRequest.granteeDid = this.delegateDid;
|
|
709
|
+
return [3 /*break*/, 4];
|
|
710
|
+
case 3:
|
|
711
|
+
_error_4 = _a.sent();
|
|
712
|
+
// if a grant is not found, we should author the request as the delegated DID to get public records
|
|
713
|
+
agentRequest.author = this.delegateDid;
|
|
714
|
+
return [3 /*break*/, 4];
|
|
715
|
+
case 4:
|
|
716
|
+
;
|
|
717
|
+
if (!request.from) return [3 /*break*/, 6];
|
|
718
|
+
return [4 /*yield*/, this.agent.sendDwnRequest(agentRequest)];
|
|
719
|
+
case 5:
|
|
720
|
+
agentResponse = _a.sent();
|
|
721
|
+
return [3 /*break*/, 8];
|
|
722
|
+
case 6: return [4 /*yield*/, this.agent.processDwnRequest(agentRequest)];
|
|
723
|
+
case 7:
|
|
724
|
+
agentResponse = _a.sent();
|
|
725
|
+
_a.label = 8;
|
|
726
|
+
case 8:
|
|
727
|
+
reply = agentResponse.reply;
|
|
728
|
+
status = reply.status, subscription = reply.subscription;
|
|
729
|
+
return [2 /*return*/, { status: status, subscription: subscription }];
|
|
730
|
+
}
|
|
731
|
+
});
|
|
732
|
+
}); },
|
|
733
|
+
/**
|
|
734
|
+
* Writes a record to the DWN
|
|
735
|
+
*
|
|
736
|
+
* As a convenience, the Record instance returned will cache a copy of the data. This is done
|
|
737
|
+
* to maintain consistency with other DWN methods, like RecordsQuery, that include relatively
|
|
738
|
+
* small data payloads when returning RecordsWrite message properties. Regardless of data
|
|
739
|
+
* size, methods such as `record.data.stream()` will return the data when called even if it
|
|
740
|
+
* requires fetching from the DWN datastore.
|
|
741
|
+
*/
|
|
742
|
+
write: function (request) { return __awaiter(_this, void 0, void 0, function () {
|
|
743
|
+
var _a, dataBlob, dataFormat, dwnRequestParams, delegatedGrant, agentResponse, responseMessage, status, record, recordOptions;
|
|
744
|
+
var _b;
|
|
745
|
+
return __generator(this, function (_c) {
|
|
746
|
+
switch (_c.label) {
|
|
747
|
+
case 0:
|
|
748
|
+
_a = (0, utils_js_1.dataToBlob)(request.data, (_b = request.message) === null || _b === void 0 ? void 0 : _b.dataFormat), dataBlob = _a.dataBlob, dataFormat = _a.dataFormat;
|
|
749
|
+
dwnRequestParams = {
|
|
750
|
+
store: request.store,
|
|
751
|
+
messageType: agent_2.DwnInterface.RecordsWrite,
|
|
752
|
+
messageParams: __assign(__assign({}, request.message), { dataFormat: dataFormat }),
|
|
753
|
+
author: this.connectedDid,
|
|
754
|
+
target: this.connectedDid,
|
|
755
|
+
dataStream: dataBlob
|
|
756
|
+
};
|
|
757
|
+
if (!this.delegateDid) return [3 /*break*/, 2];
|
|
758
|
+
return [4 /*yield*/, this.permissionsApi.getPermissionForRequest({
|
|
759
|
+
connectedDid: this.connectedDid,
|
|
760
|
+
delegateDid: this.delegateDid,
|
|
761
|
+
protocol: request.message.protocol,
|
|
762
|
+
delegate: true,
|
|
763
|
+
cached: true,
|
|
764
|
+
messageType: dwnRequestParams.messageType
|
|
765
|
+
})];
|
|
766
|
+
case 1:
|
|
767
|
+
delegatedGrant = (_c.sent()).message;
|
|
768
|
+
dwnRequestParams.messageParams = __assign(__assign({}, dwnRequestParams.messageParams), { delegatedGrant: delegatedGrant });
|
|
769
|
+
dwnRequestParams.granteeDid = this.delegateDid;
|
|
770
|
+
_c.label = 2;
|
|
771
|
+
case 2:
|
|
772
|
+
;
|
|
773
|
+
return [4 /*yield*/, this.agent.processDwnRequest(dwnRequestParams)];
|
|
774
|
+
case 3:
|
|
775
|
+
agentResponse = _c.sent();
|
|
776
|
+
responseMessage = agentResponse.message, status = agentResponse.reply.status;
|
|
777
|
+
if (200 <= status.code && status.code <= 299) {
|
|
778
|
+
recordOptions = __assign({
|
|
779
|
+
/**
|
|
780
|
+
* Assume the author is the connected DID since the record was just written to the
|
|
781
|
+
* local DWN.
|
|
782
|
+
*/
|
|
783
|
+
author: this.connectedDid,
|
|
784
|
+
/**
|
|
785
|
+
* Set the `connectedDid` to currently connected DID so that subsequent calls to
|
|
786
|
+
* {@link Record} instance methods, such as `record.update()` are executed on the
|
|
787
|
+
* local DWN.
|
|
788
|
+
*/
|
|
789
|
+
connectedDid: this.connectedDid, encodedData: dataBlob, delegateDid: this.delegateDid }, responseMessage);
|
|
790
|
+
record = new record_js_1.Record(this.agent, recordOptions, this.permissionsApi);
|
|
791
|
+
}
|
|
792
|
+
return [2 /*return*/, { record: record, status: status }];
|
|
793
|
+
}
|
|
794
|
+
});
|
|
795
|
+
}); },
|
|
796
|
+
};
|
|
797
|
+
},
|
|
798
|
+
enumerable: false,
|
|
799
|
+
configurable: true
|
|
800
|
+
});
|
|
801
|
+
return DwnApi;
|
|
802
|
+
}());
|
|
803
|
+
exports.DwnApi = DwnApi;
|
|
804
|
+
//# sourceMappingURL=dwn-api.js.map
|