@ibgib/core-gib 0.1.4 → 0.1.8
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/README.md +26 -0
- package/dist/assumptions.respec.mjs.map +1 -1
- package/dist/common/alias/alias-helper.mjs +1 -1
- package/dist/common/alias/alias-helper.mjs.map +1 -1
- package/dist/common/comment/comment-helper.mjs.map +1 -1
- package/dist/common/display/display-helper.mjs.map +1 -1
- package/dist/common/display/display-types.d.mts +1 -1
- package/dist/common/display/display-types.d.mts.map +1 -1
- package/dist/common/encrypt/encrypt-helper.mjs.map +1 -1
- package/dist/common/encrypt/encrypt-types.d.mts +1 -1
- package/dist/common/encrypt/encrypt-types.d.mts.map +1 -1
- package/dist/common/error/error-helper.mjs.map +1 -1
- package/dist/common/form/form-helper.mjs.map +1 -1
- package/dist/common/form/form-items.d.mts.map +1 -1
- package/dist/common/import-export/import-export-helper.web.mjs.map +1 -1
- package/dist/common/link/link-helper.mjs.map +1 -1
- package/dist/common/meta-stone/meta-stone-helper.mjs.map +1 -1
- package/dist/common/meta-stone/meta-stone.respec.mjs.map +1 -1
- package/dist/common/other/graph-helper.mjs +3 -3
- package/dist/common/other/graph-helper.mjs.map +1 -1
- package/dist/common/other/ibgib-helper.mjs.map +1 -1
- package/dist/common/other/ibgib-helper.respec.mjs.map +1 -1
- package/dist/common/other/other-constants.mjs +4 -4
- package/dist/common/other/other-constants.mjs.map +1 -1
- package/dist/common/other/other-helper.web.mjs.map +1 -1
- package/dist/common/other/other-types.d.mts +10 -10
- package/dist/common/other/other-types.d.mts.map +1 -1
- package/dist/common/other/svg-helper.mjs.map +1 -1
- package/dist/common/pic/pic-helper.mjs.map +1 -1
- package/dist/common/pubsub/observable/observable-base-v1.mjs +1 -1
- package/dist/common/pubsub/observable/observable-base-v1.mjs.map +1 -1
- package/dist/common/pubsub/observable/observable-event/observable-event-helper.mjs.map +1 -1
- package/dist/common/pubsub/observable/observable-helper.mjs.map +1 -1
- package/dist/common/pubsub/observable/observable-types.d.mts +1 -1
- package/dist/common/pubsub/observable/observable-types.d.mts.map +1 -1
- package/dist/common/pubsub/observer/observer-helper.mjs.map +1 -1
- package/dist/common/pubsub/observer/observer-types.d.mts.map +1 -1
- package/dist/common/pubsub/subject/subject-helper.mjs.map +1 -1
- package/dist/common/pubsub/subject/subject-types.d.mts +1 -1
- package/dist/common/pubsub/subject/subject-types.d.mts.map +1 -1
- package/dist/common/pubsub/subject/subject-v1.mjs.map +1 -1
- package/dist/common/pubsub/subject/subject.respec.mjs.map +1 -1
- package/dist/common/pubsub/subscription/subscription-helper.mjs.map +1 -1
- package/dist/common/pubsub/subscription/subscription-types.d.mts.map +1 -1
- package/dist/common/pubsub/subscription/subscription-v1.mjs.map +1 -1
- package/dist/common/secret/secret-helper.mjs.map +1 -1
- package/dist/common/secret/secret-types.d.mts +1 -1
- package/dist/common/secret/secret-types.d.mts.map +1 -1
- package/dist/common/secret/secret.respec.mjs +1 -1
- package/dist/common/secret/secret.respec.mjs.map +1 -1
- package/dist/common/tag/tag-helper.mjs.map +1 -1
- package/dist/core-helper.respec.mjs.map +1 -1
- package/dist/keystone/keystone-config-builder.d.mts +77 -0
- package/dist/keystone/keystone-config-builder.d.mts.map +1 -0
- package/dist/keystone/keystone-config-builder.mjs +157 -0
- package/dist/keystone/keystone-config-builder.mjs.map +1 -0
- package/dist/keystone/keystone-constants.d.mts +36 -0
- package/dist/keystone/keystone-constants.d.mts.map +1 -0
- package/dist/keystone/keystone-constants.mjs +39 -0
- package/dist/keystone/keystone-constants.mjs.map +1 -0
- package/dist/keystone/keystone-helpers.d.mts +117 -0
- package/dist/keystone/keystone-helpers.d.mts.map +1 -0
- package/dist/keystone/keystone-helpers.mjs +455 -0
- package/dist/keystone/keystone-helpers.mjs.map +1 -0
- package/dist/keystone/keystone-service-v1.d.mts +77 -0
- package/dist/keystone/keystone-service-v1.d.mts.map +1 -0
- package/dist/keystone/keystone-service-v1.mjs +502 -0
- package/dist/keystone/keystone-service-v1.mjs.map +1 -0
- package/dist/keystone/keystone-service-v1.respec.d.mts +2 -0
- package/dist/keystone/keystone-service-v1.respec.d.mts.map +1 -0
- package/dist/keystone/keystone-service-v1.respec.mjs +460 -0
- package/dist/keystone/keystone-service-v1.respec.mjs.map +1 -0
- package/dist/keystone/keystone-types.d.mts +248 -0
- package/dist/keystone/keystone-types.d.mts.map +1 -0
- package/dist/keystone/keystone-types.mjs +50 -0
- package/dist/keystone/keystone-types.mjs.map +1 -0
- package/dist/keystone/strategy/hash-reveal-v1/hash-reveal-v1.d.mts +35 -0
- package/dist/keystone/strategy/hash-reveal-v1/hash-reveal-v1.d.mts.map +1 -0
- package/dist/keystone/strategy/hash-reveal-v1/hash-reveal-v1.mjs +107 -0
- package/dist/keystone/strategy/hash-reveal-v1/hash-reveal-v1.mjs.map +1 -0
- package/dist/keystone/strategy/keystone-strategy-factory.d.mts +15 -0
- package/dist/keystone/strategy/keystone-strategy-factory.d.mts.map +1 -0
- package/dist/keystone/strategy/keystone-strategy-factory.mjs +26 -0
- package/dist/keystone/strategy/keystone-strategy-factory.mjs.map +1 -0
- package/dist/keystone/strategy/keystone-strategy.d.mts +48 -0
- package/dist/keystone/strategy/keystone-strategy.d.mts.map +1 -0
- package/dist/keystone/strategy/keystone-strategy.mjs +14 -0
- package/dist/keystone/strategy/keystone-strategy.mjs.map +1 -0
- package/dist/respec-gib.node.mjs +3 -1
- package/dist/respec-gib.node.mjs.map +1 -1
- package/dist/spec-helper.node.respec.d.mts.map +1 -1
- package/dist/spec-helper.node.respec.mjs +4 -6
- package/dist/spec-helper.node.respec.mjs.map +1 -1
- package/dist/timeline/timeline-api.mjs +12 -12
- package/dist/timeline/timeline-api.mjs.map +1 -1
- package/dist/witness/anonymous-fn/anonymous-fn-helper.mjs.map +1 -1
- package/dist/witness/anonymous-fn/anonymous-fn-v1.mjs.map +1 -1
- package/dist/witness/app/app-base-v1.mjs.map +1 -1
- package/dist/witness/app/app-helper.mjs.map +1 -1
- package/dist/witness/app/app-types.d.mts.map +1 -1
- package/dist/witness/factory/dynamic-form-factory-base.mjs.map +1 -1
- package/dist/witness/light-witness-base-v1.mjs.map +1 -1
- package/dist/witness/robbot/robbot-base-v1.mjs +1 -1
- package/dist/witness/robbot/robbot-base-v1.mjs.map +1 -1
- package/dist/witness/robbot/robbot-helper.mjs.map +1 -1
- package/dist/witness/robbot/robbot-types.d.mts +20 -20
- package/dist/witness/robbot/robbot-types.d.mts.map +1 -1
- package/dist/witness/space/bootstrap/bootstrap-helper.mjs.map +1 -1
- package/dist/witness/space/filesystem-space/filesystem-space-v1.mjs.map +1 -1
- package/dist/witness/space/filesystem-space/filesystem-space-v1.respec.mjs.map +1 -1
- package/dist/witness/space/filesystem-space/node-filesystem-space/node-filesystem-space-helper.mjs.map +1 -1
- package/dist/witness/space/filesystem-space/node-filesystem-space/node-filesystem-space-types.d.mts.map +1 -1
- package/dist/witness/space/filesystem-space/node-filesystem-space/node-filesystem-space-v1.mjs +1 -1
- package/dist/witness/space/filesystem-space/node-filesystem-space/node-filesystem-space-v1.mjs.map +1 -1
- package/dist/witness/space/filesystem-space/node-filesystem-space/respec/testSpace_createAndInit.node-filesystem-space-v1.respec.mjs.map +1 -1
- package/dist/witness/space/filesystem-space/node-filesystem-space/respec/testSpace_persistTransformResult.node-filesystem-space-v1.respec.mjs.map +1 -1
- package/dist/witness/space/filesystem-space/node-filesystem-space/respec/testSpace_putGetDelete.node-filesystem-space-v1.respec.mjs.map +1 -1
- package/dist/witness/space/filesystem-space/node-filesystem-space/respec/testSpace_registerNewIbGib_GetLatest.node-filesystem-space-v1.respec.mjs.map +1 -1
- package/dist/witness/space/inner-space/inner-space-v1.mjs.map +1 -1
- package/dist/witness/space/inner-space/inner-space-v1.respec.mjs.map +1 -1
- package/dist/witness/space/metaspace/metaspace-base.mjs +1 -1
- package/dist/witness/space/metaspace/metaspace-base.mjs.map +1 -1
- package/dist/witness/space/metaspace/metaspace-innerspace/metaspace-innerspace-helper.mjs.map +1 -1
- package/dist/witness/space/metaspace/metaspace-innerspace/metaspace-innerspace.mjs.map +1 -1
- package/dist/witness/space/outer-space/outer-space-helper.mjs.map +1 -1
- package/dist/witness/space/outer-space/outer-space-types.d.mts.map +1 -1
- package/dist/witness/space/outer-space/outer-space-types.mjs +1 -1
- package/dist/witness/space/outer-space/outer-space-types.mjs.map +1 -1
- package/dist/witness/space/reconciliation-space/reconciliation-space-base.mjs.map +1 -1
- package/dist/witness/space/reconciliation-space/reconciliation-space-helper.mjs.map +1 -1
- package/dist/witness/space/space-base-v1.mjs.map +1 -1
- package/dist/witness/space/space-constants.mjs +4 -4
- package/dist/witness/space/space-constants.mjs.map +1 -1
- package/dist/witness/space/space-helper.mjs +2 -2
- package/dist/witness/space/space-helper.mjs.map +1 -1
- package/dist/witness/space/space-respec-helper.mjs.map +1 -1
- package/dist/witness/space/space-types.d.mts +4 -4
- package/dist/witness/space/space-types.d.mts.map +1 -1
- package/dist/witness/witness-base-v1.mjs.map +1 -1
- package/dist/witness/witness-form-builder.mjs.map +1 -1
- package/dist/witness/witness-helper.mjs.map +1 -1
- package/dist/witness/witness-with-context/witness-with-context-base-v1.mjs.map +1 -1
- package/package.json +6 -5
- package/src/keystone/README.md +162 -0
- package/src/keystone/keystone-config-builder.mts +187 -0
- package/src/keystone/keystone-constants.mts +44 -0
- package/src/keystone/keystone-helpers.mts +571 -0
- package/src/keystone/keystone-service-v1.mts +611 -0
- package/src/keystone/keystone-service-v1.respec.mts +555 -0
- package/src/keystone/keystone-types.mts +315 -0
- package/src/keystone/strategy/hash-reveal-v1/hash-reveal-v1.mts +146 -0
- package/src/keystone/strategy/keystone-strategy-factory.mts +35 -0
- package/src/keystone/strategy/keystone-strategy.mts +71 -0
- package/src/respec-gib.node.mts +3 -1
- package/src/spec-helper.node.respec.mts +4 -6
- package/src/witness/robbot/robbot-base-v1.mts +1 -1
|
@@ -0,0 +1,571 @@
|
|
|
1
|
+
import { extractErrorMsg, hash, pretty } from "@ibgib/helper-gib/dist/helpers/utils-helper.mjs";
|
|
2
|
+
import { Ib, TransformResult } from "@ibgib/ts-gib/dist/types.mjs";
|
|
3
|
+
import { getIbAndGib, getIbGibAddr } from "@ibgib/ts-gib/dist/helper.mjs";
|
|
4
|
+
import { validateIbGibIntrinsically } from "@ibgib/ts-gib/dist/V1/validate-helper.mjs";
|
|
5
|
+
|
|
6
|
+
import { GLOBAL_LOG_A_LOT } from "../core-constants.mjs";
|
|
7
|
+
import { KEYSTONE_ATOM } from "./keystone-constants.mjs";
|
|
8
|
+
import { KeystoneData_V1, KeystoneIbGib_V1, KeystoneIbInfo_V1, KeystoneChallengePool, DeterministicResult, KeystoneProof, KeystonePoolConfig, KeystoneReplenishStrategy, KEYSTONE_REPLENISH_STRATEGY_VALID_VALUES } from "./keystone-types.mjs";
|
|
9
|
+
import { MetaspaceService } from "../witness/space/metaspace/metaspace-types.mjs";
|
|
10
|
+
import { IbGibSpaceAny } from "../witness/space/space-base-v1.mjs";
|
|
11
|
+
import { KeystoneStrategyFactory } from "./strategy/keystone-strategy-factory.mjs";
|
|
12
|
+
|
|
13
|
+
const logalot = GLOBAL_LOG_A_LOT;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* space-delimited keystone ib containing select keystone metadata.
|
|
17
|
+
*
|
|
18
|
+
* NOTE: This must match {@link parseKeystoneIb}
|
|
19
|
+
* @see {@link KeystoneIbInfo_V1}
|
|
20
|
+
*/
|
|
21
|
+
export async function getKeystoneIb({
|
|
22
|
+
keystoneData,
|
|
23
|
+
}: {
|
|
24
|
+
keystoneData: KeystoneData_V1,
|
|
25
|
+
}): Promise<Ib> {
|
|
26
|
+
const lc = `[${getKeystoneIb.name}]`;
|
|
27
|
+
try {
|
|
28
|
+
if (logalot) { console.log(`${lc} starting... (I: c3022a146faac9730154f34d1439a225)`); }
|
|
29
|
+
|
|
30
|
+
const atom = KEYSTONE_ATOM;
|
|
31
|
+
|
|
32
|
+
const ib = [
|
|
33
|
+
atom,
|
|
34
|
+
].join(' ');
|
|
35
|
+
|
|
36
|
+
return ib;
|
|
37
|
+
} catch (error) {
|
|
38
|
+
console.error(`${lc} ${extractErrorMsg(error)}`);
|
|
39
|
+
throw error;
|
|
40
|
+
} finally {
|
|
41
|
+
if (logalot) { console.log(`${lc} complete.`); }
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* NOTE: This must match {@link getKeystoneIb}
|
|
47
|
+
* @see {@link KeystoneIbInfo_V1}
|
|
48
|
+
*/
|
|
49
|
+
export async function parseKeystoneIb({
|
|
50
|
+
ib,
|
|
51
|
+
}: {
|
|
52
|
+
ib: Ib,
|
|
53
|
+
}): Promise<KeystoneIbInfo_V1> {
|
|
54
|
+
const lc = `[${parseKeystoneIb.name}]`;
|
|
55
|
+
try {
|
|
56
|
+
if (logalot) { console.log(`${lc} starting... (I: 73cb6832984255ed48b2f44db6a21e25)`); }
|
|
57
|
+
const [
|
|
58
|
+
atom
|
|
59
|
+
] = ib.split(' ');
|
|
60
|
+
|
|
61
|
+
if (atom !== KEYSTONE_ATOM) {
|
|
62
|
+
throw new Error(`invalid keystone ib. atom found in ib (${atom}) does not match keystone atom (${KEYSTONE_ATOM}) (E: 79b3d587824c4271b6e60acc76e0c825)`);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
atom,
|
|
67
|
+
}
|
|
68
|
+
} catch (error) {
|
|
69
|
+
console.error(`${lc} ${extractErrorMsg(error)}`);
|
|
70
|
+
throw error;
|
|
71
|
+
} finally {
|
|
72
|
+
if (logalot) { console.log(`${lc} complete.`); }
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The Policy Engine.
|
|
78
|
+
* Calculates exactly which challenges MUST be consumed based on config and demands.
|
|
79
|
+
* Enforces STRICT DISTINCTNESS (No double-dipping).
|
|
80
|
+
*/
|
|
81
|
+
export function getDeterministicRequirements({
|
|
82
|
+
pool,
|
|
83
|
+
requiredChallengeIds,
|
|
84
|
+
targetAddr
|
|
85
|
+
}: {
|
|
86
|
+
pool: KeystoneChallengePool;
|
|
87
|
+
requiredChallengeIds?: string[];
|
|
88
|
+
targetAddr?: string;
|
|
89
|
+
}): DeterministicResult {
|
|
90
|
+
const behavior = pool.config.behavior;
|
|
91
|
+
const mandatory = new Set<string>();
|
|
92
|
+
|
|
93
|
+
// Start with all available IDs.
|
|
94
|
+
// We assume Object.keys respects insertion order (ES2015+), crucial for FIFO.
|
|
95
|
+
let available = Object.keys(pool.challenges);
|
|
96
|
+
|
|
97
|
+
// ---------------------------------------------------------
|
|
98
|
+
// 1. Alice's Demands (Explicit Requirements)
|
|
99
|
+
// ---------------------------------------------------------
|
|
100
|
+
if (requiredChallengeIds && requiredChallengeIds.length > 0) {
|
|
101
|
+
for (const id of requiredChallengeIds) {
|
|
102
|
+
if (!pool.challenges[id]) {
|
|
103
|
+
throw new Error(`(UNEXPECTED) Required challenge ID not found in pool: ${id} (E: 2c9f8...)`);
|
|
104
|
+
}
|
|
105
|
+
// Strict: Consume it.
|
|
106
|
+
if (!available.includes(id)) {
|
|
107
|
+
// Should be caught by check above, but handles duplicates in 'demands'
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
mandatory.add(id);
|
|
111
|
+
}
|
|
112
|
+
// Remove from available pool
|
|
113
|
+
available = available.filter(id => !mandatory.has(id));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// ---------------------------------------------------------
|
|
117
|
+
// 2. Target Binding (Explicit Buckets)
|
|
118
|
+
// ---------------------------------------------------------
|
|
119
|
+
if (behavior.targetBindingChars > 0 && targetAddr) {
|
|
120
|
+
const { gib } = getIbAndGib({ ibGibAddr: targetAddr });
|
|
121
|
+
if (gib) {
|
|
122
|
+
// Get required hex prefixes (e.g. 'a', 'b', 'c', '1')
|
|
123
|
+
const prefixes = gib.substring(0, behavior.targetBindingChars).toLowerCase();
|
|
124
|
+
|
|
125
|
+
for (const char of prefixes) {
|
|
126
|
+
// Look in the Explicit Bucket
|
|
127
|
+
const bucket = pool.bindingMap[char] || [];
|
|
128
|
+
|
|
129
|
+
// Find the first ID in this bucket that is still in 'available'
|
|
130
|
+
const match = bucket.find(id => available.includes(id));
|
|
131
|
+
|
|
132
|
+
if (!match) {
|
|
133
|
+
throw new Error(`Entropy Exhaustion. Cannot satisfy binding for char '${char}'. (E: 8d3a1...)`);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Strict: Consume it.
|
|
137
|
+
mandatory.add(match);
|
|
138
|
+
available = available.filter(id => id !== match);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// ---------------------------------------------------------
|
|
144
|
+
// 3. FIFO (Sequential)
|
|
145
|
+
// ---------------------------------------------------------
|
|
146
|
+
if (behavior.selectSequentially > 0) {
|
|
147
|
+
// Take the first N from the remaining available list
|
|
148
|
+
if (available.length < behavior.selectSequentially) {
|
|
149
|
+
console.error(`[getDeterministicRequirements] Entropy Exhaustion! AvailableCount: ${available.length}, Required Seq: ${behavior.selectSequentially}. Available IDs: ${available.join(',')}`);
|
|
150
|
+
throw new Error(`Entropy Exhaustion. Insufficient challenges for FIFO requirement. (E: 9c2b4...)`);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const fifoIds = available.slice(0, behavior.selectSequentially);
|
|
154
|
+
fifoIds.forEach(id => mandatory.add(id));
|
|
155
|
+
|
|
156
|
+
// Remove from available
|
|
157
|
+
available = available.slice(behavior.selectSequentially);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return { mandatoryIds: mandatory, availableIds: available };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Helper to update the Binding Map when adding new Challenge IDs.
|
|
165
|
+
* Uses "Implicit Bucketing" (ID start char) but can be extended for full coverage.
|
|
166
|
+
*/
|
|
167
|
+
export function addToBindingMap(
|
|
168
|
+
map: { [char: string]: string[] },
|
|
169
|
+
challengeId: string
|
|
170
|
+
): void {
|
|
171
|
+
const firstChar = challengeId.charAt(0).toLowerCase();
|
|
172
|
+
// Validate it is hex
|
|
173
|
+
if (/[0-9a-f]/.test(firstChar)) {
|
|
174
|
+
if (!map[firstChar]) map[firstChar] = [];
|
|
175
|
+
map[firstChar].push(challengeId);
|
|
176
|
+
|
|
177
|
+
// OPTIONAL: Implement Full Coverage Strategy here?
|
|
178
|
+
// e.g. map[challengeId[1]].push(challengeId) ...
|
|
179
|
+
// For V1, we stick to Native/Implicit bucket (Index 0).
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Helper to clean up Binding Map when removing IDs.
|
|
185
|
+
*/
|
|
186
|
+
export function removeFromBindingMap(
|
|
187
|
+
map: { [char: string]: string[] },
|
|
188
|
+
challengeId: string
|
|
189
|
+
): void {
|
|
190
|
+
// Since we don't know exactly which buckets an ID is in (if we did multi-bucket),
|
|
191
|
+
// we strictly should scan all. For V1 Native, we check first char.
|
|
192
|
+
// SAFE IMPLEMENTATION: Scan all buckets.
|
|
193
|
+
for (const key of Object.keys(map)) {
|
|
194
|
+
map[key] = map[key].filter(id => id !== challengeId);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Selects the specific pool to use for an operation based on ID, filter criteria, or verb authorization.
|
|
200
|
+
*
|
|
201
|
+
* @returns The matching KeystoneChallengePool
|
|
202
|
+
* @throws If no pool matches or if multiple pools match but one was expected.
|
|
203
|
+
*/
|
|
204
|
+
export function resolveTargetPool({
|
|
205
|
+
pools,
|
|
206
|
+
poolId,
|
|
207
|
+
poolFilter,
|
|
208
|
+
verb,
|
|
209
|
+
}: {
|
|
210
|
+
pools: KeystoneChallengePool[];
|
|
211
|
+
/**
|
|
212
|
+
* Explicit ID of the pool to use.
|
|
213
|
+
*/
|
|
214
|
+
poolId?: string;
|
|
215
|
+
/**
|
|
216
|
+
* Optional predicate to find a pool.
|
|
217
|
+
* Useful for finding delegates via metadata.
|
|
218
|
+
*/
|
|
219
|
+
poolFilter?: (pool: KeystoneChallengePool) => boolean;
|
|
220
|
+
/**
|
|
221
|
+
* The verb being performed (e.g. 'revoke', 'manage', 'post').
|
|
222
|
+
* Used for authorization checks and auto-resolution.
|
|
223
|
+
*/
|
|
224
|
+
verb?: string;
|
|
225
|
+
}): KeystoneChallengePool {
|
|
226
|
+
const lc = `[resolveTargetPool]`;
|
|
227
|
+
try {
|
|
228
|
+
let pool: KeystoneChallengePool | undefined;
|
|
229
|
+
|
|
230
|
+
if (poolId) {
|
|
231
|
+
// 1. Explicit ID Strategy
|
|
232
|
+
pool = pools.find(p => p.id === poolId);
|
|
233
|
+
if (!pool) { throw new Error(`Pool not found with ID: ${poolId} (E: 4a2b17428515c1e82813158581898125)`); }
|
|
234
|
+
} else if (poolFilter) {
|
|
235
|
+
// 2. Filter Strategy
|
|
236
|
+
const matches = pools.filter(poolFilter);
|
|
237
|
+
if (matches.length === 0) { throw new Error(`No pool matched the provided filter. (E: 5b3c27428515c1e82813158581898125)`); }
|
|
238
|
+
// For now, we take the first match. In future, might want to be strict about uniqueness.
|
|
239
|
+
pool = matches[0];
|
|
240
|
+
} else {
|
|
241
|
+
// 3. Auto-Resolution by Verb Strategy
|
|
242
|
+
if (!verb) { throw new Error(`Cannot auto-resolve pool without a verb. (E: 6c4d37428515c1e82813158581898125)`); }
|
|
243
|
+
|
|
244
|
+
// Priority A: Look for Specific Match (Pool explicitly lists this verb)
|
|
245
|
+
pool = pools.find(p =>
|
|
246
|
+
p.config.allowedVerbs && p.config.allowedVerbs.includes(verb)
|
|
247
|
+
);
|
|
248
|
+
|
|
249
|
+
// Priority B: Look for General/Default (No restrictions / Wildcard)
|
|
250
|
+
if (!pool) {
|
|
251
|
+
pool = pools.find(p =>
|
|
252
|
+
!p.config.allowedVerbs || p.config.allowedVerbs.length === 0
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if (!pool) { throw new Error(`No suitable pool found for verb: ${verb} (E: 7d5e47428515c1e82813158581898125)`); }
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// 4. Authorization Check (Applies to all strategies if verb is present)
|
|
260
|
+
if (verb && pool.config.allowedVerbs && pool.config.allowedVerbs.length > 0) {
|
|
261
|
+
if (!pool.config.allowedVerbs.includes(verb)) {
|
|
262
|
+
throw new Error(`Pool ${pool.id} is not authorized for verb: ${verb} (E: 8e6f57428515c1e82813158581898125)`);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
return pool;
|
|
267
|
+
} catch (error) {
|
|
268
|
+
console.error(`${lc} ${extractErrorMsg(error)}`);
|
|
269
|
+
throw error;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Calculates the complete list of Challenge IDs to solve for a given operation.
|
|
275
|
+
* Combines Deterministic requirements (Mandatory/Binding/FIFO) with Stochastic requirements.
|
|
276
|
+
*
|
|
277
|
+
* @returns Array of unique challenge IDs.
|
|
278
|
+
*/
|
|
279
|
+
export function selectChallengeIds({
|
|
280
|
+
pool,
|
|
281
|
+
targetAddr,
|
|
282
|
+
requiredChallengeIds,
|
|
283
|
+
}: {
|
|
284
|
+
pool: KeystoneChallengePool;
|
|
285
|
+
/**
|
|
286
|
+
* The address of the target ibgib (used for binding entropy).
|
|
287
|
+
*/
|
|
288
|
+
targetAddr?: string;
|
|
289
|
+
/**
|
|
290
|
+
* Explicit demands from a verifier.
|
|
291
|
+
*/
|
|
292
|
+
requiredChallengeIds?: string[];
|
|
293
|
+
}): string[] {
|
|
294
|
+
const lc = `[selectChallengeIds]`;
|
|
295
|
+
try {
|
|
296
|
+
// 1. Get Deterministic Requirements
|
|
297
|
+
const { mandatoryIds, availableIds } = getDeterministicRequirements({
|
|
298
|
+
pool,
|
|
299
|
+
requiredChallengeIds,
|
|
300
|
+
targetAddr
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
// 2. Stochastic Selection
|
|
304
|
+
const randomCount = pool.config.behavior.selectRandomly;
|
|
305
|
+
const randomIds: string[] = [];
|
|
306
|
+
|
|
307
|
+
if (randomCount > 0) {
|
|
308
|
+
if (availableIds.length < randomCount) {
|
|
309
|
+
throw new Error(`Insufficient challenges for random requirement. Need ${randomCount}, have ${availableIds.length} (E: 9f7a67428515c1e82813158581898125)`);
|
|
310
|
+
}
|
|
311
|
+
// Shuffle & Pick
|
|
312
|
+
// Note: simple Math.random sort is sufficient for V1 stochastic selection
|
|
313
|
+
// as we are just picking from valid available options.
|
|
314
|
+
const shuffled = [...availableIds].sort(() => 0.5 - Math.random());
|
|
315
|
+
randomIds.push(...shuffled.slice(0, randomCount));
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// 3. Combine
|
|
319
|
+
return [...mandatoryIds, ...randomIds];
|
|
320
|
+
} catch (error) {
|
|
321
|
+
console.error(`${lc} ${extractErrorMsg(error)}`);
|
|
322
|
+
throw error;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Generates an opaque, collision-resistant ID for a challenge.
|
|
328
|
+
* atow (2025/12/22) - Hash(Salt + Timestamp + Index)
|
|
329
|
+
*/
|
|
330
|
+
async function generateOpaqueChallengeId({
|
|
331
|
+
salt,
|
|
332
|
+
timestamp,
|
|
333
|
+
index
|
|
334
|
+
}: {
|
|
335
|
+
salt: string,
|
|
336
|
+
timestamp: string,
|
|
337
|
+
index: number
|
|
338
|
+
}): Promise<string> {
|
|
339
|
+
// Use first 16 chars of hex (64 bits) for brevity + safety.
|
|
340
|
+
const raw = await hash({ s: `${salt}${timestamp}${index}` });
|
|
341
|
+
return raw.substring(0, 16);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Calculates the NEXT state of the Challenge Pools given a specific consumption event.
|
|
346
|
+
* Handles TopUp, ReplaceAll, Consume, and ScorchedEarth strategies.
|
|
347
|
+
*
|
|
348
|
+
* @returns The new array of KeystoneChallengePools (including the modified one).
|
|
349
|
+
*/
|
|
350
|
+
export async function applyReplenishmentStrategy({
|
|
351
|
+
prevPools,
|
|
352
|
+
targetPoolId,
|
|
353
|
+
consumedIds,
|
|
354
|
+
masterSecret,
|
|
355
|
+
strategy,
|
|
356
|
+
config,
|
|
357
|
+
}: {
|
|
358
|
+
prevPools: KeystoneChallengePool[],
|
|
359
|
+
targetPoolId: string,
|
|
360
|
+
consumedIds: string[],
|
|
361
|
+
masterSecret: string,
|
|
362
|
+
/**
|
|
363
|
+
* The instantiated KeystoneStrategy (e.g. HashRevealV1) used to generate new challenges.
|
|
364
|
+
*/
|
|
365
|
+
strategy: any, // Typed as 'any' or 'KeystoneStrategyAny' to avoid circular dep if possible
|
|
366
|
+
config: KeystonePoolConfig,
|
|
367
|
+
}): Promise<KeystoneChallengePool[]> {
|
|
368
|
+
const lc = `[applyReplenishmentStrategy]`;
|
|
369
|
+
try {
|
|
370
|
+
const newPools = JSON.parse(JSON.stringify(prevPools));
|
|
371
|
+
const targetIdx = newPools.findIndex((p: any) => p.id === targetPoolId);
|
|
372
|
+
if (targetIdx === -1) { throw new Error(`Target pool ${targetPoolId} not found in keystone data. (E: 75200388d22744838634524233772545)`); }
|
|
373
|
+
const pool = newPools[targetIdx];
|
|
374
|
+
|
|
375
|
+
const poolSecret = await strategy.derivePoolSecret({ masterSecret });
|
|
376
|
+
const timestamp = Date.now().toString();
|
|
377
|
+
|
|
378
|
+
const strategyType = config.behavior.replenish;
|
|
379
|
+
|
|
380
|
+
// Clean up Binding Map for consumed IDs
|
|
381
|
+
consumedIds.forEach(id => {
|
|
382
|
+
if (pool.bindingMap) { removeFromBindingMap(pool.bindingMap, id); }
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
if (strategyType === KeystoneReplenishStrategy.topUp) {
|
|
386
|
+
// Remove consumed
|
|
387
|
+
consumedIds.forEach(id => delete pool.challenges[id]);
|
|
388
|
+
|
|
389
|
+
// Add New
|
|
390
|
+
for (let i = 0; i < consumedIds.length; i++) {
|
|
391
|
+
const newId = await generateOpaqueChallengeId({
|
|
392
|
+
salt: config.salt, timestamp, index: i
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
const solution = await strategy.generateSolution({
|
|
396
|
+
poolSecret, poolId: pool.id, challengeId: newId
|
|
397
|
+
});
|
|
398
|
+
pool.challenges[newId] = await strategy.generateChallenge({ solution });
|
|
399
|
+
|
|
400
|
+
// Update Binding Map
|
|
401
|
+
if (!pool.bindingMap) { pool.bindingMap = {}; }
|
|
402
|
+
addToBindingMap(pool.bindingMap, newId);
|
|
403
|
+
}
|
|
404
|
+
} else if (strategyType === KeystoneReplenishStrategy.replaceAll) {
|
|
405
|
+
pool.challenges = {};
|
|
406
|
+
pool.bindingMap = {};
|
|
407
|
+
|
|
408
|
+
for (let i = 0; i < config.behavior.size; i++) {
|
|
409
|
+
const newId = await generateOpaqueChallengeId({
|
|
410
|
+
salt: config.salt, timestamp, index: i
|
|
411
|
+
});
|
|
412
|
+
const solution = await strategy.generateSolution({
|
|
413
|
+
poolSecret, poolId: pool.id, challengeId: newId
|
|
414
|
+
});
|
|
415
|
+
pool.challenges[newId] = await strategy.generateChallenge({ solution });
|
|
416
|
+
addToBindingMap(pool.bindingMap, newId);
|
|
417
|
+
}
|
|
418
|
+
} else if (strategyType === KeystoneReplenishStrategy.consume) {
|
|
419
|
+
consumedIds.forEach(id => delete pool.challenges[id]);
|
|
420
|
+
} else if (strategyType === KeystoneReplenishStrategy.scorchedEarth) {
|
|
421
|
+
pool.challenges = {};
|
|
422
|
+
pool.bindingMap = {};
|
|
423
|
+
} else {
|
|
424
|
+
throw new Error(`Unknown replenish strategy: ${strategyType}. Valid list: ${pretty(KEYSTONE_REPLENISH_STRATEGY_VALID_VALUES)} (E: 0acf56f1e1486240080e11e8046d0825)`);
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
return newPools;
|
|
428
|
+
} catch (error) {
|
|
429
|
+
console.error(`${lc} ${extractErrorMsg(error)}`);
|
|
430
|
+
throw error;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* Validates the transition from Prev -> Curr.
|
|
436
|
+
* Enforces Cryptography AND Behavioral Policy.
|
|
437
|
+
*
|
|
438
|
+
* @returns Array of validation error strings. Empty array means Valid.
|
|
439
|
+
*/
|
|
440
|
+
export async function validateKeystoneTransition({
|
|
441
|
+
currentIbGib,
|
|
442
|
+
prevIbGib,
|
|
443
|
+
}: {
|
|
444
|
+
currentIbGib: KeystoneIbGib_V1;
|
|
445
|
+
prevIbGib: KeystoneIbGib_V1;
|
|
446
|
+
}): Promise<string[]> {
|
|
447
|
+
const lc = `[${validateKeystoneTransition.name}]`;
|
|
448
|
+
const errors: string[] = [];
|
|
449
|
+
try {
|
|
450
|
+
if (!currentIbGib) { throw new Error(`(UNEXPECTED) currentIbGib falsy? (E: 3c0f02655fa8279e386a079ebb604b25)`); }
|
|
451
|
+
if (!prevIbGib) { throw new Error(`(UNEXPECTED) prevIbGib falsy? (E: 0d07c812634d839c784f31b8848ba825)`); }
|
|
452
|
+
|
|
453
|
+
// intrinsic validation
|
|
454
|
+
const validationErrors = await validateIbGibIntrinsically({ ibGib: currentIbGib });
|
|
455
|
+
if (validationErrors && validationErrors.length > 0) {
|
|
456
|
+
errors.push(...validationErrors);
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
const currData = currentIbGib.data!;
|
|
460
|
+
const prevData = prevIbGib.data!;
|
|
461
|
+
|
|
462
|
+
for (const proof of currData.proofs) {
|
|
463
|
+
if (proof.solutions.length === 0) {
|
|
464
|
+
errors.push(`Proof ${proof.id || 'unknown'} has no solutions.`);
|
|
465
|
+
continue;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
const poolId = proof.solutions[0].poolId;
|
|
469
|
+
|
|
470
|
+
// Standard Verification (Internal Pools Only)
|
|
471
|
+
// The pool MUST be present in the previous frame.
|
|
472
|
+
const pool = prevData.challengePools.find(p => p.id === poolId);
|
|
473
|
+
if (!pool) {
|
|
474
|
+
errors.push(`Proof references unknown pool: ${poolId}`);
|
|
475
|
+
continue;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
await verifyProofAgainstPool({ proof, pool, errors });
|
|
479
|
+
|
|
480
|
+
} // End proof loop
|
|
481
|
+
|
|
482
|
+
// Revocation Logic checks
|
|
483
|
+
if (currData.revocationInfo) {
|
|
484
|
+
const target = currData.revocationInfo.proof.claim.target;
|
|
485
|
+
const expectedTarget = getIbGibAddr({ ibGib: prevIbGib });
|
|
486
|
+
if (target !== expectedTarget) {
|
|
487
|
+
errors.push(`Revocation target mismatch. Expected ${expectedTarget}, got ${target}`);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
return errors;
|
|
492
|
+
} catch (error) {
|
|
493
|
+
console.error(`${lc} ${extractErrorMsg(error)}`);
|
|
494
|
+
throw error; // System errors still throw
|
|
495
|
+
} finally {
|
|
496
|
+
if (logalot) { console.log(`${lc} complete.`); }
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* Helper to verify a single proof against a specific pool.
|
|
502
|
+
*/
|
|
503
|
+
export async function verifyProofAgainstPool({
|
|
504
|
+
proof,
|
|
505
|
+
pool,
|
|
506
|
+
errors,
|
|
507
|
+
}: {
|
|
508
|
+
proof: KeystoneProof;
|
|
509
|
+
pool: KeystoneChallengePool;
|
|
510
|
+
errors: string[];
|
|
511
|
+
}): Promise<void> {
|
|
512
|
+
const lc = `[${verifyProofAgainstPool.name}]`;
|
|
513
|
+
try {
|
|
514
|
+
if (logalot) { console.log(`${lc} starting... (I: b8f9b6085888eea2258bf579ecd5e825)`); }
|
|
515
|
+
|
|
516
|
+
// 0. VERB AUTH
|
|
517
|
+
if (pool.config.allowedVerbs && pool.config.allowedVerbs.length > 0) {
|
|
518
|
+
if (!proof.claim.verb || !pool.config.allowedVerbs.includes(proof.claim.verb)) {
|
|
519
|
+
errors.push(`Policy Violation: Pool ${pool.id} used for unauthorized verb ${proof.claim.verb}`);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
// 1. Reconstruct Deterministic Requirements
|
|
524
|
+
const { mandatoryIds, availableIds } = getDeterministicRequirements({
|
|
525
|
+
pool,
|
|
526
|
+
requiredChallengeIds: proof.requiredChallengeIds,
|
|
527
|
+
targetAddr: proof.claim.target // Not used extensively in V1 logic yet, mainly for logging/context
|
|
528
|
+
});
|
|
529
|
+
|
|
530
|
+
// 2. Check Mandatory
|
|
531
|
+
const proofIds = new Set(proof.solutions.map(s => s.challengeId));
|
|
532
|
+
for (const id of mandatoryIds) {
|
|
533
|
+
if (!proofIds.has(id)) {
|
|
534
|
+
errors.push(`Policy Violation: Missing mandatory challenge ${id}`);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
// 3. Stochastic
|
|
539
|
+
const randomCandidates = [...proofIds].filter(id => !mandatoryIds.has(id));
|
|
540
|
+
const requiredRandomCount = pool.config.behavior.selectRandomly;
|
|
541
|
+
if (randomCandidates.length < requiredRandomCount) {
|
|
542
|
+
errors.push(`Policy Violation: Insufficient random count. Need ${requiredRandomCount}, got ${randomCandidates.length}`);
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
// 4. Validity (Double Dip / Existence)
|
|
546
|
+
for (const id of randomCandidates) {
|
|
547
|
+
if (!availableIds.includes(id)) {
|
|
548
|
+
errors.push(`Policy Violation: ID ${id} is invalid or double-dipped.`);
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
// 5. Crypto
|
|
553
|
+
const strategy = KeystoneStrategyFactory.create({ config: pool.config });
|
|
554
|
+
for (const solution of proof.solutions) {
|
|
555
|
+
const challenge = pool.challenges[solution.challengeId];
|
|
556
|
+
if (!challenge) {
|
|
557
|
+
errors.push(`Crypto Violation: Challenge ${solution.challengeId} not found in pool.`);
|
|
558
|
+
} else {
|
|
559
|
+
const isValid = await strategy.validateSolution({ solution, challenge });
|
|
560
|
+
if (!isValid) {
|
|
561
|
+
errors.push(`Crypto Violation: Solution for ${solution.challengeId} is invalid.`);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
} catch (error) {
|
|
566
|
+
console.error(`${lc} ${extractErrorMsg(error)}`);
|
|
567
|
+
throw error;
|
|
568
|
+
} finally {
|
|
569
|
+
if (logalot) { console.log(`${lc} complete.`); }
|
|
570
|
+
}
|
|
571
|
+
}
|