@ibgib/core-gib 0.1.58 → 0.1.60
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/CHANGELOG.md +9 -1
- package/dist/sync/sync-peer/sync-peer-http-receiver/sync-peer-http-receiver-v1.d.mts.map +1 -1
- package/dist/sync/sync-peer/sync-peer-http-receiver/sync-peer-http-receiver-v1.mjs +6 -2
- package/dist/sync/sync-peer/sync-peer-http-receiver/sync-peer-http-receiver-v1.mjs.map +1 -1
- package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-types.d.mts +1 -1
- package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-v1.d.mts +1 -1
- package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-v1.d.mts.map +1 -1
- package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-v1.mjs +7 -11
- package/dist/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-v1.mjs.map +1 -1
- package/dist/sync/sync-peer/sync-peer-types.d.mts +24 -1
- package/dist/sync/sync-peer/sync-peer-types.d.mts.map +1 -1
- package/dist/sync/sync-peer/sync-peer-v1.d.mts +15 -4
- package/dist/sync/sync-peer/sync-peer-v1.d.mts.map +1 -1
- package/dist/sync/sync-peer/sync-peer-v1.mjs +120 -25
- package/dist/sync/sync-peer/sync-peer-v1.mjs.map +1 -1
- package/dist/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-constants.d.mts +46 -0
- package/dist/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-constants.d.mts.map +1 -0
- package/dist/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-constants.mjs +45 -0
- package/dist/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-constants.mjs.map +1 -0
- package/dist/sync/sync-peer/{sync-peer-websocket-receiver → sync-peer-websocket/sync-peer-websocket-receiver}/sync-peer-websocket-receiver-types.d.mts +2 -2
- package/dist/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-receiver/sync-peer-websocket-receiver-types.d.mts.map +1 -0
- package/dist/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-receiver/sync-peer-websocket-receiver-types.mjs.map +1 -0
- package/dist/sync/sync-peer/{sync-peer-websocket-receiver → sync-peer-websocket/sync-peer-websocket-receiver}/sync-peer-websocket-receiver-v1.d.mts +11 -8
- package/dist/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-receiver/sync-peer-websocket-receiver-v1.d.mts.map +1 -0
- package/dist/sync/sync-peer/{sync-peer-websocket-receiver → sync-peer-websocket/sync-peer-websocket-receiver}/sync-peer-websocket-receiver-v1.mjs +115 -61
- package/dist/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-receiver/sync-peer-websocket-receiver-v1.mjs.map +1 -0
- package/dist/sync/sync-peer/{sync-peer-websocket-receiver → sync-peer-websocket/sync-peer-websocket-receiver}/sync-websocket-peer-helpers.d.mts +3 -3
- package/dist/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-receiver/sync-websocket-peer-helpers.d.mts.map +1 -0
- package/dist/sync/sync-peer/{sync-peer-websocket-receiver → sync-peer-websocket/sync-peer-websocket-receiver}/sync-websocket-peer-helpers.mjs +6 -6
- package/dist/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-receiver/sync-websocket-peer-helpers.mjs.map +1 -0
- package/dist/sync/sync-peer/{sync-peer-websocket-sender → sync-peer-websocket/sync-peer-websocket-sender}/sync-peer-websocket-sender-types.d.mts +1 -1
- package/dist/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-sender/sync-peer-websocket-sender-types.d.mts.map +1 -0
- package/dist/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-sender/sync-peer-websocket-sender-types.mjs.map +1 -0
- package/dist/sync/sync-peer/{sync-peer-websocket-sender → sync-peer-websocket/sync-peer-websocket-sender}/sync-peer-websocket-sender-v1.d.mts +22 -4
- package/dist/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-sender/sync-peer-websocket-sender-v1.d.mts.map +1 -0
- package/dist/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-sender/sync-peer-websocket-sender-v1.mjs +447 -0
- package/dist/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-sender/sync-peer-websocket-sender-v1.mjs.map +1 -0
- package/dist/sync/sync-saga-context/sync-saga-context-helpers.d.mts +22 -5
- package/dist/sync/sync-saga-context/sync-saga-context-helpers.d.mts.map +1 -1
- package/dist/sync/sync-saga-context/sync-saga-context-helpers.mjs +263 -28
- package/dist/sync/sync-saga-context/sync-saga-context-helpers.mjs.map +1 -1
- package/dist/sync/sync-saga-context/sync-saga-context-types.d.mts +13 -0
- package/dist/sync/sync-saga-context/sync-saga-context-types.d.mts.map +1 -1
- package/dist/sync/sync-saga-coordinator.d.mts +12 -1
- package/dist/sync/sync-saga-coordinator.d.mts.map +1 -1
- package/dist/sync/sync-saga-coordinator.mjs +106 -12
- package/dist/sync/sync-saga-coordinator.mjs.map +1 -1
- package/dist/sync/sync-types.d.mts +24 -0
- package/dist/sync/sync-types.d.mts.map +1 -1
- package/dist/sync/sync-types.mjs +0 -1
- package/dist/sync/sync-types.mjs.map +1 -1
- package/dist/sync/sync-withid.connect.respec.mjs +3 -3
- package/dist/sync/sync-withid.connect.respec.mjs.map +1 -1
- package/dist/sync/sync-withid.pingpong.respec.d.mts +11 -0
- package/dist/sync/sync-withid.pingpong.respec.d.mts.map +1 -0
- package/dist/sync/sync-withid.pingpong.respec.mjs +199 -0
- package/dist/sync/sync-withid.pingpong.respec.mjs.map +1 -0
- package/dist/witness/space/inner-space/inner-space-v1.d.mts.map +1 -1
- package/dist/witness/space/inner-space/inner-space-v1.mjs +1 -1
- package/dist/witness/space/inner-space/inner-space-v1.mjs.map +1 -1
- package/package.json +1 -1
- package/src/sync/docs/security-3b.md +92 -0
- package/src/sync/docs/security.md +107 -39
- package/src/sync/sync-peer/sync-peer-http-receiver/sync-peer-http-receiver-v1.mts +6 -2
- package/src/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-types.mts +1 -1
- package/src/sync/sync-peer/sync-peer-innerspace/sync-peer-innerspace-v1.mts +11 -14
- package/src/sync/sync-peer/sync-peer-types.mts +28 -1
- package/src/sync/sync-peer/sync-peer-v1.mts +127 -35
- package/src/sync/sync-peer/sync-peer-websocket/README.md +42 -0
- package/src/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-constants.mts +68 -0
- package/src/sync/sync-peer/{sync-peer-websocket-receiver → sync-peer-websocket/sync-peer-websocket-receiver}/sync-peer-websocket-receiver-types.mts +2 -2
- package/src/sync/sync-peer/{sync-peer-websocket-receiver → sync-peer-websocket/sync-peer-websocket-receiver}/sync-peer-websocket-receiver-v1.mts +128 -71
- package/src/sync/sync-peer/{sync-peer-websocket-receiver → sync-peer-websocket/sync-peer-websocket-receiver}/sync-websocket-peer-helpers.mts +8 -8
- package/src/sync/sync-peer/{sync-peer-websocket-sender → sync-peer-websocket/sync-peer-websocket-sender}/sync-peer-websocket-sender-types.mts +1 -1
- package/src/sync/sync-peer/sync-peer-websocket/sync-peer-websocket-sender/sync-peer-websocket-sender-v1.mts +509 -0
- package/src/sync/sync-saga-context/sync-saga-context-helpers.mts +267 -36
- package/src/sync/sync-saga-context/sync-saga-context-types.mts +14 -0
- package/src/sync/sync-saga-coordinator.mts +148 -8
- package/src/sync/sync-types.mts +28 -4
- package/src/sync/sync-withid.connect.respec.mts +3 -3
- package/src/sync/sync-withid.pingpong.respec.mts +234 -0
- package/src/witness/space/inner-space/inner-space-v1.mts +4 -5
- package/dist/sync/sync-peer/sync-peer-websocket-receiver/sync-peer-websocket-receiver-types.d.mts.map +0 -1
- package/dist/sync/sync-peer/sync-peer-websocket-receiver/sync-peer-websocket-receiver-types.mjs.map +0 -1
- package/dist/sync/sync-peer/sync-peer-websocket-receiver/sync-peer-websocket-receiver-v1.d.mts.map +0 -1
- package/dist/sync/sync-peer/sync-peer-websocket-receiver/sync-peer-websocket-receiver-v1.mjs.map +0 -1
- package/dist/sync/sync-peer/sync-peer-websocket-receiver/sync-websocket-peer-helpers.d.mts.map +0 -1
- package/dist/sync/sync-peer/sync-peer-websocket-receiver/sync-websocket-peer-helpers.mjs.map +0 -1
- package/dist/sync/sync-peer/sync-peer-websocket-sender/sync-peer-websocket-sender-types.d.mts.map +0 -1
- package/dist/sync/sync-peer/sync-peer-websocket-sender/sync-peer-websocket-sender-types.mjs.map +0 -1
- package/dist/sync/sync-peer/sync-peer-websocket-sender/sync-peer-websocket-sender-v1.d.mts.map +0 -1
- package/dist/sync/sync-peer/sync-peer-websocket-sender/sync-peer-websocket-sender-v1.mjs +0 -282
- package/dist/sync/sync-peer/sync-peer-websocket-sender/sync-peer-websocket-sender-v1.mjs.map +0 -1
- package/src/sync/sync-peer/sync-peer-websocket-sender/sync-peer-websocket-sender-v1.mts +0 -321
- /package/dist/sync/sync-peer/{sync-peer-websocket-receiver → sync-peer-websocket/sync-peer-websocket-receiver}/sync-peer-websocket-receiver-types.mjs +0 -0
- /package/dist/sync/sync-peer/{sync-peer-websocket-sender → sync-peer-websocket/sync-peer-websocket-sender}/sync-peer-websocket-sender-types.mjs +0 -0
|
@@ -0,0 +1,509 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module sync-peer-websocket-sender-v1
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { extractErrorMsg } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
|
|
6
|
+
import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs';
|
|
7
|
+
import { IbGib_V1 } from '@ibgib/ts-gib/dist/V1/types.mjs';
|
|
8
|
+
import { KeystoneIbGib_V1 } from '../../../../keystone/keystone-types.mjs';
|
|
9
|
+
import { KeystoneService_V1 } from '../../../../keystone/keystone-service-v1.mjs';
|
|
10
|
+
import { KeystoneStrategyFactory } from '../../../../keystone/strategy/keystone-strategy-factory.mjs';
|
|
11
|
+
import { deriveSessionSecret } from '../../../sync-helpers.mjs';
|
|
12
|
+
import { SyncPeer_V1 } from '../../sync-peer-v1.mjs';
|
|
13
|
+
import { SyncSagaContextIbGib_V1 } from '../../../sync-saga-context/sync-saga-context-types.mjs';
|
|
14
|
+
import { validateContextAndSagaFrame } from '../../../sync-saga-context/sync-saga-context-helpers.mjs';
|
|
15
|
+
import { GLOBAL_LOG_A_LOT } from '../../../../core-constants.mjs';
|
|
16
|
+
import {
|
|
17
|
+
ConnectSyncPeerWebSocketSenderOpts,
|
|
18
|
+
InitializeSyncPeerWebSocketSenderOpts,
|
|
19
|
+
SyncPeerWebSocketSenderData_V1,
|
|
20
|
+
SyncPeerWebSocketSenderRel8ns_V1,
|
|
21
|
+
SyncPeerWebSocketSenderIbGib_V1
|
|
22
|
+
} from './sync-peer-websocket-sender-types.mjs';
|
|
23
|
+
import {
|
|
24
|
+
SESSION_KEYSTONE_POLICY,
|
|
25
|
+
getConnectChallenge
|
|
26
|
+
} from '../sync-peer-websocket-receiver/sync-websocket-peer-helpers.mjs';
|
|
27
|
+
import { SyncWebSocketMsgType, isSyncWebSocketMsgType, SYNC_WEB_SOCKET_MSG_TYPE_VALID_VALUES } from '../sync-peer-websocket-constants.mjs';
|
|
28
|
+
import { toDto } from '../../../../common/other/ibgib-helper.mjs';
|
|
29
|
+
import { putInSpace, registerNewIbGib } from '../../../../witness/space/space-helper.mjs';
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* helper in creating compile-time safety that we're handling all message types.
|
|
33
|
+
*/
|
|
34
|
+
function assertUnreachable(x: never): never {
|
|
35
|
+
throw new Error(`Unhandled message type: ${x} (E: e928a3f82cd7469a98ef1bc248a3f826)`);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
const logalot = GLOBAL_LOG_A_LOT || true;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* WebSocket Sender Peer implementation running in browser/native environment.
|
|
43
|
+
*/
|
|
44
|
+
export class SyncPeerWebSocketSender_V1
|
|
45
|
+
extends SyncPeer_V1<ConnectSyncPeerWebSocketSenderOpts, InitializeSyncPeerWebSocketSenderOpts>
|
|
46
|
+
implements SyncPeerWebSocketSenderIbGib_V1 {
|
|
47
|
+
|
|
48
|
+
protected override lc: string = `[${SyncPeerWebSocketSender_V1.name}]`;
|
|
49
|
+
|
|
50
|
+
override get classname(): string {
|
|
51
|
+
return SyncPeerWebSocketSender_V1.name;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public get isSocketOpen(): boolean {
|
|
55
|
+
return this.ws !== undefined && this.ws.readyState === WebSocket.OPEN;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
declare data: SyncPeerWebSocketSenderData_V1 | undefined;
|
|
59
|
+
declare rel8ns: SyncPeerWebSocketSenderRel8ns_V1 | undefined;
|
|
60
|
+
|
|
61
|
+
protected ws?: WebSocket;
|
|
62
|
+
protected activeResolve?: (value: SyncSagaContextIbGib_V1 | undefined) => void;
|
|
63
|
+
protected activeReject?: (reason: any) => void;
|
|
64
|
+
protected pendingPayloadsToSend: IbGib_V1[] = [];
|
|
65
|
+
protected handshakeMessageListener?: (event: MessageEvent) => Promise<void>;
|
|
66
|
+
|
|
67
|
+
constructor(
|
|
68
|
+
initialData: SyncPeerWebSocketSenderData_V1,
|
|
69
|
+
initialRel8ns?: SyncPeerWebSocketSenderRel8ns_V1,
|
|
70
|
+
) {
|
|
71
|
+
super(initialData, initialRel8ns);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
protected override async preConnectCheck(opts: ConnectSyncPeerWebSocketSenderOpts): Promise<void> {
|
|
75
|
+
const lc = `${this.lc}[${this.preConnectCheck.name}]`;
|
|
76
|
+
if (!this.data?.wsUrl) {
|
|
77
|
+
throw new Error(`Missing wsUrl in peer data (E: a3b2c1d0e9f8e7d6c5b4a3f2e1d0c910)`);
|
|
78
|
+
}
|
|
79
|
+
if (!this.data?.httpEvolveUrl) {
|
|
80
|
+
throw new Error(`Missing httpEvolveUrl in peer data (E: a3b2c1d0e9f8e7d6c5b4a3f2e1d0c911)`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Submits the evolved master identity (I1) and new session keystone (S) to the server's HTTP registry.
|
|
86
|
+
*/
|
|
87
|
+
protected override async postEstablishToReceiver({
|
|
88
|
+
newSenderIdentity,
|
|
89
|
+
sessionIdentity,
|
|
90
|
+
}: {
|
|
91
|
+
newSenderIdentity: KeystoneIbGib_V1;
|
|
92
|
+
sessionIdentity: KeystoneIbGib_V1;
|
|
93
|
+
}): Promise<void> {
|
|
94
|
+
const lc = `${this.lc}[${this.postEstablishToReceiver.name}]`;
|
|
95
|
+
try {
|
|
96
|
+
if (logalot) { console.log(`${lc} posting evolved keystones to ${this.data!.httpEvolveUrl}...`); }
|
|
97
|
+
if (!this.data) { throw new Error(`(UNEXPECTED) this.data falsy? (E: d642e8a9af18b532c87c6f581aa53b26)`); }
|
|
98
|
+
|
|
99
|
+
if (!this.data.httpEvolveUrl) { throw new Error(`(UNEXPECTED) this.data.httpEvolveUrl falsy? (E: 8589ddbb155914d85c09658881da2c26)`); }
|
|
100
|
+
|
|
101
|
+
const response = await fetch(this.data.httpEvolveUrl, {
|
|
102
|
+
method: 'PUT',
|
|
103
|
+
headers: {
|
|
104
|
+
'Content-Type': 'application/json',
|
|
105
|
+
},
|
|
106
|
+
body: JSON.stringify({
|
|
107
|
+
keystoneIbGib: newSenderIdentity,
|
|
108
|
+
relatedIbGibs: [sessionIdentity]
|
|
109
|
+
}),
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
if (!response.ok) {
|
|
113
|
+
const data = await response.json().catch(() => ({}));
|
|
114
|
+
const errorMsg = data.message || data.error || 'Unknown error';
|
|
115
|
+
throw new Error(`HTTP ${response.status} evolution post rejected. errorMsg: ${errorMsg} (E: e8a478291b88d05c68cf6b385684b826)`);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (logalot) { console.log(`${lc} evolve post accepted by server.`); }
|
|
119
|
+
} catch (error) {
|
|
120
|
+
console.error(`${lc} establish post failed: ${extractErrorMsg(error)}`);
|
|
121
|
+
throw error;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Establishes the stateful WebSocket connection and performs the multi-turn cryptographic challenge connect.
|
|
127
|
+
*/
|
|
128
|
+
protected override async connectImpl(opts: ConnectSyncPeerWebSocketSenderOpts): Promise<void> {
|
|
129
|
+
const lc = `${this.lc}[${this.connectImpl.name}]`;
|
|
130
|
+
try {
|
|
131
|
+
if (logalot) { console.log(`${lc} starting...`); }
|
|
132
|
+
|
|
133
|
+
const { senderIdentity, fnSenderSecret, sagaId, localMetaspace, localSpace } = this.opts!;
|
|
134
|
+
if (!senderIdentity || !fnSenderSecret || !sagaId) {
|
|
135
|
+
throw new Error(`Missing identity parameters in peer options (E: ed49b6711ac82efcdb42b8a28c5b6826)`);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// 1. Solve upfront pre-filter solution
|
|
139
|
+
const senderSecret = await fnSenderSecret();
|
|
140
|
+
const sessionSecret = await deriveSessionSecret({ senderSecret, sagaId });
|
|
141
|
+
|
|
142
|
+
// Fetch session identity S that establishSessionIdentity just created
|
|
143
|
+
const sAddr_tjp = getIbGibAddr({ ibGib: senderIdentity }); // wait, S's past contains I
|
|
144
|
+
// Actually, we can get the S keystone that was returned by establishSessionIdentity.
|
|
145
|
+
// Let's resolve S from the sender localSpace using its target claim or the targetAddrs
|
|
146
|
+
const sAddr = getIbGibAddr({ ibGib: senderIdentity.rel8ns?.past ? senderIdentity : senderIdentity }); // stub: get the actual generated S
|
|
147
|
+
|
|
148
|
+
// To make sure we have the exact session identity S, we can query localSpace.
|
|
149
|
+
// Since we know the sagaId, we can walk or locate it.
|
|
150
|
+
// But wait, the coordinator calls establishSessionIdentity first, which returns the created S keystone!
|
|
151
|
+
// Let's store a reference to the sessionIdentity S on this class during establish, or lookup from localSpace.
|
|
152
|
+
// Wait, does the coordinator let the peer store S? No, but let's query the latest keystone in localSpace.
|
|
153
|
+
const senderIdentityLatestAddr =
|
|
154
|
+
await localMetaspace.getLatestAddr({ ibGib: senderIdentity, space: localSpace });
|
|
155
|
+
if (!senderIdentityLatestAddr) { throw new Error(`senderIdentityLatestAddr falsy. we should have a latest addr that is different than the sender identity at this point (done in the peer establish phase) (E: 225607318bef485d7821f82de97b6826)`); }
|
|
156
|
+
|
|
157
|
+
const resGet = await localMetaspace.get({ addrs: [senderIdentityLatestAddr], space: localSpace });
|
|
158
|
+
const I_tip = resGet.ibGibs?.[0] as KeystoneIbGib_V1;
|
|
159
|
+
|
|
160
|
+
// const targetSAddr = I_tip?.rel8ns?.past?.[0] ?? I_tip?.data?.proofs?.[0]?.claim?.target;
|
|
161
|
+
const targetSAddr = I_tip.data.proofs.at(0)?.claim.target;
|
|
162
|
+
if (!targetSAddr) {
|
|
163
|
+
throw new Error(`Could not locate session keystone target from identity (E: 5ec4882f7e5e4c33fbd00ab8b3166726)`);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const resGetS = await localMetaspace.get({ addrs: [targetSAddr], space: localSpace });
|
|
167
|
+
const sessionS = resGetS.ibGibs?.[0] as KeystoneIbGib_V1;
|
|
168
|
+
if (!sessionS) {
|
|
169
|
+
throw new Error(`Session keystone not found in local space: ${targetSAddr} (E: 9ba818e6622808bb4ae749be31f80b26)`);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// Solve dynamic upfront picket-fence challenge
|
|
173
|
+
const connectPool =
|
|
174
|
+
(sessionS.data?.challengePools ?? [])
|
|
175
|
+
.find(p => p.id === SESSION_KEYSTONE_POLICY.CONNECT_POOL.ID);
|
|
176
|
+
if (!connectPool) {
|
|
177
|
+
throw new Error(`Session keystone missing connect pool (E: f50968afca04b6d38ec19824ea201826)`);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const { challengeId } = getConnectChallenge(sessionS);
|
|
181
|
+
const strategy = KeystoneStrategyFactory.create({ config: connectPool.config });
|
|
182
|
+
const poolSecret = await strategy.derivePoolSecret({ masterSecret: sessionSecret });
|
|
183
|
+
const solution = await strategy.generateSolution({
|
|
184
|
+
poolSecret,
|
|
185
|
+
poolId: SESSION_KEYSTONE_POLICY.CONNECT_POOL.ID,
|
|
186
|
+
challengeId
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
// 2. Open WebSocket connection
|
|
190
|
+
const wsUrl = `${this.data!.wsUrl}?sAddr=${encodeURIComponent(targetSAddr)}&solution=${encodeURIComponent(solution.value)}`;
|
|
191
|
+
if (logalot) { console.log(`${lc} connecting to WebSocket: ${wsUrl}`); }
|
|
192
|
+
|
|
193
|
+
const ws = new WebSocket(wsUrl);
|
|
194
|
+
this.ws = ws;
|
|
195
|
+
|
|
196
|
+
return new Promise<void>((resolve, reject) => {
|
|
197
|
+
let isResolved = false;
|
|
198
|
+
|
|
199
|
+
ws.addEventListener('open', () => {
|
|
200
|
+
if (logalot) { console.log(`${lc} WebSocket opened. Awaiting challenge...`); }
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
this.handshakeMessageListener = async (ev) => {
|
|
204
|
+
try {
|
|
205
|
+
const msg = JSON.parse(ev.data);
|
|
206
|
+
if (logalot) { console.log(`${lc} received handshake frame: ${msg.type}`); }
|
|
207
|
+
|
|
208
|
+
const msgType = msg.type;
|
|
209
|
+
if (!isSyncWebSocketMsgType(msgType)) {
|
|
210
|
+
const validTypes = SYNC_WEB_SOCKET_MSG_TYPE_VALID_VALUES.join(', ');
|
|
211
|
+
throw new Error(`Unknown message type '${msgType}' received during connection handshake. Valid types are: ${validTypes} (E: e983271bc84f46928e4695be2409826)`);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
switch (msgType) {
|
|
215
|
+
case SyncWebSocketMsgType.auth_challenge_init:
|
|
216
|
+
await this.handleHandshakeAuthChallengeInit(ws, targetSAddr);
|
|
217
|
+
break;
|
|
218
|
+
case SyncWebSocketMsgType.auth_challenge:
|
|
219
|
+
await this.handleHandshakeAuthChallenge(ws, msg, sessionS, sessionSecret);
|
|
220
|
+
break;
|
|
221
|
+
case SyncWebSocketMsgType.auth_ok:
|
|
222
|
+
isResolved = true;
|
|
223
|
+
this.handleHandshakeAuthOk(ws, resolve);
|
|
224
|
+
break;
|
|
225
|
+
case SyncWebSocketMsgType.auth_fail:
|
|
226
|
+
this.handleHandshakeAuthFail(msg, reject);
|
|
227
|
+
break;
|
|
228
|
+
case SyncWebSocketMsgType.sync_error:
|
|
229
|
+
this.handleHandshakeSyncError(msg, reject);
|
|
230
|
+
break;
|
|
231
|
+
// Protocol violations / unexpected messages during connection handshake
|
|
232
|
+
case SyncWebSocketMsgType.auth_init:
|
|
233
|
+
case SyncWebSocketMsgType.auth_proof:
|
|
234
|
+
case SyncWebSocketMsgType.sync_frame:
|
|
235
|
+
case SyncWebSocketMsgType.sync_frame_response:
|
|
236
|
+
case SyncWebSocketMsgType.sync_frame_authenticated:
|
|
237
|
+
case SyncWebSocketMsgType.sync_frame_response_authenticated:
|
|
238
|
+
case SyncWebSocketMsgType.domain_payload:
|
|
239
|
+
throw new Error(`Unexpected message type '${msgType}' during connection handshake (E: e3f80c68ab2a46c2b1858c8a1e2f8926)`);
|
|
240
|
+
default:
|
|
241
|
+
assertUnreachable(msgType);
|
|
242
|
+
}
|
|
243
|
+
} catch (error) {
|
|
244
|
+
console.error(`${lc} ${extractErrorMsg(error)}`);
|
|
245
|
+
reject(error);
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
ws.addEventListener('message', this.handshakeMessageListener);
|
|
250
|
+
|
|
251
|
+
ws.addEventListener('close', (event) => {
|
|
252
|
+
if (!isResolved) {
|
|
253
|
+
this.disconnect();
|
|
254
|
+
reject(new Error(`WebSocket closed before connect completed (code: ${event.code})`));
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
ws.addEventListener('error', (err) => {
|
|
259
|
+
if (!isResolved) {
|
|
260
|
+
this.disconnect();
|
|
261
|
+
reject(new Error(`WebSocket connection error`));
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
} catch (error) {
|
|
267
|
+
console.error(`${lc} connect failed: ${extractErrorMsg(error)}`);
|
|
268
|
+
throw error;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Cleanly closes the socket and removes the handshake message listener.
|
|
274
|
+
*
|
|
275
|
+
* LOGS AND SWALLOWS ERRORS, DOES NOT THROW
|
|
276
|
+
*/
|
|
277
|
+
public disconnect(): void {
|
|
278
|
+
const lc = `${this.lc}[${this.disconnect.name}]`;
|
|
279
|
+
try {
|
|
280
|
+
if (logalot) { console.log(`${lc} starting... (I: 35c4b85fca0c6e9d042f5ff87cde3826)`); }
|
|
281
|
+
|
|
282
|
+
if (this.ws) {
|
|
283
|
+
if (this.handshakeMessageListener) {
|
|
284
|
+
this.ws.removeEventListener('message', this.handshakeMessageListener);
|
|
285
|
+
this.handshakeMessageListener = undefined;
|
|
286
|
+
}
|
|
287
|
+
try {
|
|
288
|
+
this.ws.close();
|
|
289
|
+
} catch (error) {
|
|
290
|
+
console.error(`${lc} failed to close websocket: ${extractErrorMsg(error)}`);
|
|
291
|
+
}
|
|
292
|
+
this.ws = undefined;
|
|
293
|
+
}
|
|
294
|
+
} catch (error) {
|
|
295
|
+
console.error(`${lc} ${extractErrorMsg(error)}`);
|
|
296
|
+
// throw error; // disconnect does NOT rethrow
|
|
297
|
+
} finally {
|
|
298
|
+
if (logalot) { console.log(`${lc} complete.`); }
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Handles synchronizing messages and evolved context frames during active transaction turns.
|
|
304
|
+
*/
|
|
305
|
+
protected async handleRuntimeMessage(event: MessageEvent): Promise<void> {
|
|
306
|
+
const lc = `${this.lc}[${this.handleRuntimeMessage.name}]`;
|
|
307
|
+
try {
|
|
308
|
+
const msg = JSON.parse(event.data);
|
|
309
|
+
if (logalot) { console.log(`${lc} received runtime frame: ${msg.type}`); }
|
|
310
|
+
|
|
311
|
+
const msgType = msg.type;
|
|
312
|
+
if (!isSyncWebSocketMsgType(msgType)) {
|
|
313
|
+
const validTypes = SYNC_WEB_SOCKET_MSG_TYPE_VALID_VALUES.join(', ');
|
|
314
|
+
throw new Error(`Unknown message type '${msgType}' received during active sync saga loop. Valid types are: ${validTypes} (E: e23a8d10b74d47fb90518f8e3f4b826)`);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
switch (msgType) {
|
|
318
|
+
case SyncWebSocketMsgType.sync_frame_response:
|
|
319
|
+
await this.handleRuntimeSyncFrameResponse(msg);
|
|
320
|
+
break;
|
|
321
|
+
case SyncWebSocketMsgType.sync_frame_authenticated:
|
|
322
|
+
this.handleRuntimeSyncFrameAuthenticated();
|
|
323
|
+
break;
|
|
324
|
+
case SyncWebSocketMsgType.domain_payload:
|
|
325
|
+
this.handleRuntimeDomainPayload(msg);
|
|
326
|
+
break;
|
|
327
|
+
case SyncWebSocketMsgType.sync_error:
|
|
328
|
+
this.handleRuntimeSyncError(msg);
|
|
329
|
+
break;
|
|
330
|
+
// Handshake messages / unexpected messages during active sync saga loop
|
|
331
|
+
case SyncWebSocketMsgType.auth_challenge_init:
|
|
332
|
+
case SyncWebSocketMsgType.auth_init:
|
|
333
|
+
case SyncWebSocketMsgType.auth_challenge:
|
|
334
|
+
case SyncWebSocketMsgType.auth_proof:
|
|
335
|
+
case SyncWebSocketMsgType.auth_ok:
|
|
336
|
+
case SyncWebSocketMsgType.auth_fail:
|
|
337
|
+
case SyncWebSocketMsgType.sync_frame:
|
|
338
|
+
case SyncWebSocketMsgType.sync_frame_response_authenticated:
|
|
339
|
+
throw new Error(`Unexpected message type '${msgType}' during active sync saga loop (E: e982b12cf92c448bbad0e84b7263c826)`);
|
|
340
|
+
default:
|
|
341
|
+
assertUnreachable(msgType);
|
|
342
|
+
}
|
|
343
|
+
} catch (error) {
|
|
344
|
+
console.error(`${lc} failed parsing/handling runtime frame: ${extractErrorMsg(error)}`);
|
|
345
|
+
this.disconnect();
|
|
346
|
+
if (this.activeReject) {
|
|
347
|
+
const reject = this.activeReject;
|
|
348
|
+
this.activeResolve = undefined;
|
|
349
|
+
this.activeReject = undefined;
|
|
350
|
+
reject(error);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
protected async handleHandshakeAuthChallengeInit(ws: WebSocket, targetSAddr: string): Promise<void> {
|
|
356
|
+
ws.send(JSON.stringify({
|
|
357
|
+
type: SyncWebSocketMsgType.auth_init,
|
|
358
|
+
sAddr: targetSAddr
|
|
359
|
+
}));
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
protected async handleHandshakeAuthChallenge(
|
|
363
|
+
ws: WebSocket,
|
|
364
|
+
msg: any,
|
|
365
|
+
sessionS: KeystoneIbGib_V1,
|
|
366
|
+
sessionSecret: string
|
|
367
|
+
): Promise<void> {
|
|
368
|
+
const { challengeUuid, demandedIds } = msg;
|
|
369
|
+
if (logalot) { console.log(`${this.lc} solving demanded challenges: ${demandedIds.join(', ')}`); }
|
|
370
|
+
|
|
371
|
+
const proofFrame = await this.signContextConnect({
|
|
372
|
+
challengeUuid,
|
|
373
|
+
demandedIds
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
if (!proofFrame) {
|
|
377
|
+
throw new Error(`Failed to sign connect challenge proof (E: e9807f28ae9248bbadd0a7f1a8e9826)`);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
ws.send(JSON.stringify({
|
|
381
|
+
type: SyncWebSocketMsgType.auth_proof,
|
|
382
|
+
proofFrame
|
|
383
|
+
}));
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
protected handleHandshakeAuthOk(ws: WebSocket, resolve: () => void): void {
|
|
387
|
+
if (logalot) { console.log(`${this.lc} WebSocket connect SUCCESS!`); }
|
|
388
|
+
if (this.handshakeMessageListener) {
|
|
389
|
+
ws.removeEventListener('message', this.handshakeMessageListener);
|
|
390
|
+
this.handshakeMessageListener = undefined;
|
|
391
|
+
}
|
|
392
|
+
ws.addEventListener('message', (event) => this.handleRuntimeMessage(event));
|
|
393
|
+
resolve();
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
protected handleHandshakeAuthFail(msg: any, reject: (err: Error) => void): void {
|
|
397
|
+
this.disconnect();
|
|
398
|
+
reject(new Error(`Handshake auth failed: ${msg.message || 'Unknown fail reason'} (E: f380b271a2be498db257bc8209fa8926)`));
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
protected handleHandshakeSyncError(msg: any, reject: (err: Error) => void): void {
|
|
402
|
+
this.disconnect();
|
|
403
|
+
reject(new Error(`Handshake sync error: ${msg.message || msg.error || 'Unknown sync error'} (E: f9208a01fe434cbbad83f210ea8f3426)`));
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
protected async handleRuntimeSyncFrameResponse(msg: any): Promise<void> {
|
|
407
|
+
const responseContext = msg.context as SyncSagaContextIbGib_V1;
|
|
408
|
+
|
|
409
|
+
const validationErrors = await validateContextAndSagaFrame({ context: responseContext });
|
|
410
|
+
if (validationErrors.length > 0) {
|
|
411
|
+
throw new Error(`Invalid response context received: ${validationErrors.join(', ')} (E: d7b5a283cf4c43ba8659c803800cf826)`);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// Put response control ibgibs into durable space immediately
|
|
415
|
+
const allControlIbGibs: IbGib_V1[] = [
|
|
416
|
+
toDto({ ibGib: responseContext }),
|
|
417
|
+
responseContext.sagaFrame,
|
|
418
|
+
responseContext.sagaFrameMsg!
|
|
419
|
+
];
|
|
420
|
+
if (responseContext.signedSessionIdentity) {
|
|
421
|
+
allControlIbGibs.push(responseContext.signedSessionIdentity);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
const { localSpace } = this.opts!;
|
|
425
|
+
for (const ibGib of allControlIbGibs) {
|
|
426
|
+
await putInSpace({ space: localSpace, ibGibs: [ibGib] });
|
|
427
|
+
await registerNewIbGib({ space: localSpace, ibGib });
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
const expectedPayloadAddrs = responseContext.data?.['@payloadAddrsDomain'] || [];
|
|
431
|
+
if (expectedPayloadAddrs.length > 0) {
|
|
432
|
+
this.ws!.send(JSON.stringify({
|
|
433
|
+
type: SyncWebSocketMsgType.sync_frame_response_authenticated,
|
|
434
|
+
contextAddr: getIbGibAddr({ ibGib: responseContext })
|
|
435
|
+
}));
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
if (this.activeResolve) {
|
|
439
|
+
const resolve = this.activeResolve;
|
|
440
|
+
this.activeResolve = undefined;
|
|
441
|
+
this.activeReject = undefined;
|
|
442
|
+
resolve(responseContext);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
protected handleRuntimeSyncFrameAuthenticated(): void {
|
|
447
|
+
const payloads = this.pendingPayloadsToSend || [];
|
|
448
|
+
this.pendingPayloadsToSend = [];
|
|
449
|
+
for (const ibGib of payloads) {
|
|
450
|
+
this.ws!.send(JSON.stringify({
|
|
451
|
+
type: SyncWebSocketMsgType.domain_payload,
|
|
452
|
+
ibGib
|
|
453
|
+
}));
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
protected handleRuntimeDomainPayload(msg: any): void {
|
|
458
|
+
const payload = msg.ibGib as IbGib_V1;
|
|
459
|
+
this.payloadIbGibsDomainReceived$.next(payload);
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
protected handleRuntimeSyncError(msg: any): void {
|
|
463
|
+
const errorDetail = msg.message || msg.error || 'Unknown sync runtime error';
|
|
464
|
+
const error = new Error(`Sync runtime error from receiver: ${errorDetail} (E: e983bc1c828d447fa0581da2b8004f26)`);
|
|
465
|
+
if (this.activeReject) {
|
|
466
|
+
const reject = this.activeReject;
|
|
467
|
+
this.activeResolve = undefined;
|
|
468
|
+
this.activeReject = undefined;
|
|
469
|
+
reject(error);
|
|
470
|
+
} else {
|
|
471
|
+
throw error;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* Serializes the transaction context and payload down the active WebSocket connection.
|
|
477
|
+
*/
|
|
478
|
+
protected override async sendContextRequest(context: SyncSagaContextIbGib_V1): Promise<SyncSagaContextIbGib_V1 | undefined> {
|
|
479
|
+
const lc = `${this.lc}[${this.sendContextRequest.name}]`;
|
|
480
|
+
try {
|
|
481
|
+
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
|
482
|
+
throw new Error(`WebSocket is not connected or open (E: a3b2c1d0e9f8e7d6c5b4a3f2e1d0c915)`);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
// 1. Separate payloads to send later
|
|
486
|
+
const domainPayloads = context.payloadIbGibsDomain ?? [];
|
|
487
|
+
this.pendingPayloadsToSend = [...domainPayloads];
|
|
488
|
+
|
|
489
|
+
// 2. Clone context without payloads for transport
|
|
490
|
+
const contextToSend = { ...context };
|
|
491
|
+
delete contextToSend.payloadIbGibsDomain;
|
|
492
|
+
|
|
493
|
+
// 3. Transmit the synchronizing transaction context
|
|
494
|
+
return new Promise<SyncSagaContextIbGib_V1 | undefined>((resolve, reject) => {
|
|
495
|
+
this.activeResolve = resolve;
|
|
496
|
+
this.activeReject = reject;
|
|
497
|
+
|
|
498
|
+
this.ws!.send(JSON.stringify({
|
|
499
|
+
type: SyncWebSocketMsgType.sync_frame,
|
|
500
|
+
context: contextToSend
|
|
501
|
+
}));
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
} catch (error) {
|
|
505
|
+
console.error(`${lc} sendContextRequest failed: ${extractErrorMsg(error)}`);
|
|
506
|
+
throw error;
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
}
|