nostr-crypto-utils 0.5.2 → 0.5.3
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/browser/crypto.nostr-crypto-utils.min.js +1 -1
- package/dist/browser/crypto.nostr-crypto-utils.min.js.map +3 -3
- package/dist/browser/main.nostr-crypto-utils.min.js +1 -1
- package/dist/browser/main.nostr-crypto-utils.min.js.map +3 -3
- package/dist/cjs/crypto.js +19 -5
- package/dist/cjs/crypto.js.map +1 -1
- package/dist/cjs/encoding/base64.js +29 -12
- package/dist/cjs/encoding/base64.js.map +1 -1
- package/dist/esm/crypto.js +19 -5
- package/dist/esm/crypto.js.map +1 -1
- package/dist/esm/encoding/base64.js +29 -12
- package/dist/esm/encoding/base64.js.map +1 -1
- package/dist/types/crypto.d.ts +8 -6
- package/dist/types/crypto.d.ts.map +1 -1
- package/dist/types/encoding/base64.d.ts +7 -6
- package/dist/types/encoding/base64.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/crypto.ts +24 -8
- package/src/encoding/base64.ts +31 -14
package/src/encoding/base64.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Base64 encoding utilities for Nostr
|
|
3
3
|
* Provides consistent base64 encoding/decoding across all Nostr-related projects
|
|
4
|
+
* Uses browser-compatible APIs (no Node.js Buffer dependency)
|
|
4
5
|
*/
|
|
5
6
|
|
|
6
7
|
/**
|
|
@@ -9,7 +10,8 @@
|
|
|
9
10
|
* @returns Base64 string
|
|
10
11
|
*/
|
|
11
12
|
export function stringToBase64(str: string): string {
|
|
12
|
-
|
|
13
|
+
const bytes = new TextEncoder().encode(str);
|
|
14
|
+
return bytesToBase64(bytes);
|
|
13
15
|
}
|
|
14
16
|
|
|
15
17
|
/**
|
|
@@ -22,29 +24,30 @@ export function base64ToString(base64: string): string {
|
|
|
22
24
|
if (!isValidBase64(base64)) {
|
|
23
25
|
throw new Error('Invalid base64 string');
|
|
24
26
|
}
|
|
25
|
-
|
|
27
|
+
const bytes = base64ToBytes(base64);
|
|
28
|
+
return new TextDecoder().decode(bytes);
|
|
26
29
|
}
|
|
27
30
|
|
|
28
31
|
/**
|
|
29
|
-
* Convert
|
|
30
|
-
* @param buffer
|
|
32
|
+
* Convert Uint8Array to base64
|
|
33
|
+
* @param buffer Uint8Array to convert
|
|
31
34
|
* @returns Base64 string
|
|
32
35
|
*/
|
|
33
|
-
export function bufferToBase64(buffer:
|
|
34
|
-
return buffer
|
|
36
|
+
export function bufferToBase64(buffer: Uint8Array): string {
|
|
37
|
+
return bytesToBase64(buffer);
|
|
35
38
|
}
|
|
36
39
|
|
|
37
40
|
/**
|
|
38
|
-
* Convert base64 to
|
|
41
|
+
* Convert base64 to Uint8Array
|
|
39
42
|
* @param base64 Base64 string to convert
|
|
40
|
-
* @returns
|
|
43
|
+
* @returns Uint8Array
|
|
41
44
|
* @throws Error if base64 string is invalid
|
|
42
45
|
*/
|
|
43
|
-
export function base64ToBuffer(base64: string):
|
|
46
|
+
export function base64ToBuffer(base64: string): Uint8Array {
|
|
44
47
|
if (!isValidBase64(base64)) {
|
|
45
48
|
throw new Error('Invalid base64 string');
|
|
46
49
|
}
|
|
47
|
-
return
|
|
50
|
+
return base64ToBytes(base64);
|
|
48
51
|
}
|
|
49
52
|
|
|
50
53
|
/**
|
|
@@ -90,7 +93,11 @@ export function hexToBase64(hex: string): string {
|
|
|
90
93
|
if (!hex.match(/^[0-9a-fA-F]*$/)) {
|
|
91
94
|
throw new Error('Invalid hex string');
|
|
92
95
|
}
|
|
93
|
-
|
|
96
|
+
const bytes = new Uint8Array(hex.length / 2);
|
|
97
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
98
|
+
bytes[i / 2] = parseInt(hex.substring(i, i + 2), 16);
|
|
99
|
+
}
|
|
100
|
+
return bytesToBase64(bytes);
|
|
94
101
|
}
|
|
95
102
|
|
|
96
103
|
/**
|
|
@@ -103,7 +110,8 @@ export function base64ToHex(base64: string): string {
|
|
|
103
110
|
if (!isValidBase64(base64)) {
|
|
104
111
|
throw new Error('Invalid base64 string');
|
|
105
112
|
}
|
|
106
|
-
|
|
113
|
+
const bytes = base64ToBytes(base64);
|
|
114
|
+
return Array.from(bytes).map(b => b.toString(16).padStart(2, '0')).join('');
|
|
107
115
|
}
|
|
108
116
|
|
|
109
117
|
/**
|
|
@@ -112,7 +120,11 @@ export function base64ToHex(base64: string): string {
|
|
|
112
120
|
* @returns Base64 string
|
|
113
121
|
*/
|
|
114
122
|
export function bytesToBase64(bytes: Uint8Array): string {
|
|
115
|
-
|
|
123
|
+
let binary = '';
|
|
124
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
125
|
+
binary += String.fromCharCode(bytes[i]);
|
|
126
|
+
}
|
|
127
|
+
return btoa(binary);
|
|
116
128
|
}
|
|
117
129
|
|
|
118
130
|
/**
|
|
@@ -125,7 +137,12 @@ export function base64ToBytes(base64: string): Uint8Array {
|
|
|
125
137
|
if (!isValidBase64(base64)) {
|
|
126
138
|
throw new Error('Invalid base64 string');
|
|
127
139
|
}
|
|
128
|
-
|
|
140
|
+
const binary = atob(base64);
|
|
141
|
+
const bytes = new Uint8Array(binary.length);
|
|
142
|
+
for (let i = 0; i < binary.length; i++) {
|
|
143
|
+
bytes[i] = binary.charCodeAt(i);
|
|
144
|
+
}
|
|
145
|
+
return bytes;
|
|
129
146
|
}
|
|
130
147
|
|
|
131
148
|
/**
|