@ocap/message 1.30.3 → 1.30.4
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/esm/canonical-cbor.d.mts +6 -0
- package/esm/canonical-cbor.mjs +401 -0
- package/esm/cbor.d.mts +8 -0
- package/esm/cbor.mjs +13 -0
- package/esm/{package.mjs → core/message/package.mjs} +12 -1
- package/esm/index.mjs +2 -6
- package/esm/message.mjs +30 -5
- package/esm/node_modules/cborg/cborg.mjs +6 -0
- package/esm/node_modules/cborg/lib/0uint.mjs +204 -0
- package/esm/node_modules/cborg/lib/1negint.mjs +91 -0
- package/esm/node_modules/cborg/lib/2bytes.mjs +120 -0
- package/esm/node_modules/cborg/lib/3string.mjs +103 -0
- package/esm/node_modules/cborg/lib/4array.mjs +100 -0
- package/esm/node_modules/cborg/lib/5map.mjs +100 -0
- package/esm/node_modules/cborg/lib/6tag.mjs +76 -0
- package/esm/node_modules/cborg/lib/7float.mjs +224 -0
- package/esm/node_modules/cborg/lib/bl.mjs +131 -0
- package/esm/node_modules/cborg/lib/byte-utils.mjs +104 -0
- package/esm/node_modules/cborg/lib/common.mjs +20 -0
- package/esm/node_modules/cborg/lib/decode.mjs +210 -0
- package/esm/node_modules/cborg/lib/encode.mjs +388 -0
- package/esm/node_modules/cborg/lib/is.mjs +59 -0
- package/esm/node_modules/cborg/lib/jump.mjs +154 -0
- package/esm/node_modules/cborg/lib/tagged.mjs +70 -0
- package/esm/node_modules/cborg/lib/token.mjs +73 -0
- package/esm/patch.mjs +1 -1
- package/esm/protobuf.d.mts +6 -0
- package/esm/protobuf.mjs +17 -0
- package/esm/provider-init-schema.d.mts +1 -0
- package/esm/provider-init-schema.mjs +22 -0
- package/esm/provider-init.d.mts +1 -0
- package/esm/provider-init.mjs +18 -0
- package/lib/canonical-cbor.cjs +404 -0
- package/lib/canonical-cbor.d.cts +6 -0
- package/lib/cbor.cjs +17 -0
- package/lib/cbor.d.cts +8 -0
- package/lib/{package.cjs → core/message/package.cjs} +12 -1
- package/lib/index.cjs +2 -8
- package/lib/message.cjs +30 -5
- package/lib/node_modules/cborg/cborg.cjs +4 -0
- package/lib/node_modules/cborg/lib/0uint.cjs +214 -0
- package/lib/node_modules/cborg/lib/1negint.cjs +95 -0
- package/lib/node_modules/cborg/lib/2bytes.cjs +125 -0
- package/lib/node_modules/cborg/lib/3string.cjs +108 -0
- package/lib/node_modules/cborg/lib/4array.cjs +106 -0
- package/lib/node_modules/cborg/lib/5map.cjs +106 -0
- package/lib/node_modules/cborg/lib/6tag.cjs +81 -0
- package/lib/node_modules/cborg/lib/7float.cjs +233 -0
- package/lib/node_modules/cborg/lib/bl.cjs +132 -0
- package/lib/node_modules/cborg/lib/byte-utils.cjs +111 -0
- package/lib/node_modules/cborg/lib/common.cjs +23 -0
- package/lib/node_modules/cborg/lib/decode.cjs +213 -0
- package/lib/node_modules/cborg/lib/encode.cjs +390 -0
- package/lib/node_modules/cborg/lib/is.cjs +60 -0
- package/lib/node_modules/cborg/lib/jump.cjs +156 -0
- package/lib/node_modules/cborg/lib/tagged.cjs +71 -0
- package/lib/node_modules/cborg/lib/token.cjs +75 -0
- package/lib/patch.cjs +1 -1
- package/lib/protobuf.cjs +18 -0
- package/lib/protobuf.d.cts +6 -0
- package/lib/provider-init-schema.cjs +23 -0
- package/lib/provider-init-schema.d.cts +1 -0
- package/lib/provider-init.cjs +19 -0
- package/lib/provider-init.d.cts +1 -0
- package/package.json +14 -3
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_encode = require('./node_modules/cborg/lib/encode.cjs');
|
|
3
|
+
const require_decode = require('./node_modules/cborg/lib/decode.cjs');
|
|
4
|
+
const require_tagged = require('./node_modules/cborg/lib/tagged.cjs');
|
|
5
|
+
require('./node_modules/cborg/cborg.cjs');
|
|
6
|
+
const require_provider = require('./provider.cjs');
|
|
7
|
+
let _ocap_util = require("@ocap/util");
|
|
8
|
+
|
|
9
|
+
//#region src/canonical-cbor.ts
|
|
10
|
+
/**
|
|
11
|
+
* Canonical CBOR serializer for OCAP transactions.
|
|
12
|
+
*
|
|
13
|
+
* Implements RFC 8949 §4.2.1 Core Deterministic Encoding plus the 17 OCAP-specific rules
|
|
14
|
+
* documented in planning/43-transaction-cbor-encoding/README.md §3.2.
|
|
15
|
+
*
|
|
16
|
+
* Key properties:
|
|
17
|
+
* - proto field number is the CBOR map key (integer, sorted ascending)
|
|
18
|
+
* - default/undefined values are omitted (protobuf3 semantics + JS undefined fold)
|
|
19
|
+
* - `google.protobuf.Any` (itx) is expanded into a nested map with key 0 = typeUrl
|
|
20
|
+
* - BigUint / BigSint use CBOR tag 2 / 3 (bignum); zero-valued wrappers are folded to absent
|
|
21
|
+
* - Top-level tag 55799 (self-describe) is included in the hash input
|
|
22
|
+
*/
|
|
23
|
+
const TAG_SELF_DESCRIBE = 55799;
|
|
24
|
+
const TAG_POSITIVE_BIGNUM = 2;
|
|
25
|
+
const TAG_NEGATIVE_BIGNUM = 3;
|
|
26
|
+
const SELF_DESCRIBE_PREFIX = new Uint8Array([
|
|
27
|
+
217,
|
|
28
|
+
217,
|
|
29
|
+
247
|
|
30
|
+
]);
|
|
31
|
+
const SCALAR_INT_TYPES = new Set([
|
|
32
|
+
"int32",
|
|
33
|
+
"sint32",
|
|
34
|
+
"uint32",
|
|
35
|
+
"sfixed32",
|
|
36
|
+
"fixed32",
|
|
37
|
+
"int64",
|
|
38
|
+
"sint64",
|
|
39
|
+
"uint64",
|
|
40
|
+
"sfixed64",
|
|
41
|
+
"fixed64"
|
|
42
|
+
]);
|
|
43
|
+
const SCALAR_FLOAT_TYPES = new Set(["double", "float"]);
|
|
44
|
+
const BIGINT_WRAPPER_TYPES = new Set(["BigUint", "BigSint"]);
|
|
45
|
+
const TIMESTAMP_TYPES = new Set(["google.protobuf.Timestamp", "Timestamp"]);
|
|
46
|
+
const ANY_TYPES = new Set(["google.protobuf.Any", "Any"]);
|
|
47
|
+
function getFields(type) {
|
|
48
|
+
const desc = require_provider.getMessageType(type);
|
|
49
|
+
if (!desc || !desc.fields) throw new Error(`canonical-cbor: unknown message type "${type}"`);
|
|
50
|
+
return desc.fields;
|
|
51
|
+
}
|
|
52
|
+
function isEnumType(type) {
|
|
53
|
+
return Object.prototype.hasOwnProperty.call(require_provider.enums, type);
|
|
54
|
+
}
|
|
55
|
+
function stripBignumLeadingZeros(bytes) {
|
|
56
|
+
let start = 0;
|
|
57
|
+
while (start < bytes.length - 1 && bytes[start] === 0) start += 1;
|
|
58
|
+
return start === 0 ? bytes : bytes.slice(start);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Normalize a BigUint/BigSint value into canonical bignum bytes + sign.
|
|
62
|
+
* Accepts: BigInt, number, decimal string, hex bytes, BN-compatible object, or
|
|
63
|
+
* the wrapped `{ value: Uint8Array, minus?: boolean }` shape.
|
|
64
|
+
*
|
|
65
|
+
* Returns null when the value is default/zero (→ omit from CBOR output, README §6.4).
|
|
66
|
+
*/
|
|
67
|
+
function normalizeBigIntWrapper(value, type) {
|
|
68
|
+
if (value === void 0 || value === null) return null;
|
|
69
|
+
if (typeof value === "object" && value !== null && "value" in value) {
|
|
70
|
+
const wrapped = value;
|
|
71
|
+
if (wrapped.value === void 0 || wrapped.value === null) return null;
|
|
72
|
+
const rawBytes = (0, _ocap_util.toUint8Array)(wrapped.value);
|
|
73
|
+
if (!rawBytes || rawBytes.length === 0) return null;
|
|
74
|
+
const trimmed = stripBignumLeadingZeros(rawBytes);
|
|
75
|
+
if (trimmed.length === 1 && trimmed[0] === 0) return null;
|
|
76
|
+
return {
|
|
77
|
+
bytes: trimmed,
|
|
78
|
+
negative: type === "BigSint" && wrapped.minus === true
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
if (typeof value === "bigint") {
|
|
82
|
+
if (value === 0n) return null;
|
|
83
|
+
const negative = value < 0n;
|
|
84
|
+
const hex = (negative ? -value : value).toString(16);
|
|
85
|
+
const padded = hex.length % 2 === 0 ? hex : `0${hex}`;
|
|
86
|
+
const bytes = new Uint8Array(padded.length / 2);
|
|
87
|
+
for (let i = 0; i < bytes.length; i += 1) bytes[i] = Number.parseInt(padded.slice(i * 2, i * 2 + 2), 16);
|
|
88
|
+
return {
|
|
89
|
+
bytes,
|
|
90
|
+
negative: negative && type === "BigSint"
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
const bn = (0, _ocap_util.toBN)(value);
|
|
94
|
+
if (bn.isZero()) return null;
|
|
95
|
+
return {
|
|
96
|
+
bytes: stripBignumLeadingZeros(new Uint8Array(bn.toArray())),
|
|
97
|
+
negative: bn.isNeg() && type === "BigSint"
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
function encodeScalarInteger(value) {
|
|
101
|
+
if (typeof value === "bigint") return value;
|
|
102
|
+
if (typeof value === "number") {
|
|
103
|
+
if (!Number.isInteger(value)) throw new Error(`canonical-cbor: non-integer value for integer field: ${value}`);
|
|
104
|
+
return value;
|
|
105
|
+
}
|
|
106
|
+
if (typeof value === "string") {
|
|
107
|
+
if (value === "") return 0n;
|
|
108
|
+
return BigInt(value);
|
|
109
|
+
}
|
|
110
|
+
throw new Error(`canonical-cbor: cannot encode integer value of type ${typeof value}`);
|
|
111
|
+
}
|
|
112
|
+
function isDefaultScalar(value, type) {
|
|
113
|
+
if (value === void 0 || value === null) return true;
|
|
114
|
+
if (SCALAR_INT_TYPES.has(type)) {
|
|
115
|
+
if (typeof value === "number") return value === 0;
|
|
116
|
+
if (typeof value === "bigint") return value === 0n;
|
|
117
|
+
if (typeof value === "string") return value === "" || value === "0";
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
if (SCALAR_FLOAT_TYPES.has(type)) return value === 0;
|
|
121
|
+
if (type === "bool") return value === false;
|
|
122
|
+
if (type === "string") return value === "";
|
|
123
|
+
if (type === "bytes") {
|
|
124
|
+
if (typeof value === "string") return value.length === 0;
|
|
125
|
+
const bytes = (0, _ocap_util.toUint8Array)(value);
|
|
126
|
+
return !bytes || bytes.length === 0;
|
|
127
|
+
}
|
|
128
|
+
if (isEnumType(type)) return value === 0 || value === "" || value === void 0;
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
function encodeTimestampValue(value) {
|
|
132
|
+
if (value === void 0 || value === null || value === "") return "";
|
|
133
|
+
if (typeof value === "string") {
|
|
134
|
+
const ms = Date.parse(value);
|
|
135
|
+
if (Number.isNaN(ms)) throw new Error("canonical-cbor: invalid timestamp string (unparseable)");
|
|
136
|
+
return new Date(Math.trunc(ms)).toISOString();
|
|
137
|
+
}
|
|
138
|
+
if (typeof value === "object") {
|
|
139
|
+
const { seconds = 0, nanos = 0 } = value;
|
|
140
|
+
const ms = (typeof seconds === "string" ? Number.parseInt(seconds, 10) : seconds) * 1e3 + Math.trunc((nanos || 0) / 1e6);
|
|
141
|
+
return new Date(ms).toISOString();
|
|
142
|
+
}
|
|
143
|
+
throw new Error(`canonical-cbor: unsupported timestamp input type ${typeof value}`);
|
|
144
|
+
}
|
|
145
|
+
function resolveAnyTypeUrl(value) {
|
|
146
|
+
const typeUrl = value.typeUrl || value.type_url || require_provider.toTypeUrl(value.type);
|
|
147
|
+
if (!typeUrl) throw new Error("canonical-cbor: Any value missing typeUrl/type");
|
|
148
|
+
const type = require_provider.fromTypeUrl(typeUrl);
|
|
149
|
+
const wasUnwrapped = !value.typeUrl && !value.type_url && typeof value.type === "string";
|
|
150
|
+
return {
|
|
151
|
+
typeUrl,
|
|
152
|
+
type,
|
|
153
|
+
inner: wasUnwrapped ? value.value : value,
|
|
154
|
+
wasUnwrapped
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
function normalizeOpaquePayload(value) {
|
|
158
|
+
if (value === null || value === void 0) return value;
|
|
159
|
+
if (value instanceof Date) return value.toISOString();
|
|
160
|
+
if (value instanceof Uint8Array) return value;
|
|
161
|
+
if (Array.isArray(value)) return value.map(normalizeOpaquePayload);
|
|
162
|
+
if (typeof value === "object") {
|
|
163
|
+
const out = {};
|
|
164
|
+
for (const [k, v] of Object.entries(value)) {
|
|
165
|
+
if (v === void 0) continue;
|
|
166
|
+
out[k] = normalizeOpaquePayload(v);
|
|
167
|
+
}
|
|
168
|
+
return out;
|
|
169
|
+
}
|
|
170
|
+
return value;
|
|
171
|
+
}
|
|
172
|
+
function encodeAnyValue(value) {
|
|
173
|
+
const { typeUrl, type, inner, wasUnwrapped } = resolveAnyTypeUrl(value);
|
|
174
|
+
const map = /* @__PURE__ */ new Map();
|
|
175
|
+
map.set(0, typeUrl);
|
|
176
|
+
if (typeUrl === "json" || typeUrl === "vc" || typeUrl === "fg:x:address") {
|
|
177
|
+
const payload = wasUnwrapped ? inner : value.value ?? inner;
|
|
178
|
+
map.set(1, normalizeOpaquePayload(payload));
|
|
179
|
+
return map;
|
|
180
|
+
}
|
|
181
|
+
let innerObj = {};
|
|
182
|
+
if (inner && typeof inner === "object") {
|
|
183
|
+
innerObj = { ...inner };
|
|
184
|
+
if (!wasUnwrapped) {
|
|
185
|
+
delete innerObj.typeUrl;
|
|
186
|
+
delete innerObj.type_url;
|
|
187
|
+
if (typeof innerObj.type === "string") {
|
|
188
|
+
let innerSchemaFields;
|
|
189
|
+
try {
|
|
190
|
+
innerSchemaFields = getFields(type);
|
|
191
|
+
} catch {
|
|
192
|
+
innerSchemaFields = null;
|
|
193
|
+
}
|
|
194
|
+
if (!innerSchemaFields || !("type" in innerSchemaFields)) delete innerObj.type;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
try {
|
|
199
|
+
const encoded = encodeMessageFields(type, innerObj);
|
|
200
|
+
for (const [id, val] of encoded.entries()) map.set(id, val);
|
|
201
|
+
} catch (err) {
|
|
202
|
+
if (err instanceof Error && err.message.startsWith("canonical-cbor: unknown message type")) {
|
|
203
|
+
if ("value" in value) {
|
|
204
|
+
map.set(1, value.value);
|
|
205
|
+
return map;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
throw err;
|
|
209
|
+
}
|
|
210
|
+
return map;
|
|
211
|
+
}
|
|
212
|
+
function encodeFieldValue(value, field) {
|
|
213
|
+
const type = field.type;
|
|
214
|
+
if (field.keyType !== void 0) throw new Error(`canonical-cbor: map<${field.keyType},${type}> fields are not supported yet`);
|
|
215
|
+
if (field.rule === "repeated") {
|
|
216
|
+
if (!Array.isArray(value)) throw new Error(`canonical-cbor: repeated field expects array, got ${typeof value}`);
|
|
217
|
+
const itemField = {
|
|
218
|
+
id: field.id,
|
|
219
|
+
type
|
|
220
|
+
};
|
|
221
|
+
return value.map((item) => encodeFieldValue(item, itemField));
|
|
222
|
+
}
|
|
223
|
+
if (BIGINT_WRAPPER_TYPES.has(type)) {
|
|
224
|
+
const parsed = normalizeBigIntWrapper(value, type);
|
|
225
|
+
if (!parsed) return void 0;
|
|
226
|
+
return new require_tagged.Tagged(parsed.negative ? TAG_NEGATIVE_BIGNUM : TAG_POSITIVE_BIGNUM, parsed.bytes);
|
|
227
|
+
}
|
|
228
|
+
if (TIMESTAMP_TYPES.has(type)) return encodeTimestampValue(value);
|
|
229
|
+
if (ANY_TYPES.has(type)) {
|
|
230
|
+
if (value === void 0 || value === null) return void 0;
|
|
231
|
+
return encodeAnyValue(value);
|
|
232
|
+
}
|
|
233
|
+
if (isEnumType(type)) {
|
|
234
|
+
if (typeof value === "string") {
|
|
235
|
+
const numeric = require_provider.enums[type]?.[value];
|
|
236
|
+
if (numeric === void 0) throw new Error(`canonical-cbor: unknown enum value "${value}" for ${type}`);
|
|
237
|
+
return Number(numeric);
|
|
238
|
+
}
|
|
239
|
+
return Number(value);
|
|
240
|
+
}
|
|
241
|
+
if (SCALAR_INT_TYPES.has(type)) return encodeScalarInteger(value);
|
|
242
|
+
if (SCALAR_FLOAT_TYPES.has(type)) return Number(value);
|
|
243
|
+
if (type === "bool") return Boolean(value);
|
|
244
|
+
if (type === "string") return String(value);
|
|
245
|
+
if (type === "bytes") {
|
|
246
|
+
if (value instanceof Uint8Array) return value;
|
|
247
|
+
return (0, _ocap_util.toUint8Array)(value) ?? new Uint8Array();
|
|
248
|
+
}
|
|
249
|
+
if (typeof value !== "object" || value === null) throw new Error(`canonical-cbor: expected object for nested ${type}, got ${typeof value}`);
|
|
250
|
+
return encodeMessageFields(type, value);
|
|
251
|
+
}
|
|
252
|
+
function encodeMessageFields(type, data) {
|
|
253
|
+
if (BIGINT_WRAPPER_TYPES.has(type)) {
|
|
254
|
+
const parsed = normalizeBigIntWrapper(data, type);
|
|
255
|
+
const out$1 = /* @__PURE__ */ new Map();
|
|
256
|
+
if (!parsed) return out$1;
|
|
257
|
+
const tag = parsed.negative ? TAG_NEGATIVE_BIGNUM : TAG_POSITIVE_BIGNUM;
|
|
258
|
+
out$1.set(1, new require_tagged.Tagged(tag, parsed.bytes));
|
|
259
|
+
if (type === "BigSint" && parsed.negative) out$1.set(2, true);
|
|
260
|
+
return out$1;
|
|
261
|
+
}
|
|
262
|
+
const fields = getFields(type);
|
|
263
|
+
const out = /* @__PURE__ */ new Map();
|
|
264
|
+
for (const [name, spec] of Object.entries(fields)) {
|
|
265
|
+
const aliasName = spec.rule === "repeated" ? `${name}List` : spec.keyType ? `${name}Map` : null;
|
|
266
|
+
const hasCanonical = name in data;
|
|
267
|
+
const hasAlias = aliasName !== null && aliasName in data;
|
|
268
|
+
if (!hasCanonical && !hasAlias) continue;
|
|
269
|
+
const value = hasCanonical ? data[name] : data[aliasName];
|
|
270
|
+
if (value === void 0) continue;
|
|
271
|
+
if (spec.rule === "repeated") {
|
|
272
|
+
if (value === null) continue;
|
|
273
|
+
if (!Array.isArray(value)) throw new Error(`canonical-cbor: repeated field "${name}" expects array, got ${typeof value}`);
|
|
274
|
+
if (value.length === 0) continue;
|
|
275
|
+
out.set(spec.id, encodeFieldValue(value, spec));
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
if (isDefaultScalar(value, spec.type)) continue;
|
|
279
|
+
const encoded = encodeFieldValue(value, spec);
|
|
280
|
+
if (encoded === void 0) continue;
|
|
281
|
+
out.set(spec.id, encoded);
|
|
282
|
+
}
|
|
283
|
+
return out;
|
|
284
|
+
}
|
|
285
|
+
function canonicalBytes(type, data) {
|
|
286
|
+
if (!data || typeof data !== "object") throw new Error("canonical-cbor: data must be an object");
|
|
287
|
+
return require_encode.encode(new require_tagged.Tagged(TAG_SELF_DESCRIBE, encodeMessageFields(type, data)), require_encode.rfc8949EncodeOptions);
|
|
288
|
+
}
|
|
289
|
+
function buildDecodeIndex(type) {
|
|
290
|
+
const fields = getFields(type);
|
|
291
|
+
const idToName = /* @__PURE__ */ new Map();
|
|
292
|
+
for (const [name, spec] of Object.entries(fields)) idToName.set(spec.id, {
|
|
293
|
+
name,
|
|
294
|
+
field: spec
|
|
295
|
+
});
|
|
296
|
+
return { idToName };
|
|
297
|
+
}
|
|
298
|
+
function mapsToPlainObjects(value) {
|
|
299
|
+
if (value instanceof Map) {
|
|
300
|
+
const obj = {};
|
|
301
|
+
for (const [k, v] of value.entries()) obj[String(k)] = mapsToPlainObjects(v);
|
|
302
|
+
return obj;
|
|
303
|
+
}
|
|
304
|
+
if (Array.isArray(value)) return value.map(mapsToPlainObjects);
|
|
305
|
+
return value;
|
|
306
|
+
}
|
|
307
|
+
function decodeAnyValue(payload) {
|
|
308
|
+
const typeUrl = payload.get(0);
|
|
309
|
+
if (!typeUrl || typeof typeUrl !== "string") throw new Error("canonical-cbor: Any payload missing typeUrl at key 0");
|
|
310
|
+
if (typeUrl === "json" || typeUrl === "vc" || typeUrl === "fg:x:address") return {
|
|
311
|
+
typeUrl,
|
|
312
|
+
value: mapsToPlainObjects(payload.get(1))
|
|
313
|
+
};
|
|
314
|
+
const innerType = require_provider.fromTypeUrl(typeUrl);
|
|
315
|
+
let innerFields;
|
|
316
|
+
try {
|
|
317
|
+
innerFields = getFields(innerType);
|
|
318
|
+
} catch {
|
|
319
|
+
innerFields = null;
|
|
320
|
+
}
|
|
321
|
+
if (!innerFields) return {
|
|
322
|
+
typeUrl,
|
|
323
|
+
value: payload.get(1)
|
|
324
|
+
};
|
|
325
|
+
const innerObj = { typeUrl };
|
|
326
|
+
for (const [id, raw] of payload.entries()) {
|
|
327
|
+
if (id === 0) continue;
|
|
328
|
+
const descriptor = Object.entries(innerFields).find(([, f]) => f.id === id);
|
|
329
|
+
if (!descriptor) continue;
|
|
330
|
+
const [name, spec] = descriptor;
|
|
331
|
+
innerObj[name] = decodeFieldValue(raw, spec);
|
|
332
|
+
}
|
|
333
|
+
return innerObj;
|
|
334
|
+
}
|
|
335
|
+
function decodeFieldValue(raw, field) {
|
|
336
|
+
const type = field.type;
|
|
337
|
+
if (field.rule === "repeated") {
|
|
338
|
+
if (!Array.isArray(raw)) return raw;
|
|
339
|
+
const itemField = {
|
|
340
|
+
id: field.id,
|
|
341
|
+
type
|
|
342
|
+
};
|
|
343
|
+
return raw.map((item) => decodeFieldValue(item, itemField));
|
|
344
|
+
}
|
|
345
|
+
if (BIGINT_WRAPPER_TYPES.has(type)) {
|
|
346
|
+
if (raw instanceof require_tagged.Tagged) {
|
|
347
|
+
if (raw.tag !== TAG_POSITIVE_BIGNUM && raw.tag !== TAG_NEGATIVE_BIGNUM) throw new Error(`canonical-cbor: BigInt wrapper expects tag 2/3, got ${raw.tag}`);
|
|
348
|
+
const bytes = raw.value;
|
|
349
|
+
const negative = raw.tag === TAG_NEGATIVE_BIGNUM;
|
|
350
|
+
const result = { value: bytes };
|
|
351
|
+
if (type === "BigSint") result.minus = negative;
|
|
352
|
+
return result;
|
|
353
|
+
}
|
|
354
|
+
return raw;
|
|
355
|
+
}
|
|
356
|
+
if (TIMESTAMP_TYPES.has(type)) {
|
|
357
|
+
if (typeof raw !== "string") throw new Error("canonical-cbor: Timestamp expected string");
|
|
358
|
+
return raw;
|
|
359
|
+
}
|
|
360
|
+
if (ANY_TYPES.has(type)) {
|
|
361
|
+
if (!(raw instanceof Map)) throw new Error("canonical-cbor: Any expected nested map");
|
|
362
|
+
return decodeAnyValue(raw);
|
|
363
|
+
}
|
|
364
|
+
if (isEnumType(type)) return typeof raw === "bigint" ? Number(raw) : raw;
|
|
365
|
+
if (SCALAR_INT_TYPES.has(type)) return raw;
|
|
366
|
+
if (SCALAR_FLOAT_TYPES.has(type)) return Number(raw);
|
|
367
|
+
if (type === "bool") return Boolean(raw);
|
|
368
|
+
if (type === "string") return String(raw);
|
|
369
|
+
if (type === "bytes") return raw;
|
|
370
|
+
if (raw instanceof Map) return decodeMessageMap(type, raw);
|
|
371
|
+
return raw;
|
|
372
|
+
}
|
|
373
|
+
function decodeMessageMap(type, payload) {
|
|
374
|
+
const ctx = buildDecodeIndex(type);
|
|
375
|
+
const out = {};
|
|
376
|
+
for (const [id, raw] of payload.entries()) {
|
|
377
|
+
const entry = ctx.idToName.get(id);
|
|
378
|
+
if (!entry) continue;
|
|
379
|
+
const value = decodeFieldValue(raw, entry.field);
|
|
380
|
+
out[entry.name] = value;
|
|
381
|
+
if (entry.field.rule === "repeated") out[`${entry.name}List`] = value;
|
|
382
|
+
else if (entry.field.keyType !== void 0) out[`${entry.name}Map`] = value;
|
|
383
|
+
}
|
|
384
|
+
return out;
|
|
385
|
+
}
|
|
386
|
+
function parseCanonical(type, bytes) {
|
|
387
|
+
if (!bytes || !(bytes instanceof Uint8Array)) throw new Error("canonical-cbor: input must be Uint8Array");
|
|
388
|
+
if (bytes.length < 3 || bytes[0] !== SELF_DESCRIBE_PREFIX[0] || bytes[1] !== SELF_DESCRIBE_PREFIX[1] || bytes[2] !== SELF_DESCRIBE_PREFIX[2]) throw new Error("canonical-cbor: missing self-describe tag 55799 prefix");
|
|
389
|
+
const decoded = require_decode.decode(bytes, {
|
|
390
|
+
tags: {
|
|
391
|
+
...require_tagged.Tagged.preserve(TAG_POSITIVE_BIGNUM, TAG_NEGATIVE_BIGNUM),
|
|
392
|
+
[TAG_SELF_DESCRIBE]: (decodeFn) => decodeFn()
|
|
393
|
+
},
|
|
394
|
+
useMaps: true
|
|
395
|
+
});
|
|
396
|
+
if (!(decoded instanceof Map)) throw new Error("canonical-cbor: payload is not a CBOR map");
|
|
397
|
+
return decodeMessageMap(type, decoded);
|
|
398
|
+
}
|
|
399
|
+
const CANONICAL_SELF_DESCRIBE_PREFIX = SELF_DESCRIBE_PREFIX;
|
|
400
|
+
|
|
401
|
+
//#endregion
|
|
402
|
+
exports.CANONICAL_SELF_DESCRIBE_PREFIX = CANONICAL_SELF_DESCRIBE_PREFIX;
|
|
403
|
+
exports.canonicalBytes = canonicalBytes;
|
|
404
|
+
exports.parseCanonical = parseCanonical;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
//#region src/canonical-cbor.d.ts
|
|
2
|
+
declare function canonicalBytes(type: string, data: Record<string, unknown>): Uint8Array;
|
|
3
|
+
declare function parseCanonical(type: string, bytes: Uint8Array): Record<string, unknown>;
|
|
4
|
+
declare const CANONICAL_SELF_DESCRIBE_PREFIX: Uint8Array<ArrayBuffer>;
|
|
5
|
+
//#endregion
|
|
6
|
+
export { CANONICAL_SELF_DESCRIBE_PREFIX, canonicalBytes, parseCanonical };
|
package/lib/cbor.cjs
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const require_canonical_cbor = require('./canonical-cbor.cjs');
|
|
2
|
+
require('./provider-init-schema.cjs');
|
|
3
|
+
|
|
4
|
+
//#region src/cbor.ts
|
|
5
|
+
function encodeTx(params) {
|
|
6
|
+
return require_canonical_cbor.canonicalBytes("Transaction", params);
|
|
7
|
+
}
|
|
8
|
+
function decodeTx(bytes) {
|
|
9
|
+
return require_canonical_cbor.parseCanonical("Transaction", bytes);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
//#endregion
|
|
13
|
+
exports.CANONICAL_SELF_DESCRIBE_PREFIX = require_canonical_cbor.CANONICAL_SELF_DESCRIBE_PREFIX;
|
|
14
|
+
exports.canonicalBytes = require_canonical_cbor.canonicalBytes;
|
|
15
|
+
exports.decodeTx = decodeTx;
|
|
16
|
+
exports.encodeTx = encodeTx;
|
|
17
|
+
exports.parseCanonical = require_canonical_cbor.parseCanonical;
|
package/lib/cbor.d.cts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { CANONICAL_SELF_DESCRIBE_PREFIX, canonicalBytes, parseCanonical } from "./canonical-cbor.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/cbor.d.ts
|
|
4
|
+
|
|
5
|
+
declare function encodeTx(params: Record<string, unknown>): Uint8Array;
|
|
6
|
+
declare function decodeTx(bytes: Uint8Array): Record<string, unknown>;
|
|
7
|
+
//#endregion
|
|
8
|
+
export { CANONICAL_SELF_DESCRIBE_PREFIX, canonicalBytes, decodeTx, encodeTx, parseCanonical };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const require_rolldown_runtime = require('
|
|
1
|
+
const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
|
|
2
2
|
|
|
3
3
|
//#region package.json
|
|
4
4
|
var require_package = /* @__PURE__ */ require_rolldown_runtime.__commonJSMin(((exports, module) => {
|
|
@@ -20,6 +20,7 @@ var require_package = /* @__PURE__ */ require_rolldown_runtime.__commonJSMin(((e
|
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"@ocap/proto": "workspace:*",
|
|
22
22
|
"@ocap/util": "workspace:*",
|
|
23
|
+
"cborg": "^5.1.0",
|
|
23
24
|
"debug": "^4.4.3",
|
|
24
25
|
"google-protobuf": "3.21.0",
|
|
25
26
|
"lodash": "^4.17.23"
|
|
@@ -44,6 +45,16 @@ var require_package = /* @__PURE__ */ require_rolldown_runtime.__commonJSMin(((e
|
|
|
44
45
|
"module": "./esm/index.mjs",
|
|
45
46
|
"types": "./esm/index.d.mts",
|
|
46
47
|
"exports": {
|
|
48
|
+
"./cbor": {
|
|
49
|
+
"types": "./esm/cbor.d.mts",
|
|
50
|
+
"import": "./esm/cbor.mjs",
|
|
51
|
+
"default": "./lib/cbor.cjs"
|
|
52
|
+
},
|
|
53
|
+
"./protobuf": {
|
|
54
|
+
"types": "./esm/protobuf.d.mts",
|
|
55
|
+
"import": "./esm/protobuf.mjs",
|
|
56
|
+
"default": "./lib/protobuf.cjs"
|
|
57
|
+
},
|
|
47
58
|
".": {
|
|
48
59
|
"types": "./esm/index.d.mts",
|
|
49
60
|
"import": "./esm/index.mjs",
|
package/lib/index.cjs
CHANGED
|
@@ -1,14 +1,8 @@
|
|
|
1
|
-
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
-
require('./patch.cjs');
|
|
3
1
|
const require_provider = require('./provider.cjs');
|
|
2
|
+
require('./patch.cjs');
|
|
3
|
+
require('./provider-init.cjs');
|
|
4
4
|
const require_message = require('./message.cjs');
|
|
5
|
-
let _ocap_proto = require("@ocap/proto");
|
|
6
|
-
_ocap_proto = require_rolldown_runtime.__toESM(_ocap_proto);
|
|
7
|
-
|
|
8
|
-
//#region src/index.ts
|
|
9
|
-
require_provider.addProvider(_ocap_proto.default);
|
|
10
5
|
|
|
11
|
-
//#endregion
|
|
12
6
|
exports.addProvider = require_provider.addProvider;
|
|
13
7
|
exports.attachExampleFn = require_message.attachExampleFn;
|
|
14
8
|
exports.attachFormatFn = require_message.attachFormatFn;
|
package/lib/message.cjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
2
|
const require_provider = require('./provider.cjs');
|
|
3
|
+
let _ocap_util = require("@ocap/util");
|
|
3
4
|
let debug = require("debug");
|
|
4
5
|
debug = require_rolldown_runtime.__toESM(debug);
|
|
5
6
|
let lodash_camelCase = require("lodash/camelCase");
|
|
6
7
|
lodash_camelCase = require_rolldown_runtime.__toESM(lodash_camelCase);
|
|
7
|
-
let _ocap_util = require("@ocap/util");
|
|
8
8
|
|
|
9
9
|
//#region src/message.ts
|
|
10
10
|
/**
|
|
@@ -325,13 +325,38 @@ function decodeAny(data) {
|
|
|
325
325
|
};
|
|
326
326
|
if (data.type && data.value) return data;
|
|
327
327
|
const { typeUrl, value } = data;
|
|
328
|
-
if (typeUrl && ["json", "vc"].includes(typeUrl))
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
328
|
+
if (typeUrl && ["json", "vc"].includes(typeUrl)) {
|
|
329
|
+
if (typeof value === "string" || Buffer.isBuffer(value) || value instanceof Uint8Array) try {
|
|
330
|
+
const source = typeof value === "string" ? Buffer.from(value, "base64") : Buffer.from(value);
|
|
331
|
+
return {
|
|
332
|
+
type: typeUrl,
|
|
333
|
+
value: JSON.parse(source.toString())
|
|
334
|
+
};
|
|
335
|
+
} catch (_e) {
|
|
336
|
+
return {
|
|
337
|
+
type: typeUrl,
|
|
338
|
+
value
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
return {
|
|
342
|
+
type: typeUrl,
|
|
343
|
+
value
|
|
344
|
+
};
|
|
345
|
+
}
|
|
332
346
|
const type = require_provider.fromTypeUrl(typeUrl);
|
|
333
347
|
const { fn: Message } = require_provider.getMessageType(type);
|
|
334
348
|
if (!Message) return data;
|
|
349
|
+
if (!(typeof value === "string" || Buffer.isBuffer(value) || value instanceof Uint8Array)) {
|
|
350
|
+
const inner = {};
|
|
351
|
+
for (const [k, v] of Object.entries(data)) {
|
|
352
|
+
if (k === "typeUrl" || k === "type_url") continue;
|
|
353
|
+
inner[k] = v;
|
|
354
|
+
}
|
|
355
|
+
return {
|
|
356
|
+
type,
|
|
357
|
+
value: inner
|
|
358
|
+
};
|
|
359
|
+
}
|
|
335
360
|
const buffer = Buffer.isBuffer(value) ? value : Buffer.from(value, "base64");
|
|
336
361
|
return {
|
|
337
362
|
type,
|