@peers-app/peers-sdk 0.7.40 → 0.8.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.
@@ -17,8 +17,8 @@ export declare const groupSchema: z.ZodObject<{
17
17
  }, "strip", z.ZodTypeAny, {
18
18
  name: string;
19
19
  description: string;
20
- signature: string;
21
20
  publicKey: string;
21
+ signature: string;
22
22
  publicBoxKey: string;
23
23
  groupId: string;
24
24
  founderUserId: string;
@@ -28,8 +28,8 @@ export declare const groupSchema: z.ZodObject<{
28
28
  }, {
29
29
  name: string;
30
30
  description: string;
31
- signature: string;
32
31
  publicKey: string;
32
+ signature: string;
33
33
  publicBoxKey: string;
34
34
  groupId: string;
35
35
  founderUserId: string;
package/dist/index.d.ts CHANGED
@@ -33,3 +33,4 @@ export * from "./rpc-types";
33
33
  export * from "./serial-json";
34
34
  export * from "./utils";
35
35
  export * from "./logging";
36
+ export * from "./user-connect";
package/dist/index.js CHANGED
@@ -50,3 +50,4 @@ __exportStar(require("./rpc-types"), exports);
50
50
  __exportStar(require("./serial-json"), exports);
51
51
  __exportStar(require("./utils"), exports);
52
52
  __exportStar(require("./logging"), exports);
53
+ __exportStar(require("./user-connect"), exports);
package/dist/keys.js CHANGED
@@ -28,6 +28,7 @@ const buffer_1 = require("buffer");
28
28
  const nacl = require("tweetnacl");
29
29
  const utils = require("tweetnacl-util");
30
30
  const tweetnacl_util_1 = require("tweetnacl-util");
31
+ const ed2curve = require("ed2curve");
31
32
  const tx_encoding_1 = require("./device/tx-encoding");
32
33
  const serial_json_1 = require("./serial-json");
33
34
  globalThis.Buffer = buffer_1.Buffer; // shim for browsers/RN
@@ -72,23 +73,23 @@ function newToken(size = 32) {
72
73
  return encodeBase64(nacl.randomBytes(size));
73
74
  }
74
75
  function newKeys() {
75
- const keyPair = nacl.sign.keyPair();
76
- // the first 32 bytes of the secret key are the secret part. The second 32 bytes are the public part.
77
- const secretKeyPart = keyPair.secretKey.slice(0, 32);
78
- const boxKeyPair = nacl.box.keyPair.fromSecretKey(secretKeyPart);
76
+ const sign = nacl.sign.keyPair(); // Ed25519
77
+ const curveSecret = ed2curve.convertSecretKey(sign.secretKey); // 32 bytes
78
+ const box = nacl.box.keyPair.fromSecretKey(curveSecret); // X25519
79
79
  return {
80
- secretKey: encodeBase64(keyPair.secretKey),
81
- publicKey: encodeBase64(keyPair.publicKey),
82
- publicBoxKey: encodeBase64(boxKeyPair.publicKey),
80
+ secretKey: encodeBase64(sign.secretKey),
81
+ publicKey: encodeBase64(sign.publicKey),
82
+ publicBoxKey: encodeBase64(box.publicKey),
83
83
  };
84
84
  }
85
85
  function hydrateKeys(secretKey) {
86
- let _secretKey = decodeBase64(secretKey);
87
- const boxKeyPair = nacl.box.keyPair.fromSecretKey(_secretKey.slice(0, 32));
86
+ const sk64 = decodeBase64(secretKey); // Ed25519 64-byte secretKey
87
+ const curveSecret = ed2curve.convertSecretKey(sk64);
88
+ const box = nacl.box.keyPair.fromSecretKey(curveSecret);
88
89
  return {
89
- secretKey: encodeBase64(_secretKey),
90
- publicKey: encodeBase64(_secretKey.slice(32)),
91
- publicBoxKey: encodeBase64(boxKeyPair.publicKey),
90
+ secretKey: encodeBase64(sk64),
91
+ publicKey: encodeBase64(sk64.slice(32)), // last 32 bytes = Ed25519 pk
92
+ publicBoxKey: encodeBase64(box.publicKey),
92
93
  };
93
94
  }
94
95
  function signMessageWithSecretKey(msg, secretKey) {
@@ -145,7 +146,8 @@ function openSignedObject(signedObj) {
145
146
  }
146
147
  function boxDataWithKeys(data, toPublicBoxKey, mySecretKey) {
147
148
  let _secretKey = decodeBase64(mySecretKey);
148
- const boxKeyPair = nacl.box.keyPair.fromSecretKey(_secretKey.slice(0, 32));
149
+ const curveSecret = ed2curve.convertSecretKey(_secretKey);
150
+ const boxKeyPair = nacl.box.keyPair.fromSecretKey(curveSecret);
149
151
  const _toPublicBoxKey = decodeBase64(toPublicBoxKey);
150
152
  const nonce = nacl.randomBytes(24);
151
153
  const dataByteArray = (0, tx_encoding_1.txEncode)(data);
@@ -161,7 +163,8 @@ function isBoxedData(data) {
161
163
  }
162
164
  function openBoxWithSecretKey(box, mySecretKey) {
163
165
  let _secretKey = decodeBase64(mySecretKey);
164
- const boxKeyPair = nacl.box.keyPair.fromSecretKey(_secretKey.slice(0, 32));
166
+ const curveSecret = ed2curve.convertSecretKey(_secretKey);
167
+ const boxKeyPair = nacl.box.keyPair.fromSecretKey(curveSecret);
165
168
  const boxedData = decodeBase64(box.contents);
166
169
  const nonce = decodeBase64(box.nonce);
167
170
  const _fromPublicBoxKey = decodeBase64(box.fromPublicKey);
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Utilities for generating and handling connection codes.
3
+ *
4
+ * Connection codes are 12 characters of Crockford Base32:
5
+ * - First 4 chars: device alias (rendezvous point)
6
+ * - Last 8 chars: shared secret for encryption
7
+ *
8
+ * Displayed format: XXXX-YYYY-ZZZZ
9
+ */
10
+ import type { IUserConnectInfo } from './user-connect.types';
11
+ export interface IConnectionCode {
12
+ /** Full 12-character code */
13
+ code: string;
14
+ /** First 4 characters - device alias for rendezvous */
15
+ alias: string;
16
+ /** Last 8 characters - shared secret for encryption */
17
+ secret: string;
18
+ }
19
+ /**
20
+ * Generate a new connection code.
21
+ * @returns Object with full code, alias, and secret
22
+ */
23
+ export declare function generateConnectionCode(): IConnectionCode;
24
+ /**
25
+ * Format a connection code for display.
26
+ * @param code 12-character code
27
+ * @returns Formatted as "XXXX-YYYY-ZZZZ"
28
+ */
29
+ export declare function formatConnectionCode(code: string): string;
30
+ /**
31
+ * Parse a formatted connection code.
32
+ * @param formatted Code in any format (with or without dashes)
33
+ * @returns Object with alias and secret
34
+ */
35
+ export declare function parseConnectionCode(formatted: string): {
36
+ code: string;
37
+ alias: string;
38
+ secret: string;
39
+ };
40
+ /**
41
+ * Encrypt data using the shared secret.
42
+ * @param data Data to encrypt
43
+ * @param secret 8-character shared secret
44
+ * @returns Base64-encoded encrypted data with nonce
45
+ */
46
+ export declare function encryptWithSecret(data: any, secret: string): string;
47
+ /**
48
+ * Decrypt data using the shared secret.
49
+ * @param encrypted Base64-encoded encrypted data
50
+ * @param secret 8-character shared secret
51
+ * @returns Decrypted data
52
+ */
53
+ export declare function decryptWithSecret(encrypted: string, secret: string): any;
54
+ /**
55
+ * Generate a confirmation hash from both users' information.
56
+ * Both parties should see the same hash if the exchange was successful.
57
+ * @param userA First user's info
58
+ * @param userB Second user's info
59
+ * @returns 4-character confirmation code
60
+ */
61
+ export declare function generateConfirmationHash(userA: IUserConnectInfo, userB: IUserConnectInfo): string;
62
+ /**
63
+ * Validate that a string is a valid connection code format.
64
+ * @param code Code to validate (with or without dashes)
65
+ * @returns true if valid
66
+ */
67
+ export declare function isValidConnectionCode(code: string): boolean;
@@ -0,0 +1,176 @@
1
+ "use strict";
2
+ /**
3
+ * Utilities for generating and handling connection codes.
4
+ *
5
+ * Connection codes are 12 characters of Crockford Base32:
6
+ * - First 4 chars: device alias (rendezvous point)
7
+ * - Last 8 chars: shared secret for encryption
8
+ *
9
+ * Displayed format: XXXX-YYYY-ZZZZ
10
+ */
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.generateConnectionCode = generateConnectionCode;
13
+ exports.formatConnectionCode = formatConnectionCode;
14
+ exports.parseConnectionCode = parseConnectionCode;
15
+ exports.encryptWithSecret = encryptWithSecret;
16
+ exports.decryptWithSecret = decryptWithSecret;
17
+ exports.generateConfirmationHash = generateConfirmationHash;
18
+ exports.isValidConnectionCode = isValidConnectionCode;
19
+ const nacl = require("tweetnacl");
20
+ const sha2_1 = require("@noble/hashes/sha2");
21
+ const tx_encoding_1 = require("../device/tx-encoding");
22
+ const keys_1 = require("../keys");
23
+ /**
24
+ * Crockford Base32 alphabet.
25
+ * Excludes I, L, O, U to avoid confusion with 1, 1, 0, V respectively.
26
+ */
27
+ const CROCKFORD_ALPHABET = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
28
+ /**
29
+ * Generate random bytes using nacl.
30
+ */
31
+ function randomBytes(length) {
32
+ return nacl.randomBytes(length);
33
+ }
34
+ /**
35
+ * Encode bytes to Crockford Base32 string.
36
+ */
37
+ function toCrockfordBase32(bytes, length) {
38
+ let result = '';
39
+ let i = 0;
40
+ while (result.length < length) {
41
+ if (i >= bytes.length) {
42
+ // Need more bytes
43
+ const moreBytes = randomBytes(length - result.length);
44
+ for (const b of moreBytes) {
45
+ result += CROCKFORD_ALPHABET[b % 32];
46
+ if (result.length >= length)
47
+ break;
48
+ }
49
+ break;
50
+ }
51
+ // Use 5 bits at a time for base32
52
+ result += CROCKFORD_ALPHABET[bytes[i] % 32];
53
+ i++;
54
+ }
55
+ return result.substring(0, length);
56
+ }
57
+ /**
58
+ * Derive a 32-byte encryption key from the shared secret.
59
+ */
60
+ function deriveKeyFromSecret(secret) {
61
+ // Use SHA-256 to derive a proper 32-byte key from the secret
62
+ const secretBytes = new TextEncoder().encode(secret);
63
+ return (0, sha2_1.sha256)(secretBytes);
64
+ }
65
+ /**
66
+ * Generate a new connection code.
67
+ * @returns Object with full code, alias, and secret
68
+ */
69
+ function generateConnectionCode() {
70
+ const bytes = randomBytes(16);
71
+ const alias = toCrockfordBase32(bytes.slice(0, 4), 4);
72
+ const secret = toCrockfordBase32(bytes.slice(4), 8);
73
+ return {
74
+ code: alias + secret,
75
+ alias,
76
+ secret,
77
+ };
78
+ }
79
+ /**
80
+ * Format a connection code for display.
81
+ * @param code 12-character code
82
+ * @returns Formatted as "XXXX-YYYY-ZZZZ"
83
+ */
84
+ function formatConnectionCode(code) {
85
+ const normalized = code.toUpperCase().replace(/[^0-9A-Z]/g, '');
86
+ if (normalized.length !== 12) {
87
+ throw new Error(`Connection code must be 12 characters, got ${normalized.length}`);
88
+ }
89
+ return `${normalized.slice(0, 4)}-${normalized.slice(4, 8)}-${normalized.slice(8, 12)}`;
90
+ }
91
+ /**
92
+ * Parse a formatted connection code.
93
+ * @param formatted Code in any format (with or without dashes)
94
+ * @returns Object with alias and secret
95
+ */
96
+ function parseConnectionCode(formatted) {
97
+ const normalized = formatted.toUpperCase().replace(/[^0-9A-Z]/g, '');
98
+ if (normalized.length !== 12) {
99
+ throw new Error(`Connection code must be 12 characters, got ${normalized.length}`);
100
+ }
101
+ return {
102
+ code: normalized,
103
+ alias: normalized.slice(0, 4),
104
+ secret: normalized.slice(4, 12),
105
+ };
106
+ }
107
+ /**
108
+ * Encrypt data using the shared secret.
109
+ * @param data Data to encrypt
110
+ * @param secret 8-character shared secret
111
+ * @returns Base64-encoded encrypted data with nonce
112
+ */
113
+ function encryptWithSecret(data, secret) {
114
+ const key = deriveKeyFromSecret(secret);
115
+ const nonce = nacl.randomBytes(24);
116
+ const dataBytes = (0, tx_encoding_1.txEncode)(data);
117
+ const encrypted = nacl.secretbox(dataBytes, nonce, key);
118
+ // Combine nonce + encrypted data
119
+ const combined = new Uint8Array(nonce.length + encrypted.length);
120
+ combined.set(nonce);
121
+ combined.set(encrypted, nonce.length);
122
+ return (0, keys_1.encodeBase64)(combined);
123
+ }
124
+ /**
125
+ * Decrypt data using the shared secret.
126
+ * @param encrypted Base64-encoded encrypted data
127
+ * @param secret 8-character shared secret
128
+ * @returns Decrypted data
129
+ */
130
+ function decryptWithSecret(encrypted, secret) {
131
+ const key = deriveKeyFromSecret(secret);
132
+ const combined = (0, keys_1.decodeBase64)(encrypted);
133
+ const nonce = combined.slice(0, 24);
134
+ const ciphertext = combined.slice(24);
135
+ const decrypted = nacl.secretbox.open(ciphertext, nonce, key);
136
+ if (!decrypted) {
137
+ throw new Error('Decryption failed - invalid secret or corrupted data');
138
+ }
139
+ return (0, tx_encoding_1.txDecode)(decrypted);
140
+ }
141
+ /**
142
+ * Generate a confirmation hash from both users' information.
143
+ * Both parties should see the same hash if the exchange was successful.
144
+ * @param userA First user's info
145
+ * @param userB Second user's info
146
+ * @returns 4-character confirmation code
147
+ */
148
+ function generateConfirmationHash(userA, userB) {
149
+ // Sort by userId to ensure consistent ordering regardless of who is A or B
150
+ const sorted = [userA, userB].sort((a, b) => a.userId.localeCompare(b.userId));
151
+ const combined = JSON.stringify(sorted);
152
+ const hash = (0, sha2_1.sha256)(new TextEncoder().encode(combined));
153
+ // Take first 4 characters of the hash encoded in Crockford Base32
154
+ return toCrockfordBase32(hash.slice(0, 4), 4);
155
+ }
156
+ /**
157
+ * Validate that a string is a valid connection code format.
158
+ * @param code Code to validate (with or without dashes)
159
+ * @returns true if valid
160
+ */
161
+ function isValidConnectionCode(code) {
162
+ try {
163
+ const normalized = code.toUpperCase().replace(/[^0-9A-Z]/g, '');
164
+ if (normalized.length !== 12)
165
+ return false;
166
+ // Check all characters are valid Crockford Base32
167
+ for (const char of normalized) {
168
+ if (!CROCKFORD_ALPHABET.includes(char))
169
+ return false;
170
+ }
171
+ return true;
172
+ }
173
+ catch {
174
+ return false;
175
+ }
176
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,213 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const connection_code_1 = require("./connection-code");
4
+ describe('connection-code', () => {
5
+ describe('generateConnectionCode', () => {
6
+ it('should generate a code with 12 characters', () => {
7
+ const result = (0, connection_code_1.generateConnectionCode)();
8
+ expect(result.code).toHaveLength(12);
9
+ });
10
+ it('should have alias of 4 characters', () => {
11
+ const result = (0, connection_code_1.generateConnectionCode)();
12
+ expect(result.alias).toHaveLength(4);
13
+ });
14
+ it('should have secret of 8 characters', () => {
15
+ const result = (0, connection_code_1.generateConnectionCode)();
16
+ expect(result.secret).toHaveLength(8);
17
+ });
18
+ it('should have code = alias + secret', () => {
19
+ const result = (0, connection_code_1.generateConnectionCode)();
20
+ expect(result.code).toBe(result.alias + result.secret);
21
+ });
22
+ it('should only contain valid Crockford Base32 characters', () => {
23
+ const validChars = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
24
+ for (let i = 0; i < 10; i++) {
25
+ const result = (0, connection_code_1.generateConnectionCode)();
26
+ for (const char of result.code) {
27
+ expect(validChars).toContain(char);
28
+ }
29
+ }
30
+ });
31
+ it('should generate different codes each time', () => {
32
+ const codes = new Set();
33
+ for (let i = 0; i < 100; i++) {
34
+ codes.add((0, connection_code_1.generateConnectionCode)().code);
35
+ }
36
+ expect(codes.size).toBe(100);
37
+ });
38
+ });
39
+ describe('formatConnectionCode', () => {
40
+ it('should format a 12-character code as XXXX-YYYY-ZZZZ', () => {
41
+ expect((0, connection_code_1.formatConnectionCode)('ABCD1234EFGH')).toBe('ABCD-1234-EFGH');
42
+ });
43
+ it('should handle lowercase input', () => {
44
+ expect((0, connection_code_1.formatConnectionCode)('abcd1234efgh')).toBe('ABCD-1234-EFGH');
45
+ });
46
+ it('should handle input with existing dashes', () => {
47
+ expect((0, connection_code_1.formatConnectionCode)('ABCD-1234-EFGH')).toBe('ABCD-1234-EFGH');
48
+ });
49
+ it('should handle input with spaces', () => {
50
+ expect((0, connection_code_1.formatConnectionCode)('ABCD 1234 EFGH')).toBe('ABCD-1234-EFGH');
51
+ });
52
+ it('should throw for codes that are too short', () => {
53
+ expect(() => (0, connection_code_1.formatConnectionCode)('ABCD1234')).toThrow('must be 12 characters');
54
+ });
55
+ it('should throw for codes that are too long', () => {
56
+ expect(() => (0, connection_code_1.formatConnectionCode)('ABCD1234EFGH5678')).toThrow('must be 12 characters');
57
+ });
58
+ });
59
+ describe('parseConnectionCode', () => {
60
+ it('should parse a plain 12-character code', () => {
61
+ const result = (0, connection_code_1.parseConnectionCode)('ABCD12345678');
62
+ expect(result.alias).toBe('ABCD');
63
+ expect(result.secret).toBe('12345678');
64
+ });
65
+ it('should parse a formatted code with dashes', () => {
66
+ const result = (0, connection_code_1.parseConnectionCode)('ABCD-1234-5678');
67
+ expect(result.alias).toBe('ABCD');
68
+ expect(result.secret).toBe('12345678');
69
+ });
70
+ it('should handle lowercase input', () => {
71
+ const result = (0, connection_code_1.parseConnectionCode)('abcd-1234-5678');
72
+ expect(result.alias).toBe('ABCD');
73
+ expect(result.secret).toBe('12345678');
74
+ });
75
+ it('should handle mixed case and spaces', () => {
76
+ const result = (0, connection_code_1.parseConnectionCode)('AbCd 1234 5678');
77
+ expect(result.alias).toBe('ABCD');
78
+ expect(result.secret).toBe('12345678');
79
+ });
80
+ it('should throw for invalid length', () => {
81
+ expect(() => (0, connection_code_1.parseConnectionCode)('ABCD1234')).toThrow('must be 12 characters');
82
+ });
83
+ it('should be the inverse of formatConnectionCode', () => {
84
+ const original = (0, connection_code_1.generateConnectionCode)();
85
+ const formatted = (0, connection_code_1.formatConnectionCode)(original.code);
86
+ const parsed = (0, connection_code_1.parseConnectionCode)(formatted);
87
+ expect(parsed.alias).toBe(original.alias);
88
+ expect(parsed.secret).toBe(original.secret);
89
+ });
90
+ });
91
+ describe('encryptWithSecret and decryptWithSecret', () => {
92
+ it('should round-trip a simple string', () => {
93
+ const secret = 'ABCD1234';
94
+ const data = 'Hello, World!';
95
+ const encrypted = (0, connection_code_1.encryptWithSecret)(data, secret);
96
+ const decrypted = (0, connection_code_1.decryptWithSecret)(encrypted, secret);
97
+ expect(decrypted).toBe(data);
98
+ });
99
+ it('should round-trip an object', () => {
100
+ const secret = 'ZYXW9876';
101
+ const data = { userId: 'user123', name: 'Test User', count: 42 };
102
+ const encrypted = (0, connection_code_1.encryptWithSecret)(data, secret);
103
+ const decrypted = (0, connection_code_1.decryptWithSecret)(encrypted, secret);
104
+ expect(decrypted).toEqual(data);
105
+ });
106
+ it('should round-trip user connect info', () => {
107
+ const secret = 'TESTCODE';
108
+ const userInfo = {
109
+ userId: 'user123',
110
+ publicKey: 'pk_abc123',
111
+ publicBoxKey: 'pbk_xyz789',
112
+ deviceId: 'device456',
113
+ };
114
+ const encrypted = (0, connection_code_1.encryptWithSecret)(userInfo, secret);
115
+ const decrypted = (0, connection_code_1.decryptWithSecret)(encrypted, secret);
116
+ expect(decrypted).toEqual(userInfo);
117
+ });
118
+ it('should produce different ciphertext for same data (due to random nonce)', () => {
119
+ const secret = 'ABCD1234';
120
+ const data = 'Same data';
121
+ const encrypted1 = (0, connection_code_1.encryptWithSecret)(data, secret);
122
+ const encrypted2 = (0, connection_code_1.encryptWithSecret)(data, secret);
123
+ expect(encrypted1).not.toBe(encrypted2);
124
+ });
125
+ it('should fail decryption with wrong secret', () => {
126
+ const data = 'Secret message';
127
+ const encrypted = (0, connection_code_1.encryptWithSecret)(data, 'CORRECT1');
128
+ expect(() => (0, connection_code_1.decryptWithSecret)(encrypted, 'WRONGKEY')).toThrow('Decryption failed');
129
+ });
130
+ it('should fail decryption with corrupted data', () => {
131
+ const secret = 'ABCD1234';
132
+ const data = 'Hello';
133
+ const encrypted = (0, connection_code_1.encryptWithSecret)(data, secret);
134
+ const corrupted = encrypted.slice(0, -5) + 'XXXXX';
135
+ expect(() => (0, connection_code_1.decryptWithSecret)(corrupted, secret)).toThrow();
136
+ });
137
+ });
138
+ describe('generateConfirmationHash', () => {
139
+ const userA = {
140
+ userId: 'userA123',
141
+ publicKey: 'pkA',
142
+ publicBoxKey: 'pbkA',
143
+ deviceId: 'deviceA',
144
+ };
145
+ const userB = {
146
+ userId: 'userB456',
147
+ publicKey: 'pkB',
148
+ publicBoxKey: 'pbkB',
149
+ deviceId: 'deviceB',
150
+ };
151
+ it('should return a 4-character hash', () => {
152
+ const hash = (0, connection_code_1.generateConfirmationHash)(userA, userB);
153
+ expect(hash).toHaveLength(4);
154
+ });
155
+ it('should only contain valid Crockford Base32 characters', () => {
156
+ const validChars = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
157
+ const hash = (0, connection_code_1.generateConfirmationHash)(userA, userB);
158
+ for (const char of hash) {
159
+ expect(validChars).toContain(char);
160
+ }
161
+ });
162
+ it('should produce same hash regardless of argument order', () => {
163
+ const hashAB = (0, connection_code_1.generateConfirmationHash)(userA, userB);
164
+ const hashBA = (0, connection_code_1.generateConfirmationHash)(userB, userA);
165
+ expect(hashAB).toBe(hashBA);
166
+ });
167
+ it('should produce different hash for different users', () => {
168
+ const userC = {
169
+ userId: 'userC789',
170
+ publicKey: 'pkC',
171
+ publicBoxKey: 'pbkC',
172
+ deviceId: 'deviceC',
173
+ };
174
+ const hashAB = (0, connection_code_1.generateConfirmationHash)(userA, userB);
175
+ const hashAC = (0, connection_code_1.generateConfirmationHash)(userA, userC);
176
+ expect(hashAB).not.toBe(hashAC);
177
+ });
178
+ it('should be deterministic', () => {
179
+ const hash1 = (0, connection_code_1.generateConfirmationHash)(userA, userB);
180
+ const hash2 = (0, connection_code_1.generateConfirmationHash)(userA, userB);
181
+ expect(hash1).toBe(hash2);
182
+ });
183
+ });
184
+ describe('isValidConnectionCode', () => {
185
+ it('should return true for valid 12-character codes', () => {
186
+ expect((0, connection_code_1.isValidConnectionCode)('ABCD1234EFGH')).toBe(true);
187
+ });
188
+ it('should return true for codes with dashes', () => {
189
+ expect((0, connection_code_1.isValidConnectionCode)('ABCD-1234-EFGH')).toBe(true);
190
+ });
191
+ it('should return true for lowercase codes', () => {
192
+ expect((0, connection_code_1.isValidConnectionCode)('abcd1234efgh')).toBe(true);
193
+ });
194
+ it('should return true for generated codes', () => {
195
+ const code = (0, connection_code_1.generateConnectionCode)();
196
+ expect((0, connection_code_1.isValidConnectionCode)(code.code)).toBe(true);
197
+ expect((0, connection_code_1.isValidConnectionCode)((0, connection_code_1.formatConnectionCode)(code.code))).toBe(true);
198
+ });
199
+ it('should return false for codes that are too short', () => {
200
+ expect((0, connection_code_1.isValidConnectionCode)('ABCD1234')).toBe(false);
201
+ });
202
+ it('should return false for codes that are too long', () => {
203
+ expect((0, connection_code_1.isValidConnectionCode)('ABCD1234EFGH5678')).toBe(false);
204
+ });
205
+ it('should return false for codes with invalid characters', () => {
206
+ // I, L, O, U are not in Crockford Base32
207
+ expect((0, connection_code_1.isValidConnectionCode)('ABCDILOU1234')).toBe(false);
208
+ });
209
+ it('should return false for empty string', () => {
210
+ expect((0, connection_code_1.isValidConnectionCode)('')).toBe(false);
211
+ });
212
+ });
213
+ });
@@ -0,0 +1,3 @@
1
+ export * from './connection-code';
2
+ export * from './user-connect.types';
3
+ export * from './user-connect.pvars';
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./connection-code"), exports);
18
+ __exportStar(require("./user-connect.types"), exports);
19
+ __exportStar(require("./user-connect.pvars"), exports);
@@ -0,0 +1,3 @@
1
+ export declare const userConnectStatus: import("../data/persistent-vars").PersistentVar<any>;
2
+ export declare const userConnectCodeOffer: import("../data/persistent-vars").PersistentVar<string>;
3
+ export declare const userConnectCodeAnswer: import("../data/persistent-vars").PersistentVar<string>;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.userConnectCodeAnswer = exports.userConnectCodeOffer = exports.userConnectStatus = void 0;
4
+ const persistent_vars_1 = require("../data/persistent-vars");
5
+ exports.userConnectStatus = (0, persistent_vars_1.deviceVar)('userConnectStatus', { defaultValue: undefined });
6
+ exports.userConnectCodeOffer = (0, persistent_vars_1.deviceVar)('userConnectCodeOffer', { defaultValue: '' });
7
+ exports.userConnectCodeOffer.loadingPromise.then(() => {
8
+ let codeTimeout = undefined;
9
+ (0, exports.userConnectStatus)('');
10
+ exports.userConnectCodeOffer.subscribe(() => {
11
+ (0, exports.userConnectStatus)('');
12
+ clearTimeout(codeTimeout);
13
+ if ((0, exports.userConnectCodeOffer)()) {
14
+ codeTimeout = setTimeout(() => {
15
+ (0, exports.userConnectCodeOffer)('');
16
+ }, 600_000); // 10 minutes
17
+ }
18
+ });
19
+ });
20
+ exports.userConnectCodeAnswer = (0, persistent_vars_1.deviceVar)('userConnectCodeAnswer', { defaultValue: '' });
21
+ exports.userConnectCodeAnswer.loadingPromise.then(() => {
22
+ let codeTimeout = undefined;
23
+ (0, exports.userConnectStatus)('');
24
+ exports.userConnectCodeAnswer.subscribe(() => {
25
+ (0, exports.userConnectStatus)('');
26
+ clearTimeout(codeTimeout);
27
+ if ((0, exports.userConnectCodeAnswer)()) {
28
+ codeTimeout = setTimeout(() => {
29
+ (0, exports.userConnectCodeAnswer)('');
30
+ }, 600_000); // 10 minutes
31
+ }
32
+ });
33
+ });
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Types for the user connection flow.
3
+ *
4
+ * This flow allows two users to securely exchange user information
5
+ * using a 12-character code (4-char rendezvous alias + 8-char shared secret).
6
+ */
7
+ /**
8
+ * User information exchanged during the connection flow.
9
+ */
10
+ export interface IUserConnectInfo {
11
+ userId: string;
12
+ name?: string;
13
+ publicKey: string;
14
+ publicBoxKey: string;
15
+ deviceId: string;
16
+ }
17
+ /**
18
+ * Message sent by the responder to initiate the connection.
19
+ * The payload is encrypted with the shared secret from the connection code.
20
+ */
21
+ export interface IUserConnectRequest {
22
+ type: 'user-connect-request';
23
+ /** User info encrypted with the shared secret */
24
+ encryptedUserInfo: string;
25
+ /** The responder's deviceId so the initiator can respond directly */
26
+ responseDeviceId: string;
27
+ }
28
+ /**
29
+ * Message sent by the initiator in response to a connection request.
30
+ * The payload is encrypted with the shared secret.
31
+ */
32
+ export interface IUserConnectResponse {
33
+ type: 'user-connect-response';
34
+ /** User info encrypted with the shared secret */
35
+ encryptedUserInfo: string;
36
+ }
37
+ /**
38
+ * Message broadcast to register a short-lived device alias.
39
+ * Other devices store this mapping for routing purposes.
40
+ */
41
+ export interface IDeviceAliasMessage {
42
+ type: 'device-alias';
43
+ /** The short alias (4 chars Crockford Base32) */
44
+ alias: string;
45
+ /** The actual deviceId this alias maps to */
46
+ deviceId: string;
47
+ /** TTL in milliseconds (typically 10 minutes) */
48
+ ttlMs: number;
49
+ }
50
+ /**
51
+ * Result of a successful user connection.
52
+ */
53
+ export interface IUserConnectResult {
54
+ /** The remote user's information */
55
+ remoteUser: IUserConnectInfo;
56
+ /** Confirmation hash (first 4 chars) for visual verification */
57
+ confirmationHash: string;
58
+ }
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ /**
3
+ * Types for the user connection flow.
4
+ *
5
+ * This flow allows two users to securely exchange user information
6
+ * using a 12-character code (4-char rendezvous alias + 8-char shared secret).
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peers-app/peers-sdk",
3
- "version": "0.7.40",
3
+ "version": "0.8.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/peers-app/peers-sdk.git"
@@ -32,6 +32,7 @@
32
32
  "dependencies": {
33
33
  "@msgpack/msgpack": "^3.1.2",
34
34
  "@noble/hashes": "^1.8.0",
35
+ "ed2curve": "^0.3.0",
35
36
  "fast-json-stable-stringify": "^2.1.0",
36
37
  "fflate": "^0.8.2",
37
38
  "lodash": "^4.17.21",
@@ -43,6 +44,7 @@
43
44
  },
44
45
  "devDependencies": {
45
46
  "@types/better-sqlite3": "^7.6.12",
47
+ "@types/ed2curve": "^0.2.4",
46
48
  "@types/jest": "^29.5.13",
47
49
  "@types/lodash": "^4.17.7",
48
50
  "@types/moment": "^2.13.0",