@liveauth-labs/sdk 0.1.1 → 0.2.0
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/LICENSE +21 -0
- package/README.md +178 -49
- package/dist/index.cjs +165 -49
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +72 -6
- package/dist/index.d.ts +72 -6
- package/dist/index.js +159 -48
- package/dist/index.js.map +1 -1
- package/dist/pow.worker.cjs +48 -3
- package/dist/pow.worker.cjs.map +1 -1
- package/dist/pow.worker.d.cts +15 -1
- package/dist/pow.worker.d.ts +15 -1
- package/dist/pow.worker.js +33 -3
- package/dist/pow.worker.js.map +1 -1
- package/package.json +16 -4
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/errors.ts","../src/index.ts"],"sourcesContent":["export class LiveAuthTimeoutError extends Error {\n constructor(message = 'Lightning verification timed out') {\n super(message);\n this.name = 'LiveAuthTimeoutError';\n }\n}\n\nexport class LiveAuthCancelledError extends Error {\n constructor(message = 'Lightning verification cancelled') {\n super(message);\n this.name = 'LiveAuthCancelledError';\n }\n}\n","import type {\n LiveAuthResult,\n VerifyOptions,\n PowChallengeResponse,\n PowVerifyRequest,\n PowVerifyResponse,\n PowSolution,\n AuthStartResponse,\n AuthConfirmResponse,\n LiveAuthConfig, LightningStart\n} from './types';\nimport {LiveAuthCancelledError, LiveAuthTimeoutError} from \"./errors\";\n\nexport class LiveAuth {\n private readonly baseUrl: string;\n private readonly headers: HeadersInit;\n\n constructor(private readonly config: LiveAuthConfig) {\n if (!config.publicKey) {\n throw new Error('LiveAuth: publicKey is required');\n }\n\n this.baseUrl = config.baseUrl ?? 'https://api.liveauth.app';\n\n this.headers = {\n 'Content-Type': 'application/json',\n 'X-LW-Public': config.publicKey\n };\n }\n\n /* ======================================================\n * PUBLIC ENTRYPOINT\n * ====================================================== */\n\n async verify(): Promise<LiveAuthResult> {\n const startedAt = performance.now();\n\n const challenge = await this.getPowChallenge();\n const solution = await this.solvePow(challenge);\n\n const verifyRes = await this.verifyPow({\n challengeHex: challenge.challengeHex,\n nonce: solution.nonce,\n hashHex: solution.hashHex,\n expiresAtUnix: challenge.expiresAtUnix,\n sig: challenge.sig\n });\n\n if (verifyRes.verified && verifyRes.token) {\n return {\n method: 'pow',\n token: verifyRes.token,\n solveMs: Math.round(performance.now() - startedAt),\n difficultyBits: challenge.difficultyBits\n };\n }\n\n if (verifyRes.fallback === 'lightning') {\n const lightning = await this.startLightning();\n return {\n method: 'lightning',\n lightning\n };\n }\n\n throw new Error('LiveAuth: verification failed');\n }\n\n /* ======================================================\n * POW FLOW\n * ====================================================== */\n\n private async getPowChallenge(): Promise<PowChallengeResponse> {\n const res = await fetch(`${this.baseUrl}/api/public/pow/challenge`, {\n headers: this.headers\n });\n\n if (!res.ok) throw new Error('PoW challenge failed');\n return res.json();\n }\n\n private async verifyPow(req: PowVerifyRequest): Promise<PowVerifyResponse> {\n const res = await fetch(`${this.baseUrl}/api/public/pow/verify`, {\n method: 'POST',\n headers: this.headers,\n body: JSON.stringify(req)\n });\n\n if (!res.ok) throw new Error('PoW verify failed');\n return res.json();\n }\n\n /* ======================================================\n * POW SOLVER (WORKER)\n * ====================================================== */\n\n private solvePow(challenge: PowChallengeResponse): Promise<PowSolution> {\n if (!this.canUsePow()) {\n return Promise.reject(\n new Error('LiveAuth: PoW not supported in this environment')\n );\n }\n\n return new Promise((resolve, reject) => {\n const worker = new Worker(\n new URL('./pow.worker.js', import.meta.url),\n {type: 'module'}\n );\n\n worker.onmessage = e => {\n worker.terminate();\n resolve(e.data);\n };\n\n worker.onerror = e => {\n worker.terminate();\n reject(e);\n };\n\n worker.postMessage({\n projectPublicKey: challenge.projectPublicKey,\n challengeHex: challenge.challengeHex,\n targetHex: challenge.targetHex\n });\n });\n }\n\n /* ======================================================\n * LIGHTNING FALLBACK\n * ====================================================== */\n\n private async startLightning(): Promise<LightningStart> {\n const res = await fetch(`${this.baseUrl}/api/public/auth/start`, {\n method: 'POST',\n headers: this.headers,\n body: JSON.stringify({userHint: 'browser'})\n });\n\n if (!res.ok) {\n throw new Error('Lightning auth start failed');\n }\n\n return res.json();\n }\n\n private canUsePow(): boolean {\n try {\n // SSR / Node guard\n if (typeof window === 'undefined') return false;\n\n // Worker support\n if (typeof Worker === 'undefined') return false;\n\n // Basic URL support (needed for module workers)\n if (typeof URL === 'undefined') return false;\n\n return true;\n } catch {\n return false;\n }\n }\n\n async pollLightning(\n sessionId: string,\n options?: {\n timeoutMs?: number;\n signal?: AbortSignal;\n intervalMs?: number;\n }\n ): Promise<string> {\n const timeoutMs = options?.timeoutMs ?? 5 * 60_000; // 5 min\n const intervalMs = options?.intervalMs ?? 2000;\n const externalSignal = options?.signal;\n\n const controller = new AbortController();\n const signal = controller.signal;\n\n // If caller passes a signal, bridge it\n if (externalSignal) {\n if (externalSignal.aborted) {\n throw new LiveAuthCancelledError();\n }\n\n externalSignal.addEventListener('abort', () => {\n controller.abort();\n });\n }\n\n // Timeout enforcement\n const timeoutId = setTimeout(() => {\n controller.abort();\n }, timeoutMs);\n\n try {\n while (true) {\n if (signal.aborted) {\n throw externalSignal?.aborted\n ? new LiveAuthCancelledError()\n : new LiveAuthTimeoutError();\n }\n\n const res = await fetch(\n `${this.baseUrl}/api/public/auth/confirm`,\n {\n method: 'POST',\n headers: this.headers,\n body: JSON.stringify({sessionId}),\n signal\n }\n );\n\n if (!res.ok) {\n throw new Error('Lightning confirm failed');\n }\n\n const json = await res.json();\n\n if (json.verified && json.token) {\n return json.token;\n }\n\n await sleep(intervalMs);\n }\n } finally {\n clearTimeout(timeoutId);\n }\n }\n\n\n}\n\n/* ======================================================\n * UTILS\n * ====================================================== */\n\nconst sleep = (ms: number) =>\n new Promise(resolve => setTimeout(resolve, ms));\n"],"mappings":";AAAO,IAAM,uBAAN,cAAmC,MAAM;AAAA,EAC5C,YAAY,UAAU,oCAAoC;AACtD,UAAM,OAAO;AACb,SAAK,OAAO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AA,MAC1B;AAAA,IACJ,UAAE;AACE,mBAAa,SAAS;AAAA,IAC1B;AAAA,EACJ;AAGJ;AAMA,IAAM,QAAQ,CAAC,OACX,IAAI,QAAQ,aAAW,WAAW,SAAS,EAAE,CAAC;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/errors.ts","../src/index.ts"],"sourcesContent":["export class LiveAuthTimeoutError extends Error {\n constructor(message = 'LiveAuth verification timed out') {\n super(message);\n this.name = 'LiveAuthTimeoutError';\n }\n}\n\nexport class LiveAuthCancelledError extends Error {\n constructor(message = 'LiveAuth verification cancelled') {\n super(message);\n this.name = 'LiveAuthCancelledError';\n }\n}\n\nexport class LiveAuthNetworkError extends Error {\n constructor(message = 'LiveAuth network request failed', public readonly cause?: Error) {\n super(message);\n this.name = 'LiveAuthNetworkError';\n }\n}\n\nexport class LiveAuthPowUnsupportedError extends Error {\n constructor(message = 'Proof-of-Work is not supported in this environment') {\n super(message);\n this.name = 'LiveAuthPowUnsupportedError';\n }\n}\n\nexport class LiveAuthPowTimeoutError extends Error {\n constructor(message = 'Proof-of-Work timed out') {\n super(message);\n this.name = 'LiveAuthPowTimeoutError';\n }\n}\n","import type {\n LiveAuthResult,\n VerifyOptions,\n PowChallengeResponse,\n PowVerifyRequest,\n PowVerifyResponse,\n PowSolution,\n LiveAuthConfig,\n LightningStart\n} from './types';\nimport {\n LiveAuthCancelledError,\n LiveAuthTimeoutError,\n LiveAuthNetworkError,\n LiveAuthPowUnsupportedError,\n LiveAuthPowTimeoutError\n} from './errors';\nimport type { PowWorkerResult } from './pow.worker';\n\n// Re-export for consumers\nexport * from './types';\nexport * from './errors';\n\nconst SDK_VERSION = '0.2.0';\n\nexport class LiveAuth {\n private readonly baseUrl: string;\n private readonly headers: HeadersInit;\n\n constructor(private readonly config: LiveAuthConfig) {\n if (!config.publicKey) {\n throw new Error('LiveAuth: publicKey is required');\n }\n\n this.baseUrl = config.baseUrl ?? 'https://api.liveauth.app';\n\n this.headers = {\n 'Content-Type': 'application/json',\n 'X-LW-Public': config.publicKey,\n 'X-LW-SDK-Version': SDK_VERSION\n };\n }\n\n /* ======================================================\n * PUBLIC ENTRYPOINT\n * ====================================================== */\n\n async verify(options: VerifyOptions = {}): Promise<LiveAuthResult> {\n const { \n forceLightning = false, \n onProgress,\n powTimeoutMs = 30_000,\n maxPowIterations = 50_000_000\n } = options;\n\n // Skip PoW if forced to Lightning or PoW not supported\n if (forceLightning) {\n const lightning = await this.startLightning();\n return {\n method: 'lightning',\n lightning,\n diagnostics: { reason: 'forced_lightning' }\n };\n }\n\n if (!this.canUsePow()) {\n const lightning = await this.startLightning();\n return {\n method: 'lightning',\n lightning,\n diagnostics: { reason: 'pow_unsupported' }\n };\n }\n\n const startedAt = performance.now();\n\n try {\n const challenge = await this.getPowChallenge();\n const solution = await this.solvePow(challenge, { \n onProgress, \n timeoutMs: powTimeoutMs,\n maxIterations: maxPowIterations\n });\n\n const verifyRes = await this.verifyPow({\n challengeHex: challenge.challengeHex,\n nonce: solution.nonce,\n hashHex: solution.hashHex,\n expiresAtUnix: challenge.expiresAtUnix,\n difficultyBits: challenge.difficultyBits,\n sig: challenge.sig\n });\n\n if (verifyRes.verified && verifyRes.token) {\n return {\n method: 'pow',\n token: verifyRes.token,\n solveMs: Math.round(performance.now() - startedAt),\n difficultyBits: challenge.difficultyBits\n };\n }\n\n if (verifyRes.fallback === 'lightning') {\n const lightning = await this.startLightning();\n return {\n method: 'lightning',\n lightning,\n diagnostics: { reason: 'pow_server_fallback' }\n };\n }\n\n throw new Error('LiveAuth: verification failed');\n\n } catch (err) {\n // On PoW failure, fall back to Lightning\n if (err instanceof LiveAuthPowTimeoutError || err instanceof LiveAuthPowUnsupportedError) {\n const lightning = await this.startLightning();\n return {\n method: 'lightning',\n lightning,\n diagnostics: { \n reason: 'pow_failed', \n detail: err.message \n }\n };\n }\n throw err;\n }\n }\n\n /* ======================================================\n * POW FLOW\n * ====================================================== */\n\n private async getPowChallenge(): Promise<PowChallengeResponse> {\n const res = await this.fetchWithRetry(`${this.baseUrl}/api/public/pow/challenge`, {\n headers: this.headers\n });\n\n if (!res.ok) throw new LiveAuthNetworkError('PoW challenge failed');\n return res.json();\n }\n\n private async verifyPow(req: PowVerifyRequest): Promise<PowVerifyResponse> {\n const res = await this.fetchWithRetry(`${this.baseUrl}/api/public/pow/verify`, {\n method: 'POST',\n headers: this.headers,\n body: JSON.stringify(req)\n });\n\n if (!res.ok) throw new LiveAuthNetworkError('PoW verify failed');\n return res.json();\n }\n\n /* ======================================================\n * POW SOLVER (WORKER)\n * ====================================================== */\n\n private solvePow(\n challenge: PowChallengeResponse, \n options: {\n onProgress?: (hashesPerSec: number, iterations: number) => void;\n timeoutMs?: number;\n maxIterations?: number;\n } = {}\n ): Promise<PowSolution> {\n const { onProgress, timeoutMs = 30_000, maxIterations = 50_000_000 } = options;\n\n if (!this.canUsePow()) {\n return Promise.reject(new LiveAuthPowUnsupportedError());\n }\n\n return new Promise((resolve, reject) => {\n const worker = new Worker(\n new URL('./pow.worker.js', import.meta.url),\n { type: 'module' }\n );\n\n const timeoutId = setTimeout(() => {\n worker.terminate();\n reject(new LiveAuthPowTimeoutError(`PoW timed out after ${timeoutMs}ms`));\n }, timeoutMs);\n\n worker.onmessage = (e: MessageEvent<PowWorkerResult>) => {\n const data = e.data;\n\n if (data.type === 'progress') {\n onProgress?.(data.hashesPerSec ?? 0, data.iterations ?? 0);\n return;\n }\n\n if (data.type === 'timeout') {\n clearTimeout(timeoutId);\n worker.terminate();\n reject(new LiveAuthPowTimeoutError(`PoW hit max iterations (${maxIterations})`));\n return;\n }\n\n if (data.type === 'solution' && data.nonce !== undefined && data.hashHex) {\n clearTimeout(timeoutId);\n worker.terminate();\n resolve({ nonce: data.nonce, hashHex: data.hashHex });\n return;\n }\n };\n\n worker.onerror = e => {\n clearTimeout(timeoutId);\n worker.terminate();\n reject(new LiveAuthPowUnsupportedError(`Worker error: ${e.message}`));\n };\n\n worker.postMessage({\n projectPublicKey: challenge.projectPublicKey,\n challengeHex: challenge.challengeHex,\n targetHex: challenge.targetHex,\n maxIterations\n });\n });\n }\n\n /* ======================================================\n * LIGHTNING FALLBACK\n * ====================================================== */\n\n private async startLightning(): Promise<LightningStart> {\n const res = await this.fetchWithRetry(`${this.baseUrl}/api/public/auth/start`, {\n method: 'POST',\n headers: this.headers,\n body: JSON.stringify({ userHint: 'browser' })\n });\n\n if (!res.ok) {\n throw new LiveAuthNetworkError('Lightning auth start failed');\n }\n\n return res.json();\n }\n\n async pollLightning(\n sessionId: string,\n options?: {\n timeoutMs?: number;\n signal?: AbortSignal;\n intervalMs?: number;\n }\n ): Promise<string> {\n const timeoutMs = options?.timeoutMs ?? 5 * 60_000; // 5 min\n const intervalMs = options?.intervalMs ?? 2000;\n const externalSignal = options?.signal;\n\n const controller = new AbortController();\n const signal = controller.signal;\n\n // If caller passes a signal, bridge it\n if (externalSignal) {\n if (externalSignal.aborted) {\n throw new LiveAuthCancelledError();\n }\n\n externalSignal.addEventListener('abort', () => {\n controller.abort();\n });\n }\n\n // Timeout enforcement\n const timeoutId = setTimeout(() => {\n controller.abort();\n }, timeoutMs);\n\n try {\n while (true) {\n if (signal.aborted) {\n throw externalSignal?.aborted\n ? new LiveAuthCancelledError()\n : new LiveAuthTimeoutError();\n }\n\n const res = await fetch(\n `${this.baseUrl}/api/public/auth/confirm`,\n {\n method: 'POST',\n headers: this.headers,\n body: JSON.stringify({ sessionId }),\n signal\n }\n );\n\n if (!res.ok) {\n throw new LiveAuthNetworkError('Lightning confirm failed');\n }\n\n const json = await res.json();\n\n if (json.verified && json.token) {\n return json.token;\n }\n\n await sleep(intervalMs);\n }\n } finally {\n clearTimeout(timeoutId);\n }\n }\n\n /* ======================================================\n * UTILITIES\n * ====================================================== */\n\n private canUsePow(): boolean {\n try {\n // SSR / Node guard\n if (typeof window === 'undefined') return false;\n\n // Worker support\n if (typeof Worker === 'undefined') return false;\n\n // Basic URL support (needed for module workers)\n if (typeof URL === 'undefined') return false;\n\n // Crypto.subtle required for SHA-256\n if (typeof crypto === 'undefined' || !crypto.subtle) return false;\n\n return true;\n } catch {\n return false;\n }\n }\n\n private async fetchWithRetry(\n url: string,\n init?: RequestInit,\n retries = 2,\n backoffMs = 500\n ): Promise<Response> {\n let lastError: Error | undefined;\n\n for (let attempt = 0; attempt <= retries; attempt++) {\n try {\n const res = await fetch(url, init);\n return res;\n } catch (err) {\n lastError = err instanceof Error ? err : new Error(String(err));\n \n if (attempt < retries) {\n await sleep(backoffMs * Math.pow(2, attempt));\n }\n }\n }\n\n throw new LiveAuthNetworkError(\n `Request failed after ${retries + 1} attempts`,\n lastError\n );\n }\n}\n\n/* ======================================================\n * UTILS\n * ====================================================== */\n\nconst sleep = (ms: number) =>\n new Promise(resolve => setTimeout(resolve, ms));\n"],"mappings":";AAAO,IAAM,uBAAN,cAAmC,MAAM;AAAA,EAC5C,YAAY,UAAU,mCAAmC;AACrD,UAAM,OAAO;AACb,SAAK,OAAO;AAAA,EAChB;AACJ;AAEO,IAAM,yBAAN,cAAqC,MAAM;AAAA,EAC9C,YAAY,UAAU,mCAAmC;AACrD,UAAM,OAAO;AACb,SAAK,OAAO;AAAA,EAChB;AACJ;AAEO,IAAM,uBAAN,cAAmC,MAAM;AAAA,EAC5C,YAAY,UAAU,mCAAmD,OAAe;AACpF,UAAM,OAAO;AADwD;AAErE,SAAK,OAAO;AAAA,EAChB;AACJ;AAEO,IAAM,8BAAN,cAA0C,MAAM;AAAA,EACnD,YAAY,UAAU,sDAAsD;AACxE,UAAM,OAAO;AACb,SAAK,OAAO;AAAA,EAChB;AACJ;AAEO,IAAM,0BAAN,cAAsC,MAAM;AAAA,EAC/C,YAAY,UAAU,2BAA2B;AAC7C,UAAM,OAAO;AACb,SAAK,OAAO;AAAA,EAChB;AACJ;;;ACVA,IAAM,cAAc;AAEb,IAAM,WAAN,MAAe;AAAA,EAIlB,YAA6B,QAAwB;AAAxB;AACzB,QAAI,CAAC,OAAO,WAAW;AACnB,YAAM,IAAI,MA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AI;AAEA,UAAI,OAAO,WAAW,YAAa,QAAO;AAG1C,UAAI,OAAO,WAAW,YAAa,QAAO;AAG1C,UAAI,OAAO,QAAQ,YAAa,QAAO;AAGvC,UAAI,OAAO,WAAW,eAAe,CAAC,OAAO,OAAQ,QAAO;AAE5D,aAAO;AAAA,IACX,QAAQ;AACJ,aAAO;AAAA,IACX;AAAA,EACJ;AAAA,EAEA,MAAc,eACV,KACA,MACA,UAAU,GACV,YAAY,KACK;AACjB,QAAI;AAEJ,aAAS,UAAU,GAAG,WAAW,SAAS,WAAW;AACjD,UAAI;AACA,cAAM,MAAM,MAAM,MAAM,KAAK,IAAI;AACjC,eAAO;AAAA,MACX,SAAS,KAAK;AACV,oBAAY,eAAe,QAAQ,MAAM,IAAI,MAAM,OAAO,GAAG,CAAC;AAE9D,YAAI,UAAU,SAAS;AACnB,gBAAM,MAAM,YAAY,KAAK,IAAI,GAAG,OAAO,CAAC;AAAA,QAChD;AAAA,MACJ;AAAA,IACJ;AAEA,UAAM,IAAI;AAAA,MACN,wBAAwB,UAAU,CAAC;AAAA,MACnC;AAAA,IACJ;AAAA,EACJ;AACJ;AAMA,IAAM,QAAQ,CAAC,OACX,IAAI,QAAQ,aAAW,WAAW,SAAS,EAAE,CAAC;","names":[]}
|
package/dist/pow.worker.cjs
CHANGED
|
@@ -1,18 +1,63 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
2
15
|
|
|
3
16
|
// src/pow.worker.ts
|
|
17
|
+
var pow_worker_exports = {};
|
|
18
|
+
module.exports = __toCommonJS(pow_worker_exports);
|
|
4
19
|
self.onmessage = async (e) => {
|
|
5
|
-
const {
|
|
20
|
+
const {
|
|
21
|
+
projectPublicKey,
|
|
22
|
+
challengeHex,
|
|
23
|
+
targetHex,
|
|
24
|
+
maxIterations = 5e7,
|
|
25
|
+
progressInterval = 1e4
|
|
26
|
+
} = e.data;
|
|
6
27
|
let nonce = 0;
|
|
7
|
-
|
|
28
|
+
const startTime = performance.now();
|
|
29
|
+
let lastProgressTime = startTime;
|
|
30
|
+
while (nonce < maxIterations) {
|
|
8
31
|
const input = `${projectPublicKey}:${challengeHex}:${nonce}`;
|
|
9
32
|
const hash = await sha256Hex(input);
|
|
10
33
|
if (hash <= targetHex) {
|
|
11
|
-
|
|
34
|
+
const elapsed = performance.now() - startTime;
|
|
35
|
+
postMessage({
|
|
36
|
+
type: "solution",
|
|
37
|
+
nonce,
|
|
38
|
+
hashHex: hash,
|
|
39
|
+
iterations: nonce + 1,
|
|
40
|
+
hashesPerSec: Math.round((nonce + 1) / (elapsed / 1e3))
|
|
41
|
+
});
|
|
12
42
|
return;
|
|
13
43
|
}
|
|
14
44
|
nonce++;
|
|
45
|
+
if (nonce % progressInterval === 0) {
|
|
46
|
+
const now = performance.now();
|
|
47
|
+
const elapsed = now - startTime;
|
|
48
|
+
const recentElapsed = now - lastProgressTime;
|
|
49
|
+
postMessage({
|
|
50
|
+
type: "progress",
|
|
51
|
+
iterations: nonce,
|
|
52
|
+
hashesPerSec: Math.round(progressInterval / (recentElapsed / 1e3))
|
|
53
|
+
});
|
|
54
|
+
lastProgressTime = now;
|
|
55
|
+
}
|
|
15
56
|
}
|
|
57
|
+
postMessage({
|
|
58
|
+
type: "timeout",
|
|
59
|
+
iterations: nonce
|
|
60
|
+
});
|
|
16
61
|
};
|
|
17
62
|
async function sha256Hex(input) {
|
|
18
63
|
const buf = await crypto.subtle.digest(
|
package/dist/pow.worker.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/pow.worker.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"sources":["../src/pow.worker.ts"],"sourcesContent":["export interface PowWorkerMessage {\n projectPublicKey: string;\n challengeHex: string;\n targetHex: string;\n maxIterations?: number;\n progressInterval?: number;\n}\n\nexport interface PowWorkerResult {\n type: 'solution' | 'progress' | 'timeout';\n nonce?: number;\n hashHex?: string;\n iterations?: number;\n hashesPerSec?: number;\n}\n\nself.onmessage = async (e: MessageEvent<PowWorkerMessage>) => {\n const { \n projectPublicKey, \n challengeHex, \n targetHex, \n maxIterations = 50_000_000,\n progressInterval = 10_000\n } = e.data;\n\n let nonce = 0;\n const startTime = performance.now();\n let lastProgressTime = startTime;\n\n while (nonce < maxIterations) {\n const input = `${projectPublicKey}:${challengeHex}:${nonce}`;\n const hash = await sha256Hex(input);\n\n if (hash <= targetHex) {\n const elapsed = performance.now() - startTime;\n postMessage({ \n type: 'solution', \n nonce, \n hashHex: hash,\n iterations: nonce + 1,\n hashesPerSec: Math.round((nonce + 1) / (elapsed / 1000))\n } satisfies PowWorkerResult);\n return;\n }\n\n nonce++;\n\n // Send progress updates periodically\n if (nonce % progressInterval === 0) {\n const now = performance.now();\n const elapsed = now - startTime;\n const recentElapsed = now - lastProgressTime;\n \n postMessage({ \n type: 'progress', \n iterations: nonce,\n hashesPerSec: Math.round(progressInterval / (recentElapsed / 1000))\n } satisfies PowWorkerResult);\n \n lastProgressTime = now;\n }\n }\n\n // Hit max iterations without solution\n postMessage({ \n type: 'timeout', \n iterations: nonce \n } satisfies PowWorkerResult);\n};\n\nasync function sha256Hex(input: string): Promise<string> {\n const buf = await crypto.subtle.digest(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G,CAAC,EACzB,IAAI,OAAK,EAAE,SAAS,EAAE,EAAE,SAAS,GAAG,GAAG,CAAC,EACxC,KAAK,EAAE;AAChB;","names":[]}
|
package/dist/pow.worker.d.cts
CHANGED
|
@@ -1,2 +1,16 @@
|
|
|
1
|
+
interface PowWorkerMessage {
|
|
2
|
+
projectPublicKey: string;
|
|
3
|
+
challengeHex: string;
|
|
4
|
+
targetHex: string;
|
|
5
|
+
maxIterations?: number;
|
|
6
|
+
progressInterval?: number;
|
|
7
|
+
}
|
|
8
|
+
interface PowWorkerResult {
|
|
9
|
+
type: 'solution' | 'progress' | 'timeout';
|
|
10
|
+
nonce?: number;
|
|
11
|
+
hashHex?: string;
|
|
12
|
+
iterations?: number;
|
|
13
|
+
hashesPerSec?: number;
|
|
14
|
+
}
|
|
1
15
|
|
|
2
|
-
export {
|
|
16
|
+
export type { PowWorkerMessage, PowWorkerResult };
|
package/dist/pow.worker.d.ts
CHANGED
|
@@ -1,2 +1,16 @@
|
|
|
1
|
+
interface PowWorkerMessage {
|
|
2
|
+
projectPublicKey: string;
|
|
3
|
+
challengeHex: string;
|
|
4
|
+
targetHex: string;
|
|
5
|
+
maxIterations?: number;
|
|
6
|
+
progressInterval?: number;
|
|
7
|
+
}
|
|
8
|
+
interface PowWorkerResult {
|
|
9
|
+
type: 'solution' | 'progress' | 'timeout';
|
|
10
|
+
nonce?: number;
|
|
11
|
+
hashHex?: string;
|
|
12
|
+
iterations?: number;
|
|
13
|
+
hashesPerSec?: number;
|
|
14
|
+
}
|
|
1
15
|
|
|
2
|
-
export {
|
|
16
|
+
export type { PowWorkerMessage, PowWorkerResult };
|
package/dist/pow.worker.js
CHANGED
|
@@ -1,16 +1,46 @@
|
|
|
1
1
|
// src/pow.worker.ts
|
|
2
2
|
self.onmessage = async (e) => {
|
|
3
|
-
const {
|
|
3
|
+
const {
|
|
4
|
+
projectPublicKey,
|
|
5
|
+
challengeHex,
|
|
6
|
+
targetHex,
|
|
7
|
+
maxIterations = 5e7,
|
|
8
|
+
progressInterval = 1e4
|
|
9
|
+
} = e.data;
|
|
4
10
|
let nonce = 0;
|
|
5
|
-
|
|
11
|
+
const startTime = performance.now();
|
|
12
|
+
let lastProgressTime = startTime;
|
|
13
|
+
while (nonce < maxIterations) {
|
|
6
14
|
const input = `${projectPublicKey}:${challengeHex}:${nonce}`;
|
|
7
15
|
const hash = await sha256Hex(input);
|
|
8
16
|
if (hash <= targetHex) {
|
|
9
|
-
|
|
17
|
+
const elapsed = performance.now() - startTime;
|
|
18
|
+
postMessage({
|
|
19
|
+
type: "solution",
|
|
20
|
+
nonce,
|
|
21
|
+
hashHex: hash,
|
|
22
|
+
iterations: nonce + 1,
|
|
23
|
+
hashesPerSec: Math.round((nonce + 1) / (elapsed / 1e3))
|
|
24
|
+
});
|
|
10
25
|
return;
|
|
11
26
|
}
|
|
12
27
|
nonce++;
|
|
28
|
+
if (nonce % progressInterval === 0) {
|
|
29
|
+
const now = performance.now();
|
|
30
|
+
const elapsed = now - startTime;
|
|
31
|
+
const recentElapsed = now - lastProgressTime;
|
|
32
|
+
postMessage({
|
|
33
|
+
type: "progress",
|
|
34
|
+
iterations: nonce,
|
|
35
|
+
hashesPerSec: Math.round(progressInterval / (recentElapsed / 1e3))
|
|
36
|
+
});
|
|
37
|
+
lastProgressTime = now;
|
|
38
|
+
}
|
|
13
39
|
}
|
|
40
|
+
postMessage({
|
|
41
|
+
type: "timeout",
|
|
42
|
+
iterations: nonce
|
|
43
|
+
});
|
|
14
44
|
};
|
|
15
45
|
async function sha256Hex(input) {
|
|
16
46
|
const buf = await crypto.subtle.digest(
|
package/dist/pow.worker.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/pow.worker.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"sources":["../src/pow.worker.ts"],"sourcesContent":["export interface PowWorkerMessage {\n projectPublicKey: string;\n challengeHex: string;\n targetHex: string;\n maxIterations?: number;\n progressInterval?: number;\n}\n\nexport interface PowWorkerResult {\n type: 'solution' | 'progress' | 'timeout';\n nonce?: number;\n hashHex?: string;\n iterations?: number;\n hashesPerSec?: number;\n}\n\nself.onmessage = async (e: MessageEvent<PowWorkerMessage>) => {\n const { \n projectPublicKey, \n challengeHex, \n targetHex, \n maxIterations = 50_000_000,\n progressInterval = 10_000\n } = e.data;\n\n let nonce = 0;\n const startTime = performance.now();\n let lastProgressTime = startTime;\n\n while (nonce < maxIterations) {\n const input = `${projectPublicKey}:${challengeHex}:${nonce}`;\n const hash = await sha256Hex(input);\n\n if (hash <= targetHex) {\n const elapsed = performance.now() - startTime;\n postMessage({ \n type: 'solution', \n nonce, \n hashHex: hash,\n iterations: nonce + 1,\n hashesPerSec: Math.round((nonce + 1) / (elapsed / 1000))\n } satisfies PowWorkerResult);\n return;\n }\n\n nonce++;\n\n // Send progress updates periodically\n if (nonce % progressInterval === 0) {\n const now = performance.now();\n const elapsed = now - startTime;\n const recentElapsed = now - lastProgressTime;\n \n postMessage({ \n type: 'progress', \n iterations: nonce,\n hashesPerSec: Math.round(progressInterval / (recentElapsed / 1000))\n } satisfies PowWorkerResult);\n \n lastProgressTime = now;\n }\n }\n\n // Hit max iterations without solution\n postMessage({ \n type: 'timeout', \n iterations: nonce \n } satisfies PowWorkerResult);\n};\n\nasync function sha256Hex(input: string): Promise<string> {\n const buf = await crypto.subtle.digest(\n 'SHA-256',\n new TextEncoder().encode(input)\n );\n\n return [...new Uint8Array(buf)]\n .map(b => b.toString(16).padStart(2, '0'))\n .join('');\n}\n"],"mappings":";AAgBA,KAAK,YAAY,OAAO,MAAsC;AAC1D,QAAM;AAAA,IACF;AAAA,IACA;AAAA,IACA;AAAA,IACA,gBAAgB;AAAA,IAChB,mBAAmB;AAAA,EACvB,IAAI,EAAE;AAEN,MAAI,QAAQ;AACZ,QAAM,YAAY,YAAY,IAAI;AAClC,MAAI,mBAAmB;AAEvB,SAAO,QAAQ,eAAe;AAC1B,UAAM,QAAQ,GAAG,gBAAgB,IAAI,YAAY,IAAI,KAAK;AAC1D,UAAM,OAAO,MAAM,UAAU,KAAK;AAElC,QAAI,QAAQ,WAAW;AACnB,YAAM,UAAU,YAAY,IAAI,IAAI;AACpC,kBAAY;AAAA,QACR,MAAM;AAAA,QACN;AAAA,QACA,SAAS;AAAA,QACT,YAAY,QAAQ;AAAA,QACpB,cAAc,KAAK,OAAO,QAAQ,MAAM,UAAU,IAAK;AAAA,MAC3D,CAA2B;AAC3B;AAAA,IACJ;AAEA;AAGA,QAAI,QAAQ,qBAAqB,GAAG;AAChC,YAAM,MAAM,YAAY,IAAI;AAC5B,YAAM,UAAU,MAAM;AACtB,YAAM,gBAAgB,MAAM;AAE5B,kBAAY;AAAA,QACR,MAAM;AAAA,QACN,YAAY;AAAA,QACZ,cAAc,KAAK,MAAM,oBAAoB,gBAAgB,IAAK;AAAA,MACtE,CAA2B;AAE3B,yBAAmB;AAAA,IACvB;AAAA,EACJ;AAGA,cAAY;AAAA,IACR,MAAM;AAAA,IACN,YAAY;AAAA,EAChB,CAA2B;AAC/B;AAEA,eAAe,UAAU,OAAgC;AACrD,QAAM,MAAM,MAAM,OAAO,OAAO;AAAA,IAC5B;AAAA,IACA,IAAI,YAAY,EAAE,OAAO,KAAK;AAAA,EAClC;AAEA,SAAO,CAAC,GAAG,IAAI,WAAW,GAAG,CAAC,EACzB,IAAI,OAAK,EAAE,SAAS,EAAE,EAAE,SAAS,GAAG,GAAG,CAAC,EACxC,KAAK,EAAE;AAChB;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@liveauth-labs/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "LiveAuth browser SDK (PoW + Lightning human verification)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
7
7
|
"module": "./dist/index.mjs",
|
|
8
8
|
"types": "./dist/index.d.ts",
|
|
9
|
+
"sideEffects": false,
|
|
9
10
|
"exports": {
|
|
10
11
|
".": {
|
|
11
12
|
"import": "./dist/index.mjs",
|
|
12
|
-
"require": "./dist/index.js"
|
|
13
|
+
"require": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts"
|
|
13
15
|
}
|
|
14
16
|
},
|
|
15
17
|
"files": [
|
|
@@ -25,6 +27,16 @@
|
|
|
25
27
|
"lightning",
|
|
26
28
|
"bitcoin",
|
|
27
29
|
"bot-protection",
|
|
28
|
-
"auth"
|
|
29
|
-
|
|
30
|
+
"auth",
|
|
31
|
+
"verification"
|
|
32
|
+
],
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "https://github.com/dulzuradev/liveauth-js"
|
|
36
|
+
},
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"tsup": "^8.0.0",
|
|
40
|
+
"typescript": "^5.3.0"
|
|
41
|
+
}
|
|
30
42
|
}
|