@worldcoin/idkit-core 4.0.1-dev.fe98789 → 4.0.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.
package/dist/index.cjs CHANGED
@@ -1,5 +1,11 @@
1
1
  'use strict';
2
2
 
3
+ var sha3 = require('@noble/hashes/sha3');
4
+ var utils = require('@noble/hashes/utils');
5
+ var hmac = require('@noble/hashes/hmac');
6
+ var sha2 = require('@noble/hashes/sha2');
7
+ var secp256k1 = require('@noble/secp256k1');
8
+
3
9
  var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
4
10
  var __defProp = Object.defineProperty;
5
11
  var __export = (target, all2) => {
@@ -388,12 +394,12 @@ function hashSignal(signal) {
388
394
  wasm.__wbindgen_export4(deferred2_0, deferred2_1, 1);
389
395
  }
390
396
  }
391
- function __wasm_bindgen_func_elem_597(arg0, arg1) {
392
- wasm.__wasm_bindgen_func_elem_597(arg0, arg1);
393
- }
394
397
  function __wasm_bindgen_func_elem_960(arg0, arg1, arg2) {
395
398
  wasm.__wasm_bindgen_func_elem_960(arg0, arg1, addHeapObject(arg2));
396
399
  }
400
+ function __wasm_bindgen_func_elem_597(arg0, arg1) {
401
+ wasm.__wasm_bindgen_func_elem_597(arg0, arg1);
402
+ }
397
403
  function __wasm_bindgen_func_elem_1345(arg0, arg1, arg2, arg3) {
398
404
  wasm.__wasm_bindgen_func_elem_1345(arg0, arg1, addHeapObject(arg2), addHeapObject(arg3));
399
405
  }
@@ -772,6 +778,34 @@ var IDKitBuilder = class _IDKitBuilder {
772
778
  const ret = wasm.idkitbuilder_constraints(ptr, addHeapObject(constraints_json));
773
779
  return takeObject(ret);
774
780
  }
