@claw-network/protocol 0.1.0
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/dist/contracts/events.d.ts +238 -0
- package/dist/contracts/events.d.ts.map +1 -0
- package/dist/contracts/events.js +499 -0
- package/dist/contracts/events.js.map +1 -0
- package/dist/contracts/index.d.ts +5 -0
- package/dist/contracts/index.d.ts.map +1 -0
- package/dist/contracts/index.js +5 -0
- package/dist/contracts/index.js.map +1 -0
- package/dist/contracts/state.d.ts +16 -0
- package/dist/contracts/state.d.ts.map +1 -0
- package/dist/contracts/state.js +399 -0
- package/dist/contracts/state.js.map +1 -0
- package/dist/contracts/store.d.ts +19 -0
- package/dist/contracts/store.d.ts.map +1 -0
- package/dist/contracts/store.js +22 -0
- package/dist/contracts/store.js.map +1 -0
- package/dist/contracts/types.d.ts +81 -0
- package/dist/contracts/types.d.ts.map +1 -0
- package/dist/contracts/types.js +2 -0
- package/dist/contracts/types.js.map +1 -0
- package/dist/dao/events.d.ts +191 -0
- package/dist/dao/events.d.ts.map +1 -0
- package/dist/dao/events.js +294 -0
- package/dist/dao/events.js.map +1 -0
- package/dist/dao/index.d.ts +6 -0
- package/dist/dao/index.d.ts.map +1 -0
- package/dist/dao/index.js +6 -0
- package/dist/dao/index.js.map +1 -0
- package/dist/dao/state.d.ts +44 -0
- package/dist/dao/state.d.ts.map +1 -0
- package/dist/dao/state.js +373 -0
- package/dist/dao/state.js.map +1 -0
- package/dist/dao/store.d.ts +47 -0
- package/dist/dao/store.d.ts.map +1 -0
- package/dist/dao/store.js +52 -0
- package/dist/dao/store.js.map +1 -0
- package/dist/dao/types.d.ts +149 -0
- package/dist/dao/types.d.ts.map +1 -0
- package/dist/dao/types.js +95 -0
- package/dist/dao/types.js.map +1 -0
- package/dist/dao/voting.d.ts +56 -0
- package/dist/dao/voting.d.ts.map +1 -0
- package/dist/dao/voting.js +129 -0
- package/dist/dao/voting.js.map +1 -0
- package/dist/identity/document.d.ts +35 -0
- package/dist/identity/document.d.ts.map +1 -0
- package/dist/identity/document.js +84 -0
- package/dist/identity/document.js.map +1 -0
- package/dist/identity/events.d.ts +96 -0
- package/dist/identity/events.d.ts.map +1 -0
- package/dist/identity/events.js +165 -0
- package/dist/identity/events.js.map +1 -0
- package/dist/identity/index.d.ts +4 -0
- package/dist/identity/index.d.ts.map +1 -0
- package/dist/identity/index.js +4 -0
- package/dist/identity/index.js.map +1 -0
- package/dist/identity/resolver.d.ts +10 -0
- package/dist/identity/resolver.d.ts.map +1 -0
- package/dist/identity/resolver.js +10 -0
- package/dist/identity/resolver.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/markets/broadcast.d.ts +5 -0
- package/dist/markets/broadcast.d.ts.map +1 -0
- package/dist/markets/broadcast.js +9 -0
- package/dist/markets/broadcast.js.map +1 -0
- package/dist/markets/capability.d.ts +129 -0
- package/dist/markets/capability.d.ts.map +1 -0
- package/dist/markets/capability.js +262 -0
- package/dist/markets/capability.js.map +1 -0
- package/dist/markets/events.d.ts +448 -0
- package/dist/markets/events.d.ts.map +1 -0
- package/dist/markets/events.js +1074 -0
- package/dist/markets/events.js.map +1 -0
- package/dist/markets/index.d.ts +12 -0
- package/dist/markets/index.d.ts.map +1 -0
- package/dist/markets/index.js +12 -0
- package/dist/markets/index.js.map +1 -0
- package/dist/markets/info-flow.d.ts +144 -0
- package/dist/markets/info-flow.d.ts.map +1 -0
- package/dist/markets/info-flow.js +257 -0
- package/dist/markets/info-flow.js.map +1 -0
- package/dist/markets/info-store.d.ts +80 -0
- package/dist/markets/info-store.d.ts.map +1 -0
- package/dist/markets/info-store.js +384 -0
- package/dist/markets/info-store.js.map +1 -0
- package/dist/markets/info.d.ts +118 -0
- package/dist/markets/info.d.ts.map +1 -0
- package/dist/markets/info.js +123 -0
- package/dist/markets/info.js.map +1 -0
- package/dist/markets/search.d.ts +70 -0
- package/dist/markets/search.d.ts.map +1 -0
- package/dist/markets/search.js +659 -0
- package/dist/markets/search.js.map +1 -0
- package/dist/markets/state.d.ts +30 -0
- package/dist/markets/state.d.ts.map +1 -0
- package/dist/markets/state.js +610 -0
- package/dist/markets/state.js.map +1 -0
- package/dist/markets/store.d.ts +43 -0
- package/dist/markets/store.d.ts.map +1 -0
- package/dist/markets/store.js +460 -0
- package/dist/markets/store.js.map +1 -0
- package/dist/markets/task.d.ts +96 -0
- package/dist/markets/task.d.ts.map +1 -0
- package/dist/markets/task.js +286 -0
- package/dist/markets/task.js.map +1 -0
- package/dist/markets/types.d.ts +340 -0
- package/dist/markets/types.d.ts.map +1 -0
- package/dist/markets/types.js +88 -0
- package/dist/markets/types.js.map +1 -0
- package/dist/p2p/codec.d.ts +42 -0
- package/dist/p2p/codec.d.ts.map +1 -0
- package/dist/p2p/codec.js +187 -0
- package/dist/p2p/codec.js.map +1 -0
- package/dist/p2p/flatbuffers.d.ts +57 -0
- package/dist/p2p/flatbuffers.d.ts.map +1 -0
- package/dist/p2p/flatbuffers.js +418 -0
- package/dist/p2p/flatbuffers.js.map +1 -0
- package/dist/p2p/index.d.ts +3 -0
- package/dist/p2p/index.d.ts.map +1 -0
- package/dist/p2p/index.js +3 -0
- package/dist/p2p/index.js.map +1 -0
- package/dist/p2p/types.d.ts +79 -0
- package/dist/p2p/types.d.ts.map +1 -0
- package/dist/p2p/types.js +15 -0
- package/dist/p2p/types.js.map +1 -0
- package/dist/reputation/anti-cheat.d.ts +24 -0
- package/dist/reputation/anti-cheat.d.ts.map +1 -0
- package/dist/reputation/anti-cheat.js +109 -0
- package/dist/reputation/anti-cheat.js.map +1 -0
- package/dist/reputation/events.d.ts +29 -0
- package/dist/reputation/events.d.ts.map +1 -0
- package/dist/reputation/events.js +122 -0
- package/dist/reputation/events.js.map +1 -0
- package/dist/reputation/index.d.ts +6 -0
- package/dist/reputation/index.d.ts.map +1 -0
- package/dist/reputation/index.js +6 -0
- package/dist/reputation/index.js.map +1 -0
- package/dist/reputation/scoring.d.ts +40 -0
- package/dist/reputation/scoring.d.ts.map +1 -0
- package/dist/reputation/scoring.js +114 -0
- package/dist/reputation/scoring.js.map +1 -0
- package/dist/reputation/state.d.ts +48 -0
- package/dist/reputation/state.d.ts.map +1 -0
- package/dist/reputation/state.js +135 -0
- package/dist/reputation/state.js.map +1 -0
- package/dist/reputation/store.d.ts +23 -0
- package/dist/reputation/store.d.ts.map +1 -0
- package/dist/reputation/store.js +33 -0
- package/dist/reputation/store.js.map +1 -0
- package/dist/wallet/events.d.ts +121 -0
- package/dist/wallet/events.d.ts.map +1 -0
- package/dist/wallet/events.js +175 -0
- package/dist/wallet/events.js.map +1 -0
- package/dist/wallet/finality.d.ts +4 -0
- package/dist/wallet/finality.d.ts.map +1 -0
- package/dist/wallet/finality.js +32 -0
- package/dist/wallet/finality.js.map +1 -0
- package/dist/wallet/index.d.ts +4 -0
- package/dist/wallet/index.d.ts.map +1 -0
- package/dist/wallet/index.js +4 -0
- package/dist/wallet/index.js.map +1 -0
- package/dist/wallet/state.d.ts +32 -0
- package/dist/wallet/state.d.ts.map +1 -0
- package/dist/wallet/state.js +170 -0
- package/dist/wallet/state.js.map +1 -0
- package/package.json +42 -0
|
@@ -0,0 +1,1074 @@
|
|
|
1
|
+
import { multibaseEncode } from '@claw-network/core/encoding';
|
|
2
|
+
import { publicKeyFromDid } from '@claw-network/core/identity';
|
|
3
|
+
import { eventHashHex, signEvent } from '@claw-network/core/protocol';
|
|
4
|
+
import { isDeliveryStatus, isListingStatus, isListingVisibility, isMarketType, isOrderStatus, isPaymentStatus, isPricingType, } from './types.js';
|
|
5
|
+
function requireNonEmpty(value, field) {
|
|
6
|
+
if (!value || value.trim().length === 0) {
|
|
7
|
+
throw new Error(`${field} is required`);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
function assertRecord(value, field) {
|
|
11
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
12
|
+
throw new Error(`${field} must be an object`);
|
|
13
|
+
}
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
function assertValidDid(value, field) {
|
|
17
|
+
requireNonEmpty(value, field);
|
|
18
|
+
try {
|
|
19
|
+
publicKeyFromDid(value);
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
throw new Error(`${field} must be a valid did:claw identifier`);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
function normalizeTokenAmount(value, field) {
|
|
26
|
+
let parsed;
|
|
27
|
+
if (typeof value === 'bigint') {
|
|
28
|
+
parsed = value;
|
|
29
|
+
}
|
|
30
|
+
else if (typeof value === 'number') {
|
|
31
|
+
if (!Number.isFinite(value) || !Number.isInteger(value)) {
|
|
32
|
+
throw new Error(`${field} must be an integer`);
|
|
33
|
+
}
|
|
34
|
+
parsed = BigInt(value);
|
|
35
|
+
}
|
|
36
|
+
else if (typeof value === 'string') {
|
|
37
|
+
if (value.trim().length === 0) {
|
|
38
|
+
throw new Error(`${field} is required`);
|
|
39
|
+
}
|
|
40
|
+
parsed = BigInt(value);
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
throw new Error(`${field} must be a token amount`);
|
|
44
|
+
}
|
|
45
|
+
if (parsed < 0n) {
|
|
46
|
+
throw new Error(`${field} must be >= 0`);
|
|
47
|
+
}
|
|
48
|
+
return parsed.toString();
|
|
49
|
+
}
|
|
50
|
+
function parseStringArray(value, field) {
|
|
51
|
+
if (!Array.isArray(value)) {
|
|
52
|
+
throw new Error(`${field} must be an array`);
|
|
53
|
+
}
|
|
54
|
+
const result = [];
|
|
55
|
+
for (const entry of value) {
|
|
56
|
+
if (typeof entry !== 'string') {
|
|
57
|
+
throw new Error(`${field} must contain strings`);
|
|
58
|
+
}
|
|
59
|
+
const trimmed = entry.trim();
|
|
60
|
+
if (trimmed.length) {
|
|
61
|
+
result.push(trimmed);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (result.length === 0) {
|
|
65
|
+
throw new Error(`${field} must contain at least one tag`);
|
|
66
|
+
}
|
|
67
|
+
return result;
|
|
68
|
+
}
|
|
69
|
+
const CAPABILITY_PLAN_TYPES = ['pay_per_use', 'time_based', 'subscription', 'credits'];
|
|
70
|
+
function parseCapabilityLeasePlan(value) {
|
|
71
|
+
const record = assertRecord(value, 'plan');
|
|
72
|
+
const typeValue = String(record.type ?? '');
|
|
73
|
+
if (!CAPABILITY_PLAN_TYPES.includes(typeValue)) {
|
|
74
|
+
throw new Error('plan.type is invalid');
|
|
75
|
+
}
|
|
76
|
+
let details;
|
|
77
|
+
if (record.details !== undefined) {
|
|
78
|
+
details = assertRecord(record.details, 'plan.details');
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
type: typeValue,
|
|
82
|
+
details,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
function parsePricingModel(value) {
|
|
86
|
+
const pricing = assertRecord(value, 'pricing');
|
|
87
|
+
const typeValue = String(pricing.type ?? '');
|
|
88
|
+
if (!isPricingType(typeValue)) {
|
|
89
|
+
throw new Error('pricing.type is invalid');
|
|
90
|
+
}
|
|
91
|
+
const currencyValue = pricing.currency ?? 'TOKEN';
|
|
92
|
+
if (currencyValue !== 'TOKEN') {
|
|
93
|
+
throw new Error('pricing.currency must be TOKEN');
|
|
94
|
+
}
|
|
95
|
+
const negotiable = pricing.negotiable;
|
|
96
|
+
if (typeof negotiable !== 'boolean') {
|
|
97
|
+
throw new Error('pricing.negotiable must be a boolean');
|
|
98
|
+
}
|
|
99
|
+
const model = {
|
|
100
|
+
type: typeValue,
|
|
101
|
+
currency: 'TOKEN',
|
|
102
|
+
negotiable,
|
|
103
|
+
};
|
|
104
|
+
if (pricing.fixedPrice !== undefined) {
|
|
105
|
+
model.fixedPrice = normalizeTokenAmount(pricing.fixedPrice, 'pricing.fixedPrice');
|
|
106
|
+
}
|
|
107
|
+
if (pricing.priceRange) {
|
|
108
|
+
const range = assertRecord(pricing.priceRange, 'pricing.priceRange');
|
|
109
|
+
model.priceRange = {
|
|
110
|
+
min: normalizeTokenAmount(range.min, 'pricing.priceRange.min'),
|
|
111
|
+
max: normalizeTokenAmount(range.max, 'pricing.priceRange.max'),
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
if (pricing.usagePrice) {
|
|
115
|
+
const usage = assertRecord(pricing.usagePrice, 'pricing.usagePrice');
|
|
116
|
+
model.usagePrice = {
|
|
117
|
+
...usage,
|
|
118
|
+
unit: String(usage.unit ?? ''),
|
|
119
|
+
pricePerUnit: normalizeTokenAmount(usage.pricePerUnit, 'pricing.usagePrice.pricePerUnit'),
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
if (pricing.subscriptionPrice) {
|
|
123
|
+
const sub = assertRecord(pricing.subscriptionPrice, 'pricing.subscriptionPrice');
|
|
124
|
+
model.subscriptionPrice = {
|
|
125
|
+
...sub,
|
|
126
|
+
period: String(sub.period ?? ''),
|
|
127
|
+
price: normalizeTokenAmount(sub.price, 'pricing.subscriptionPrice.price'),
|
|
128
|
+
trialPeriod: typeof sub.trialPeriod === 'number' ? sub.trialPeriod : undefined,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
if (pricing.auction) {
|
|
132
|
+
const auction = assertRecord(pricing.auction, 'pricing.auction');
|
|
133
|
+
model.auction = {
|
|
134
|
+
...auction,
|
|
135
|
+
startingPrice: normalizeTokenAmount(auction.startingPrice, 'pricing.auction.startingPrice'),
|
|
136
|
+
reservePrice: auction.reservePrice !== undefined
|
|
137
|
+
? normalizeTokenAmount(auction.reservePrice, 'pricing.auction.reservePrice')
|
|
138
|
+
: undefined,
|
|
139
|
+
bidIncrement: normalizeTokenAmount(auction.bidIncrement, 'pricing.auction.bidIncrement'),
|
|
140
|
+
duration: Number(auction.duration ?? 0),
|
|
141
|
+
endTime: Number(auction.endTime ?? 0),
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
if (pricing.discounts) {
|
|
145
|
+
if (!Array.isArray(pricing.discounts)) {
|
|
146
|
+
throw new Error('pricing.discounts must be an array');
|
|
147
|
+
}
|
|
148
|
+
model.discounts = pricing.discounts;
|
|
149
|
+
for (const discount of model.discounts ?? []) {
|
|
150
|
+
if (discount.condition?.minValue !== undefined) {
|
|
151
|
+
discount.condition.minValue = normalizeTokenAmount(discount.condition.minValue, 'pricing.discounts.minValue');
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return model;
|
|
156
|
+
}
|
|
157
|
+
function parseOrderPricing(value) {
|
|
158
|
+
const pricing = assertRecord(value, 'pricing');
|
|
159
|
+
const subtotal = normalizeTokenAmount(pricing.subtotal, 'pricing.subtotal');
|
|
160
|
+
const total = normalizeTokenAmount(pricing.total, 'pricing.total');
|
|
161
|
+
let discounts;
|
|
162
|
+
if (pricing.discounts !== undefined) {
|
|
163
|
+
if (!Array.isArray(pricing.discounts)) {
|
|
164
|
+
throw new Error('pricing.discounts must be an array');
|
|
165
|
+
}
|
|
166
|
+
discounts = pricing.discounts.map((entry) => {
|
|
167
|
+
if (!entry || typeof entry !== 'object') {
|
|
168
|
+
throw new Error('pricing.discounts entries must be objects');
|
|
169
|
+
}
|
|
170
|
+
const record = entry;
|
|
171
|
+
return {
|
|
172
|
+
...record,
|
|
173
|
+
amount: normalizeTokenAmount(record.amount, 'pricing.discounts.amount'),
|
|
174
|
+
};
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
let fees;
|
|
178
|
+
if (pricing.fees !== undefined) {
|
|
179
|
+
if (!Array.isArray(pricing.fees)) {
|
|
180
|
+
throw new Error('pricing.fees must be an array');
|
|
181
|
+
}
|
|
182
|
+
fees = pricing.fees.map((entry) => {
|
|
183
|
+
if (!entry || typeof entry !== 'object') {
|
|
184
|
+
throw new Error('pricing.fees entries must be objects');
|
|
185
|
+
}
|
|
186
|
+
const record = entry;
|
|
187
|
+
return {
|
|
188
|
+
...record,
|
|
189
|
+
amount: normalizeTokenAmount(record.amount, 'pricing.fees.amount'),
|
|
190
|
+
};
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
return {
|
|
194
|
+
subtotal,
|
|
195
|
+
discounts,
|
|
196
|
+
fees,
|
|
197
|
+
total,
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
function parseOrderPayment(value, field, requireStatus) {
|
|
201
|
+
const payment = assertRecord(value, field);
|
|
202
|
+
const statusValue = payment.status;
|
|
203
|
+
if (statusValue === undefined && requireStatus) {
|
|
204
|
+
throw new Error(`${field}.status is required`);
|
|
205
|
+
}
|
|
206
|
+
const statusString = statusValue !== undefined ? String(statusValue) : 'pending';
|
|
207
|
+
if (!isPaymentStatus(statusString)) {
|
|
208
|
+
throw new Error(`${field}.status is invalid`);
|
|
209
|
+
}
|
|
210
|
+
return {
|
|
211
|
+
...payment,
|
|
212
|
+
status: statusString,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
function parseOrderDelivery(value, field, requireStatus) {
|
|
216
|
+
const delivery = assertRecord(value, field);
|
|
217
|
+
const statusValue = delivery.status;
|
|
218
|
+
if (statusValue === undefined && requireStatus) {
|
|
219
|
+
throw new Error(`${field}.status is required`);
|
|
220
|
+
}
|
|
221
|
+
const statusString = statusValue !== undefined ? String(statusValue) : 'pending';
|
|
222
|
+
if (!isDeliveryStatus(statusString)) {
|
|
223
|
+
throw new Error(`${field}.status is invalid`);
|
|
224
|
+
}
|
|
225
|
+
return {
|
|
226
|
+
...delivery,
|
|
227
|
+
status: statusString,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
function parseOrderItems(orderId, value) {
|
|
231
|
+
if (!Array.isArray(value)) {
|
|
232
|
+
throw new Error('items must be an array');
|
|
233
|
+
}
|
|
234
|
+
if (value.length === 0) {
|
|
235
|
+
throw new Error('items must not be empty');
|
|
236
|
+
}
|
|
237
|
+
return value.map((entry, index) => {
|
|
238
|
+
const record = assertRecord(entry, `items[${index}]`);
|
|
239
|
+
const listingId = String(record.listingId ?? '');
|
|
240
|
+
requireNonEmpty(listingId, `items[${index}].listingId`);
|
|
241
|
+
const quantity = Number(record.quantity ?? 0);
|
|
242
|
+
if (!Number.isInteger(quantity) || quantity <= 0) {
|
|
243
|
+
throw new Error(`items[${index}].quantity must be a positive integer`);
|
|
244
|
+
}
|
|
245
|
+
const unitPrice = normalizeTokenAmount(record.unitPrice, `items[${index}].unitPrice`);
|
|
246
|
+
const totalPrice = record.totalPrice !== undefined
|
|
247
|
+
? normalizeTokenAmount(record.totalPrice, `items[${index}].totalPrice`)
|
|
248
|
+
: (BigInt(unitPrice) * BigInt(quantity)).toString();
|
|
249
|
+
const id = typeof record.id === 'string' && record.id.trim().length > 0
|
|
250
|
+
? record.id
|
|
251
|
+
: `${orderId}:${index}`;
|
|
252
|
+
return {
|
|
253
|
+
id,
|
|
254
|
+
listingId,
|
|
255
|
+
title: typeof record.title === 'string' ? record.title : undefined,
|
|
256
|
+
description: typeof record.description === 'string' ? record.description : undefined,
|
|
257
|
+
quantity,
|
|
258
|
+
unitPrice,
|
|
259
|
+
totalPrice,
|
|
260
|
+
itemData: typeof record.itemData === 'object' && record.itemData !== null
|
|
261
|
+
? record.itemData
|
|
262
|
+
: undefined,
|
|
263
|
+
};
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
function buildEnvelope(type, issuer, publicKey, payload, ts, nonce, prev) {
|
|
267
|
+
return {
|
|
268
|
+
v: 1,
|
|
269
|
+
type,
|
|
270
|
+
issuer,
|
|
271
|
+
ts,
|
|
272
|
+
nonce,
|
|
273
|
+
payload,
|
|
274
|
+
prev,
|
|
275
|
+
sig: '',
|
|
276
|
+
pub: multibaseEncode(publicKey),
|
|
277
|
+
hash: '',
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
export function parseMarketListingPublishPayload(payload) {
|
|
281
|
+
const listingId = String(payload.listingId ?? '');
|
|
282
|
+
requireNonEmpty(listingId, 'listingId');
|
|
283
|
+
const marketTypeValue = String(payload.marketType ?? '');
|
|
284
|
+
if (!isMarketType(marketTypeValue)) {
|
|
285
|
+
throw new Error('marketType is invalid');
|
|
286
|
+
}
|
|
287
|
+
const sellerRecord = assertRecord(payload.seller, 'seller');
|
|
288
|
+
const sellerDid = String(sellerRecord.did ?? '');
|
|
289
|
+
assertValidDid(sellerDid, 'seller.did');
|
|
290
|
+
const title = String(payload.title ?? '').trim();
|
|
291
|
+
requireNonEmpty(title, 'title');
|
|
292
|
+
const description = String(payload.description ?? '').trim();
|
|
293
|
+
requireNonEmpty(description, 'description');
|
|
294
|
+
const category = String(payload.category ?? '').trim();
|
|
295
|
+
requireNonEmpty(category, 'category');
|
|
296
|
+
const tags = parseStringArray(payload.tags, 'tags');
|
|
297
|
+
const pricing = parsePricingModel(payload.pricing);
|
|
298
|
+
const visibilityValue = String(payload.visibility ?? '');
|
|
299
|
+
if (!isListingVisibility(visibilityValue)) {
|
|
300
|
+
throw new Error('visibility is invalid');
|
|
301
|
+
}
|
|
302
|
+
const marketData = assertRecord(payload.marketData, 'marketData');
|
|
303
|
+
const restrictions = payload.restrictions
|
|
304
|
+
? assertRecord(payload.restrictions, 'restrictions')
|
|
305
|
+
: undefined;
|
|
306
|
+
const metadata = payload.metadata
|
|
307
|
+
? assertRecord(payload.metadata, 'metadata')
|
|
308
|
+
: undefined;
|
|
309
|
+
const expiresAt = payload.expiresAt;
|
|
310
|
+
if (expiresAt !== undefined && typeof expiresAt !== 'number') {
|
|
311
|
+
throw new Error('expiresAt must be a number');
|
|
312
|
+
}
|
|
313
|
+
const statusValue = payload.status !== undefined ? String(payload.status) : undefined;
|
|
314
|
+
if (statusValue !== undefined && !isListingStatus(statusValue)) {
|
|
315
|
+
throw new Error('status is invalid');
|
|
316
|
+
}
|
|
317
|
+
if (payload.resourcePrev !== undefined && payload.resourcePrev !== null) {
|
|
318
|
+
throw new Error('resourcePrev must be null for listing publish');
|
|
319
|
+
}
|
|
320
|
+
return {
|
|
321
|
+
listingId,
|
|
322
|
+
marketType: marketTypeValue,
|
|
323
|
+
seller: {
|
|
324
|
+
did: sellerDid,
|
|
325
|
+
name: typeof sellerRecord.name === 'string' ? sellerRecord.name : undefined,
|
|
326
|
+
},
|
|
327
|
+
title,
|
|
328
|
+
description,
|
|
329
|
+
category,
|
|
330
|
+
tags,
|
|
331
|
+
pricing,
|
|
332
|
+
visibility: visibilityValue,
|
|
333
|
+
marketData,
|
|
334
|
+
restrictions,
|
|
335
|
+
expiresAt: typeof expiresAt === 'number' ? expiresAt : undefined,
|
|
336
|
+
metadata,
|
|
337
|
+
status: statusValue,
|
|
338
|
+
resourcePrev: undefined,
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
export function parseMarketListingUpdatePayload(payload) {
|
|
342
|
+
const listingId = String(payload.listingId ?? '');
|
|
343
|
+
requireNonEmpty(listingId, 'listingId');
|
|
344
|
+
const resourcePrev = String(payload.resourcePrev ?? '');
|
|
345
|
+
requireNonEmpty(resourcePrev, 'resourcePrev');
|
|
346
|
+
const statusValue = String(payload.status ?? '');
|
|
347
|
+
if (!isListingStatus(statusValue)) {
|
|
348
|
+
throw new Error('status is invalid');
|
|
349
|
+
}
|
|
350
|
+
const metadata = payload.metadata
|
|
351
|
+
? assertRecord(payload.metadata, 'metadata')
|
|
352
|
+
: undefined;
|
|
353
|
+
return {
|
|
354
|
+
listingId,
|
|
355
|
+
resourcePrev,
|
|
356
|
+
status: statusValue,
|
|
357
|
+
metadata,
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
export function parseMarketListingRemovePayload(payload) {
|
|
361
|
+
const listingId = String(payload.listingId ?? '');
|
|
362
|
+
requireNonEmpty(listingId, 'listingId');
|
|
363
|
+
const resourcePrev = String(payload.resourcePrev ?? '');
|
|
364
|
+
requireNonEmpty(resourcePrev, 'resourcePrev');
|
|
365
|
+
return {
|
|
366
|
+
listingId,
|
|
367
|
+
resourcePrev,
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
export function parseMarketOrderCreatePayload(payload) {
|
|
371
|
+
const orderId = String(payload.orderId ?? '');
|
|
372
|
+
requireNonEmpty(orderId, 'orderId');
|
|
373
|
+
const listingId = String(payload.listingId ?? '');
|
|
374
|
+
requireNonEmpty(listingId, 'listingId');
|
|
375
|
+
const marketTypeValue = String(payload.marketType ?? '');
|
|
376
|
+
if (!isMarketType(marketTypeValue)) {
|
|
377
|
+
throw new Error('marketType is invalid');
|
|
378
|
+
}
|
|
379
|
+
const buyerRecord = assertRecord(payload.buyer, 'buyer');
|
|
380
|
+
const buyerDid = String(buyerRecord.did ?? '');
|
|
381
|
+
assertValidDid(buyerDid, 'buyer.did');
|
|
382
|
+
const sellerRecord = payload.seller !== undefined
|
|
383
|
+
? assertRecord(payload.seller, 'seller')
|
|
384
|
+
: undefined;
|
|
385
|
+
if (sellerRecord) {
|
|
386
|
+
const sellerDid = String(sellerRecord.did ?? '');
|
|
387
|
+
assertValidDid(sellerDid, 'seller.did');
|
|
388
|
+
}
|
|
389
|
+
const items = parseOrderItems(orderId, payload.items);
|
|
390
|
+
const pricing = parseOrderPricing(payload.pricing);
|
|
391
|
+
const payment = parseOrderPayment(payload.payment, 'payment', true);
|
|
392
|
+
const delivery = parseOrderDelivery(payload.delivery, 'delivery', true);
|
|
393
|
+
const statusValue = payload.status !== undefined ? String(payload.status) : undefined;
|
|
394
|
+
if (statusValue !== undefined && !isOrderStatus(statusValue)) {
|
|
395
|
+
throw new Error('status is invalid');
|
|
396
|
+
}
|
|
397
|
+
if (payload.resourcePrev !== undefined && payload.resourcePrev !== null) {
|
|
398
|
+
throw new Error('resourcePrev must be null for order create');
|
|
399
|
+
}
|
|
400
|
+
return {
|
|
401
|
+
orderId,
|
|
402
|
+
listingId,
|
|
403
|
+
marketType: marketTypeValue,
|
|
404
|
+
buyer: {
|
|
405
|
+
did: buyerDid,
|
|
406
|
+
name: typeof buyerRecord.name === 'string' ? buyerRecord.name : undefined,
|
|
407
|
+
},
|
|
408
|
+
seller: sellerRecord
|
|
409
|
+
? {
|
|
410
|
+
did: String(sellerRecord.did ?? ''),
|
|
411
|
+
name: typeof sellerRecord.name === 'string' ? sellerRecord.name : undefined,
|
|
412
|
+
}
|
|
413
|
+
: undefined,
|
|
414
|
+
items,
|
|
415
|
+
pricing,
|
|
416
|
+
payment,
|
|
417
|
+
delivery,
|
|
418
|
+
status: statusValue,
|
|
419
|
+
resourcePrev: undefined,
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
export function parseMarketOrderUpdatePayload(payload) {
|
|
423
|
+
const orderId = String(payload.orderId ?? '');
|
|
424
|
+
requireNonEmpty(orderId, 'orderId');
|
|
425
|
+
const resourcePrev = String(payload.resourcePrev ?? '');
|
|
426
|
+
requireNonEmpty(resourcePrev, 'resourcePrev');
|
|
427
|
+
const statusValue = String(payload.status ?? '');
|
|
428
|
+
if (!isOrderStatus(statusValue)) {
|
|
429
|
+
throw new Error('status is invalid');
|
|
430
|
+
}
|
|
431
|
+
const payment = payload.payment
|
|
432
|
+
? parseOrderPayment(payload.payment, 'payment', false)
|
|
433
|
+
: undefined;
|
|
434
|
+
const delivery = payload.delivery
|
|
435
|
+
? parseOrderDelivery(payload.delivery, 'delivery', false)
|
|
436
|
+
: undefined;
|
|
437
|
+
const review = payload.review
|
|
438
|
+
? assertRecord(payload.review, 'review')
|
|
439
|
+
: undefined;
|
|
440
|
+
return {
|
|
441
|
+
orderId,
|
|
442
|
+
resourcePrev,
|
|
443
|
+
status: statusValue,
|
|
444
|
+
payment,
|
|
445
|
+
delivery,
|
|
446
|
+
review,
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
function parseBidder(value) {
|
|
450
|
+
const bidder = assertRecord(value, 'bidder');
|
|
451
|
+
const bidderDid = String(bidder.did ?? '');
|
|
452
|
+
assertValidDid(bidderDid, 'bidder.did');
|
|
453
|
+
return {
|
|
454
|
+
did: bidderDid,
|
|
455
|
+
name: typeof bidder.name === 'string' ? bidder.name : undefined,
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
function parseBidProposal(value) {
|
|
459
|
+
const proposal = assertRecord(value, 'proposal');
|
|
460
|
+
const price = normalizeTokenAmount(proposal.price, 'proposal.price');
|
|
461
|
+
const timeline = Number(proposal.timeline ?? NaN);
|
|
462
|
+
if (!Number.isFinite(timeline)) {
|
|
463
|
+
throw new Error('proposal.timeline must be a number');
|
|
464
|
+
}
|
|
465
|
+
const approach = String(proposal.approach ?? '').trim();
|
|
466
|
+
requireNonEmpty(approach, 'proposal.approach');
|
|
467
|
+
let milestones;
|
|
468
|
+
if (proposal.milestones !== undefined) {
|
|
469
|
+
if (!Array.isArray(proposal.milestones)) {
|
|
470
|
+
throw new Error('proposal.milestones must be an array');
|
|
471
|
+
}
|
|
472
|
+
milestones = proposal.milestones.map((entry, index) => {
|
|
473
|
+
return assertRecord(entry, `proposal.milestones[${index}]`);
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
return {
|
|
477
|
+
price,
|
|
478
|
+
timeline,
|
|
479
|
+
approach,
|
|
480
|
+
milestones,
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
function parseSubmissionDeliverables(value) {
|
|
484
|
+
if (!Array.isArray(value)) {
|
|
485
|
+
throw new Error('deliverables must be an array');
|
|
486
|
+
}
|
|
487
|
+
return value.map((entry, index) => assertRecord(entry, `deliverables[${index}]`));
|
|
488
|
+
}
|
|
489
|
+
export function parseMarketBidSubmitPayload(payload) {
|
|
490
|
+
const bidId = String(payload.bidId ?? '');
|
|
491
|
+
requireNonEmpty(bidId, 'bidId');
|
|
492
|
+
const taskId = String(payload.taskId ?? '');
|
|
493
|
+
requireNonEmpty(taskId, 'taskId');
|
|
494
|
+
const bidder = parseBidder(payload.bidder);
|
|
495
|
+
const proposal = parseBidProposal(payload.proposal);
|
|
496
|
+
if (payload.resourcePrev !== undefined && payload.resourcePrev !== null) {
|
|
497
|
+
throw new Error('resourcePrev must be null for bid submit');
|
|
498
|
+
}
|
|
499
|
+
return {
|
|
500
|
+
bidId,
|
|
501
|
+
taskId,
|
|
502
|
+
bidder,
|
|
503
|
+
proposal,
|
|
504
|
+
resourcePrev: undefined,
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
export function parseMarketBidUpdatePayload(payload) {
|
|
508
|
+
const bidId = String(payload.bidId ?? '');
|
|
509
|
+
requireNonEmpty(bidId, 'bidId');
|
|
510
|
+
const resourcePrev = String(payload.resourcePrev ?? '');
|
|
511
|
+
requireNonEmpty(resourcePrev, 'resourcePrev');
|
|
512
|
+
return {
|
|
513
|
+
bidId,
|
|
514
|
+
resourcePrev,
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
export function parseMarketSubmissionSubmitPayload(payload) {
|
|
518
|
+
const orderId = String(payload.orderId ?? '');
|
|
519
|
+
requireNonEmpty(orderId, 'orderId');
|
|
520
|
+
const submissionId = String(payload.submissionId ?? '');
|
|
521
|
+
requireNonEmpty(submissionId, 'submissionId');
|
|
522
|
+
const worker = String(payload.worker ?? '');
|
|
523
|
+
assertValidDid(worker, 'worker');
|
|
524
|
+
const deliverables = parseSubmissionDeliverables(payload.deliverables);
|
|
525
|
+
const notes = typeof payload.notes === 'string' ? payload.notes : undefined;
|
|
526
|
+
if (payload.resourcePrev !== undefined && payload.resourcePrev !== null) {
|
|
527
|
+
throw new Error('resourcePrev must be null for submission submit');
|
|
528
|
+
}
|
|
529
|
+
return {
|
|
530
|
+
orderId,
|
|
531
|
+
submissionId,
|
|
532
|
+
worker,
|
|
533
|
+
deliverables,
|
|
534
|
+
notes,
|
|
535
|
+
resourcePrev: undefined,
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
export function parseMarketSubmissionReviewPayload(payload) {
|
|
539
|
+
const submissionId = String(payload.submissionId ?? '');
|
|
540
|
+
requireNonEmpty(submissionId, 'submissionId');
|
|
541
|
+
const resourcePrev = String(payload.resourcePrev ?? '');
|
|
542
|
+
requireNonEmpty(resourcePrev, 'resourcePrev');
|
|
543
|
+
const approved = payload.approved;
|
|
544
|
+
if (typeof approved !== 'boolean') {
|
|
545
|
+
throw new Error('approved must be a boolean');
|
|
546
|
+
}
|
|
547
|
+
const feedback = String(payload.feedback ?? '').trim();
|
|
548
|
+
requireNonEmpty(feedback, 'feedback');
|
|
549
|
+
const ratingValue = payload.rating;
|
|
550
|
+
if (ratingValue !== undefined && typeof ratingValue !== 'number') {
|
|
551
|
+
throw new Error('rating must be a number');
|
|
552
|
+
}
|
|
553
|
+
const revisionDeadline = payload.revisionDeadline;
|
|
554
|
+
if (revisionDeadline !== undefined && typeof revisionDeadline !== 'number') {
|
|
555
|
+
throw new Error('revisionDeadline must be a number');
|
|
556
|
+
}
|
|
557
|
+
return {
|
|
558
|
+
submissionId,
|
|
559
|
+
resourcePrev,
|
|
560
|
+
approved,
|
|
561
|
+
feedback,
|
|
562
|
+
rating: typeof ratingValue === 'number' ? ratingValue : undefined,
|
|
563
|
+
revisionDeadline: typeof revisionDeadline === 'number' ? revisionDeadline : undefined,
|
|
564
|
+
};
|
|
565
|
+
}
|
|
566
|
+
export function parseMarketSubscriptionStartPayload(payload) {
|
|
567
|
+
const subscriptionId = String(payload.subscriptionId ?? '');
|
|
568
|
+
requireNonEmpty(subscriptionId, 'subscriptionId');
|
|
569
|
+
const listingId = String(payload.listingId ?? '');
|
|
570
|
+
requireNonEmpty(listingId, 'listingId');
|
|
571
|
+
const buyerRecord = assertRecord(payload.buyer, 'buyer');
|
|
572
|
+
const buyerDid = String(buyerRecord.did ?? '');
|
|
573
|
+
assertValidDid(buyerDid, 'buyer.did');
|
|
574
|
+
if (payload.resourcePrev !== undefined && payload.resourcePrev !== null) {
|
|
575
|
+
throw new Error('resourcePrev must be null for subscription start');
|
|
576
|
+
}
|
|
577
|
+
return {
|
|
578
|
+
subscriptionId,
|
|
579
|
+
listingId,
|
|
580
|
+
buyer: {
|
|
581
|
+
did: buyerDid,
|
|
582
|
+
name: typeof buyerRecord.name === 'string' ? buyerRecord.name : undefined,
|
|
583
|
+
},
|
|
584
|
+
resourcePrev: undefined,
|
|
585
|
+
};
|
|
586
|
+
}
|
|
587
|
+
export function parseMarketSubscriptionCancelPayload(payload) {
|
|
588
|
+
const subscriptionId = String(payload.subscriptionId ?? '');
|
|
589
|
+
requireNonEmpty(subscriptionId, 'subscriptionId');
|
|
590
|
+
const resourcePrev = String(payload.resourcePrev ?? '');
|
|
591
|
+
requireNonEmpty(resourcePrev, 'resourcePrev');
|
|
592
|
+
return {
|
|
593
|
+
subscriptionId,
|
|
594
|
+
resourcePrev,
|
|
595
|
+
};
|
|
596
|
+
}
|
|
597
|
+
export function parseMarketDisputeOpenPayload(payload) {
|
|
598
|
+
const disputeId = String(payload.disputeId ?? '');
|
|
599
|
+
requireNonEmpty(disputeId, 'disputeId');
|
|
600
|
+
const orderId = String(payload.orderId ?? '');
|
|
601
|
+
requireNonEmpty(orderId, 'orderId');
|
|
602
|
+
const type = String(payload.type ?? '').trim();
|
|
603
|
+
requireNonEmpty(type, 'type');
|
|
604
|
+
const description = String(payload.description ?? '').trim();
|
|
605
|
+
requireNonEmpty(description, 'description');
|
|
606
|
+
let claimAmount;
|
|
607
|
+
if (payload.claimAmount !== undefined) {
|
|
608
|
+
claimAmount = normalizeTokenAmount(payload.claimAmount, 'claimAmount');
|
|
609
|
+
}
|
|
610
|
+
if (payload.resourcePrev !== undefined && payload.resourcePrev !== null) {
|
|
611
|
+
throw new Error('resourcePrev must be null for dispute open');
|
|
612
|
+
}
|
|
613
|
+
return {
|
|
614
|
+
disputeId,
|
|
615
|
+
orderId,
|
|
616
|
+
type,
|
|
617
|
+
description,
|
|
618
|
+
claimAmount,
|
|
619
|
+
resourcePrev: undefined,
|
|
620
|
+
};
|
|
621
|
+
}
|
|
622
|
+
export function parseMarketDisputeResponsePayload(payload) {
|
|
623
|
+
const disputeId = String(payload.disputeId ?? '');
|
|
624
|
+
requireNonEmpty(disputeId, 'disputeId');
|
|
625
|
+
const resourcePrev = String(payload.resourcePrev ?? '');
|
|
626
|
+
requireNonEmpty(resourcePrev, 'resourcePrev');
|
|
627
|
+
const response = String(payload.response ?? '').trim();
|
|
628
|
+
requireNonEmpty(response, 'response');
|
|
629
|
+
let evidence;
|
|
630
|
+
if (payload.evidence !== undefined) {
|
|
631
|
+
if (!Array.isArray(payload.evidence)) {
|
|
632
|
+
throw new Error('evidence must be an array');
|
|
633
|
+
}
|
|
634
|
+
evidence = payload.evidence.map((entry, index) => assertRecord(entry, `evidence[${index}]`));
|
|
635
|
+
}
|
|
636
|
+
return {
|
|
637
|
+
disputeId,
|
|
638
|
+
resourcePrev,
|
|
639
|
+
response,
|
|
640
|
+
evidence,
|
|
641
|
+
};
|
|
642
|
+
}
|
|
643
|
+
export function parseMarketDisputeResolvePayload(payload) {
|
|
644
|
+
const disputeId = String(payload.disputeId ?? '');
|
|
645
|
+
requireNonEmpty(disputeId, 'disputeId');
|
|
646
|
+
const resourcePrev = String(payload.resourcePrev ?? '');
|
|
647
|
+
requireNonEmpty(resourcePrev, 'resourcePrev');
|
|
648
|
+
const resolution = String(payload.resolution ?? '').trim();
|
|
649
|
+
requireNonEmpty(resolution, 'resolution');
|
|
650
|
+
const notes = typeof payload.notes === 'string' ? payload.notes : undefined;
|
|
651
|
+
return {
|
|
652
|
+
disputeId,
|
|
653
|
+
resourcePrev,
|
|
654
|
+
resolution,
|
|
655
|
+
notes,
|
|
656
|
+
};
|
|
657
|
+
}
|
|
658
|
+
export function parseMarketCapabilityLeaseStartPayload(payload) {
|
|
659
|
+
const listingId = String(payload.listingId ?? '');
|
|
660
|
+
requireNonEmpty(listingId, 'listingId');
|
|
661
|
+
const leaseId = String(payload.leaseId ?? '');
|
|
662
|
+
requireNonEmpty(leaseId, 'leaseId');
|
|
663
|
+
const lessee = String(payload.lessee ?? '');
|
|
664
|
+
assertValidDid(lessee, 'lessee');
|
|
665
|
+
const plan = parseCapabilityLeasePlan(payload.plan);
|
|
666
|
+
const credentials = payload.credentials
|
|
667
|
+
? assertRecord(payload.credentials, 'credentials')
|
|
668
|
+
: undefined;
|
|
669
|
+
const metadata = payload.metadata
|
|
670
|
+
? assertRecord(payload.metadata, 'metadata')
|
|
671
|
+
: undefined;
|
|
672
|
+
const expiresAt = payload.expiresAt;
|
|
673
|
+
if (expiresAt !== undefined && typeof expiresAt !== 'number') {
|
|
674
|
+
throw new Error('expiresAt must be a number');
|
|
675
|
+
}
|
|
676
|
+
if (payload.resourcePrev !== undefined && payload.resourcePrev !== null) {
|
|
677
|
+
throw new Error('resourcePrev must be null for lease start');
|
|
678
|
+
}
|
|
679
|
+
return {
|
|
680
|
+
listingId,
|
|
681
|
+
leaseId,
|
|
682
|
+
lessee,
|
|
683
|
+
plan,
|
|
684
|
+
credentials,
|
|
685
|
+
metadata,
|
|
686
|
+
expiresAt: typeof expiresAt === 'number' ? expiresAt : undefined,
|
|
687
|
+
resourcePrev: undefined,
|
|
688
|
+
};
|
|
689
|
+
}
|
|
690
|
+
export function parseMarketCapabilityLeaseUpdatePayload(payload) {
|
|
691
|
+
const leaseId = String(payload.leaseId ?? '');
|
|
692
|
+
requireNonEmpty(leaseId, 'leaseId');
|
|
693
|
+
const resourcePrev = String(payload.resourcePrev ?? '');
|
|
694
|
+
requireNonEmpty(resourcePrev, 'resourcePrev');
|
|
695
|
+
return {
|
|
696
|
+
leaseId,
|
|
697
|
+
resourcePrev,
|
|
698
|
+
};
|
|
699
|
+
}
|
|
700
|
+
export function parseMarketCapabilityInvokePayload(payload) {
|
|
701
|
+
const leaseId = String(payload.leaseId ?? '');
|
|
702
|
+
requireNonEmpty(leaseId, 'leaseId');
|
|
703
|
+
const resource = String(payload.resource ?? '').trim();
|
|
704
|
+
requireNonEmpty(resource, 'resource');
|
|
705
|
+
const unitsValue = Number(payload.units ?? NaN);
|
|
706
|
+
if (!Number.isFinite(unitsValue) || !Number.isInteger(unitsValue) || unitsValue <= 0) {
|
|
707
|
+
throw new Error('units must be a positive integer');
|
|
708
|
+
}
|
|
709
|
+
const latencyValue = Number(payload.latency ?? NaN);
|
|
710
|
+
if (!Number.isFinite(latencyValue) || latencyValue < 0) {
|
|
711
|
+
throw new Error('latency must be a non-negative number');
|
|
712
|
+
}
|
|
713
|
+
const successValue = payload.success;
|
|
714
|
+
if (typeof successValue !== 'boolean') {
|
|
715
|
+
throw new Error('success must be a boolean');
|
|
716
|
+
}
|
|
717
|
+
let cost;
|
|
718
|
+
if (payload.cost !== undefined) {
|
|
719
|
+
cost = normalizeTokenAmount(payload.cost, 'cost');
|
|
720
|
+
}
|
|
721
|
+
return {
|
|
722
|
+
leaseId,
|
|
723
|
+
resource,
|
|
724
|
+
units: unitsValue,
|
|
725
|
+
latency: latencyValue,
|
|
726
|
+
success: successValue,
|
|
727
|
+
cost,
|
|
728
|
+
};
|
|
729
|
+
}
|
|
730
|
+
export async function createMarketListingPublishEnvelope(params) {
|
|
731
|
+
const sellerDid = params.sellerDid ?? params.issuer;
|
|
732
|
+
assertValidDid(params.issuer, 'issuer');
|
|
733
|
+
if (sellerDid !== params.issuer) {
|
|
734
|
+
throw new Error('issuer must match seller.did');
|
|
735
|
+
}
|
|
736
|
+
const payload = parseMarketListingPublishPayload({
|
|
737
|
+
listingId: params.listingId,
|
|
738
|
+
marketType: params.marketType,
|
|
739
|
+
seller: {
|
|
740
|
+
did: sellerDid,
|
|
741
|
+
name: params.sellerName,
|
|
742
|
+
},
|
|
743
|
+
title: params.title,
|
|
744
|
+
description: params.description,
|
|
745
|
+
category: params.category,
|
|
746
|
+
tags: params.tags,
|
|
747
|
+
pricing: params.pricing,
|
|
748
|
+
visibility: params.visibility,
|
|
749
|
+
marketData: params.marketData,
|
|
750
|
+
restrictions: params.restrictions,
|
|
751
|
+
expiresAt: params.expiresAt,
|
|
752
|
+
metadata: params.metadata,
|
|
753
|
+
status: params.status,
|
|
754
|
+
resourcePrev: params.resourcePrev,
|
|
755
|
+
});
|
|
756
|
+
const publicKey = publicKeyFromDid(params.issuer);
|
|
757
|
+
const baseEnvelope = buildEnvelope('market.listing.publish', params.issuer, publicKey, payload, params.ts, params.nonce, params.prev);
|
|
758
|
+
const hash = eventHashHex(baseEnvelope);
|
|
759
|
+
const sig = await signEvent(baseEnvelope, params.privateKey);
|
|
760
|
+
return { ...baseEnvelope, hash, sig };
|
|
761
|
+
}
|
|
762
|
+
export async function createMarketListingUpdateEnvelope(params) {
|
|
763
|
+
assertValidDid(params.issuer, 'issuer');
|
|
764
|
+
const payload = parseMarketListingUpdatePayload({
|
|
765
|
+
listingId: params.listingId,
|
|
766
|
+
resourcePrev: params.resourcePrev,
|
|
767
|
+
status: params.status,
|
|
768
|
+
metadata: params.metadata,
|
|
769
|
+
});
|
|
770
|
+
const publicKey = publicKeyFromDid(params.issuer);
|
|
771
|
+
const baseEnvelope = buildEnvelope('market.listing.update', params.issuer, publicKey, payload, params.ts, params.nonce, params.prev);
|
|
772
|
+
const hash = eventHashHex(baseEnvelope);
|
|
773
|
+
const sig = await signEvent(baseEnvelope, params.privateKey);
|
|
774
|
+
return { ...baseEnvelope, hash, sig };
|
|
775
|
+
}
|
|
776
|
+
export async function createMarketListingRemoveEnvelope(params) {
|
|
777
|
+
assertValidDid(params.issuer, 'issuer');
|
|
778
|
+
const payload = parseMarketListingRemovePayload({
|
|
779
|
+
listingId: params.listingId,
|
|
780
|
+
resourcePrev: params.resourcePrev,
|
|
781
|
+
});
|
|
782
|
+
const publicKey = publicKeyFromDid(params.issuer);
|
|
783
|
+
const baseEnvelope = buildEnvelope('market.listing.remove', params.issuer, publicKey, payload, params.ts, params.nonce, params.prev);
|
|
784
|
+
const hash = eventHashHex(baseEnvelope);
|
|
785
|
+
const sig = await signEvent(baseEnvelope, params.privateKey);
|
|
786
|
+
return { ...baseEnvelope, hash, sig };
|
|
787
|
+
}
|
|
788
|
+
export async function createMarketOrderCreateEnvelope(params) {
|
|
789
|
+
const buyerDid = params.buyerDid ?? params.issuer;
|
|
790
|
+
assertValidDid(params.issuer, 'issuer');
|
|
791
|
+
if (buyerDid !== params.issuer) {
|
|
792
|
+
throw new Error('issuer must match buyer.did');
|
|
793
|
+
}
|
|
794
|
+
const sellerDid = params.sellerDid;
|
|
795
|
+
const payload = parseMarketOrderCreatePayload({
|
|
796
|
+
orderId: params.orderId,
|
|
797
|
+
listingId: params.listingId,
|
|
798
|
+
marketType: params.marketType,
|
|
799
|
+
buyer: {
|
|
800
|
+
did: buyerDid,
|
|
801
|
+
name: params.buyerName,
|
|
802
|
+
},
|
|
803
|
+
seller: sellerDid
|
|
804
|
+
? {
|
|
805
|
+
did: sellerDid,
|
|
806
|
+
name: params.sellerName,
|
|
807
|
+
}
|
|
808
|
+
: undefined,
|
|
809
|
+
items: params.items,
|
|
810
|
+
pricing: params.pricing,
|
|
811
|
+
payment: {
|
|
812
|
+
status: params.payment?.status ?? 'pending',
|
|
813
|
+
...params.payment,
|
|
814
|
+
},
|
|
815
|
+
delivery: {
|
|
816
|
+
status: params.delivery?.status ?? 'pending',
|
|
817
|
+
...params.delivery,
|
|
818
|
+
},
|
|
819
|
+
status: params.status,
|
|
820
|
+
resourcePrev: params.resourcePrev,
|
|
821
|
+
});
|
|
822
|
+
const publicKey = publicKeyFromDid(params.issuer);
|
|
823
|
+
const baseEnvelope = buildEnvelope('market.order.create', params.issuer, publicKey, payload, params.ts, params.nonce, params.prev);
|
|
824
|
+
const hash = eventHashHex(baseEnvelope);
|
|
825
|
+
const sig = await signEvent(baseEnvelope, params.privateKey);
|
|
826
|
+
return { ...baseEnvelope, hash, sig };
|
|
827
|
+
}
|
|
828
|
+
export async function createMarketOrderUpdateEnvelope(params) {
|
|
829
|
+
assertValidDid(params.issuer, 'issuer');
|
|
830
|
+
const payload = parseMarketOrderUpdatePayload({
|
|
831
|
+
orderId: params.orderId,
|
|
832
|
+
resourcePrev: params.resourcePrev,
|
|
833
|
+
status: params.status,
|
|
834
|
+
payment: params.payment,
|
|
835
|
+
delivery: params.delivery,
|
|
836
|
+
review: params.review,
|
|
837
|
+
});
|
|
838
|
+
const publicKey = publicKeyFromDid(params.issuer);
|
|
839
|
+
const baseEnvelope = buildEnvelope('market.order.update', params.issuer, publicKey, payload, params.ts, params.nonce, params.prev);
|
|
840
|
+
const hash = eventHashHex(baseEnvelope);
|
|
841
|
+
const sig = await signEvent(baseEnvelope, params.privateKey);
|
|
842
|
+
return { ...baseEnvelope, hash, sig };
|
|
843
|
+
}
|
|
844
|
+
export async function createMarketBidSubmitEnvelope(params) {
|
|
845
|
+
const bidderDid = params.bidderDid ?? params.issuer;
|
|
846
|
+
assertValidDid(params.issuer, 'issuer');
|
|
847
|
+
if (bidderDid !== params.issuer) {
|
|
848
|
+
throw new Error('issuer must match bidder.did');
|
|
849
|
+
}
|
|
850
|
+
const payload = parseMarketBidSubmitPayload({
|
|
851
|
+
bidId: params.bidId,
|
|
852
|
+
taskId: params.taskId,
|
|
853
|
+
bidder: {
|
|
854
|
+
did: bidderDid,
|
|
855
|
+
name: params.bidderName,
|
|
856
|
+
},
|
|
857
|
+
proposal: params.proposal,
|
|
858
|
+
resourcePrev: params.resourcePrev,
|
|
859
|
+
});
|
|
860
|
+
const publicKey = publicKeyFromDid(params.issuer);
|
|
861
|
+
const baseEnvelope = buildEnvelope('market.bid.submit', params.issuer, publicKey, payload, params.ts, params.nonce, params.prev);
|
|
862
|
+
const hash = eventHashHex(baseEnvelope);
|
|
863
|
+
const sig = await signEvent(baseEnvelope, params.privateKey);
|
|
864
|
+
return { ...baseEnvelope, hash, sig };
|
|
865
|
+
}
|
|
866
|
+
export async function createMarketBidAcceptEnvelope(params) {
|
|
867
|
+
assertValidDid(params.issuer, 'issuer');
|
|
868
|
+
const payload = parseMarketBidUpdatePayload({
|
|
869
|
+
bidId: params.bidId,
|
|
870
|
+
resourcePrev: params.resourcePrev,
|
|
871
|
+
});
|
|
872
|
+
const publicKey = publicKeyFromDid(params.issuer);
|
|
873
|
+
const baseEnvelope = buildEnvelope('market.bid.accept', params.issuer, publicKey, payload, params.ts, params.nonce, params.prev);
|
|
874
|
+
const hash = eventHashHex(baseEnvelope);
|
|
875
|
+
const sig = await signEvent(baseEnvelope, params.privateKey);
|
|
876
|
+
return { ...baseEnvelope, hash, sig };
|
|
877
|
+
}
|
|
878
|
+
export async function createMarketBidRejectEnvelope(params) {
|
|
879
|
+
assertValidDid(params.issuer, 'issuer');
|
|
880
|
+
const payload = parseMarketBidUpdatePayload({
|
|
881
|
+
bidId: params.bidId,
|
|
882
|
+
resourcePrev: params.resourcePrev,
|
|
883
|
+
});
|
|
884
|
+
const publicKey = publicKeyFromDid(params.issuer);
|
|
885
|
+
const baseEnvelope = buildEnvelope('market.bid.reject', params.issuer, publicKey, payload, params.ts, params.nonce, params.prev);
|
|
886
|
+
const hash = eventHashHex(baseEnvelope);
|
|
887
|
+
const sig = await signEvent(baseEnvelope, params.privateKey);
|
|
888
|
+
return { ...baseEnvelope, hash, sig };
|
|
889
|
+
}
|
|
890
|
+
export async function createMarketBidWithdrawEnvelope(params) {
|
|
891
|
+
assertValidDid(params.issuer, 'issuer');
|
|
892
|
+
const payload = parseMarketBidUpdatePayload({
|
|
893
|
+
bidId: params.bidId,
|
|
894
|
+
resourcePrev: params.resourcePrev,
|
|
895
|
+
});
|
|
896
|
+
const publicKey = publicKeyFromDid(params.issuer);
|
|
897
|
+
const baseEnvelope = buildEnvelope('market.bid.withdraw', params.issuer, publicKey, payload, params.ts, params.nonce, params.prev);
|
|
898
|
+
const hash = eventHashHex(baseEnvelope);
|
|
899
|
+
const sig = await signEvent(baseEnvelope, params.privateKey);
|
|
900
|
+
return { ...baseEnvelope, hash, sig };
|
|
901
|
+
}
|
|
902
|
+
export async function createMarketSubmissionSubmitEnvelope(params) {
|
|
903
|
+
const workerDid = params.workerDid ?? params.issuer;
|
|
904
|
+
assertValidDid(params.issuer, 'issuer');
|
|
905
|
+
if (workerDid !== params.issuer) {
|
|
906
|
+
throw new Error('issuer must match worker');
|
|
907
|
+
}
|
|
908
|
+
const payload = parseMarketSubmissionSubmitPayload({
|
|
909
|
+
orderId: params.orderId,
|
|
910
|
+
submissionId: params.submissionId,
|
|
911
|
+
worker: workerDid,
|
|
912
|
+
deliverables: params.deliverables,
|
|
913
|
+
notes: params.notes,
|
|
914
|
+
resourcePrev: params.resourcePrev,
|
|
915
|
+
});
|
|
916
|
+
const publicKey = publicKeyFromDid(params.issuer);
|
|
917
|
+
const baseEnvelope = buildEnvelope('market.submission.submit', params.issuer, publicKey, payload, params.ts, params.nonce, params.prev);
|
|
918
|
+
const hash = eventHashHex(baseEnvelope);
|
|
919
|
+
const sig = await signEvent(baseEnvelope, params.privateKey);
|
|
920
|
+
return { ...baseEnvelope, hash, sig };
|
|
921
|
+
}
|
|
922
|
+
export async function createMarketSubmissionReviewEnvelope(params) {
|
|
923
|
+
assertValidDid(params.issuer, 'issuer');
|
|
924
|
+
const payload = parseMarketSubmissionReviewPayload({
|
|
925
|
+
submissionId: params.submissionId,
|
|
926
|
+
resourcePrev: params.resourcePrev,
|
|
927
|
+
approved: params.approved,
|
|
928
|
+
feedback: params.feedback,
|
|
929
|
+
rating: params.rating,
|
|
930
|
+
revisionDeadline: params.revisionDeadline,
|
|
931
|
+
});
|
|
932
|
+
const publicKey = publicKeyFromDid(params.issuer);
|
|
933
|
+
const baseEnvelope = buildEnvelope('market.submission.review', params.issuer, publicKey, payload, params.ts, params.nonce, params.prev);
|
|
934
|
+
const hash = eventHashHex(baseEnvelope);
|
|
935
|
+
const sig = await signEvent(baseEnvelope, params.privateKey);
|
|
936
|
+
return { ...baseEnvelope, hash, sig };
|
|
937
|
+
}
|
|
938
|
+
export async function createMarketSubscriptionStartEnvelope(params) {
|
|
939
|
+
const buyerDid = params.buyerDid ?? params.issuer;
|
|
940
|
+
assertValidDid(params.issuer, 'issuer');
|
|
941
|
+
if (buyerDid !== params.issuer) {
|
|
942
|
+
throw new Error('issuer must match buyer.did');
|
|
943
|
+
}
|
|
944
|
+
const payload = parseMarketSubscriptionStartPayload({
|
|
945
|
+
subscriptionId: params.subscriptionId,
|
|
946
|
+
listingId: params.listingId,
|
|
947
|
+
buyer: {
|
|
948
|
+
did: buyerDid,
|
|
949
|
+
name: params.buyerName,
|
|
950
|
+
},
|
|
951
|
+
resourcePrev: params.resourcePrev,
|
|
952
|
+
});
|
|
953
|
+
const publicKey = publicKeyFromDid(params.issuer);
|
|
954
|
+
const baseEnvelope = buildEnvelope('market.subscription.start', params.issuer, publicKey, payload, params.ts, params.nonce, params.prev);
|
|
955
|
+
const hash = eventHashHex(baseEnvelope);
|
|
956
|
+
const sig = await signEvent(baseEnvelope, params.privateKey);
|
|
957
|
+
return { ...baseEnvelope, hash, sig };
|
|
958
|
+
}
|
|
959
|
+
export async function createMarketSubscriptionCancelEnvelope(params) {
|
|
960
|
+
assertValidDid(params.issuer, 'issuer');
|
|
961
|
+
const payload = parseMarketSubscriptionCancelPayload({
|
|
962
|
+
subscriptionId: params.subscriptionId,
|
|
963
|
+
resourcePrev: params.resourcePrev,
|
|
964
|
+
});
|
|
965
|
+
const publicKey = publicKeyFromDid(params.issuer);
|
|
966
|
+
const baseEnvelope = buildEnvelope('market.subscription.cancel', params.issuer, publicKey, payload, params.ts, params.nonce, params.prev);
|
|
967
|
+
const hash = eventHashHex(baseEnvelope);
|
|
968
|
+
const sig = await signEvent(baseEnvelope, params.privateKey);
|
|
969
|
+
return { ...baseEnvelope, hash, sig };
|
|
970
|
+
}
|
|
971
|
+
export async function createMarketDisputeOpenEnvelope(params) {
|
|
972
|
+
assertValidDid(params.issuer, 'issuer');
|
|
973
|
+
const payload = parseMarketDisputeOpenPayload({
|
|
974
|
+
disputeId: params.disputeId,
|
|
975
|
+
orderId: params.orderId,
|
|
976
|
+
type: params.type,
|
|
977
|
+
description: params.description,
|
|
978
|
+
claimAmount: params.claimAmount,
|
|
979
|
+
resourcePrev: params.resourcePrev,
|
|
980
|
+
});
|
|
981
|
+
const publicKey = publicKeyFromDid(params.issuer);
|
|
982
|
+
const baseEnvelope = buildEnvelope('market.dispute.open', params.issuer, publicKey, payload, params.ts, params.nonce, params.prev);
|
|
983
|
+
const hash = eventHashHex(baseEnvelope);
|
|
984
|
+
const sig = await signEvent(baseEnvelope, params.privateKey);
|
|
985
|
+
return { ...baseEnvelope, hash, sig };
|
|
986
|
+
}
|
|
987
|
+
export async function createMarketDisputeResponseEnvelope(params) {
|
|
988
|
+
assertValidDid(params.issuer, 'issuer');
|
|
989
|
+
const payload = parseMarketDisputeResponsePayload({
|
|
990
|
+
disputeId: params.disputeId,
|
|
991
|
+
resourcePrev: params.resourcePrev,
|
|
992
|
+
response: params.response,
|
|
993
|
+
evidence: params.evidence,
|
|
994
|
+
});
|
|
995
|
+
const publicKey = publicKeyFromDid(params.issuer);
|
|
996
|
+
const baseEnvelope = buildEnvelope('market.dispute.response', params.issuer, publicKey, payload, params.ts, params.nonce, params.prev);
|
|
997
|
+
const hash = eventHashHex(baseEnvelope);
|
|
998
|
+
const sig = await signEvent(baseEnvelope, params.privateKey);
|
|
999
|
+
return { ...baseEnvelope, hash, sig };
|
|
1000
|
+
}
|
|
1001
|
+
export async function createMarketDisputeResolveEnvelope(params) {
|
|
1002
|
+
assertValidDid(params.issuer, 'issuer');
|
|
1003
|
+
const payload = parseMarketDisputeResolvePayload({
|
|
1004
|
+
disputeId: params.disputeId,
|
|
1005
|
+
resourcePrev: params.resourcePrev,
|
|
1006
|
+
resolution: params.resolution,
|
|
1007
|
+
notes: params.notes,
|
|
1008
|
+
});
|
|
1009
|
+
const publicKey = publicKeyFromDid(params.issuer);
|
|
1010
|
+
const baseEnvelope = buildEnvelope('market.dispute.resolve', params.issuer, publicKey, payload, params.ts, params.nonce, params.prev);
|
|
1011
|
+
const hash = eventHashHex(baseEnvelope);
|
|
1012
|
+
const sig = await signEvent(baseEnvelope, params.privateKey);
|
|
1013
|
+
return { ...baseEnvelope, hash, sig };
|
|
1014
|
+
}
|
|
1015
|
+
export async function createMarketCapabilityLeaseStartEnvelope(params) {
|
|
1016
|
+
const lessee = params.lessee ?? params.issuer;
|
|
1017
|
+
assertValidDid(params.issuer, 'issuer');
|
|
1018
|
+
if (lessee !== params.issuer) {
|
|
1019
|
+
throw new Error('issuer must match lessee');
|
|
1020
|
+
}
|
|
1021
|
+
const payload = parseMarketCapabilityLeaseStartPayload({
|
|
1022
|
+
listingId: params.listingId,
|
|
1023
|
+
leaseId: params.leaseId,
|
|
1024
|
+
lessee,
|
|
1025
|
+
plan: params.plan,
|
|
1026
|
+
credentials: params.credentials,
|
|
1027
|
+
metadata: params.metadata,
|
|
1028
|
+
expiresAt: params.expiresAt,
|
|
1029
|
+
resourcePrev: params.resourcePrev,
|
|
1030
|
+
});
|
|
1031
|
+
const publicKey = publicKeyFromDid(params.issuer);
|
|
1032
|
+
const baseEnvelope = buildEnvelope('market.capability.lease.start', params.issuer, publicKey, payload, params.ts, params.nonce, params.prev);
|
|
1033
|
+
const hash = eventHashHex(baseEnvelope);
|
|
1034
|
+
const sig = await signEvent(baseEnvelope, params.privateKey);
|
|
1035
|
+
return { ...baseEnvelope, hash, sig };
|
|
1036
|
+
}
|
|
1037
|
+
async function createMarketCapabilityLeaseUpdateEnvelope(type, params) {
|
|
1038
|
+
assertValidDid(params.issuer, 'issuer');
|
|
1039
|
+
const payload = parseMarketCapabilityLeaseUpdatePayload({
|
|
1040
|
+
leaseId: params.leaseId,
|
|
1041
|
+
resourcePrev: params.resourcePrev,
|
|
1042
|
+
});
|
|
1043
|
+
const publicKey = publicKeyFromDid(params.issuer);
|
|
1044
|
+
const baseEnvelope = buildEnvelope(type, params.issuer, publicKey, payload, params.ts, params.nonce, params.prev);
|
|
1045
|
+
const hash = eventHashHex(baseEnvelope);
|
|
1046
|
+
const sig = await signEvent(baseEnvelope, params.privateKey);
|
|
1047
|
+
return { ...baseEnvelope, hash, sig };
|
|
1048
|
+
}
|
|
1049
|
+
export async function createMarketCapabilityLeasePauseEnvelope(params) {
|
|
1050
|
+
return createMarketCapabilityLeaseUpdateEnvelope('market.capability.lease.pause', params);
|
|
1051
|
+
}
|
|
1052
|
+
export async function createMarketCapabilityLeaseResumeEnvelope(params) {
|
|
1053
|
+
return createMarketCapabilityLeaseUpdateEnvelope('market.capability.lease.resume', params);
|
|
1054
|
+
}
|
|
1055
|
+
export async function createMarketCapabilityLeaseTerminateEnvelope(params) {
|
|
1056
|
+
return createMarketCapabilityLeaseUpdateEnvelope('market.capability.lease.terminate', params);
|
|
1057
|
+
}
|
|
1058
|
+
export async function createMarketCapabilityInvokeEnvelope(params) {
|
|
1059
|
+
assertValidDid(params.issuer, 'issuer');
|
|
1060
|
+
const payload = parseMarketCapabilityInvokePayload({
|
|
1061
|
+
leaseId: params.leaseId,
|
|
1062
|
+
resource: params.resource,
|
|
1063
|
+
units: params.units,
|
|
1064
|
+
latency: params.latency,
|
|
1065
|
+
success: params.success,
|
|
1066
|
+
cost: params.cost,
|
|
1067
|
+
});
|
|
1068
|
+
const publicKey = publicKeyFromDid(params.issuer);
|
|
1069
|
+
const baseEnvelope = buildEnvelope('market.capability.invoke', params.issuer, publicKey, payload, params.ts, params.nonce, params.prev);
|
|
1070
|
+
const hash = eventHashHex(baseEnvelope);
|
|
1071
|
+
const sig = await signEvent(baseEnvelope, params.privateKey);
|
|
1072
|
+
return { ...baseEnvelope, hash, sig };
|
|
1073
|
+
}
|
|
1074
|
+
//# sourceMappingURL=events.js.map
|