@armory-sh/base 0.2.26-beta.20260216.68 → 0.2.27-alpha.23.73
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +44 -15
- package/dist/utils/base64.d.ts +4 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -56,17 +56,51 @@ function combineSignature(v, r, s) {
|
|
|
56
56
|
return `0x${rClean}${sClean}${vHex}`;
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
+
// src/utils/base64.ts
|
|
60
|
+
var textEncoder = new TextEncoder();
|
|
61
|
+
var textDecoder = new TextDecoder();
|
|
62
|
+
function toBase64(bytes) {
|
|
63
|
+
if (typeof btoa === "function") {
|
|
64
|
+
let binary = "";
|
|
65
|
+
for (let index = 0; index < bytes.length; index += 1) {
|
|
66
|
+
binary += String.fromCharCode(bytes[index]);
|
|
67
|
+
}
|
|
68
|
+
return btoa(binary);
|
|
69
|
+
}
|
|
70
|
+
throw new Error("No base64 encoder available in this runtime");
|
|
71
|
+
}
|
|
72
|
+
function fromBase64(base64) {
|
|
73
|
+
if (typeof atob === "function") {
|
|
74
|
+
const binary = atob(base64);
|
|
75
|
+
const bytes = new Uint8Array(binary.length);
|
|
76
|
+
for (let index = 0; index < binary.length; index += 1) {
|
|
77
|
+
bytes[index] = binary.charCodeAt(index);
|
|
78
|
+
}
|
|
79
|
+
return bytes;
|
|
80
|
+
}
|
|
81
|
+
throw new Error("No base64 decoder available in this runtime");
|
|
82
|
+
}
|
|
83
|
+
function encodeUtf8ToBase64(value) {
|
|
84
|
+
const bytes = textEncoder.encode(value);
|
|
85
|
+
return toBase64(bytes);
|
|
86
|
+
}
|
|
87
|
+
function decodeBase64ToUtf8(value) {
|
|
88
|
+
const bytes = fromBase64(value);
|
|
89
|
+
return textDecoder.decode(bytes);
|
|
90
|
+
}
|
|
91
|
+
function toBase64Url(base64) {
|
|
92
|
+
return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
93
|
+
}
|
|
94
|
+
function normalizeBase64Url(value) {
|
|
95
|
+
return value.replace(/-/g, "+").replace(/_/g, "/").padEnd(Math.ceil(value.length / 4) * 4, "=");
|
|
96
|
+
}
|
|
97
|
+
|
|
59
98
|
// src/encoding/x402.ts
|
|
60
99
|
function safeBase64Encode(str) {
|
|
61
|
-
return
|
|
100
|
+
return toBase64Url(encodeUtf8ToBase64(str));
|
|
62
101
|
}
|
|
63
102
|
function safeBase64Decode(str) {
|
|
64
|
-
|
|
65
|
-
if (padding !== 4) {
|
|
66
|
-
str += "=".repeat(padding);
|
|
67
|
-
}
|
|
68
|
-
str = str.replace(/-/g, "+").replace(/_/g, "/");
|
|
69
|
-
return Buffer.from(str, "base64").toString("utf-8");
|
|
103
|
+
return decodeBase64ToUtf8(normalizeBase64Url(str));
|
|
70
104
|
}
|
|
71
105
|
function encodePayment(payload) {
|
|
72
106
|
if (!isPaymentPayload(payload)) {
|
|
@@ -1181,15 +1215,10 @@ function validateRouteConfig(config) {
|
|
|
1181
1215
|
|
|
1182
1216
|
// src/encoding.ts
|
|
1183
1217
|
function safeBase64Decode2(str) {
|
|
1184
|
-
|
|
1185
|
-
if (padding !== 4) {
|
|
1186
|
-
str += "=".repeat(padding);
|
|
1187
|
-
}
|
|
1188
|
-
str = str.replace(/-/g, "+").replace(/_/g, "/");
|
|
1189
|
-
return Buffer.from(str, "base64").toString("utf-8");
|
|
1218
|
+
return decodeBase64ToUtf8(normalizeBase64Url(str));
|
|
1190
1219
|
}
|
|
1191
1220
|
function safeBase64Encode2(str) {
|
|
1192
|
-
return
|
|
1221
|
+
return toBase64Url(encodeUtf8ToBase64(str));
|
|
1193
1222
|
}
|
|
1194
1223
|
var base64JsonEncode = (data) => safeBase64Encode2(JSON.stringify(data));
|
|
1195
1224
|
var base64JsonDecode = (encoded) => JSON.parse(safeBase64Decode2(encoded));
|
|
@@ -1296,7 +1325,7 @@ function decodePayloadHeader(headerValue, defaults) {
|
|
|
1296
1325
|
}
|
|
1297
1326
|
const normalized = headerValue.replace(/-/g, "+").replace(/_/g, "/").padEnd(Math.ceil(headerValue.length / 4) * 4, "=");
|
|
1298
1327
|
try {
|
|
1299
|
-
const decoded = JSON.parse(
|
|
1328
|
+
const decoded = JSON.parse(decodeBase64ToUtf8(normalized));
|
|
1300
1329
|
if (isPaymentPayload(decoded)) return decoded;
|
|
1301
1330
|
if (isCompactV2Payload(decoded)) {
|
|
1302
1331
|
const compact = decoded;
|