781
+ /**
782
+ * Builds the native payload for constraints (synchronous, no bridge connection).
783
+ *
784
+ * Used by the native transport to get the same payload format as the bridge
785
+ * without creating a network connection.
786
+ *
787
+ * # Errors
788
+ *
789
+ * Returns an error if constraints are invalid or payload construction fails.
790
+ * @param {any} constraints_json
791
+ * @returns {any}
792
+ */
793
+ nativePayload(constraints_json) {
794
+ try {
795
+ const ptr = this.__destroy_into_raw();
796
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
797
+ wasm.idkitbuilder_nativePayload(retptr, ptr, addHeapObject(constraints_json));
798
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
799
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
800
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
801
+ if (r2) {
802
+ throw takeObject(r1);
803
+ }
804
+ return takeObject(r0);
805
+ } finally {
806
+ wasm.__wbindgen_add_to_stack_pointer(16);
807
+ }
808
+ }
775
809
  /**
776
810
  * Creates a new builder for proving an existing session
777
811
  * @param {string} session_id
@@ -827,6 +861,34 @@ var IDKitBuilder = class _IDKitBuilder {
827
861
  const ret = wasm.createSession(ptr0, len0, ptr1, ptr2, len2, ptr3, len3, ptr4, len4, ptr5, len5);
828
862
  return _IDKitBuilder.__wrap(ret);
829
863
  }
864
+ /**
865
+ * Builds the native payload from a preset (synchronous, no bridge connection).
866
+ *
867
+ * Used by the native transport to get the same payload format as the bridge
868
+ * without creating a network connection.
869
+ *
870
+ * # Errors
871
+ *
872
+ * Returns an error if the preset is invalid or payload construction fails.
873
+ * @param {any} preset_json
874
+ * @returns {any}
875
+ */
876
+ nativePayloadFromPreset(preset_json) {
877
+ try {
878
+ const ptr = this.__destroy_into_raw();
879
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
880
+ wasm.idkitbuilder_nativePayloadFromPreset(retptr, ptr, addHeapObject(preset_json));
881
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
882
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
883
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
884
+ if (r2) {
885
+ throw takeObject(r1);
886
+ }
887
+ return takeObject(r0);
888
+ } finally {
889
+ wasm.__wbindgen_add_to_stack_pointer(16);
890
+ }
891
+ }
830
892
  /**
831
893
  * Creates a new builder for uniqueness requests
832
894
  * @param {string} app_id
@@ -1796,10 +1858,6 @@ var idkit_wasm_default = __wbg_init;
1796
1858
  // src/lib/wasm.ts
1797
1859
  var wasmInitialized = false;
1798
1860
  var wasmInitPromise = null;
1799
- async function importNodeModule(specifier) {
1800
- const dynamicImport = Function("moduleName", "return import(moduleName)");
1801
- return dynamicImport(specifier);
1802
- }
1803
1861
  async function initIDKit() {
1804
1862
  if (wasmInitialized) {
1805
1863
  return;
@@ -1818,35 +1876,206 @@ async function initIDKit() {
1818
1876
  })();
1819
1877
  return wasmInitPromise;
1820
1878
  }
1821
- async function initIDKitServer() {
1822
- if (wasmInitialized) {
1823
- return;
1879
+
1880
+ // src/transports/native.ts
1881
+ function isInWorldApp() {
1882
+ return typeof window !== "undefined" && Boolean(window.WorldApp);
1883
+ }
1884
+ var _requestCounter = 0;
1885
+ var _activeNativeRequest = null;
1886
+ function createNativeRequest(wasmPayload, config) {
1887
+ if (_activeNativeRequest?.isPending()) {
1888
+ console.warn(
1889
+ "IDKit native request already in flight. Reusing active request."
1890
+ );
1891
+ return _activeNativeRequest;
1824
1892
  }
1825
- if (wasmInitPromise) {
1826
- return wasmInitPromise;
1893
+ const request2 = new NativeIDKitRequest(wasmPayload, config);
1894
+ _activeNativeRequest = request2;
1895
+ return request2;
1896
+ }
1897
+ var NativeIDKitRequest = class {
1898
+ constructor(wasmPayload, config) {
1899
+ this.connectorURI = "";
1900
+ this.resolved = false;
1901
+ this.cancelled = false;
1902
+ this.settled = false;
1903
+ this.resolvedResult = null;
1904
+ this.messageHandler = null;
1905
+ this.rejectFn = null;
1906
+ this.requestId = crypto.randomUUID?.() ?? `native-${Date.now()}-${++_requestCounter}`;
1907
+ this.resultPromise = new Promise((resolve, reject) => {
1908
+ this.rejectFn = reject;
1909
+ const handler = (event) => {
1910
+ if (this.cancelled) return;
1911
+ const data = event.data;
1912
+ if (data?.type === "miniapp-verify-action" || data?.command === "miniapp-verify-action") {
1913
+ this.cleanup();
1914
+ const responsePayload = data.payload ?? data;
1915
+ if (responsePayload.status === "error") {
1916
+ reject(
1917
+ new NativeVerifyError(
1918
+ responsePayload.error_code ?? "generic_error" /* GenericError */
1919
+ )
1920
+ );
1921
+ } else {
1922
+ this.resolved = true;
1923
+ const result = nativeResultToIDKitResult(responsePayload, config);
1924
+ this.resolvedResult = result;
1925
+ resolve(result);
1926
+ }
1927
+ }
1928
+ };
1929
+ this.messageHandler = handler;
1930
+ window.addEventListener("message", handler);
1931
+ const sendPayload = {
1932
+ command: "verify",
1933
+ version: 2,
1934
+ payload: wasmPayload
1935
+ };
1936
+ const w = window;
1937
+ if (w.webkit?.messageHandlers?.minikit) {
1938
+ w.webkit.messageHandlers.minikit.postMessage(sendPayload);
1939
+ } else if (w.Android) {
1940
+ w.Android.postMessage(JSON.stringify(sendPayload));
1941
+ } else {
1942
+ this.cleanup();
1943
+ reject(new Error("No WebView bridge available"));
1944
+ }
1945
+ });
1946
+ this.resultPromise.catch(() => {
1947
+ }).finally(() => {
1948
+ this.settled = true;
1949
+ this.cleanup();
1950
+ if (_activeNativeRequest === this) {
1951
+ _activeNativeRequest = null;
1952
+ }
1953
+ });
1827
1954
  }
