@iam-protocol/pulse-sdk 0.2.0 → 0.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iam-protocol/pulse-sdk",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "Client-side SDK for IAM Protocol — sensor capture, TBH generation, ZK proof construction",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -54,3 +54,32 @@ export function generateLissajousPoints(params: LissajousParams): Point2D[] {
54
54
 
55
55
  return result;
56
56
  }
57
+
58
+ /**
59
+ * Generate a sequence of Lissajous curves for dynamic mid-session switching.
60
+ * Each curve uses different parameters, preventing pre-computation.
61
+ */
62
+ export function generateLissajousSequence(
63
+ count: number = 2
64
+ ): { params: LissajousParams; points: Point2D[] }[] {
65
+ const allRatios: [number, number][] = [
66
+ [1, 2], [2, 3], [3, 4], [3, 5], [4, 5],
67
+ [1, 3], [2, 5], [5, 6], [3, 7], [4, 7],
68
+ ];
69
+
70
+ const shuffled = [...allRatios].sort(() => Math.random() - 0.5);
71
+ const sequence: { params: LissajousParams; points: Point2D[] }[] = [];
72
+
73
+ for (let i = 0; i < count; i++) {
74
+ const pair = shuffled[i % shuffled.length]!;
75
+ const params: LissajousParams = {
76
+ a: pair[0],
77
+ b: pair[1],
78
+ delta: Math.PI * (0.1 + Math.random() * 0.8),
79
+ points: 200,
80
+ };
81
+ sequence.push({ params, points: generateLissajousPoints(params) });
82
+ }
83
+
84
+ return sequence;
85
+ }
@@ -17,7 +17,7 @@ const SYLLABLES = [
17
17
  export function generatePhrase(wordCount: number = 5): string {
18
18
  const words: string[] = [];
19
19
  for (let w = 0; w < wordCount; w++) {
20
- const syllableCount = 2 + Math.floor(Math.random() * 2); // 2-3 syllables per word
20
+ const syllableCount = 2 + Math.floor(Math.random() * 2);
21
21
  let word = "";
22
22
  for (let s = 0; s < syllableCount; s++) {
23
23
  const idx = Math.floor(Math.random() * SYLLABLES.length);
@@ -27,3 +27,36 @@ export function generatePhrase(wordCount: number = 5): string {
27
27
  }
28
28
  return words.join(" ");
29
29
  }
30
+
31
+ /**
32
+ * Generate a sequence of phrases for dynamic mid-session switching.
33
+ * Each phrase uses a different syllable subset to prevent pre-computation.
34
+ */
35
+ export function generatePhraseSequence(
36
+ count: number = 3,
37
+ wordCount: number = 4
38
+ ): string[] {
39
+ const subsetSize = Math.floor(SYLLABLES.length / count);
40
+ const phrases: string[] = [];
41
+
42
+ for (let p = 0; p < count; p++) {
43
+ const start = (p * subsetSize) % SYLLABLES.length;
44
+ const subset = [
45
+ ...SYLLABLES.slice(start, start + subsetSize),
46
+ ...SYLLABLES.slice(0, Math.max(0, (start + subsetSize) - SYLLABLES.length)),
47
+ ];
48
+
49
+ const words: string[] = [];
50
+ for (let w = 0; w < wordCount; w++) {
51
+ const syllableCount = 2 + Math.floor(Math.random() * 2);
52
+ let word = "";
53
+ for (let s = 0; s < syllableCount; s++) {
54
+ word += subset[Math.floor(Math.random() * subset.length)];
55
+ }
56
+ words.push(word);
57
+ }
58
+ phrases.push(words.join(" "));
59
+ }
60
+
61
+ return phrases;
62
+ }
package/src/index.ts CHANGED
@@ -38,6 +38,6 @@ export { fetchIdentityState, storeVerificationData, loadVerificationData } from
38
38
  export type { AudioCapture, MotionSample, TouchSample, SensorData, CaptureOptions, CaptureStage, StageState } from "./sensor/types";
39
39
 
40
40
  // Challenge
41
- export { generatePhrase } from "./challenge/phrase";
42
- export { randomLissajousParams, generateLissajousPoints } from "./challenge/lissajous";
41
+ export { generatePhrase, generatePhraseSequence } from "./challenge/phrase";
42
+ export { randomLissajousParams, generateLissajousPoints, generateLissajousSequence } from "./challenge/lissajous";
43
43
  export type { LissajousParams, Point2D } from "./challenge/lissajous";
package/src/pulse.ts CHANGED
@@ -189,13 +189,14 @@ export class PulseSession {
189
189
 
190
190
  // --- Audio ---
191
191
 
192
- async startAudio(): Promise<void> {
192
+ async startAudio(onAudioLevel?: (rms: number) => void): Promise<void> {
193
193
  if (this.audioStageState !== "idle")
194
194
  throw new Error("Audio capture already started");
195
195
  this.audioStageState = "capturing";
196
196
  this.audioController = new AbortController();
197
197
  this.audioPromise = captureAudio({
198
198
  signal: this.audioController.signal,
199
+ onAudioLevel,
199
200
  }).catch(() => null);
200
201
  }
201
202
 
@@ -19,6 +19,7 @@ export async function captureAudio(
19
19
  signal,
20
20
  minDurationMs = MIN_CAPTURE_MS,
21
21
  maxDurationMs = MAX_CAPTURE_MS,
22
+ onAudioLevel,
22
23
  } = options;
23
24
 
24
25
  const stream = await navigator.mediaDevices.getUserMedia({
@@ -42,7 +43,14 @@ export async function captureAudio(
42
43
  const processor = ctx.createScriptProcessor(bufferSize, 1, 1);
43
44
 
44
45
  processor.onaudioprocess = (e: AudioProcessingEvent) => {
45
- chunks.push(new Float32Array(e.inputBuffer.getChannelData(0)));
46
+ const data = e.inputBuffer.getChannelData(0);
47
+ chunks.push(new Float32Array(data));
48
+
49
+ if (onAudioLevel) {
50
+ let sum = 0;
51
+ for (let i = 0; i < data.length; i++) sum += data[i]! * data[i]!;
52
+ onAudioLevel(Math.sqrt(sum / data.length));
53
+ }
46
54
  };
47
55
 
48
56
  source.connect(processor);
@@ -34,6 +34,8 @@ export interface CaptureOptions {
34
34
  minDurationMs?: number;
35
35
  /** Maximum capture duration in ms. Auto-stops if signal hasn't fired. Default: 60000 */
36
36
  maxDurationMs?: number;
37
+ /** Called with RMS audio level (0-1) on each buffer during audio capture (~4x per second). */
38
+ onAudioLevel?: (rms: number) => void;
37
39
  }
38
40
 
39
41
  /** Stage of a capture session */