shogun-core 6.2.1 → 6.2.2

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.
@@ -68,15 +68,77 @@ const generateSignalSigningKeyPair = async () => {
68
68
  };
69
69
  exports.generateSignalSigningKeyPair = generateSignalSigningKeyPair;
70
70
  const exportSignalPublicKey = async (publicKey) => {
71
- // Handle fallback keys for testing
72
- if (publicKey &&
73
- typeof publicKey === "object" &&
74
- publicKey.algorithm &&
75
- !publicKey.extractable) {
76
- // This is a fallback key object, return a mock ArrayBuffer
77
- return new ArrayBuffer(32);
71
+ try {
72
+ // Check key properties
73
+ const algorithmName = publicKey?.algorithm?.name;
74
+ const isExtractable = publicKey?.extractable;
75
+ if (!isExtractable) {
76
+ throw new Error(`Cannot export non-extractable key. Algorithm: ${algorithmName}, Type: ${publicKey?.type}`);
77
+ }
78
+ // For Ed25519 keys, try raw format first, fallback to spki if needed
79
+ let exported;
80
+ if (algorithmName === "Ed25519") {
81
+ try {
82
+ exported = await crypto.subtle.exportKey("raw", publicKey);
83
+ // Validate that we got actual data (not all zeros)
84
+ const bytes = new Uint8Array(exported);
85
+ const isAllZeros = bytes.every((byte) => byte === 0);
86
+ if (isAllZeros) {
87
+ throw new Error("Export returned all zeros");
88
+ }
89
+ }
90
+ catch (rawError) {
91
+ // Try SPKI format as fallback for Ed25519
92
+ try {
93
+ const spki = await crypto.subtle.exportKey("spki", publicKey);
94
+ // Extract the raw 32-byte key from SPKI format (Ed25519 public key is last 32 bytes)
95
+ // SPKI structure: [header bytes...][32-byte public key]
96
+ const spkiBytes = new Uint8Array(spki);
97
+ if (spkiBytes.length < 32) {
98
+ throw new Error(`SPKI format too short: ${spkiBytes.length} bytes`);
99
+ }
100
+ // Extract last 32 bytes (Ed25519 public key)
101
+ exported = spkiBytes.slice(-32).buffer;
102
+ }
103
+ catch (spkiError) {
104
+ throw new Error(`Failed to export Ed25519 key in both raw and spki formats. Raw error: ${rawError instanceof Error ? rawError.message : rawError}, SPKI error: ${spkiError instanceof Error ? spkiError.message : spkiError}`);
105
+ }
106
+ }
107
+ }
108
+ else {
109
+ // For X25519 and other keys, use raw format
110
+ exported = await crypto.subtle.exportKey("raw", publicKey);
111
+ }
112
+ // Final validation
113
+ const finalBytes = new Uint8Array(exported);
114
+ const isAllZeros = finalBytes.every((byte) => byte === 0);
115
+ if (isAllZeros) {
116
+ throw new Error(`Exported key is all zeros. Algorithm: ${algorithmName}, Size: ${exported.byteLength} bytes`);
117
+ }
118
+ return exported;
119
+ }
120
+ catch (error) {
121
+ // If export fails, it might be a fallback/mock key
122
+ const errorMessage = error instanceof Error ? error.message : "Unknown error";
123
+ const algorithmName = publicKey?.algorithm?.name;
124
+ console.error("exportSignalPublicKey failed:", errorMessage, {
125
+ algorithm: algorithmName,
126
+ type: publicKey?.type,
127
+ extractable: publicKey?.extractable,
128
+ });
129
+ // Check if this is a fallback key object (doesn't have standard CryptoKey properties)
130
+ // Fallback keys are just plain objects, not real CryptoKey instances
131
+ if (publicKey &&
132
+ typeof publicKey === "object" &&
133
+ publicKey.algorithm &&
134
+ (!publicKey.extractable || !(publicKey instanceof CryptoKey))) {
135
+ // This is a fallback key object - we can't export it, so we need to generate a proper key
136
+ // This should not happen in production - it means Ed25519 is not supported
137
+ throw new Error(`Cannot export fallback ${algorithmName} key. ${algorithmName} is not supported in this environment. Error: ${errorMessage}`);
138
+ }
139
+ // Re-throw if it's not a fallback key issue
140
+ throw error;
78
141
  }
79
- return await crypto.subtle.exportKey("raw", publicKey);
80
142
  };
81
143
  exports.exportSignalPublicKey = exportSignalPublicKey;
82
144
  const importSignalPublicKey = async (keyBytes) => {
@@ -194,13 +256,30 @@ const getSignalPublicKeyBundle = async (user) => {
194
256
  try {
195
257
  console.log("Exporting identity X25519 key...");
196
258
  const identityKey = await (0, exports.exportSignalPublicKey)(user.identityKeyPair.publicKey);
259
+ console.log(`✓ Identity X25519 key exported: ${identityKey.byteLength} bytes`);
197
260
  console.log("Exporting identity signing key...");
261
+ const identitySigningKeyBefore = user.identitySigningKeyPair.publicKey;
262
+ console.log("Identity signing key properties:", {
263
+ algorithm: identitySigningKeyBefore?.algorithm?.name,
264
+ type: identitySigningKeyBefore?.type,
265
+ extractable: identitySigningKeyBefore?.extractable,
266
+ });
198
267
  const identitySigningKey = await (0, exports.exportSignalPublicKey)(user.identitySigningKeyPair.publicKey);
268
+ const signingKeyBytes = new Uint8Array(identitySigningKey);
269
+ console.log(`✓ Identity signing key exported: ${identitySigningKey.byteLength} bytes, first 8 bytes:`, Array.from(signingKeyBytes.slice(0, 8)));
270
+ const isAllZeros = signingKeyBytes.every((byte) => byte === 0);
271
+ if (isAllZeros) {
272
+ throw new Error("Identity signing key export returned all zeros - this should have been caught by exportSignalPublicKey");
273
+ }
199
274
  console.log("Exporting signed prekey...");
200
275
  const signedPrekey = await (0, exports.exportSignalPublicKey)(user.signedPrekeyPair.publicKey);
276
+ console.log(`✓ Signed prekey exported: ${signedPrekey.byteLength} bytes`);
201
277
  const oneTimePrekey = user.oneTimePrekeyPairs.length > 0
202
278
  ? await (0, exports.exportSignalPublicKey)(user.oneTimePrekeyPairs[0].publicKey)
203
279
  : null;
280
+ if (oneTimePrekey) {
281
+ console.log(`✓ One-time prekey exported: ${oneTimePrekey.byteLength} bytes`);
282
+ }
204
283
  const bundle = {
205
284
  identityKey, // X25519 key
206
285
  identitySigningKey, // Ed25519 key
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shogun-core",
3
- "version": "6.2.1",
3
+ "version": "6.2.2",
4
4
  "description": "SHOGUN CORE - Core library for Shogun Ecosystem",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.esm.js",