1828
- wasmInitPromise = (async () => {
1955
+ /**
1956
+ * Cancel this request. Removes the message listener so it cannot consume
1957
+ * a response meant for a later request, and rejects the pending promise.
1958
+ */
1959
+ cancel() {
1960
+ if (this.resolved || this.cancelled) return;
1961
+ this.cancelled = true;
1962
+ this.cleanup();
1963
+ this.rejectFn?.(new NativeVerifyError("cancelled" /* Cancelled */));
1964
+ if (_activeNativeRequest === this) {
1965
+ _activeNativeRequest = null;
1966
+ }
1967
+ }
1968
+ cleanup() {
1969
+ if (this.messageHandler) {
1970
+ window.removeEventListener("message", this.messageHandler);
1971
+ this.messageHandler = null;
1972
+ }
1973
+ }
1974
+ isPending() {
1975
+ return !this.settled && !this.cancelled;
1976
+ }
1977
+ async pollOnce() {
1978
+ if (this.resolved && this.resolvedResult) {
1979
+ return { type: "confirmed", result: this.resolvedResult };
1980
+ }
1981
+ return { type: "awaiting_confirmation" };
1982
+ }
1983
+ async pollUntilCompletion(options) {
1984
+ const timeout = options?.timeout ?? 3e5;
1985
+ let timeoutId;
1986
+ let abortHandler = null;
1987
+ let waiterTerminationCode = null;
1829
1988
  try {
1830
- const { readFile } = await importNodeModule(
1831
- "node:fs/promises"
1832
- );
1833
- const { fileURLToPath } = await importNodeModule(
1834
- "node:url"
1835
- );
1836
- const { dirname, join } = await importNodeModule(
1837
- "node:path"
1838
- );
1839
- const modulePath = fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
1840
- const wasmPath = join(dirname(modulePath), "idkit_wasm_bg.wasm");
1841
- const wasmBuffer = await readFile(wasmPath);
1842
- await idkit_wasm_default({ module_or_path: wasmBuffer });
1843
- wasmInitialized = true;
1989
+ const result = await Promise.race([
1990
+ this.resultPromise,
1991
+ new Promise((_, reject) => {
1992
+ if (options?.signal) {
1993
+ abortHandler = () => {
1994
+ waiterTerminationCode = "cancelled" /* Cancelled */;
1995
+ reject(new NativeVerifyError("cancelled" /* Cancelled */));
1996
+ };
1997
+ if (options.signal.aborted) {
1998
+ abortHandler();
1999
+ return;
2000
+ }
2001
+ options.signal.addEventListener("abort", abortHandler, {
2002
+ once: true
2003
+ });
2004
+ }
2005
+ timeoutId = setTimeout(() => {
2006
+ waiterTerminationCode = "timeout" /* Timeout */;
2007
+ reject(new NativeVerifyError("timeout" /* Timeout */));
2008
+ }, timeout);
2009
+ })
2010
+ ]);
2011
+ return { success: true, result };
1844
2012
  } catch (error) {
1845
- wasmInitPromise = null;
1846
- throw new Error(`Failed to initialize IDKit WASM for server: ${error}`);
2013
+ if (error instanceof NativeVerifyError) {
2014
+ if (waiterTerminationCode === error.code && this.isPending()) {
2015
+ this.cancel();
2016
+ }
2017
+ return { success: false, error: error.code };
2018
+ }
2019
+ return { success: false, error: "generic_error" /* GenericError */ };
2020
+ } finally {
2021
+ if (timeoutId) {
2022
+ clearTimeout(timeoutId);
2023
+ }
2024
+ if (options?.signal && abortHandler) {
2025
+ options.signal.removeEventListener("abort", abortHandler);
2026
+ }
1847
2027
  }
1848
- })();
1849
- return wasmInitPromise;
2028
+ }
2029
+ };
2030
+ var NativeVerifyError = class extends Error {
2031
+ constructor(code) {
2032
+ super(code);
2033
+ this.code = code;
2034
+ }
2035
+ };
2036
+ function nativeResultToIDKitResult(payload, config) {
2037
+ const rpNonce = config.rp_context?.nonce ?? "";
2038
+ if ("responses" in payload && Array.isArray(payload.responses)) {
2039
+ return {
2040
+ protocol_version: payload.protocol_version ?? "4.0",
2041
+ nonce: payload.nonce ?? rpNonce,
2042
+ action: payload.action ?? config.action ?? "",
2043
+ action_description: payload.action_description,
2044
+ session_id: payload.session_id,
2045
+ responses: payload.responses,
2046
+ environment: payload.environment ?? config.environment ?? "production"
2047
+ };
2048
+ }
2049
+ if ("verifications" in payload) {
2050
+ return {
2051
+ protocol_version: "4.0",
2052
+ nonce: rpNonce,
2053
+ action: config.action ?? "",
2054
+ responses: payload.verifications.map((v) => ({
2055
+ identifier: v.verification_level,
2056
+ proof: [v.proof],
2057
+ nullifier: v.nullifier_hash,
2058
+ merkle_root: v.merkle_root,
2059
+ issuer_schema_id: 0,
2060
+ expires_at_min: 0
2061
+ })),
2062
+ environment: "production"
2063
+ };
2064
+ }
2065
+ return {
2066
+ protocol_version: "3.0",
2067
+ nonce: rpNonce,
2068
+ action: config.action ?? "",
2069
+ responses: [
2070
+ {
2071
+ identifier: payload.verification_level,
2072
+ proof: payload.proof,
2073
+ merkle_root: payload.merkle_root,
2074
+ nullifier: payload.nullifier_hash
2075
+ }
2076
+ ],
2077
+ environment: "production"
2078
+ };
1850
2079
  }
