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.
Files changed (44) hide show
  1. package/cjs/bin_to_str.js +44 -0
  2. package/cjs/collect-body-keys.js +470 -0
  3. package/cjs/encode-array-item.js +110 -0
  4. package/cjs/encode-utils.js +236 -0
  5. package/cjs/encode.js +1318 -0
  6. package/cjs/erl_json.js +317 -0
  7. package/cjs/erl_str.js +1037 -0
  8. package/cjs/flat.js +222 -0
  9. package/cjs/http-message-signatures/httpbis.js +489 -0
  10. package/cjs/http-message-signatures/index.js +25 -0
  11. package/cjs/http-message-signatures/structured-header.js +129 -0
  12. package/cjs/httpsig.js +716 -0
  13. package/cjs/httpsig2.js +1160 -0
  14. package/cjs/id.js +470 -0
  15. package/cjs/index.js +63 -0
  16. package/cjs/send.js +194 -0
  17. package/cjs/signer-utils.js +617 -0
  18. package/cjs/signer.js +606 -0
  19. package/cjs/structured.js +296 -0
  20. package/cjs/test.js +27 -0
  21. package/cjs/utils.js +42 -0
  22. package/esm/bin_to_str.js +46 -0
  23. package/esm/collect-body-keys.js +436 -0
  24. package/esm/encode-array-item.js +112 -0
  25. package/esm/encode-utils.js +185 -0
  26. package/esm/encode.js +1219 -0
  27. package/esm/erl_json.js +289 -0
  28. package/esm/erl_str.js +1139 -0
  29. package/esm/flat.js +196 -0
  30. package/esm/http-message-signatures/httpbis.js +438 -0
  31. package/esm/http-message-signatures/index.js +4 -0
  32. package/esm/http-message-signatures/structured-header.js +105 -0
  33. package/esm/httpsig.js +658 -0
  34. package/esm/httpsig2.js +1097 -0
  35. package/esm/id.js +459 -0
  36. package/esm/index.js +4 -0
  37. package/esm/package.json +3 -0
  38. package/esm/send.js +124 -0
  39. package/esm/signer-utils.js +494 -0
  40. package/esm/signer.js +452 -0
  41. package/esm/structured.js +269 -0
  42. package/esm/test.js +6 -0
  43. package/esm/utils.js +28 -0
  44. package/package.json +28 -0
