hbsig 0.2.8 → 0.3.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/cjs/signer.js CHANGED
@@ -21,12 +21,13 @@ var _aoconnect = require("@permaweb/aoconnect");
21
21
  var _send = require("./send.js");
22
22
  var _signerUtils = require("./signer-utils.js");
23
23
  var _excluded = ["body"],
24
- _excluded2 = ["path", "method"],
25
- _excluded3 = ["body"];
26
- function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
27
- function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
24
+ _excluded2 = ["method"],
25
+ _excluded3 = ["path"],
26
+ _excluded4 = ["body"];
28
27
  function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); }
29
28
  function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); } r ? i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n : (o("next", 0), o("throw", 1), o("return", 2)); }, _regeneratorDefine2(e, r, n, t); }
29
+ function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
30
+ function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
30
31
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
31
32
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
32
33
  function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
@@ -124,285 +125,112 @@ var hasBinaryData = function hasBinaryData(obj) {
124
125
  return false;
125
126
  };
126
127
 
127
- // Helper to check if value is a simple array that should use structured fields
128
- var isSimpleArray = function isSimpleArray(value) {
129
- if (!Array.isArray(value)) return false;
130
- return value.every(function (item) {
131
- // Simple types that can be in structured field lists
132
- if (typeof item === "string") return true;
133
- if (typeof item === "number") return true;
134
- if (typeof item === "boolean") return true;
135
- if ((0, _encodeUtils.isBytes)(item)) return true;
136
-
137
- // Complex types cannot be in structured field lists
138
- if (item && _typeof(item) === "object") return false;
139
- return true;
140
- });
141
- };
142
-
143
- // Helper to encode array as structured field list
144
- var encodeAsStructuredFieldList = function encodeAsStructuredFieldList(arr) {
145
- return arr.map(function (item) {
146
- if (typeof item === "string") {
147
- // String values are quoted
148
- return "\"".concat(item.replace(/\\/g, "\\\\").replace(/"/g, '\\"'), "\"");
149
- } else if (typeof item === "number") {
150
- // Numbers are bare
151
- return String(item);
152
- } else if (typeof item === "boolean") {
153
- // Booleans use ?0 or ?1
154
- return item ? "?1" : "?0";
155
- } else if ((0, _encodeUtils.isBytes)(item)) {
156
- // Binary data as byte sequences
157
- var buffer = Buffer.isBuffer(item) ? item : Buffer.from(item);
158
- return ":".concat(buffer.toString("base64"), ":");
159
- } else {
160
- // Fallback
161
- return "\"".concat(String(item), "\"");
128
+ // Helper to build ao-types string from an object
129
+ var buildAoTypes = function buildAoTypes(obj) {
130
+ var types = [];
131
+ for (var _i4 = 0, _Object$entries4 = Object.entries(obj); _i4 < _Object$entries4.length; _i4++) {
132
+ var _Object$entries4$_i = _slicedToArray(_Object$entries4[_i4], 2),
133
+ key = _Object$entries4$_i[0],
134
+ value = _Object$entries4$_i[1];
135
+ if (typeof value === "number") {
136
+ types.push("".concat(key, "=\"").concat(Number.isInteger(value) ? "integer" : "float", "\""));
137
+ } else if (typeof value === "boolean") {
138
+ types.push("".concat(key, "=\"atom\""));
139
+ } else if (value === null) {
140
+ types.push("".concat(key, "=\"atom\""));
141
+ } else if (_typeof(value) === "symbol") {
142
+ // Symbols are Erlang atoms
143
+ types.push("".concat(key, "=\"atom\""));
162
144
  }
163
- }).join(", ");
145
+ }
146
+ return types.length > 0 ? types.join(", ") : null;
164
147
  };
165
- var smartSign = /*#__PURE__*/function () {
166
- var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(obj, path) {
167
- var filtered, canUseSimpleEncoding, hasBodyField, _i4, _Object$entries4, _Object$entries4$_i, key, value, message, types, _i5, _Object$entries5, _Object$entries5$_i, _key, _value, normalized, result, flattened, encoded, _result, _i6, _Object$entries6, _Object$entries6$_i, _key2, _value2, _t;
168
- return _regenerator().w(function (_context) {
169
- while (1) switch (_context.p = _context.n) {
170
- case 0:
171
- _context.p = 0;
172
- // Filter out undefined values
173
- filtered = _filterUndefined(obj); // Check if we can encode everything as headers (no multipart needed)
174
- canUseSimpleEncoding = true;
175
- hasBodyField = false;
176
- _i4 = 0, _Object$entries4 = Object.entries(filtered);
177
- case 1:
178
- if (!(_i4 < _Object$entries4.length)) {
179
- _context.n = 6;
180
- break;
181
- }
182
- _Object$entries4$_i = _slicedToArray(_Object$entries4[_i4], 2), key = _Object$entries4$_i[0], value = _Object$entries4$_i[1];
183
- if (!(key === "path")) {
184
- _context.n = 2;
185
- break;
186
- }
187
- return _context.a(3, 5);
188
- case 2:
189
- if (!(key === "body" || key === "data")) {
190
- _context.n = 3;
191
- break;
192
- }
193
- hasBodyField = true;
194
- // Only use multipart if body/data is actually binary
195
- if (!(0, _encodeUtils.isBytes)(value)) {
196
- _context.n = 3;
197
- break;
198
- }
199
- canUseSimpleEncoding = false;
200
- return _context.a(3, 6);
201
- case 3:
202
- if (!(value && _typeof(value) === "object" && !Array.isArray(value) && !(0, _encodeUtils.isBytes)(value))) {
203
- _context.n = 4;
204
- break;
205
- }
206
- if (!(Object.keys(value).length > 0)) {
207
- _context.n = 4;
208
- break;
209
- }
210
- canUseSimpleEncoding = false;
211
- return _context.a(3, 6);
212
- case 4:
213
- if (!(Array.isArray(value) && !isSimpleArray(value))) {
214
- _context.n = 5;
215
- break;
216
- }
217
- canUseSimpleEncoding = false;
218
- return _context.a(3, 6);
219
- case 5:
220
- _i4++;
221
- _context.n = 1;
222
- break;
223
- case 6:
224
- if (!canUseSimpleEncoding) {
225
- _context.n = 11;
226
- break;
227
- }
228
- // Build a simple message that won't trigger multipart
229
- message = {};
230
- if (path) message.path = path;
231
- types = [];
232
- _i5 = 0, _Object$entries5 = Object.entries(filtered);
233
- case 7:
234
- if (!(_i5 < _Object$entries5.length)) {
235
- _context.n = 10;
236
- break;
237
- }
238
- _Object$entries5$_i = _slicedToArray(_Object$entries5[_i5], 2), _key = _Object$entries5$_i[0], _value = _Object$entries5$_i[1];
239
- if (!(_key === "path")) {
240
- _context.n = 8;
241
- break;
242
- }
243
- return _context.a(3, 9);
244
- case 8:
245
- if (_value === "") {
246
- types.push("".concat(_key, "=\"empty-binary\""));
247
- } else if (Array.isArray(_value) && _value.length === 0) {
248
- types.push("".concat(_key, "=\"empty-list\""));
249
- } else if (_value && _typeof(_value) === "object" && Object.keys(_value).length === 0) {
250
- types.push("".concat(_key, "=\"empty-message\""));
251
- } else if (isSimpleArray(_value)) {
252
- types.push("".concat(_key, "=\"list\""));
253
- message[_key] = encodeAsStructuredFieldList(_value);
254
- } else if (typeof _value === "number") {
255
- types.push("".concat(_key, "=\"").concat(Number.isInteger(_value) ? "integer" : "float", "\""));
256
- message[_key] = String(_value);
257
- } else if (typeof _value === "boolean") {
258
- types.push("".concat(_key, "=\"atom\""));
259
- message[_key] = String(_value);
260
- } else if (_value === null || _value === undefined) {
261
- types.push("".concat(_key, "=\"atom\""));
262
- message[_key] = String(_value);
263
- } else if (typeof _value === "string") {
264
- message[_key] = _value;
265
- }
266
- case 9:
267
- _i5++;
268
- _context.n = 7;
269
- break;
270
- case 10:
271
- if (types.length > 0) {
272
- message["ao-types"] = types.join(", ");
273
- }
274
- return _context.a(2, (0, _httpsig.httpsig_to)(message));
275
- case 11:
276
- // For complex structures that need multipart, use enc()
277
- normalized = (0, _erl_json.normalize)(_objectSpread(_objectSpread({}, filtered), path && {
278
- path: path
279
- }));
280
- _context.n = 12;
281
- return (0, _encode.enc)(normalized);
282
- case 12:
283
- result = _context.v;
284
- // enc() returns { headers: {...}, body: ... }
285
- // We need to flatten this for httpsig_to
286
- flattened = _objectSpread(_objectSpread({}, result.headers), {}, {
287
- body: result.body
288
- }, path && {
289
- path: path
290
- }); // httpsig_to expects the structured format
291
- encoded = (0, _httpsig.httpsig_to)(flattened);
292
- return _context.a(2, encoded);
293
- case 13:
294
- _context.p = 13;
295
- _t = _context.v;
296
- console.error("Encoding failed:", _t);
297
-
298
- // Fallback: create a simple structure
299
- _result = {};
300
- if (path) _result.path = path;
301
- _i6 = 0, _Object$entries6 = Object.entries(obj);
302
- case 14:
303
- if (!(_i6 < _Object$entries6.length)) {
304
- _context.n = 17;
305
- break;
306
- }
307
- _Object$entries6$_i = _slicedToArray(_Object$entries6[_i6], 2), _key2 = _Object$entries6$_i[0], _value2 = _Object$entries6$_i[1];
308
- if (!(_key2 === "path")) {
309
- _context.n = 15;
310
- break;
311
- }
312
- return _context.a(3, 16);
313
- case 15:
314
- if (!(0, _encodeUtils.isBytes)(_value2) && _value2 !== undefined) {
315
- _result[_key2] = _value2;
316
- }
317
- case 16:
318
- _i6++;
319
- _context.n = 14;
320
- break;
321
- case 17:
322
- return _context.a(2, _result);
323
- }
324
- }, _callee, null, [[0, 13]]);
325
- }));
326
- return function smartSign(_x, _x2) {
327
- return _ref.apply(this, arguments);
328
- };
329
- }();
330
148
 
331
149
  // Internal encode function that uses the original impl as much as possible
332
150
  var encode = /*#__PURE__*/function () {
333
- var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(obj, path) {
334
- var filtered, fields, encoded, body, headers;
335
- return _regenerator().w(function (_context2) {
336
- while (1) switch (_context2.n) {
151
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(obj, path) {
152
+ var filtered, fields, aoTypes, existing, encoded, encResult, body, headers;
153
+ return _regenerator().w(function (_context) {
154
+ while (1) switch (_context.n) {
337
155
  case 0:
338
156
  // Filter out undefined values before processing
339
157
  filtered = _filterUndefined(obj); // If object contains binary data, use enc() directly
340
158
  if (!hasBinaryData(filtered)) {
341
- _context2.n = 2;
159
+ _context.n = 2;
342
160
  break;
343
161
  }
344
- _context2.n = 1;
162
+ _context.n = 1;
345
163
  return (0, _encode.enc)(filtered);
346
164
  case 1:
347
- return _context2.a(2, _context2.v);
165
+ return _context.a(2, _context.v);
348
166
  case 2:
349
- // Otherwise use the standard pipeline
350
- fields = _objectSpread({}, filtered); // Only add path if explicitly provided
167
+ // Only add path if explicitly provided
168
+ fields = _objectSpread({}, filtered);
351
169
  if (path) fields.path = path;
352
170
 
171
+ // Build ao-types annotation for typed values (integers, booleans, etc.)
172
+ // This tells HyperBEAM how to convert values during verification
173
+ // Merge with any existing ao-types (e.g., list annotations from hb.js)
174
+ aoTypes = buildAoTypes(filtered);
175
+ if (aoTypes) {
176
+ existing = fields["ao-types"];
177
+ fields["ao-types"] = existing ? existing + ", " + aoTypes : aoTypes;
178
+ }
179
+
353
180
  // Try the standard encoding pipeline
354
181
  encoded = (0, _httpsig.httpsig_to)((0, _erl_json.normalize)((0, _structured.structured_from)((0, _erl_json.normalize)(fields)))); // Check if the encoded result is valid for HTTP headers
355
182
  if (isValid(encoded)) {
356
- _context2.n = 4;
183
+ _context.n = 4;
357
184
  break;
358
185
  }
359
- _context2.n = 3;
186
+ _context.n = 3;
360
187
  return (0, _encode.enc)(filtered);
361
188
  case 3:
362
- return _context2.a(2, _context2.v);
189
+ encResult = _context.v;
190
+ return _context.a(2, encResult);
363
191
  case 4:
364
192
  // For non-binary data, return in the same format as enc()
365
193
  // httpsig_to returns a flattened object, so we need to separate headers and body
366
194
  body = encoded.body, headers = _objectWithoutProperties(encoded, _excluded);
367
- return _context2.a(2, {
195
+ return _context.a(2, {
368
196
  headers: headers,
369
197
  body: body
370
198
  });
371
199
  }
372
- }, _callee2);
200
+ }, _callee);
373
201
  }));
374
- return function encode(_x3, _x4) {
375
- return _ref2.apply(this, arguments);
202
+ return function encode(_x, _x2) {
203
+ return _ref.apply(this, arguments);
376
204
  };
377
205
  }();
378
206
 
379
207
  // Helper to join URL and path
380
- var joinUrl = function joinUrl(_ref3) {
381
- var url = _ref3.url,
382
- path = _ref3.path;
208
+ var joinUrl = function joinUrl(_ref2) {
209
+ var url = _ref2.url,
210
+ path = _ref2.path;
383
211
  if (path.startsWith("http://") || path.startsWith("https://")) return path;
384
212
  var normalizedPath = path.startsWith("/") ? path : "/" + path;
385
213
  return url.endsWith("/") ? url.slice(0, -1) + normalizedPath : url + normalizedPath;
386
214
  };
387
215
 
388
216
  // Main sign function that matches signer.js API
389
- function sign(_x5) {
217
+ function sign(_x3) {
390
218
  return _sign2.apply(this, arguments);
391
219
  } // Helper function to recursively filter out undefined values
392
220
  function _sign2() {
393
- _sign2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(_ref4) {
394
- var url, path, encoded, jwk, _ref4$signPath, signPath, signer, _encoded$body, body, headers, _enc;
395
- return _regenerator().w(function (_context4) {
396
- while (1) switch (_context4.n) {
221
+ _sign2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(_ref3) {
222
+ var url, path, encoded, jwk, _ref3$signPath, signPath, signer, _encoded$body, body, headers, _enc;
223
+ return _regenerator().w(function (_context3) {
224
+ while (1) switch (_context3.n) {
397
225
  case 0:
398
- url = _ref4.url, path = _ref4.path, encoded = _ref4.msg, jwk = _ref4.jwk, _ref4$signPath = _ref4.signPath, signPath = _ref4$signPath === void 0 ? true : _ref4$signPath;
226
+ url = _ref3.url, path = _ref3.path, encoded = _ref3.msg, jwk = _ref3.jwk, _ref3$signPath = _ref3.signPath, signPath = _ref3$signPath === void 0 ? true : _ref3$signPath;
399
227
  signer = (0, _aoconnect.createSigner)(jwk, url);
400
- _encoded$body = encoded.body, body = _encoded$body === void 0 ? null : _encoded$body, headers = _objectWithoutProperties(encoded, _excluded3);
228
+ _encoded$body = encoded.body, body = _encoded$body === void 0 ? null : _encoded$body, headers = _objectWithoutProperties(encoded, _excluded4);
401
229
  _enc = {
402
230
  headers: headers
403
231
  };
404
232
  if (body) _enc.body = new Blob([body]);
405
- _context4.n = 1;
233
+ _context3.n = 1;
406
234
  return _sign({
407
235
  path: path,
408
236
  signPath: signPath,
@@ -411,9 +239,9 @@ function _sign2() {
411
239
  url: url
412
240
  });
413
241
  case 1:
414
- return _context4.a(2, _context4.v);
242
+ return _context3.a(2, _context3.v);
415
243
  }
416
- }, _callee4);
244
+ }, _callee3);
417
245
  }));
418
246
  return _sign2.apply(this, arguments);
419
247
  }
@@ -426,10 +254,10 @@ var _filterUndefined = function filterUndefined(obj) {
426
254
  }
427
255
  if (_typeof(obj) === "object" && obj.constructor === Object) {
428
256
  var filtered = {};
429
- for (var _i7 = 0, _Object$entries7 = Object.entries(obj); _i7 < _Object$entries7.length; _i7++) {
430
- var _Object$entries7$_i = _slicedToArray(_Object$entries7[_i7], 2),
431
- key = _Object$entries7$_i[0],
432
- value = _Object$entries7$_i[1];
257
+ for (var _i5 = 0, _Object$entries5 = Object.entries(obj); _i5 < _Object$entries5.length; _i5++) {
258
+ var _Object$entries5$_i = _slicedToArray(_Object$entries5[_i5], 2),
259
+ key = _Object$entries5$_i[0],
260
+ value = _Object$entries5$_i[1];
433
261
  var filteredValue = _filterUndefined(value);
434
262
  if (filteredValue !== undefined) {
435
263
  filtered[key] = filteredValue;
@@ -439,43 +267,59 @@ var _filterUndefined = function filterUndefined(obj) {
439
267
  }
440
268
  return obj;
441
269
  };
442
- function _sign(_x6) {
270
+ function _sign(_x4) {
443
271
  return _sign3.apply(this, arguments);
444
272
  }
445
273
  function _sign3() {
446
- _sign3 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(_ref5) {
447
- var path, _ref5$signPath, signPath, _ref5$method, method, encoded, signer, url, headersObj, body, url_path, _url, bodySize, lowercaseHeaders, _i8, _Object$entries8, _Object$entries8$_i, key, value, bodyKeys, isPath, signingFields, signedRequest, finalHeaders, _i9, _Object$entries9, _Object$entries9$_i, _key3, _value3, result;
448
- return _regenerator().w(function (_context5) {
449
- while (1) switch (_context5.n) {
274
+ _sign3 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(_ref4) {
275
+ var path, _ref4$signPath, signPath, _ref4$method, method, encoded, signer, url, headersObj, body, url_path, _url, isDataFieldPath, bodySize, lowercaseHeaders, _i6, _Object$entries6, _Object$entries6$_i, key, value, bodyKeys, metadataFields, isPath, signingFields, signedRequest, finalHeaders, _i7, _Object$entries7, _Object$entries7$_i, _key, _value, result;
276
+ return _regenerator().w(function (_context4) {
277
+ while (1) switch (_context4.n) {
450
278
  case 0:
451
- path = _ref5.path, _ref5$signPath = _ref5.signPath, signPath = _ref5$signPath === void 0 ? true : _ref5$signPath, _ref5$method = _ref5.method, method = _ref5$method === void 0 ? "POST" : _ref5$method, encoded = _ref5.encoded, signer = _ref5.signer, url = _ref5.url;
279
+ path = _ref4.path, _ref4$signPath = _ref4.signPath, signPath = _ref4$signPath === void 0 ? true : _ref4$signPath, _ref4$method = _ref4.method, method = _ref4$method === void 0 ? "POST" : _ref4$method, encoded = _ref4.encoded, signer = _ref4.signer, url = _ref4.url;
452
280
  headersObj = encoded ? encoded.headers : {};
453
281
  body = encoded ? encoded.body : undefined;
454
282
  url_path = typeof signPath === "string" ? signPath : path;
455
283
  _url = joinUrl({
456
284
  url: url,
457
285
  path: url_path
458
- }); // Only add path header if path is provided
459
- if (path) headersObj["path"] = path;
286
+ }); // Only add path header if it's a data field (doesn't start with "/").
287
+ // URL paths (like "/relay/process") should NOT be added to headers.
288
+ // Data fields named "path" (e.g., "credit-notice") should be signed.
289
+ isDataFieldPath = path && typeof path === "string" && !path.startsWith("/");
290
+ if (isDataFieldPath && !headersObj["path"]) headersObj["path"] = path;
291
+
292
+ // Add accept-bundle header to request inline data instead of links
293
+ headersObj["accept-bundle"] = "true";
460
294
  if (body && !headersObj["content-length"]) {
461
295
  bodySize = body.size || body.byteLength || 0;
462
296
  if (bodySize > 0) headersObj["content-length"] = String(bodySize);
463
297
  }
464
298
  lowercaseHeaders = {};
465
- for (_i8 = 0, _Object$entries8 = Object.entries(headersObj); _i8 < _Object$entries8.length; _i8++) {
466
- _Object$entries8$_i = _slicedToArray(_Object$entries8[_i8], 2), key = _Object$entries8$_i[0], value = _Object$entries8$_i[1];
299
+ for (_i6 = 0, _Object$entries6 = Object.entries(headersObj); _i6 < _Object$entries6.length; _i6++) {
300
+ _Object$entries6$_i = _slicedToArray(_Object$entries6[_i6], 2), key = _Object$entries6$_i[0], value = _Object$entries6$_i[1];
467
301
  lowercaseHeaders[key.toLowerCase()] = value;
468
302
  }
469
303
  bodyKeys = headersObj["body-keys"] ? headersObj["body-keys"].replace(/"/g, "").split(",").map(function (k) {
470
304
  return k.trim();
471
- }) : [];
305
+ }) : []; // Exclude metadata fields that get consumed/stripped during JSON codec parsing:
306
+ // - ao-types: used for type conversion, then removed by structured codec
307
+ // - accept-bundle: request metadata for inlining nested data
308
+ // - content-digest: only exclude when no body; when body exists, sign it so
309
+ // HyperBEAM can map content-digest → body → ao-body-key field in committed list
310
+ // Note: "path" as a data field (e.g., path: "credit-notice") should be signed.
311
+ // The @path derived component (HTTP request URL) is handled separately.
312
+ metadataFields = ["body-keys", "ao-types", "accept-bundle", "content-length"];
313
+ if (!body) {
314
+ metadataFields.push("content-digest");
315
+ }
472
316
  isPath = false;
473
317
  signingFields = Object.keys(lowercaseHeaders).filter(function (key) {
474
318
  if (key === "path") isPath = true;
475
- return key !== "body-keys" && key !== "path" && !bodyKeys.includes(key);
319
+ return !metadataFields.includes(key) && !bodyKeys.includes(key);
476
320
  }); // Only add @path if signPath is enabled AND path header exists
477
321
  if (signPath !== false && isPath) signingFields.push("@path");
478
- _context5.n = 1;
322
+ _context4.n = 1;
479
323
  return (0, _send.toHttpSigner)(signer)({
480
324
  request: {
481
325
  url: _url,
@@ -485,11 +329,11 @@ function _sign3() {
485
329
  fields: signingFields
486
330
  });
487
331
  case 1:
488
- signedRequest = _context5.v;
332
+ signedRequest = _context4.v;
489
333
  finalHeaders = {};
490
- for (_i9 = 0, _Object$entries9 = Object.entries(headersObj); _i9 < _Object$entries9.length; _i9++) {
491
- _Object$entries9$_i = _slicedToArray(_Object$entries9[_i9], 2), _key3 = _Object$entries9$_i[0], _value3 = _Object$entries9$_i[1];
492
- finalHeaders[_key3] = _value3;
334
+ for (_i7 = 0, _Object$entries7 = Object.entries(headersObj); _i7 < _Object$entries7.length; _i7++) {
335
+ _Object$entries7$_i = _slicedToArray(_Object$entries7[_i7], 2), _key = _Object$entries7$_i[0], _value = _Object$entries7$_i[1];
336
+ finalHeaders[_key] = _value;
493
337
  }
494
338
  finalHeaders["signature"] = signedRequest.headers["signature"];
495
339
  finalHeaders["signature-input"] = signedRequest.headers["signature-input"];
@@ -502,9 +346,9 @@ function _sign3() {
502
346
  headers: finalHeaders
503
347
  };
504
348
  if (body) result.body = body;
505
- return _context5.a(2, result);
349
+ return _context4.a(2, result);
506
350
  }
507
- }, _callee5);
351
+ }, _callee4);
508
352
  }));
509
353
  return _sign3.apply(this, arguments);
510
354
  }
@@ -514,42 +358,57 @@ function signer(config) {
514
358
  url = _config$url === void 0 ? "http://localhost:10001" : _config$url;
515
359
  if (!signer) throw new Error("Signer is required for mainnet mode");
516
360
  return /*#__PURE__*/function () {
517
- var _ref6 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(fields) {
518
- var _ref7,
519
- _ref7$encoded,
361
+ var _ref5 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(fields) {
362
+ var _ref6,
363
+ _ref6$encoded,
520
364
  _encoded,
521
- _ref7$path,
365
+ _ref6$path,
522
366
  signPath,
523
- _fields$path,
524
- path,
525
367
  _fields$method,
526
368
  method,
369
+ restFields,
370
+ fieldsPath,
371
+ isUrlPath,
372
+ path,
527
373
  aoFields,
374
+ _,
375
+ rest,
528
376
  filteredFields,
529
377
  encoded,
530
- _args3 = arguments,
531
- _t2;
532
- return _regenerator().w(function (_context3) {
533
- while (1) switch (_context3.n) {
378
+ _args2 = arguments,
379
+ _t;
380
+ return _regenerator().w(function (_context2) {
381
+ while (1) switch (_context2.n) {
534
382
  case 0:
535
- _ref7 = _args3.length > 1 && _args3[1] !== undefined ? _args3[1] : {}, _ref7$encoded = _ref7.encoded, _encoded = _ref7$encoded === void 0 ? false : _ref7$encoded, _ref7$path = _ref7.path, signPath = _ref7$path === void 0 ? true : _ref7$path;
536
- _fields$path = fields.path, path = _fields$path === void 0 ? "/relay/process" : _fields$path, _fields$method = fields.method, method = _fields$method === void 0 ? "POST" : _fields$method, aoFields = _objectWithoutProperties(fields, _excluded2);
383
+ _ref6 = _args2.length > 1 && _args2[1] !== undefined ? _args2[1] : {}, _ref6$encoded = _ref6.encoded, _encoded = _ref6$encoded === void 0 ? false : _ref6$encoded, _ref6$path = _ref6.path, signPath = _ref6$path === void 0 ? true : _ref6$path;
384
+ _fields$method = fields.method, method = _fields$method === void 0 ? "POST" : _fields$method, restFields = _objectWithoutProperties(fields, _excluded2); // Distinguish URL paths from data fields:
385
+ // - URL paths start with "/" (e.g., "/relay/process")
386
+ // - Data fields don't (e.g., "credit-notice" for P4 ledger actions)
387
+ fieldsPath = restFields.path;
388
+ isUrlPath = typeof fieldsPath === "string" && fieldsPath.startsWith("/");
389
+ path = isUrlPath ? fieldsPath : "/relay/process"; // Keep path in data fields if it's not a URL path
390
+ if (isUrlPath) {
391
+ _ = restFields.path, rest = _objectWithoutProperties(restFields, _excluded3);
392
+ aoFields = rest;
393
+ } else {
394
+ aoFields = restFields; // path stays as data field
395
+ }
537
396
  filteredFields = _filterUndefined(aoFields);
538
397
  if (!_encoded) {
539
- _context3.n = 1;
398
+ _context2.n = 1;
540
399
  break;
541
400
  }
542
- _t2 = filteredFields;
543
- _context3.n = 3;
401
+ _t = filteredFields;
402
+ _context2.n = 3;
544
403
  break;
545
404
  case 1:
546
- _context3.n = 2;
547
- return encode(filteredFields, path);
405
+ _context2.n = 2;
406
+ return encode(filteredFields, null);
548
407
  case 2:
549
- _t2 = _context3.v;
408
+ _t = _context2.v;
550
409
  case 3:
551
- encoded = _t2;
552
- _context3.n = 4;
410
+ encoded = _t;
411
+ _context2.n = 4;
553
412
  return _sign({
554
413
  path: path,
555
414
  signPath: signPath,
@@ -559,12 +418,12 @@ function signer(config) {
559
418
  url: url
560
419
  });
561
420
  case 4:
562
- return _context3.a(2, _context3.v);
421
+ return _context2.a(2, _context2.v);
563
422
  }
564
- }, _callee3);
423
+ }, _callee2);
565
424
  }));
566
- return function (_x7) {
567
- return _ref6.apply(this, arguments);
425
+ return function (_x5) {
426
+ return _ref5.apply(this, arguments);
568
427
  };
569
428
  }();
570
429
  }