1851
2080
 
1852
2081
  // src/request.ts
@@ -1913,9 +2142,52 @@ function secureDocumentLegacy(opts = {}) {
1913
2142
  function documentLegacy(opts = {}) {
1914
2143
  return { type: "DocumentLegacy", signal: opts.signal };
1915
2144
  }
2145
+ function createWasmBuilderFromConfig(config) {
2146
+ if (!config.rp_context) {
2147
+ throw new Error("rp_context is required for WASM bridge transport");
2148
+ }
2149
+ const rpContext = new idkit_wasm_exports.RpContextWasm(
2150
+ config.rp_context.rp_id,
2151
+ config.rp_context.nonce,
2152
+ BigInt(config.rp_context.created_at),
2153
+ BigInt(config.rp_context.expires_at),
2154
+ config.rp_context.signature
2155
+ );
2156
+ if (config.type === "request") {
2157
+ return idkit_wasm_exports.request(
2158
+ config.app_id,
2159
+ String(config.action ?? ""),
2160
+ rpContext,
2161
+ config.action_description ?? null,
2162
+ config.bridge_url ?? null,
2163
+ config.allow_legacy_proofs ?? false,
2164
+ config.override_connect_base_url ?? null,
2165
+ config.environment ?? null
2166
+ );
2167
+ }
2168
+ if (config.type === "proveSession") {
2169
+ return idkit_wasm_exports.proveSession(
2170
+ config.session_id,
2171
+ config.app_id,
2172
+ rpContext,
2173
+ config.action_description ?? null,
2174
+ config.bridge_url ?? null,
2175
+ config.override_connect_base_url ?? null,
2176
+ config.environment ?? null
2177
+ );
2178
+ }
2179
+ return idkit_wasm_exports.createSession(
2180
+ config.app_id,
2181
+ rpContext,
2182
+ config.action_description ?? null,
2183
+ config.bridge_url ?? null,
2184
+ config.override_connect_base_url ?? null,
2185
+ config.environment ?? null
2186
+ );
2187
+ }
1916
2188
  var IDKitBuilder2 = class {
1917
- constructor(wasmBuilder) {
1918
- this.wasmBuilder = wasmBuilder;
2189
+ constructor(config) {
2190
+ this.config = config;
1919
2191
  }
1920
2192
  /**
1921
2193
  * Creates an IDKit request with the given constraints
@@ -1925,36 +2197,45 @@ var IDKitBuilder2 = class {
1925
2197
  *
1926
2198
  * @example
1927
2199
  * ```typescript
1928
- * const builder = await IDKit.request({ app_id, action, rp_context });
1929
- * const request = await builder.constraints(any(CredentialRequest('orb'), CredentialRequest('face')));
2200
+ * const request = await IDKit.request({ app_id, action, rp_context, allow_legacy_proofs: false })
2201
+ * .constraints(any(CredentialRequest('orb'), CredentialRequest('face')));
1930
2202
  * ```
1931
2203
  */
1932
- //TODO: re-enable once this is supported and World ID 4.0 is rolled out live
1933
- // async constraints(constraints: ConstraintNode): Promise<IDKitRequest> {
1934
- // await initIDKit();
1935
- // const wasmRequest = (await this.wasmBuilder.constraints(
1936
- // constraints,
1937
- // )) as unknown as WasmModule.IDKitRequest;
1938
- // return new IDKitRequestImpl(wasmRequest);
1939
- // }
2204
+ async constraints(constraints) {
2205
+ await initIDKit();
2206
+ const wasmBuilder = createWasmBuilderFromConfig(this.config);
2207
+ if (isInWorldApp()) {
2208
+ const payload = wasmBuilder.nativePayload(constraints);
2209
+ return createNativeRequest(payload, this.config);
2210
+ }
2211
+ const wasmRequest = await wasmBuilder.constraints(
2212
+ constraints
2213
+ );
2214
+ return new IDKitRequestImpl(wasmRequest);
2215
+ }
1940
2216
  /**
1941
2217
  * Creates an IDKit request from a preset (works for all request types)
1942
2218
  *
1943
2219
  * Presets provide a simplified way to create requests with predefined
1944
2220
  * credential configurations.
1945
2221
  *
1946
- * @param preset - A preset object from orbLegacy()
2222
+ * @param preset - A preset object from orbLegacy(), secureDocumentLegacy(), or documentLegacy()
1947
2223
  * @returns A new IDKitRequest instance
1948
2224
  *
1949
2225
  * @example
1950
2226
  * ```typescript
1951
- * const builder = await IDKit.request({ app_id, action, rp_context });
1952
- * const request = await builder.preset(orbLegacy({ signal: 'user-123' }));
2227
+ * const request = await IDKit.request({ app_id, action, rp_context, allow_legacy_proofs: true })
2228
+ * .preset(orbLegacy({ signal: 'user-123' }));
1953
2229
  * ```
1954
2230
  */
1955
2231
  async preset(preset) {
1956
2232
  await initIDKit();
1957
- const wasmRequest = await this.wasmBuilder.preset(
2233
+ const wasmBuilder = createWasmBuilderFromConfig(this.config);
2234
+ if (isInWorldApp()) {
2235
+ const payload = wasmBuilder.nativePayloadFromPreset(preset);
2236
+ return createNativeRequest(payload, this.config);
2237
+ }
2238
+ const wasmRequest = await wasmBuilder.preset(
1958
2239
  preset
1959
2240
  );
1960
2241
  return new IDKitRequestImpl(wasmRequest);
@@ -1968,55 +2249,45 @@ function createRequest(config) {
1968
2249
  throw new Error("action is required");
1969
2250
  }
1970
2251
  if (!config.rp_context) {
1971
- throw new Error("rp_context is required");
2252
+ throw new Error(
2253
+ "rp_context is required. Generate it on your backend using signRequest()."
2254
+ );
1972
2255
  }
1973
2256
  if (typeof config.allow_legacy_proofs !== "boolean") {
1974
2257
  throw new Error(
1975
2258
  "allow_legacy_proofs is required. Set to true to accept v3 proofs during migration, or false to only accept v4 proofs."
1976
2259
  );
1977
2260
  }
1978
- const rpContext = new idkit_wasm_exports.RpContextWasm(
1979
- config.rp_context.rp_id,
1980
- config.rp_context.nonce,
1981
- BigInt(config.rp_context.created_at),
1982
- BigInt(config.rp_context.expires_at),
1983
- config.rp_context.signature
1984
- );
1985
- const wasmBuilder = idkit_wasm_exports.request(
1986
- config.app_id,
1987
- String(config.action),
1988
- rpContext,
1989
- config.action_description ?? null,
1990
- config.bridge_url ?? null,
1991
- config.allow_legacy_proofs,
1992
- config.override_connect_base_url ?? null,
1993
- config.environment ?? null
1994
- );
1995
- return new IDKitBuilder2(wasmBuilder);
2261
+ return new IDKitBuilder2({
2262
+ type: "request",
2263
+ app_id: config.app_id,
2264
+ action: String(config.action),
2265
+ rp_context: config.rp_context,
2266
+ action_description: config.action_description,
2267
+ bridge_url: config.bridge_url,
2268
+ allow_legacy_proofs: config.allow_legacy_proofs,
2269
+ override_connect_base_url: config.override_connect_base_url,
2270
+ environment: config.environment
2271
+ });
1996
2272
  }
1997
2273
  function createSession2(config) {
1998
2274
  if (!config.app_id) {
1999
2275
  throw new Error("app_id is required");
2000
2276
  }
2001
2277
  if (!config.rp_context) {
2002
- throw new Error("rp_context is required");
2278
+ throw new Error(
2279
+ "rp_context is required. Generate it on your backend using signRequest()."
2280
+ );
2003
2281
  }
2004
- const rpContext = new idkit_wasm_exports.RpContextWasm(
2005
- config.rp_context.rp_id,
2006
- config.rp_context.nonce,
2007
- BigInt(config.rp_context.created_at),
2008
- BigInt(config.rp_context.expires_at),
2009
- config.rp_context.signature
2010
- );
2011
- const wasmBuilder = idkit_wasm_exports.createSession(
2012
- config.app_id,
2013
- rpContext,
2014
- config.action_description ?? null,
2015
- config.bridge_url ?? null,
2016
- config.override_connect_base_url ?? null,
2017
- config.environment ?? null
2018
- );
2019
- return new IDKitBuilder2(wasmBuilder);
2282
+ return new IDKitBuilder2({
2283
+ type: "session",
2284
+ app_id: config.app_id,
2285
+ rp_context: config.rp_context,
2286
+ action_description: config.action_description,
2287
+ bridge_url: config.bridge_url,
2288
+ override_connect_base_url: config.override_connect_base_url,
2289
+ environment: config.environment
2290
+ });
2020
2291
  }
2021
2292
  function proveSession2(sessionId, config) {
2022
2293
  if (!sessionId) {
@@ -2026,31 +2297,22 @@ function proveSession2(sessionId, config) {
2026
2297
  throw new Error("app_id is required");
2027
2298
  }
2028
2299
  if (!config.rp_context) {
2029
- throw new Error("rp_context is required");
2300
+ throw new Error(
2301
+ "rp_context is required. Generate it on your backend using signRequest()."
2302
+ );
2030
2303
  }
2031
- const rpContext = new idkit_wasm_exports.RpContextWasm(
2032
- config.rp_context.rp_id,
2033
- config.rp_context.nonce,
2034
- BigInt(config.rp_context.created_at),
2035
- BigInt(config.rp_context.expires_at),
2036
- config.rp_context.signature
2037
- );
2038
- const wasmBuilder = idkit_wasm_exports.proveSession(
2039
- sessionId,
2040
- config.app_id,
2041
- rpContext,
2042
- config.action_description ?? null,
2043
- config.bridge_url ?? null,
2044
- config.override_connect_base_url ?? null,
2045
- config.environment ?? null
2046
- );
2047
- return new IDKitBuilder2(wasmBuilder);
2304
+ return new IDKitBuilder2({
2305
+ type: "proveSession",
2306
+ session_id: sessionId,
2307
+ app_id: config.app_id,
2308
+ rp_context: config.rp_context,
2309
+ action_description: config.action_description,
2310
+ bridge_url: config.bridge_url,
2311
+ override_connect_base_url: config.override_connect_base_url,
2312
+ environment: config.environment
2313
+ });
2048
2314
  }
2049
2315
  var IDKit = {
2050
- /** Initialize WASM for browser environments */
2051
- init: initIDKit,
2052
- /** Initialize WASM for Node.js/server environments */
2053
- initServer: initIDKitServer,
2054
2316
  /** Create a new verification request */
2055
2317
  request: createRequest,
2056
2318
  /** Create a new session (no action, no existing session_id) */
@@ -2093,21 +2355,71 @@ var isServerEnvironment = () => {
2093
2355
  }
2094
2356
  return false;
2095
2357
  };
2358
+ function hashToField(input) {
2359
+ const hash = BigInt("0x" + utils.bytesToHex(sha3.keccak_256(input))) >> 8n;
2360
+ return utils.hexToBytes(hash.toString(16).padStart(64, "0"));
2361
+ }
2362
+ function hashSignal2(signal) {
2363
+ let input;
2364
+ if (signal instanceof Uint8Array) {
2365
+ input = signal;
2366
+ } else if (signal.startsWith("0x") && isValidHex(signal.slice(2))) {
2367
+ input = utils.hexToBytes(signal.slice(2));
2368
+ } else {
2369
+ input = new TextEncoder().encode(signal);
2370
+ }
2371
+ return "0x" + utils.bytesToHex(hashToField(input));
2372
+ }
2373
+ function isValidHex(s) {
2374
+ if (s.length === 0) return false;
2375
+ if (s.length % 2 !== 0) return false;
2376
+ return /^[0-9a-fA-F]+$/.test(s);
2377
+ }
2096
2378
 
2097
- // src/lib/rp-signature.ts
2098
- function signRequest2(action, signingKeyHex, ttlSeconds) {
2379
+ // src/lib/signing.ts
2380
+ secp256k1.etc.hmacSha256Sync = (key, ...msgs) => hmac.hmac(sha2.sha256, key, secp256k1.etc.concatBytes(...msgs));
2381
+ var DEFAULT_TTL_SEC = 300;
2382
+ function computeRpSignatureMessage(nonceBytes, createdAt, expiresAt) {
2383
+ const message = new Uint8Array(48);
2384
+ message.set(nonceBytes, 0);
2385
+ const view = new DataView(message.buffer);
2386
+ view.setBigUint64(32, BigInt(createdAt), false);
2387
+ view.setBigUint64(40, BigInt(expiresAt), false);
2388
+ return message;
2389
+ }
2390
+ function signRequest2(_action, signingKeyHex, ttl = DEFAULT_TTL_SEC) {
2099
2391
  if (!isServerEnvironment()) {
2100
2392
  throw new Error(
2101
2393
  "signRequest can only be used in Node.js environments. This function requires access to signing keys and should never be called from browser/client-side code."
2102
2394
  );
2103
2395
  }
2104
- const ttlBigInt = ttlSeconds !== void 0 ? BigInt(ttlSeconds) : void 0;
2105
- return idkit_wasm_exports.signRequest(action, signingKeyHex, ttlBigInt);
2106
- }
2107
-
2108
- // src/lib/hashing.ts
2109
- function hashSignal2(signal) {
2110
- return idkit_wasm_exports.hashSignal(signal);
2396
+ const keyHex = signingKeyHex.startsWith("0x") ? signingKeyHex.slice(2) : signingKeyHex;
2397
+ if (!/^[0-9a-fA-F]+$/.test(keyHex)) {
2398
+ throw new Error("Invalid signing key: contains non-hex characters");
2399
+ }
2400
+ if (keyHex.length !== 64) {
2401
+ throw new Error(
2402
+ `Invalid signing key: expected 32 bytes (64 hex chars), got ${keyHex.length / 2} bytes`
2403
+ );
2404
+ }
2405
+ const privKey = secp256k1.etc.hexToBytes(keyHex);
2406
+ const randomBytes = crypto.getRandomValues(new Uint8Array(32));
2407
+ const nonceBytes = hashToField(randomBytes);
2408
+ const createdAt = Math.floor(Date.now() / 1e3);
2409
+ const expiresAt = createdAt + ttl;
2410
+ const message = computeRpSignatureMessage(nonceBytes, createdAt, expiresAt);
2411
+ const msgHash = sha3.keccak_256(message);
2412
+ const recSig = secp256k1.sign(msgHash, privKey);
2413
+ const compact = recSig.toCompactRawBytes();
2414
+ const sig65 = new Uint8Array(65);
2415
+ sig65.set(compact, 0);
2416
+ sig65[64] = recSig.recovery + 27;
2417
+ return {
2418
+ sig: "0x" + utils.bytesToHex(sig65),
2419
+ nonce: "0x" + utils.bytesToHex(nonceBytes),
2420
+ createdAt,
2421
+ expiresAt
2422
+ };
2111
2423
  }
2112
2424
 
2113
2425
  exports.CredentialRequest = CredentialRequest;