hbsig 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/bin_to_str.js +44 -0
- package/cjs/collect-body-keys.js +470 -0
- package/cjs/encode-array-item.js +110 -0
- package/cjs/encode-utils.js +236 -0
- package/cjs/encode.js +1318 -0
- package/cjs/erl_json.js +317 -0
- package/cjs/erl_str.js +1037 -0
- package/cjs/flat.js +222 -0
- package/cjs/http-message-signatures/httpbis.js +489 -0
- package/cjs/http-message-signatures/index.js +25 -0
- package/cjs/http-message-signatures/structured-header.js +129 -0
- package/cjs/httpsig.js +716 -0
- package/cjs/httpsig2.js +1160 -0
- package/cjs/id.js +470 -0
- package/cjs/index.js +63 -0
- package/cjs/send.js +194 -0
- package/cjs/signer-utils.js +617 -0
- package/cjs/signer.js +606 -0
- package/cjs/structured.js +296 -0
- package/cjs/test.js +27 -0
- package/cjs/utils.js +42 -0
- package/esm/bin_to_str.js +46 -0
- package/esm/collect-body-keys.js +436 -0
- package/esm/encode-array-item.js +112 -0
- package/esm/encode-utils.js +185 -0
- package/esm/encode.js +1219 -0
- package/esm/erl_json.js +289 -0
- package/esm/erl_str.js +1139 -0
- package/esm/flat.js +196 -0
- package/esm/http-message-signatures/httpbis.js +438 -0
- package/esm/http-message-signatures/index.js +4 -0
- package/esm/http-message-signatures/structured-header.js +105 -0
- package/esm/httpsig.js +658 -0
- package/esm/httpsig2.js +1097 -0
- package/esm/id.js +459 -0
- package/esm/index.js +4 -0
- package/esm/package.json +3 -0
- package/esm/send.js +124 -0
- package/esm/signer-utils.js +494 -0
- package/esm/signer.js +452 -0
- package/esm/structured.js +269 -0
- package/esm/test.js +6 -0
- package/esm/utils.js +28 -0
- package/package.json +28 -0
package/cjs/signer.js
ADDED
|
@@ -0,0 +1,606 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.sign = sign;
|
|
7
|
+
exports.signInternal = void 0;
|
|
8
|
+
exports.signer = signer;
|
|
9
|
+
Object.defineProperty(exports, "verify", {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
get: function get() {
|
|
12
|
+
return _signerUtils.verify;
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
var _httpsig = require("./httpsig.js");
|
|
16
|
+
var _structured = require("./structured.js");
|
|
17
|
+
var _erl_json = require("./erl_json.js");
|
|
18
|
+
var _encode = require("./encode.js");
|
|
19
|
+
var _encodeUtils = require("./encode-utils.js");
|
|
20
|
+
var _aoconnect = require("@permaweb/aoconnect");
|
|
21
|
+
var _send = require("./send.js");
|
|
22
|
+
var _signerUtils = require("./signer-utils.js");
|
|
23
|
+
var _excluded = ["path", "method"],
|
|
24
|
+
_excluded2 = ["body"];
|
|
25
|
+
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; }
|
|
26
|
+
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; }
|
|
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 }; })(); }
|
|
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 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; }
|
|
30
|
+
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; }
|
|
31
|
+
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; }
|
|
32
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
33
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
34
|
+
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
|
|
35
|
+
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
|
|
36
|
+
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
|
|
37
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
38
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
39
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
40
|
+
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
41
|
+
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
|
|
42
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } // Export verify from signer-utils.js for compatibility
|
|
43
|
+
// Helper to check if an array contains binary data
|
|
44
|
+
var _arrayHasBinaryData = function arrayHasBinaryData(arr) {
|
|
45
|
+
if (!Array.isArray(arr)) return false;
|
|
46
|
+
return arr.some(function (item) {
|
|
47
|
+
if ((0, _encodeUtils.isBytes)(item)) return true;
|
|
48
|
+
if (Array.isArray(item)) return _arrayHasBinaryData(item);
|
|
49
|
+
return false;
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
// Helper to check if a string contains non-printable characters
|
|
54
|
+
var hasNonPrintableChars = function hasNonPrintableChars(str) {
|
|
55
|
+
if (typeof str !== "string") return false;
|
|
56
|
+
for (var i = 0; i < str.length; i++) {
|
|
57
|
+
var code = str.charCodeAt(i);
|
|
58
|
+
// Allow only printable ASCII (32-126)
|
|
59
|
+
// Note: tabs (9), newlines (10), and carriage returns (13) are not allowed in HTTP headers
|
|
60
|
+
if (code < 32 || code > 126) {
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return false;
|
|
65
|
+
};
|
|
66
|
+
var isValid = function isValid(encoded) {
|
|
67
|
+
if (!encoded || _typeof(encoded) !== "object") return false;
|
|
68
|
+
|
|
69
|
+
// Check if all header values are valid for HTTP headers
|
|
70
|
+
for (var _i = 0, _Object$entries = Object.entries(encoded); _i < _Object$entries.length; _i++) {
|
|
71
|
+
var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
|
|
72
|
+
key = _Object$entries$_i[0],
|
|
73
|
+
value = _Object$entries$_i[1];
|
|
74
|
+
if (key === "body") {
|
|
75
|
+
// Body can be string, Buffer, or undefined
|
|
76
|
+
if (value !== undefined && typeof value !== "string" && !Buffer.isBuffer(value)) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
} else {
|
|
80
|
+
// All other fields (headers) must be strings or numbers
|
|
81
|
+
if (typeof value !== "string" && typeof value !== "number") {
|
|
82
|
+
// Check for Buffer in headers - this will fail HTTP signing
|
|
83
|
+
if (Buffer.isBuffer(value) || (0, _encodeUtils.isBytes)(value)) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Check if string contains non-printable characters
|
|
90
|
+
if (typeof value === "string" && hasNonPrintableChars(value)) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return true;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// Check if object contains any binary data or arrays with binary data
|
|
99
|
+
var hasBinaryData = function hasBinaryData(obj) {
|
|
100
|
+
for (var _i2 = 0, _Object$entries2 = Object.entries(obj); _i2 < _Object$entries2.length; _i2++) {
|
|
101
|
+
var _Object$entries2$_i = _slicedToArray(_Object$entries2[_i2], 2),
|
|
102
|
+
key = _Object$entries2$_i[0],
|
|
103
|
+
value = _Object$entries2$_i[1];
|
|
104
|
+
if (key === "path") continue;
|
|
105
|
+
if ((0, _encodeUtils.isBytes)(value)) {
|
|
106
|
+
return true;
|
|
107
|
+
} else if (Array.isArray(value) && _arrayHasBinaryData(value)) {
|
|
108
|
+
return true;
|
|
109
|
+
} else if (_typeof(value) === "object" && value !== null && !Array.isArray(value)) {
|
|
110
|
+
// Check nested objects
|
|
111
|
+
for (var _i3 = 0, _Object$entries3 = Object.entries(value); _i3 < _Object$entries3.length; _i3++) {
|
|
112
|
+
var _Object$entries3$_i = _slicedToArray(_Object$entries3[_i3], 2),
|
|
113
|
+
k = _Object$entries3$_i[0],
|
|
114
|
+
v = _Object$entries3$_i[1];
|
|
115
|
+
if ((0, _encodeUtils.isBytes)(v)) {
|
|
116
|
+
return true;
|
|
117
|
+
} else if (Array.isArray(v) && _arrayHasBinaryData(v)) {
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return false;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
// Helper to check if value is a simple array that should use structured fields
|
|
127
|
+
var isSimpleArray = function isSimpleArray(value) {
|
|
128
|
+
if (!Array.isArray(value)) return false;
|
|
129
|
+
return value.every(function (item) {
|
|
130
|
+
// Simple types that can be in structured field lists
|
|
131
|
+
if (typeof item === "string") return true;
|
|
132
|
+
if (typeof item === "number") return true;
|
|
133
|
+
if (typeof item === "boolean") return true;
|
|
134
|
+
if ((0, _encodeUtils.isBytes)(item)) return true;
|
|
135
|
+
|
|
136
|
+
// Complex types cannot be in structured field lists
|
|
137
|
+
if (item && _typeof(item) === "object") return false;
|
|
138
|
+
return true;
|
|
139
|
+
});
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
// Helper to encode array as structured field list
|
|
143
|
+
var encodeAsStructuredFieldList = function encodeAsStructuredFieldList(arr) {
|
|
144
|
+
return arr.map(function (item) {
|
|
145
|
+
if (typeof item === "string") {
|
|
146
|
+
// String values are quoted
|
|
147
|
+
return "\"".concat(item.replace(/\\/g, "\\\\").replace(/"/g, '\\"'), "\"");
|
|
148
|
+
} else if (typeof item === "number") {
|
|
149
|
+
// Numbers are bare
|
|
150
|
+
return String(item);
|
|
151
|
+
} else if (typeof item === "boolean") {
|
|
152
|
+
// Booleans use ?0 or ?1
|
|
153
|
+
return item ? "?1" : "?0";
|
|
154
|
+
} else if ((0, _encodeUtils.isBytes)(item)) {
|
|
155
|
+
// Binary data as byte sequences
|
|
156
|
+
var buffer = Buffer.isBuffer(item) ? item : Buffer.from(item);
|
|
157
|
+
return ":".concat(buffer.toString("base64"), ":");
|
|
158
|
+
} else {
|
|
159
|
+
// Fallback
|
|
160
|
+
return "\"".concat(String(item), "\"");
|
|
161
|
+
}
|
|
162
|
+
}).join(", ");
|
|
163
|
+
};
|
|
164
|
+
var smartSign = /*#__PURE__*/function () {
|
|
165
|
+
var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(obj, path) {
|
|
166
|
+
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;
|
|
167
|
+
return _regenerator().w(function (_context) {
|
|
168
|
+
while (1) switch (_context.p = _context.n) {
|
|
169
|
+
case 0:
|
|
170
|
+
_context.p = 0;
|
|
171
|
+
// Filter out undefined values
|
|
172
|
+
filtered = _filterUndefined(obj); // Check if we can encode everything as headers (no multipart needed)
|
|
173
|
+
canUseSimpleEncoding = true;
|
|
174
|
+
hasBodyField = false;
|
|
175
|
+
_i4 = 0, _Object$entries4 = Object.entries(filtered);
|
|
176
|
+
case 1:
|
|
177
|
+
if (!(_i4 < _Object$entries4.length)) {
|
|
178
|
+
_context.n = 6;
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
_Object$entries4$_i = _slicedToArray(_Object$entries4[_i4], 2), key = _Object$entries4$_i[0], value = _Object$entries4$_i[1];
|
|
182
|
+
if (!(key === "path")) {
|
|
183
|
+
_context.n = 2;
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
return _context.a(3, 5);
|
|
187
|
+
case 2:
|
|
188
|
+
if (!(key === "body" || key === "data")) {
|
|
189
|
+
_context.n = 3;
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
hasBodyField = true;
|
|
193
|
+
// Only use multipart if body/data is actually binary
|
|
194
|
+
if (!(0, _encodeUtils.isBytes)(value)) {
|
|
195
|
+
_context.n = 3;
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
198
|
+
canUseSimpleEncoding = false;
|
|
199
|
+
return _context.a(3, 6);
|
|
200
|
+
case 3:
|
|
201
|
+
if (!(value && _typeof(value) === "object" && !Array.isArray(value) && !(0, _encodeUtils.isBytes)(value))) {
|
|
202
|
+
_context.n = 4;
|
|
203
|
+
break;
|
|
204
|
+
}
|
|
205
|
+
if (!(Object.keys(value).length > 0)) {
|
|
206
|
+
_context.n = 4;
|
|
207
|
+
break;
|
|
208
|
+
}
|
|
209
|
+
canUseSimpleEncoding = false;
|
|
210
|
+
return _context.a(3, 6);
|
|
211
|
+
case 4:
|
|
212
|
+
if (!(Array.isArray(value) && !isSimpleArray(value))) {
|
|
213
|
+
_context.n = 5;
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
canUseSimpleEncoding = false;
|
|
217
|
+
return _context.a(3, 6);
|
|
218
|
+
case 5:
|
|
219
|
+
_i4++;
|
|
220
|
+
_context.n = 1;
|
|
221
|
+
break;
|
|
222
|
+
case 6:
|
|
223
|
+
if (!canUseSimpleEncoding) {
|
|
224
|
+
_context.n = 11;
|
|
225
|
+
break;
|
|
226
|
+
}
|
|
227
|
+
// Build a simple message that won't trigger multipart
|
|
228
|
+
message = {
|
|
229
|
+
path: path || filtered.path || "/~wao@1.0/httpsig"
|
|
230
|
+
};
|
|
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), {}, {
|
|
278
|
+
path: path || "/~wao@1.0/httpsig"
|
|
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: normalized.path || path || "/~wao@1.0/httpsig"
|
|
289
|
+
}); // httpsig_to expects the structured format
|
|
290
|
+
encoded = (0, _httpsig.httpsig_to)(flattened);
|
|
291
|
+
return _context.a(2, encoded);
|
|
292
|
+
case 13:
|
|
293
|
+
_context.p = 13;
|
|
294
|
+
_t = _context.v;
|
|
295
|
+
console.error("Encoding failed:", _t);
|
|
296
|
+
|
|
297
|
+
// Fallback: create a simple structure
|
|
298
|
+
_result = {
|
|
299
|
+
path: path || "/~wao@1.0/httpsig"
|
|
300
|
+
};
|
|
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
|
+
|
|
331
|
+
// Internal sign function that matches the original signature
|
|
332
|
+
var _sign = exports.signInternal = /*#__PURE__*/function () {
|
|
333
|
+
var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(obj, path) {
|
|
334
|
+
var filtered, encoded;
|
|
335
|
+
return _regenerator().w(function (_context2) {
|
|
336
|
+
while (1) switch (_context2.n) {
|
|
337
|
+
case 0:
|
|
338
|
+
// Filter out undefined values before processing
|
|
339
|
+
filtered = _filterUndefined(obj); // If object contains binary data, use enc() directly
|
|
340
|
+
if (!hasBinaryData(filtered)) {
|
|
341
|
+
_context2.n = 2;
|
|
342
|
+
break;
|
|
343
|
+
}
|
|
344
|
+
_context2.n = 1;
|
|
345
|
+
return smartSign(filtered, path);
|
|
346
|
+
case 1:
|
|
347
|
+
return _context2.a(2, _context2.v);
|
|
348
|
+
case 2:
|
|
349
|
+
// Otherwise use the standard pipeline
|
|
350
|
+
encoded = (0, _httpsig.httpsig_to)((0, _erl_json.normalize)((0, _structured.structured_from)((0, _erl_json.normalize)(_objectSpread(_objectSpread({}, filtered), {}, {
|
|
351
|
+
path: path || "/~wao@1.0/httpsig"
|
|
352
|
+
}))))); // Check if the encoded result is valid for HTTP headers
|
|
353
|
+
if (isValid(encoded)) {
|
|
354
|
+
_context2.n = 4;
|
|
355
|
+
break;
|
|
356
|
+
}
|
|
357
|
+
_context2.n = 3;
|
|
358
|
+
return smartSign(filtered, path);
|
|
359
|
+
case 3:
|
|
360
|
+
return _context2.a(2, _context2.v);
|
|
361
|
+
case 4:
|
|
362
|
+
return _context2.a(2, encoded);
|
|
363
|
+
}
|
|
364
|
+
}, _callee2);
|
|
365
|
+
}));
|
|
366
|
+
return function _sign(_x3, _x4) {
|
|
367
|
+
return _ref2.apply(this, arguments);
|
|
368
|
+
};
|
|
369
|
+
}();
|
|
370
|
+
|
|
371
|
+
// Helper to join URL and path
|
|
372
|
+
var joinUrl = function joinUrl(_ref3) {
|
|
373
|
+
var url = _ref3.url,
|
|
374
|
+
path = _ref3.path;
|
|
375
|
+
if (path.startsWith("http://") || path.startsWith("https://")) return path;
|
|
376
|
+
var normalizedPath = path.startsWith("/") ? path : "/" + path;
|
|
377
|
+
return url.endsWith("/") ? url.slice(0, -1) + normalizedPath : url + normalizedPath;
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
// Main sign function that matches signer.js API
|
|
381
|
+
function sign(_x5) {
|
|
382
|
+
return _sign2.apply(this, arguments);
|
|
383
|
+
} // Helper function to recursively filter out undefined values
|
|
384
|
+
function _sign2() {
|
|
385
|
+
_sign2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(_ref4) {
|
|
386
|
+
var url, path, encoded, jwk, _ref4$signPath, signPath, signer, _encoded$body, body, headers, _enc, headersObj, bodyData, url_path, _url, bodySize, lowercaseHeaders, _i0, _Object$entries0, _Object$entries0$_i, key, value, bodyKeys, isPath, signingFields, signedRequest, finalHeaders, _i1, _Object$entries1, _Object$entries1$_i, _key4, _value4, result;
|
|
387
|
+
return _regenerator().w(function (_context4) {
|
|
388
|
+
while (1) switch (_context4.n) {
|
|
389
|
+
case 0:
|
|
390
|
+
url = _ref4.url, path = _ref4.path, encoded = _ref4.msg, jwk = _ref4.jwk, _ref4$signPath = _ref4.signPath, signPath = _ref4$signPath === void 0 ? true : _ref4$signPath;
|
|
391
|
+
signer = (0, _aoconnect.createSigner)(jwk, url);
|
|
392
|
+
_encoded$body = encoded.body, body = _encoded$body === void 0 ? null : _encoded$body, headers = _objectWithoutProperties(encoded, _excluded2);
|
|
393
|
+
_enc = {
|
|
394
|
+
headers: headers
|
|
395
|
+
};
|
|
396
|
+
if (body) _enc.body = new Blob([body]);
|
|
397
|
+
headersObj = _enc.headers || {};
|
|
398
|
+
bodyData = _enc.body || undefined;
|
|
399
|
+
url_path = typeof signPath === "string" ? signPath : path;
|
|
400
|
+
_url = joinUrl({
|
|
401
|
+
url: url,
|
|
402
|
+
path: url_path
|
|
403
|
+
});
|
|
404
|
+
headersObj["path"] = path;
|
|
405
|
+
if (bodyData && !headersObj["content-length"]) {
|
|
406
|
+
bodySize = bodyData.size || bodyData.byteLength || 0;
|
|
407
|
+
if (bodySize > 0) headersObj["content-length"] = String(bodySize);
|
|
408
|
+
}
|
|
409
|
+
lowercaseHeaders = {};
|
|
410
|
+
for (_i0 = 0, _Object$entries0 = Object.entries(headersObj); _i0 < _Object$entries0.length; _i0++) {
|
|
411
|
+
_Object$entries0$_i = _slicedToArray(_Object$entries0[_i0], 2), key = _Object$entries0$_i[0], value = _Object$entries0$_i[1];
|
|
412
|
+
lowercaseHeaders[key.toLowerCase()] = value;
|
|
413
|
+
}
|
|
414
|
+
bodyKeys = headersObj["body-keys"] ? headersObj["body-keys"].replace(/"/g, "").split(",").map(function (k) {
|
|
415
|
+
return k.trim();
|
|
416
|
+
}) : [];
|
|
417
|
+
isPath = false;
|
|
418
|
+
signingFields = Object.keys(lowercaseHeaders).filter(function (key) {
|
|
419
|
+
if (key === "path") isPath = true;
|
|
420
|
+
return key !== "body-keys" && key !== "path" && !bodyKeys.includes(key);
|
|
421
|
+
});
|
|
422
|
+
if (signPath !== false && isPath) signingFields.push("@path");
|
|
423
|
+
_context4.n = 1;
|
|
424
|
+
return (0, _send.toHttpSigner)(signer)({
|
|
425
|
+
request: {
|
|
426
|
+
url: _url,
|
|
427
|
+
method: "POST",
|
|
428
|
+
headers: lowercaseHeaders
|
|
429
|
+
},
|
|
430
|
+
fields: signingFields
|
|
431
|
+
});
|
|
432
|
+
case 1:
|
|
433
|
+
signedRequest = _context4.v;
|
|
434
|
+
finalHeaders = {};
|
|
435
|
+
for (_i1 = 0, _Object$entries1 = Object.entries(headersObj); _i1 < _Object$entries1.length; _i1++) {
|
|
436
|
+
_Object$entries1$_i = _slicedToArray(_Object$entries1[_i1], 2), _key4 = _Object$entries1$_i[0], _value4 = _Object$entries1$_i[1];
|
|
437
|
+
finalHeaders[_key4] = _value4;
|
|
438
|
+
}
|
|
439
|
+
finalHeaders["signature"] = signedRequest.headers["signature"];
|
|
440
|
+
finalHeaders["signature-input"] = signedRequest.headers["signature-input"];
|
|
441
|
+
if (headersObj["body-keys"]) {
|
|
442
|
+
finalHeaders["body-keys"] = headersObj["body-keys"];
|
|
443
|
+
}
|
|
444
|
+
result = {
|
|
445
|
+
url: _url,
|
|
446
|
+
method: "POST",
|
|
447
|
+
headers: finalHeaders
|
|
448
|
+
};
|
|
449
|
+
if (bodyData) result.body = bodyData;
|
|
450
|
+
return _context4.a(2, result);
|
|
451
|
+
}
|
|
452
|
+
}, _callee4);
|
|
453
|
+
}));
|
|
454
|
+
return _sign2.apply(this, arguments);
|
|
455
|
+
}
|
|
456
|
+
var _filterUndefined = function filterUndefined(obj) {
|
|
457
|
+
if (obj === null || obj === undefined) return obj;
|
|
458
|
+
if (Array.isArray(obj)) {
|
|
459
|
+
return obj.map(_filterUndefined).filter(function (item) {
|
|
460
|
+
return item !== undefined;
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
if (_typeof(obj) === "object" && obj.constructor === Object) {
|
|
464
|
+
var filtered = {};
|
|
465
|
+
for (var _i7 = 0, _Object$entries7 = Object.entries(obj); _i7 < _Object$entries7.length; _i7++) {
|
|
466
|
+
var _Object$entries7$_i = _slicedToArray(_Object$entries7[_i7], 2),
|
|
467
|
+
key = _Object$entries7$_i[0],
|
|
468
|
+
value = _Object$entries7$_i[1];
|
|
469
|
+
var filteredValue = _filterUndefined(value);
|
|
470
|
+
if (filteredValue !== undefined) {
|
|
471
|
+
filtered[key] = filteredValue;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
return filtered;
|
|
475
|
+
}
|
|
476
|
+
return obj;
|
|
477
|
+
};
|
|
478
|
+
|
|
479
|
+
// Signer factory function that matches signer.js API
|
|
480
|
+
function signer(config) {
|
|
481
|
+
var signer = config.signer,
|
|
482
|
+
_config$url = config.url,
|
|
483
|
+
url = _config$url === void 0 ? "http://localhost:10001" : _config$url;
|
|
484
|
+
if (!signer) throw new Error("Signer is required for mainnet mode");
|
|
485
|
+
return /*#__PURE__*/function () {
|
|
486
|
+
var _ref5 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(fields) {
|
|
487
|
+
var _ref6,
|
|
488
|
+
_ref6$encoded,
|
|
489
|
+
_encoded,
|
|
490
|
+
_ref6$path,
|
|
491
|
+
_path,
|
|
492
|
+
_fields$path,
|
|
493
|
+
path,
|
|
494
|
+
_fields$method,
|
|
495
|
+
method,
|
|
496
|
+
aoFields,
|
|
497
|
+
filteredFields,
|
|
498
|
+
encoded,
|
|
499
|
+
headersObj,
|
|
500
|
+
body,
|
|
501
|
+
url_path,
|
|
502
|
+
_url,
|
|
503
|
+
bodySize,
|
|
504
|
+
lowercaseHeaders,
|
|
505
|
+
_i8,
|
|
506
|
+
_Object$entries8,
|
|
507
|
+
_Object$entries8$_i,
|
|
508
|
+
key,
|
|
509
|
+
value,
|
|
510
|
+
bodyKeys,
|
|
511
|
+
isPath,
|
|
512
|
+
signingFields,
|
|
513
|
+
signedRequest,
|
|
514
|
+
finalHeaders,
|
|
515
|
+
_i9,
|
|
516
|
+
_Object$entries9,
|
|
517
|
+
_Object$entries9$_i,
|
|
518
|
+
_key3,
|
|
519
|
+
_value3,
|
|
520
|
+
result,
|
|
521
|
+
_args3 = arguments,
|
|
522
|
+
_t2;
|
|
523
|
+
return _regenerator().w(function (_context3) {
|
|
524
|
+
while (1) switch (_context3.n) {
|
|
525
|
+
case 0:
|
|
526
|
+
_ref6 = _args3.length > 1 && _args3[1] !== undefined ? _args3[1] : {}, _ref6$encoded = _ref6.encoded, _encoded = _ref6$encoded === void 0 ? false : _ref6$encoded, _ref6$path = _ref6.path, _path = _ref6$path === void 0 ? true : _ref6$path;
|
|
527
|
+
_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, _excluded); // Filter out undefined values before encoding
|
|
528
|
+
filteredFields = _filterUndefined(aoFields);
|
|
529
|
+
if (!_encoded) {
|
|
530
|
+
_context3.n = 1;
|
|
531
|
+
break;
|
|
532
|
+
}
|
|
533
|
+
_t2 = filteredFields;
|
|
534
|
+
_context3.n = 3;
|
|
535
|
+
break;
|
|
536
|
+
case 1:
|
|
537
|
+
_context3.n = 2;
|
|
538
|
+
return (0, _encode.enc)(filteredFields);
|
|
539
|
+
case 2:
|
|
540
|
+
_t2 = _context3.v;
|
|
541
|
+
case 3:
|
|
542
|
+
encoded = _t2;
|
|
543
|
+
headersObj = encoded ? encoded.headers : {};
|
|
544
|
+
body = encoded ? encoded.body : undefined;
|
|
545
|
+
url_path = typeof _path === "string" ? _path : path;
|
|
546
|
+
_url = joinUrl({
|
|
547
|
+
url: url,
|
|
548
|
+
path: url_path
|
|
549
|
+
});
|
|
550
|
+
headersObj["path"] = path;
|
|
551
|
+
if (body && !headersObj["content-length"]) {
|
|
552
|
+
bodySize = body.size || body.byteLength || 0;
|
|
553
|
+
if (bodySize > 0) headersObj["content-length"] = String(bodySize);
|
|
554
|
+
}
|
|
555
|
+
lowercaseHeaders = {};
|
|
556
|
+
for (_i8 = 0, _Object$entries8 = Object.entries(headersObj); _i8 < _Object$entries8.length; _i8++) {
|
|
557
|
+
_Object$entries8$_i = _slicedToArray(_Object$entries8[_i8], 2), key = _Object$entries8$_i[0], value = _Object$entries8$_i[1];
|
|
558
|
+
lowercaseHeaders[key.toLowerCase()] = value;
|
|
559
|
+
}
|
|
560
|
+
bodyKeys = headersObj["body-keys"] ? headersObj["body-keys"].replace(/"/g, "").split(",").map(function (k) {
|
|
561
|
+
return k.trim();
|
|
562
|
+
}) : [];
|
|
563
|
+
isPath = false;
|
|
564
|
+
signingFields = Object.keys(lowercaseHeaders).filter(function (key) {
|
|
565
|
+
if (key === "path") isPath = true;
|
|
566
|
+
return key !== "body-keys" && key !== "path" && !bodyKeys.includes(key);
|
|
567
|
+
});
|
|
568
|
+
if (_path !== false && isPath) signingFields.push("@path");
|
|
569
|
+
_context3.n = 4;
|
|
570
|
+
return (0, _send.toHttpSigner)(signer)({
|
|
571
|
+
request: {
|
|
572
|
+
url: _url,
|
|
573
|
+
method: method,
|
|
574
|
+
headers: lowercaseHeaders
|
|
575
|
+
},
|
|
576
|
+
fields: signingFields
|
|
577
|
+
});
|
|
578
|
+
case 4:
|
|
579
|
+
signedRequest = _context3.v;
|
|
580
|
+
finalHeaders = {};
|
|
581
|
+
for (_i9 = 0, _Object$entries9 = Object.entries(headersObj); _i9 < _Object$entries9.length; _i9++) {
|
|
582
|
+
_Object$entries9$_i = _slicedToArray(_Object$entries9[_i9], 2), _key3 = _Object$entries9$_i[0], _value3 = _Object$entries9$_i[1];
|
|
583
|
+
finalHeaders[_key3] = _value3;
|
|
584
|
+
}
|
|
585
|
+
finalHeaders["signature"] = signedRequest.headers["signature"];
|
|
586
|
+
finalHeaders["signature-input"] = signedRequest.headers["signature-input"];
|
|
587
|
+
if (headersObj["body-keys"]) {
|
|
588
|
+
finalHeaders["body-keys"] = headersObj["body-keys"];
|
|
589
|
+
}
|
|
590
|
+
result = {
|
|
591
|
+
url: _url,
|
|
592
|
+
method: method,
|
|
593
|
+
headers: finalHeaders
|
|
594
|
+
};
|
|
595
|
+
if (body) result.body = body;
|
|
596
|
+
return _context3.a(2, result);
|
|
597
|
+
}
|
|
598
|
+
}, _callee3);
|
|
599
|
+
}));
|
|
600
|
+
return function (_x6) {
|
|
601
|
+
return _ref5.apply(this, arguments);
|
|
602
|
+
};
|
|
603
|
+
}();
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
// Export the internal sign function for backward compatibility
|