@@ -0,0 +1,296 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.structured_from = structured_from;
7
+ exports.structured_to = structured_to;
8
+ var _erl_str = require("./erl_str.js");
9
+ function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
10
+ 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."); }
11
+ 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; } }
12
+ function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
13
+ 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); }
14
+ function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
15
+ 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; } }
16
+ 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; } /**
17
+ * Structured field codec for JavaScript-Erlang interoperability
18
+ * Implements the same behavior as dev_codec_structured.erl
19
+ */
20
+ /**
21
+ * Convert from structured format
22
+ * @param {string|object} input - Erlang term string or JavaScript object
23
+ * @returns {object} - Processed object
24
+ */
25
+ function structured_from(input) {
26
+ // If input is a string (Erlang response), parse it
27
+ if (typeof input === "string") {
28
+ return (0, _erl_str.erl_str_from)(input, false);
29
+ }
30
+
31
+ // Otherwise process the object like Erlang's from/1
32
+ return from(input);
33
+ }
34
+
35
+ /**
36
+ * Convert to structured format
37
+ * @param {*} obj - JavaScript object
38
+ * @returns {*} - Structured format object
39
+ */
40
+ function structured_to(obj) {
41
+ // TODO: Implement the inverse of structured_from
42
+ return obj;
43
+ }
44
+
45
+ /**
46
+ * Check if an array should be converted to numbered map
47
+ * Rules based on Erlang behavior:
48
+ * 1. Contains any objects/maps → convert
49
+ * 2. Contains empty arrays (but NOT empty buffers) → convert
50
+ * 3. All items are arrays (array of arrays) → convert
51
+ * 4. Otherwise → encode as string
52
+ */
53
+ function shouldConvertToNumberedMap(arr) {
54
+ var allArrays = true;
55
+ var hasObjects = false;
56
+ var hasEmptyArrays = false;
57
+ var _iterator = _createForOfIteratorHelper(arr),
58
+ _step;
59
+ try {
60
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
61
+ var item = _step.value;
62
+ // Check for objects (not arrays or buffers)
63
+ if (_typeof(item) === "object" && item !== null && !Array.isArray(item) && !Buffer.isBuffer(item)) {
64
+ hasObjects = true;
65
+ }
66
+ // Check for empty arrays only (NOT empty buffers)
67
+ else if (Array.isArray(item) && item.length === 0) {
68
+ hasEmptyArrays = true;
69
+ }
70
+ // Track if all items are arrays
71
+ else if (!Array.isArray(item)) {
72
+ allArrays = false;
73
+ }
74
+ }
75
+
76
+ // Convert if: has objects, has empty arrays, or all items are non-empty arrays
77
+ } catch (err) {
78
+ _iterator.e(err);
79
+ } finally {
80
+ _iterator.f();
81
+ }
82
+ return hasObjects || hasEmptyArrays || allArrays && arr.length > 0 && arr.every(function (item) {
83
+ return Array.isArray(item);
84
+ });
85
+ }
86
+
87
+ /**
88
+ * Implementation of Erlang's dev_codec_structured:from/1
89
+ */
90
+ function from(msg) {
91
+ // Handle non-map values
92
+ if (msg instanceof Buffer || _typeof(msg) !== "object" || msg === null || Array.isArray(msg)) {
93
+ return msg;
94
+ }
95
+
96
+ // Normalize keys first
97
+ var normalizedMap = {};
98
+ for (var _i = 0, _Object$entries = Object.entries(msg); _i < _Object$entries.length; _i++) {
99
+ var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
100
+ key = _Object$entries$_i[0],
101
+ value = _Object$entries$_i[1];
102
+ var normKey = key.toLowerCase();
103
+ normalizedMap[normKey] = value;
104
+ }
105
+
106
+ // Get sorted keys (normalized)
107
+ var sortedKeys = Object.keys(normalizedMap).sort();
108
+ var types = [];
109
+ var values = [];
110
+
111
+ // Process each key in sorted order
112
+ var _iterator2 = _createForOfIteratorHelper(sortedKeys),
113
+ _step2;
114
+ try {
115
+ var _loop = function _loop() {
116
+ var normKey = _step2.value;
117
+ var value = normalizedMap[normKey];
118
+
119
+ // Handle empty values
120
+ if (value === "" || value instanceof Buffer && value.length === 0) {
121
+ types.push([normKey, "empty-binary"]);
122
+ return 0; // continue
123
+ }
124
+ if (Array.isArray(value) && value.length === 0) {
125
+ types.push([normKey, "empty-list"]);
126
+ return 0; // continue
127
+ }
128
+ if (_typeof(value) === "object" && value !== null && !Array.isArray(value) && !(value instanceof Buffer) && Object.keys(value).length === 0) {
129
+ types.push([normKey, "empty-message"]);
130
+ return 0; // continue
131
+ }
132
+
133
+ // Handle binary/string values
134
+ if (value instanceof Buffer || value instanceof Uint8Array) {
135
+ // Keep buffers as buffers
136
+ values.push([normKey, value]);
137
+ return 0; // continue
138
+ }
139
+ if (typeof value === "string") {
140
+ // Keep strings as strings
141
+ values.push([normKey, value]);
142
+ return 0; // continue
143
+ }
144
+
145
+ // Handle nested maps
146
+ if (_typeof(value) === "object" && !Array.isArray(value) && value !== null) {
147
+ values.push([normKey, from(value)]);
148
+ return 0; // continue
149
+ }
150
+
151
+ // Handle arrays
152
+ if (Array.isArray(value) && value.length > 0) {
153
+ if (shouldConvertToNumberedMap(value)) {
154
+ // Convert to numbered map (1-based indexing)
155
+ var numberedMap = {};
156
+ value.forEach(function (item, idx) {
157
+ numberedMap[(idx + 1).toString()] = item;
158
+ });
159
+ types.push([normKey, "list"]);
160
+ values.push([normKey, from(numberedMap)]);
161
+ } else {
162
+ // Encode as list string
163
+ var _encodeValue = encodeValue(value),
164
+ _encodeValue2 = _slicedToArray(_encodeValue, 2),
165
+ type = _encodeValue2[0],
166
+ encoded = _encodeValue2[1];
167
+ types.push([normKey, type]);
168
+ values.push([normKey, encoded]);
169
+ }
170
+ return 0; // continue
171
+ }
172
+
173
+ // Handle typed values (need encoding)
174
+ if (_typeof(value) === "symbol" || typeof value === "number" || Array.isArray(value) || typeof value === "boolean" || value === null) {
175
+ var _encodeValue3 = encodeValue(value),
176
+ _encodeValue4 = _slicedToArray(_encodeValue3, 2),
177
+ _type = _encodeValue4[0],
178
+ _encoded = _encodeValue4[1];
179
+ types.push([normKey, _type]);
180
+ values.push([normKey, _encoded]);
181
+ return 0; // continue
182
+ }
183
+ },
184
+ _ret;
185
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
186
+ _ret = _loop();
187
+ if (_ret === 0) continue;
188
+ }
189
+
190
+ // Build result
191
+ } catch (err) {
192
+ _iterator2.e(err);
193
+ } finally {
194
+ _iterator2.f();
195
+ }
196
+ var result = {};
197
+
198
+ // Add ao-types if present
199
+ if (types.length > 0) {
200
+ result["ao-types"] = types.map(function (_ref) {
201
+ var _ref2 = _slicedToArray(_ref, 2),
202
+ k = _ref2[0],
203
+ t = _ref2[1];
204
+ return "".concat(k, "=\"").concat(t, "\"");
205
+ }).join(", ");
206
+ }
207
+
208
+ // Add values (but NOT empty values)
209
+ for (var _i2 = 0, _values = values; _i2 < _values.length; _i2++) {
210
+ var _values$_i = _slicedToArray(_values[_i2], 2),
211
+ k = _values$_i[0],
212
+ v = _values$_i[1];
213
+ result[k] = v;
214
+ }
215
+ return result;
216
+ }
217
+
218
+ /**
219
+ * Encode a value with its type
220
+ */
221
+ function encodeValue(value) {
222
+ // Null (as atom)
223
+ if (value === null) {
224
+ return ["atom", '"null"'];
225
+ }
226
+
227
+ // Integer
228
+ if (typeof value === "number" && Number.isInteger(value)) {
229
+ return ["integer", value.toString()];
230
+ }
231
+
232
+ // Float
233
+ if (typeof value === "number") {
234
+ // Format like Erlang with scientific notation
235
+ var str = value.toExponential(20);
236
+ // Remove trailing zeros but keep at least one
237
+ str = str.replace(/(\.\d*?)0+e/, "$1e").replace(/\.e/, ".0e");
238
+ // Ensure 2-digit exponent
239
+ str = str.replace(/e([+-])(\d)$/, "e$10$2");
240
+ return ["float", str];
241
+ }
242
+
243
+ // Boolean (as atom)
244
+ if (typeof value === "boolean") {
245
+ return ["atom", "\"".concat(value, "\"")];
246
+ }
247
+
248
+ // Symbol (as atom)
249
+ if (_typeof(value) === "symbol") {
250
+ var name = Symbol.keyFor(value) || value.description || "";
251
+ return ["atom", "\"".concat(name, "\"")];
252
+ }
253
+
254
+ // List
255
+ if (Array.isArray(value)) {
256
+ var parts = [];
257
+ var _iterator3 = _createForOfIteratorHelper(value),
258
+ _step3;
259
+ try {
260
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
261
+ var item = _step3.value;
262
+ if (item instanceof Buffer) {
263
+ // Empty buffer => empty string
264
+ parts.push(item.length === 0 ? '""' : "\"".concat(item.toString(), "\""));
265
+ } else if (typeof item === "string") {
266
+ parts.push("\"".concat(item, "\""));
267
+ } else {
268
+ var _encodeValue5 = encodeValue(item),
269
+ _encodeValue6 = _slicedToArray(_encodeValue5, 2),
270
+ itemType = _encodeValue6[0],
271
+ itemEncoded = _encodeValue6[1];
272
+ if (itemType === "list") {
273
+ // Escape nested list quotes
274
+ var escaped = itemEncoded.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
275
+ parts.push("\"(ao-type-list) ".concat(escaped, "\""));
276
+ } else if (itemType === "atom") {
277
+ // Escape atom quotes
278
+ var _escaped = itemEncoded.replace(/"/g, '\\"');
279
+ parts.push("\"(ao-type-atom) ".concat(_escaped, "\""));
280
+ } else {
281
+ parts.push("\"(ao-type-".concat(itemType, ") ").concat(itemEncoded, "\""));
282
+ }
283
+ }
284
+ }
285
+ } catch (err) {
286
+ _iterator3.e(err);
287
+ } finally {
288
+ _iterator3.f();
289
+ }
290
+ return ["list", parts.join(", ")];
291
+ }
292
+ if (value instanceof Buffer) {
293
+ return ["binary", value];
294
+ }
295
+ return ["unknown", String(value)];
296
+ }
package/cjs/test.js ADDED
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "acc", {
7
+ enumerable: true,
8
+ get: function get() {
9
+ return _accounts.acc;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "toAddr", {
13
+ enumerable: true,
14
+ get: function get() {
15
+ return _utils.toAddr;
16
+ }
17
+ });
18
+ Object.defineProperty(exports, "wait", {
19
+ enumerable: true,
20
+ get: function get() {
21
+ return _utils.wait;
22
+ }
23
+ });
24
+ var _fs = require("fs");
25
+ var _path = require("path");
26
+ var _accounts = require("./accounts.js");
27
+ var _utils = require("./utils.js");
package/cjs/utils.js ADDED
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.toAddr = toAddr;
7
+ var _fastSha = _interopRequireDefault(require("fast-sha256"));
8
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
9
+ function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
10
+ 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; } }
11
+ 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; }
12
+ function base64urlDecode(str) {
13
+ str = str.replace(/-/g, "+").replace(/_/g, "/");
14
+ var pad = str.length % 4;
15
+ if (pad === 2) str += "==";else if (pad === 3) str += "=";else if (pad !== 0) throw new Error("Invalid base64url string");
16
+ var bin = atob(str);
17
+ var bytes = new Uint8Array(bin.length);
18
+ for (var i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
19
+ return bytes;
20
+ }
21
+ function base64urlEncode(bytes) {
22
+ var bin = "";
23
+ var _iterator = _createForOfIteratorHelper(bytes),
24
+ _step;
25
+ try {
26
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
27
+ var b = _step.value;
28
+ bin += String.fromCharCode(b);
29
+ }
30
+ } catch (err) {
31
+ _iterator.e(err);
32
+ } finally {
33
+ _iterator.f();
34
+ }
35
+ var b64 = btoa(bin);
36
+ return b64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/g, "");
37
+ }
38
+ function toAddr(n) {
39
+ var pubBytes = base64urlDecode(n);
40
+ var hash = (0, _fastSha["default"])(pubBytes);
41
+ return base64urlEncode(hash);
42
+ }
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Convert a Buffer to string if it contains valid UTF-8 text
3
+ * @param {Buffer} buffer - Buffer to check and potentially convert
4
+ * @returns {string|Buffer} - String if valid UTF-8, otherwise original Buffer
5
+ */
6
+ export default function bin_to_str(buffer) {
7
+ if (!Buffer.isBuffer(buffer)) {
8
+ return buffer
9
+ }
10
+
11
+ // Empty buffer stays as buffer
12
+ if (buffer.length === 0) {
13
+ return buffer
14
+ }
15
+
16
+ try {
17
+ const str = buffer.toString("utf8")
18
+ // Check if it's valid UTF-8 by seeing if it round-trips correctly
19
+ if (Buffer.from(str, "utf8").equals(buffer)) {
20
+ // Additional check: ensure all characters are printable or common whitespace
21
+ // This prevents converting binary data that happens to be valid UTF-8
22
+ let isPrintable = true
23
+ for (let i = 0; i < str.length; i++) {
24
+ const code = str.charCodeAt(i)
25
+ // Allow printable ASCII (32-126) and common whitespace (tab, newline, carriage return)
26
+ if (
27
+ !(code >= 32 && code <= 126) &&
28
+ code !== 9 &&
29
+ code !== 10 &&
30
+ code !== 13
31
+ ) {
32
+ isPrintable = false
33
+ break
34
+ }
35
+ }
36
+
37
+ if (isPrintable) {
38
+ return str
39
+ }
40
+ }
41
+ } catch (e) {
42
+ // Not valid UTF-8, return original buffer
43
+ }
44
+
45
+ return buffer
46
+ }