@insumermodel/mppx-condition-gate 2.0.3 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +33 -5
- package/dist/index.d.ts +62 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +113 -76
- package/dist/index.js.map +1 -1
- package/package.json +3 -2
- package/src/index.ts +151 -68
package/README.md
CHANGED
|
@@ -6,16 +6,23 @@ Condition-based access for [mppx](https://github.com/wevm/mppx) routes. One sign
|
|
|
6
6
|
|
|
7
7
|
## How it works
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
Free access requires a payer your payment method has **proven** controls the request. You supply that payer through `provenPayer`; the gate never infers one.
|
|
10
10
|
|
|
11
11
|
1. Request arrives with a payment credential
|
|
12
|
-
2.
|
|
13
|
-
3.
|
|
14
|
-
4.
|
|
15
|
-
5. **
|
|
12
|
+
2. The gate calls the method's non-mutating `validate` hook
|
|
13
|
+
3. Your `provenPayer` resolver receives mppx's method-specific validation `details` and returns the proven payer, or `null`
|
|
14
|
+
4. [InsumerAPI](https://insumermodel.com/developers/api-reference/) evaluates the conditions for that payer and returns an ECDSA P-256 signed attestation
|
|
15
|
+
5. **Pass** → free receipt returned (`reference: "condition-gate:free:{attestationId}"`)
|
|
16
|
+
6. **Fail, or no proven payer** → the normal paid path runs
|
|
16
17
|
|
|
17
18
|
The signed attestation is verifiable offline via [JWKS](https://insumermodel.com/.well-known/jwks.json). The adapter does not re-sign or wrap the result; the signature on the attestation is the one InsumerAPI produced.
|
|
18
19
|
|
|
20
|
+
### Why you must supply the payer
|
|
21
|
+
|
|
22
|
+
mppx credentials carry an optional `source` DID, but mppx documents it as *"an asserted identity, not independent proof of control"* — it is a claim by the caller, not a proof. Versions before 3.0.0 granted free access on it, which let anyone name a qualifying wallet and skip payment ([GHSA-jg6q-3qfh-r9f8](https://github.com/douglasborthwick-crypto/mppx-condition-gate/security/advisories/GHSA-jg6q-3qfh-r9f8)). The gate now refuses to guess.
|
|
23
|
+
|
|
24
|
+
`provenPayer` receives **only** the validation `details` — never the credential and never `source` — so the unproven identity is not reachable from it. Return a payer only if the payment method has actually established control of it. **Every path that cannot justify free access falls through to payment:** no resolver, no `validate` hook on the method, a resolver returning `null` or throwing, a credential that fails validation, conditions not met, or an attestation error.
|
|
25
|
+
|
|
19
26
|
## Install
|
|
20
27
|
|
|
21
28
|
```bash
|
|
@@ -35,6 +42,9 @@ const tempoCharge = tempo({
|
|
|
35
42
|
|
|
36
43
|
const gatedCharge = conditionGate(tempoCharge, {
|
|
37
44
|
apiKey: process.env.INSUMER_API_KEY,
|
|
45
|
+
// Return only a payer this method has PROVEN for this request, else null.
|
|
46
|
+
// The argument is mppx's method-specific validation `details`.
|
|
47
|
+
provenPayer: (details) => (details as { payer?: string }).payer ?? null,
|
|
38
48
|
conditions: [{
|
|
39
49
|
type: 'nft_ownership',
|
|
40
50
|
contractAddress: '0xYourNFT',
|
|
@@ -222,6 +232,24 @@ The condition shapes for `token_balance` and `nft_ownership` are unchanged. `eas
|
|
|
222
232
|
|
|
223
233
|
The `parsXrplDid` typo from v1 is fixed: use `parseXrplDid`. The cache helper renamed: `clearTokenGateCache` is now `clearConditionGateCache`.
|
|
224
234
|
|
|
235
|
+
## Migrating from 2.x (security release)
|
|
236
|
+
|
|
237
|
+
3.0.0 fixes an authentication bypass ([GHSA-jg6q-3qfh-r9f8](https://github.com/douglasborthwick-crypto/mppx-condition-gate/security/advisories/GHSA-jg6q-3qfh-r9f8)). All 2.x versions, and all versions of the predecessor `@insumermodel/mppx-token-gate`, are affected.
|
|
238
|
+
|
|
239
|
+
Add a `provenPayer` resolver:
|
|
240
|
+
|
|
241
|
+
```diff
|
|
242
|
+
const gated = conditionGate(server, {
|
|
243
|
+
apiKey: process.env.INSUMER_API_KEY,
|
|
244
|
+
+ provenPayer: (details) => (details as { payer?: string }).payer ?? null,
|
|
245
|
+
conditions: [ ... ],
|
|
246
|
+
})
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
Without it the gate compiles and runs, but never grants free access — every request takes the paid path. That is deliberate: the insecure default is gone, and the safe default is to charge.
|
|
250
|
+
|
|
251
|
+
Your method must expose mppx's `validate` hook. Legacy `verify`-only methods have no non-mutating pre-check, so they are never gated.
|
|
252
|
+
|
|
225
253
|
## License
|
|
226
254
|
|
|
227
255
|
MIT
|
package/dist/index.d.ts
CHANGED
|
@@ -77,9 +77,42 @@ export type RatioToSupplyCondition = {
|
|
|
77
77
|
};
|
|
78
78
|
/** Any condition accepted by /v1/attest */
|
|
79
79
|
export type Condition = TokenBalanceCondition | NftOwnershipCondition | EasAttestationCondition | FarcasterIdCondition | RatioToAmountCondition | RatioToSupplyCondition;
|
|
80
|
+
/** Wallet family for a proven payer. */
|
|
81
|
+
export type WalletType = 'evm' | 'solana' | 'xrpl' | 'bitcoin';
|
|
82
|
+
/**
|
|
83
|
+
* A payer the caller has cryptographically proven for this request.
|
|
84
|
+
*
|
|
85
|
+
* Accepts a `did:pkh:...` string, a bare `0x` EVM address, or an explicit
|
|
86
|
+
* `{ address, type }` pair. Bare non-EVM addresses are rejected because their
|
|
87
|
+
* wallet family cannot be inferred unambiguously — pass the object form.
|
|
88
|
+
*/
|
|
89
|
+
export type ProvenPayer = string | {
|
|
90
|
+
address: string;
|
|
91
|
+
type: WalletType;
|
|
92
|
+
} | null | undefined;
|
|
80
93
|
export type ConditionGateOptions = {
|
|
81
94
|
/** InsumerAPI key. Falls back to INSUMER_API_KEY env var. */
|
|
82
95
|
apiKey?: string;
|
|
96
|
+
/**
|
|
97
|
+
* Resolves the payer that the payment method has PROVEN controls this
|
|
98
|
+
* request. Required for free access — without it every request takes the
|
|
99
|
+
* paid path.
|
|
100
|
+
*
|
|
101
|
+
* It receives only mppx's method-specific validation `details` (for example
|
|
102
|
+
* a recovered payer address). It is deliberately NOT given the credential or
|
|
103
|
+
* the declared `source`: mppx documents `source` as "an asserted identity,
|
|
104
|
+
* not independent proof of control", and granting on it is the vulnerability
|
|
105
|
+
* fixed in 3.0.0 (GHSA-jg6q-3qfh-r9f8).
|
|
106
|
+
*
|
|
107
|
+
* Return `null` when no payer has been proven. Returning an address you have
|
|
108
|
+
* not proven reintroduces that vulnerability.
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```ts
|
|
112
|
+
* provenPayer: (details) => (details as { payer?: string }).payer ?? null
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
provenPayer?: (details: unknown) => ProvenPayer | Promise<ProvenPayer>;
|
|
83
116
|
/** One or more conditions to evaluate. Mix any of the six types. */
|
|
84
117
|
conditions: Condition[];
|
|
85
118
|
/** Whether the wallet must satisfy "any" (default) or "all" conditions. */
|
|
@@ -142,20 +175,37 @@ export declare function parseSolanaDid(source: string): string | null;
|
|
|
142
175
|
export declare function parseXrplDid(source: string): string | null;
|
|
143
176
|
/** Extracts a Bitcoin address from a `did:pkh:bip122:{chainId}:{address}` string. */
|
|
144
177
|
export declare function parseBitcoinDid(source: string): string | null;
|
|
178
|
+
/**
|
|
179
|
+
* Normalizes a caller-supplied proven payer into an address + wallet family.
|
|
180
|
+
*
|
|
181
|
+
* Fail-closed: anything it cannot resolve unambiguously returns null, which
|
|
182
|
+
* sends the request to the paid path.
|
|
183
|
+
*/
|
|
184
|
+
export declare function normalizeProvenPayer(payer: ProvenPayer): {
|
|
185
|
+
address: string;
|
|
186
|
+
type: WalletType;
|
|
187
|
+
} | null;
|
|
145
188
|
/**
|
|
146
189
|
* Wraps an mppx Method.Server with condition-based access using signed attestations.
|
|
147
190
|
*
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
*
|
|
151
|
-
*
|
|
191
|
+
* Free access requires a PROVEN payer. The gate calls the method's non-mutating
|
|
192
|
+
* `validate` hook, hands the resulting method-specific `details` to your
|
|
193
|
+
* `provenPayer` resolver, and only then evaluates conditions. It never reads
|
|
194
|
+
* `credential.source` — mppx documents that field as an asserted identity
|
|
195
|
+
* rather than proof of control.
|
|
196
|
+
*
|
|
197
|
+
* The gate falls through to the paid path whenever free access cannot be
|
|
198
|
+
* justified: no `provenPayer` resolver, no `validate` hook on the method
|
|
199
|
+
* (legacy `verify`-only methods included), a resolver returning null, a
|
|
200
|
+
* credential that fails validation, conditions not met, or an attestation
|
|
201
|
+
* error. There is no path from an unproven wallet to a free receipt.
|
|
152
202
|
*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
*
|
|
203
|
+
* Conditions are evaluated across the 35 chains this adapter reaches (32 EVM +
|
|
204
|
+
* Solana + XRPL + Bitcoin; the engine itself covers 38). Six condition types
|
|
205
|
+
* are typed here: token_balance, nft_ownership, eas_attestation, farcaster_id,
|
|
206
|
+
* ratio_to_amount, and ratio_to_supply. InsumerAPI also offers evm_view_call,
|
|
207
|
+
* erc8004_agent, and erc7710_delegation; those are not typed or normalized
|
|
208
|
+
* here, so call /v1/attest directly if you need them.
|
|
159
209
|
*
|
|
160
210
|
* The attestation is ECDSA P-256 signed and verifiable offline via the public
|
|
161
211
|
* JWKS at https://insumermodel.com/.well-known/jwks.json.
|
|
@@ -165,6 +215,8 @@ export declare function parseBitcoinDid(source: string): string | null;
|
|
|
165
215
|
* import { conditionGate } from '@insumermodel/mppx-condition-gate'
|
|
166
216
|
*
|
|
167
217
|
* const gated = conditionGate(tempoCharge, {
|
|
218
|
+
* // Return only a payer this method has proven for THIS request.
|
|
219
|
+
* provenPayer: (details) => (details as { payer?: string }).payer ?? null,
|
|
168
220
|
* conditions: [
|
|
169
221
|
* { type: 'eas_attestation', template: 'coinbase_verified_account', chainId: 8453 },
|
|
170
222
|
* { type: 'farcaster_id' },
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAW,MAAM,MAAM,CAAA;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b,CAAA;IACD,IAAI,EAAE;QACJ,OAAO,EAAE,MAAM,CAAA;QACf,SAAS,EAAE,MAAM,CAAA;QACjB,gBAAgB,EAAE,MAAM,CAAA;QACxB,cAAc,EAAE,MAAM,CAAA;KACvB,CAAA;CACF,CAAA;AA+BD,4DAA4D;AAC5D,wBAAgB,uBAAuB,IAAI,IAAI,CAE9C;AAMD;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,KAAK,MAAM,EAAE,GAAG,IAAI,CAO7D;AAED,oFAAoF;AACpF,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAK5D;AAED,iFAAiF;AACjF,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAO1D;AAED,qFAAqF;AACrF,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAK7D;AAiHD;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,WAAW,GACjB;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,UAAU,CAAA;CAAE,GAAG,IAAI,CAuB9C;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,wBAAgB,aAAa,CAC3B,MAAM,EAAE,MAAM,CAAC,SAAS,EACxB,OAAO,EAAE,oBAAoB,GAC5B,MAAM,CAAC,SAAS,CA8FlB"}
|
package/dist/index.js
CHANGED
|
@@ -177,20 +177,61 @@ async function callAttest(wallet, walletType, conditions, options) {
|
|
|
177
177
|
// ---------------------------------------------------------------------------
|
|
178
178
|
// conditionGate adapter
|
|
179
179
|
// ---------------------------------------------------------------------------
|
|
180
|
+
/**
|
|
181
|
+
* Normalizes a caller-supplied proven payer into an address + wallet family.
|
|
182
|
+
*
|
|
183
|
+
* Fail-closed: anything it cannot resolve unambiguously returns null, which
|
|
184
|
+
* sends the request to the paid path.
|
|
185
|
+
*/
|
|
186
|
+
export function normalizeProvenPayer(payer) {
|
|
187
|
+
if (!payer)
|
|
188
|
+
return null;
|
|
189
|
+
if (typeof payer === 'object') {
|
|
190
|
+
const { address, type } = payer;
|
|
191
|
+
if (!address || !type)
|
|
192
|
+
return null;
|
|
193
|
+
return { address, type };
|
|
194
|
+
}
|
|
195
|
+
if (typeof payer !== 'string')
|
|
196
|
+
return null;
|
|
197
|
+
const evm = parseDid(payer);
|
|
198
|
+
if (evm)
|
|
199
|
+
return { address: evm, type: 'evm' };
|
|
200
|
+
const sol = parseSolanaDid(payer);
|
|
201
|
+
if (sol)
|
|
202
|
+
return { address: sol, type: 'solana' };
|
|
203
|
+
const xrpl = parseXrplDid(payer);
|
|
204
|
+
if (xrpl)
|
|
205
|
+
return { address: xrpl, type: 'xrpl' };
|
|
206
|
+
const btc = parseBitcoinDid(payer);
|
|
207
|
+
if (btc)
|
|
208
|
+
return { address: btc, type: 'bitcoin' };
|
|
209
|
+
// Bare EVM address is unambiguous; anything else needs the object form.
|
|
210
|
+
if (payer.startsWith('0x'))
|
|
211
|
+
return { address: payer, type: 'evm' };
|
|
212
|
+
return null;
|
|
213
|
+
}
|
|
180
214
|
/**
|
|
181
215
|
* Wraps an mppx Method.Server with condition-based access using signed attestations.
|
|
182
216
|
*
|
|
183
|
-
*
|
|
184
|
-
*
|
|
185
|
-
*
|
|
186
|
-
*
|
|
217
|
+
* Free access requires a PROVEN payer. The gate calls the method's non-mutating
|
|
218
|
+
* `validate` hook, hands the resulting method-specific `details` to your
|
|
219
|
+
* `provenPayer` resolver, and only then evaluates conditions. It never reads
|
|
220
|
+
* `credential.source` — mppx documents that field as an asserted identity
|
|
221
|
+
* rather than proof of control.
|
|
222
|
+
*
|
|
223
|
+
* The gate falls through to the paid path whenever free access cannot be
|
|
224
|
+
* justified: no `provenPayer` resolver, no `validate` hook on the method
|
|
225
|
+
* (legacy `verify`-only methods included), a resolver returning null, a
|
|
226
|
+
* credential that fails validation, conditions not met, or an attestation
|
|
227
|
+
* error. There is no path from an unproven wallet to a free receipt.
|
|
187
228
|
*
|
|
188
|
-
*
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
*
|
|
192
|
-
*
|
|
193
|
-
*
|
|
229
|
+
* Conditions are evaluated across the 35 chains this adapter reaches (32 EVM +
|
|
230
|
+
* Solana + XRPL + Bitcoin; the engine itself covers 38). Six condition types
|
|
231
|
+
* are typed here: token_balance, nft_ownership, eas_attestation, farcaster_id,
|
|
232
|
+
* ratio_to_amount, and ratio_to_supply. InsumerAPI also offers evm_view_call,
|
|
233
|
+
* erc8004_agent, and erc7710_delegation; those are not typed or normalized
|
|
234
|
+
* here, so call /v1/attest directly if you need them.
|
|
194
235
|
*
|
|
195
236
|
* The attestation is ECDSA P-256 signed and verifiable offline via the public
|
|
196
237
|
* JWKS at https://insumermodel.com/.well-known/jwks.json.
|
|
@@ -200,6 +241,8 @@ async function callAttest(wallet, walletType, conditions, options) {
|
|
|
200
241
|
* import { conditionGate } from '@insumermodel/mppx-condition-gate'
|
|
201
242
|
*
|
|
202
243
|
* const gated = conditionGate(tempoCharge, {
|
|
244
|
+
* // Return only a payer this method has proven for THIS request.
|
|
245
|
+
* provenPayer: (details) => (details as { payer?: string }).payer ?? null,
|
|
203
246
|
* conditions: [
|
|
204
247
|
* { type: 'eas_attestation', template: 'coinbase_verified_account', chainId: 8453 },
|
|
205
248
|
* { type: 'farcaster_id' },
|
|
@@ -211,87 +254,81 @@ async function callAttest(wallet, walletType, conditions, options) {
|
|
|
211
254
|
* ```
|
|
212
255
|
*/
|
|
213
256
|
export function conditionGate(server, options) {
|
|
214
|
-
const { conditions, matchMode = 'any', cacheTtlSeconds = 300 } = options;
|
|
215
|
-
const
|
|
216
|
-
const
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
if (!
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
257
|
+
const { conditions, matchMode = 'any', cacheTtlSeconds = 300, provenPayer } = options;
|
|
258
|
+
const anyServer = server;
|
|
259
|
+
const originalVerify = anyServer.verify;
|
|
260
|
+
const originalBroadcast = anyServer.broadcast;
|
|
261
|
+
const originalValidate = anyServer.validate;
|
|
262
|
+
// Free access is only reachable when BOTH exist: a resolver to supply the
|
|
263
|
+
// proven payer, and a non-mutating validate hook to derive it from. A method
|
|
264
|
+
// without `validate` (legacy verify-only, or broadcast-without-validate) has
|
|
265
|
+
// no safe pre-check, so it is never gated.
|
|
266
|
+
const canGate = typeof provenPayer === 'function' && typeof originalValidate === 'function';
|
|
267
|
+
/** Returns a free receipt, or null to take the paid path. */
|
|
268
|
+
async function tryFree(params) {
|
|
269
|
+
if (!canGate)
|
|
270
|
+
return null;
|
|
271
|
+
let details;
|
|
272
|
+
try {
|
|
273
|
+
const validation = await originalValidate(params);
|
|
274
|
+
details = validation?.details;
|
|
230
275
|
}
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
walletType = 'xrpl';
|
|
276
|
+
catch {
|
|
277
|
+
// Credential is not currently acceptable → let the payment method decide.
|
|
278
|
+
return null;
|
|
235
279
|
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
280
|
+
let resolved;
|
|
281
|
+
try {
|
|
282
|
+
resolved = await provenPayer(details);
|
|
283
|
+
}
|
|
284
|
+
catch {
|
|
285
|
+
return null;
|
|
240
286
|
}
|
|
241
|
-
|
|
242
|
-
if (!
|
|
243
|
-
return
|
|
244
|
-
|
|
245
|
-
const key = cacheKey(wallet, conditions);
|
|
287
|
+
const payer = normalizeProvenPayer(resolved);
|
|
288
|
+
if (!payer)
|
|
289
|
+
return null;
|
|
290
|
+
const key = cacheKey(payer.address, conditions);
|
|
246
291
|
const cached = cache.get(key);
|
|
247
292
|
if (cached && cached.expiresAt > Date.now()) {
|
|
248
|
-
if (cached.pass)
|
|
249
|
-
return
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
}
|
|
256
|
-
// Cached non-holder → fall through
|
|
257
|
-
return originalVerify(params);
|
|
293
|
+
if (!cached.pass)
|
|
294
|
+
return null;
|
|
295
|
+
return {
|
|
296
|
+
method: server.name,
|
|
297
|
+
reference: `condition-gate:free:${cached.attestationId}`,
|
|
298
|
+
status: 'success',
|
|
299
|
+
timestamp: new Date().toISOString(),
|
|
300
|
+
};
|
|
258
301
|
}
|
|
259
|
-
// Call InsumerAPI
|
|
260
302
|
try {
|
|
261
|
-
const result = await callAttest(
|
|
303
|
+
const result = await callAttest(payer.address, payer.type, conditions, options);
|
|
262
304
|
const attestation = result.data.attestation;
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
pass = attestation.pass; // all conditions must be met
|
|
267
|
-
}
|
|
268
|
-
else {
|
|
269
|
-
// "any" — at least one condition met
|
|
270
|
-
pass = attestation.results.some((r) => r.met);
|
|
271
|
-
}
|
|
272
|
-
// Cache the result
|
|
305
|
+
const pass = matchMode === 'all'
|
|
306
|
+
? attestation.pass
|
|
307
|
+
: attestation.results.some((r) => r.met);
|
|
273
308
|
cache.set(key, {
|
|
274
309
|
pass,
|
|
275
310
|
attestationId: attestation.id,
|
|
276
311
|
expiresAt: Date.now() + cacheTtlSeconds * 1000,
|
|
277
312
|
});
|
|
278
|
-
if (pass)
|
|
279
|
-
return
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
}
|
|
313
|
+
if (!pass)
|
|
314
|
+
return null;
|
|
315
|
+
return {
|
|
316
|
+
method: server.name,
|
|
317
|
+
reference: `condition-gate:free:${attestation.id}`,
|
|
318
|
+
status: 'success',
|
|
319
|
+
timestamp: new Date().toISOString(),
|
|
320
|
+
};
|
|
286
321
|
}
|
|
287
322
|
catch {
|
|
288
|
-
// Attestation
|
|
323
|
+
// Attestation error → paid path.
|
|
324
|
+
return null;
|
|
289
325
|
}
|
|
290
|
-
|
|
291
|
-
};
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
326
|
+
}
|
|
327
|
+
const wrapped = { ...server };
|
|
328
|
+
if (typeof originalBroadcast === 'function') {
|
|
329
|
+
wrapped.broadcast = async (params) => (await tryFree(params)) ?? originalBroadcast(params);
|
|
330
|
+
}
|
|
331
|
+
wrapped.verify = async (params) => (await tryFree(params)) ?? originalVerify(params);
|
|
332
|
+
return wrapped;
|
|
296
333
|
}
|
|
297
334
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AA2MA,MAAM,KAAK,GAAG,IAAI,GAAG,EAAsB,CAAA;AAE3C,SAAS,gBAAgB,CAAC,CAAY;IACpC,IAAI,CAAC,CAAC,IAAI,KAAK,cAAc;QAAE,OAAO,cAAc,CAAA;IACpD,IAAI,CAAC,CAAC,IAAI,KAAK,iBAAiB,EAAE,CAAC;QACjC,OAAO,OAAO,CAAC,CAAC,OAAO,IAAI,CAAC,CAAC,CAAC,QAAQ,IAAI,CAAC,CAAC,QAAQ,IAAI,EAAE,CAAC,CAAC,WAAW,EAAE,EAAE,CAAA;IAC7E,CAAC;IACD,OAAO,GAAG,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,OAAO,IAAI,CAAC,CAAC,eAAe,CAAC,WAAW,EAAE,EAAE,CAAA;AACpE,CAAC;AAED,SAAS,QAAQ,CAAC,OAAe,EAAE,UAAuB;IACxD,MAAM,MAAM,GAAG,CAAC,GAAG,UAAU,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE;QAC3C,MAAM,EAAE,GAAG,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AE,EAAE;gBAClD,MAAM,EAAE,SAAkB;gBAC1B,SAAS,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;aACpC,CAAA;QACH,CAAC;QAAC,MAAM,CAAC;YACP,iCAAiC;YACjC,OAAO,IAAI,CAAA;QACb,CAAC;IACH,CAAC;IAED,MAAM,OAAO,GAAG,EAAE,GAAG,MAAM,EAAwC,CAAA;IAEnE,IAAI,OAAO,iBAAiB,KAAK,UAAU,EAAE,CAAC;QAC5C,OAAO,CAAC,SAAS,GAAG,KAAK,EAAE,MAAW,EAAE,EAAE,CACxC,CAAC,MAAM,OAAO,CAAC,MAAM,CAAC,CAAC,IAAI,iBAAiB,CAAC,MAAM,CAAC,CAAA;IACxD,CAAC;IAED,OAAO,CAAC,MAAM,GAAG,KAAK,EAAE,MAAW,EAAE,EAAE,CACrC,CAAC,MAAM,OAAO,CAAC,MAAM,CAAC,CAAC,IAAI,cAAc,CAAC,MAAM,CAAC,CAAA;IAEnD,OAAO,OAAsC,CAAA;AAC/C,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@insumermodel/mppx-condition-gate",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Condition-based access for mppx routes. Wallet auth via signed attestations. Six condition types: token balance, NFT ownership, EAS attestation, Farcaster ID, ratio_to_amount, ratio_to_supply.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
],
|
|
18
18
|
"scripts": {
|
|
19
19
|
"build": "tsc",
|
|
20
|
-
"prepublishOnly": "npm run build"
|
|
20
|
+
"prepublishOnly": "npm run build",
|
|
21
|
+
"test": "tsc && node --test test/*.test.mjs"
|
|
21
22
|
},
|
|
22
23
|
"keywords": [
|
|
23
24
|
"condition-based-access",
|
package/src/index.ts
CHANGED
|
@@ -101,9 +101,45 @@ export type Condition =
|
|
|
101
101
|
| RatioToAmountCondition
|
|
102
102
|
| RatioToSupplyCondition
|
|
103
103
|
|
|
104
|
+
/** Wallet family for a proven payer. */
|
|
105
|
+
export type WalletType = 'evm' | 'solana' | 'xrpl' | 'bitcoin'
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* A payer the caller has cryptographically proven for this request.
|
|
109
|
+
*
|
|
110
|
+
* Accepts a `did:pkh:...` string, a bare `0x` EVM address, or an explicit
|
|
111
|
+
* `{ address, type }` pair. Bare non-EVM addresses are rejected because their
|
|
112
|
+
* wallet family cannot be inferred unambiguously — pass the object form.
|
|
113
|
+
*/
|
|
114
|
+
export type ProvenPayer =
|
|
115
|
+
| string
|
|
116
|
+
| { address: string; type: WalletType }
|
|
117
|
+
| null
|
|
118
|
+
| undefined
|
|
119
|
+
|
|
104
120
|
export type ConditionGateOptions = {
|
|
105
121
|
/** InsumerAPI key. Falls back to INSUMER_API_KEY env var. */
|
|
106
122
|
apiKey?: string
|
|
123
|
+
/**
|
|
124
|
+
* Resolves the payer that the payment method has PROVEN controls this
|
|
125
|
+
* request. Required for free access — without it every request takes the
|
|
126
|
+
* paid path.
|
|
127
|
+
*
|
|
128
|
+
* It receives only mppx's method-specific validation `details` (for example
|
|
129
|
+
* a recovered payer address). It is deliberately NOT given the credential or
|
|
130
|
+
* the declared `source`: mppx documents `source` as "an asserted identity,
|
|
131
|
+
* not independent proof of control", and granting on it is the vulnerability
|
|
132
|
+
* fixed in 3.0.0 (GHSA-jg6q-3qfh-r9f8).
|
|
133
|
+
*
|
|
134
|
+
* Return `null` when no payer has been proven. Returning an address you have
|
|
135
|
+
* not proven reintroduces that vulnerability.
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* ```ts
|
|
139
|
+
* provenPayer: (details) => (details as { payer?: string }).payer ?? null
|
|
140
|
+
* ```
|
|
141
|
+
*/
|
|
142
|
+
provenPayer?: (details: unknown) => ProvenPayer | Promise<ProvenPayer>
|
|
107
143
|
/** One or more conditions to evaluate. Mix any of the six types. */
|
|
108
144
|
conditions: Condition[]
|
|
109
145
|
/** Whether the wallet must satisfy "any" (default) or "all" conditions. */
|
|
@@ -343,20 +379,60 @@ async function callAttest(
|
|
|
343
379
|
// conditionGate adapter
|
|
344
380
|
// ---------------------------------------------------------------------------
|
|
345
381
|
|
|
382
|
+
/**
|
|
383
|
+
* Normalizes a caller-supplied proven payer into an address + wallet family.
|
|
384
|
+
*
|
|
385
|
+
* Fail-closed: anything it cannot resolve unambiguously returns null, which
|
|
386
|
+
* sends the request to the paid path.
|
|
387
|
+
*/
|
|
388
|
+
export function normalizeProvenPayer(
|
|
389
|
+
payer: ProvenPayer,
|
|
390
|
+
): { address: string; type: WalletType } | null {
|
|
391
|
+
if (!payer) return null
|
|
392
|
+
|
|
393
|
+
if (typeof payer === 'object') {
|
|
394
|
+
const { address, type } = payer
|
|
395
|
+
if (!address || !type) return null
|
|
396
|
+
return { address, type }
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
if (typeof payer !== 'string') return null
|
|
400
|
+
|
|
401
|
+
const evm = parseDid(payer)
|
|
402
|
+
if (evm) return { address: evm, type: 'evm' }
|
|
403
|
+
const sol = parseSolanaDid(payer)
|
|
404
|
+
if (sol) return { address: sol, type: 'solana' }
|
|
405
|
+
const xrpl = parseXrplDid(payer)
|
|
406
|
+
if (xrpl) return { address: xrpl, type: 'xrpl' }
|
|
407
|
+
const btc = parseBitcoinDid(payer)
|
|
408
|
+
if (btc) return { address: btc, type: 'bitcoin' }
|
|
409
|
+
|
|
410
|
+
// Bare EVM address is unambiguous; anything else needs the object form.
|
|
411
|
+
if (payer.startsWith('0x')) return { address: payer, type: 'evm' }
|
|
412
|
+
return null
|
|
413
|
+
}
|
|
414
|
+
|
|
346
415
|
/**
|
|
347
416
|
* Wraps an mppx Method.Server with condition-based access using signed attestations.
|
|
348
417
|
*
|
|
349
|
-
*
|
|
350
|
-
*
|
|
351
|
-
*
|
|
352
|
-
*
|
|
418
|
+
* Free access requires a PROVEN payer. The gate calls the method's non-mutating
|
|
419
|
+
* `validate` hook, hands the resulting method-specific `details` to your
|
|
420
|
+
* `provenPayer` resolver, and only then evaluates conditions. It never reads
|
|
421
|
+
* `credential.source` — mppx documents that field as an asserted identity
|
|
422
|
+
* rather than proof of control.
|
|
423
|
+
*
|
|
424
|
+
* The gate falls through to the paid path whenever free access cannot be
|
|
425
|
+
* justified: no `provenPayer` resolver, no `validate` hook on the method
|
|
426
|
+
* (legacy `verify`-only methods included), a resolver returning null, a
|
|
427
|
+
* credential that fails validation, conditions not met, or an attestation
|
|
428
|
+
* error. There is no path from an unproven wallet to a free receipt.
|
|
353
429
|
*
|
|
354
|
-
*
|
|
355
|
-
*
|
|
356
|
-
*
|
|
357
|
-
*
|
|
358
|
-
*
|
|
359
|
-
*
|
|
430
|
+
* Conditions are evaluated across the 35 chains this adapter reaches (32 EVM +
|
|
431
|
+
* Solana + XRPL + Bitcoin; the engine itself covers 38). Six condition types
|
|
432
|
+
* are typed here: token_balance, nft_ownership, eas_attestation, farcaster_id,
|
|
433
|
+
* ratio_to_amount, and ratio_to_supply. InsumerAPI also offers evm_view_call,
|
|
434
|
+
* erc8004_agent, and erc7710_delegation; those are not typed or normalized
|
|
435
|
+
* here, so call /v1/attest directly if you need them.
|
|
360
436
|
*
|
|
361
437
|
* The attestation is ECDSA P-256 signed and verifiable offline via the public
|
|
362
438
|
* JWKS at https://insumermodel.com/.well-known/jwks.json.
|
|
@@ -366,6 +442,8 @@ async function callAttest(
|
|
|
366
442
|
* import { conditionGate } from '@insumermodel/mppx-condition-gate'
|
|
367
443
|
*
|
|
368
444
|
* const gated = conditionGate(tempoCharge, {
|
|
445
|
+
* // Return only a payer this method has proven for THIS request.
|
|
446
|
+
* provenPayer: (details) => (details as { payer?: string }).payer ?? null,
|
|
369
447
|
* conditions: [
|
|
370
448
|
* { type: 'eas_attestation', template: 'coinbase_verified_account', chainId: 8453 },
|
|
371
449
|
* { type: 'farcaster_id' },
|
|
@@ -380,92 +458,97 @@ export function conditionGate(
|
|
|
380
458
|
server: Method.AnyServer,
|
|
381
459
|
options: ConditionGateOptions,
|
|
382
460
|
): Method.AnyServer {
|
|
383
|
-
const { conditions, matchMode = 'any', cacheTtlSeconds = 300 } = options
|
|
461
|
+
const { conditions, matchMode = 'any', cacheTtlSeconds = 300, provenPayer } = options
|
|
384
462
|
|
|
385
|
-
const
|
|
463
|
+
const anyServer = server as unknown as {
|
|
464
|
+
verify: (params: any) => Promise<any>
|
|
465
|
+
broadcast?: (params: any) => Promise<any>
|
|
466
|
+
validate?: (params: any) => Promise<any>
|
|
467
|
+
}
|
|
386
468
|
|
|
387
|
-
const
|
|
388
|
-
|
|
389
|
-
|
|
469
|
+
const originalVerify = anyServer.verify
|
|
470
|
+
const originalBroadcast = anyServer.broadcast
|
|
471
|
+
const originalValidate = anyServer.validate
|
|
390
472
|
|
|
391
|
-
|
|
392
|
-
|
|
473
|
+
// Free access is only reachable when BOTH exist: a resolver to supply the
|
|
474
|
+
// proven payer, and a non-mutating validate hook to derive it from. A method
|
|
475
|
+
// without `validate` (legacy verify-only, or broadcast-without-validate) has
|
|
476
|
+
// no safe pre-check, so it is never gated.
|
|
477
|
+
const canGate =
|
|
478
|
+
typeof provenPayer === 'function' && typeof originalValidate === 'function'
|
|
393
479
|
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
480
|
+
/** Returns a free receipt, or null to take the paid path. */
|
|
481
|
+
async function tryFree(params: any): Promise<any | null> {
|
|
482
|
+
if (!canGate) return null
|
|
397
483
|
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
if (wallet) walletType = 'xrpl'
|
|
484
|
+
let details: unknown
|
|
485
|
+
try {
|
|
486
|
+
const validation = await originalValidate!(params)
|
|
487
|
+
details = (validation as { details?: unknown } | undefined)?.details
|
|
488
|
+
} catch {
|
|
489
|
+
// Credential is not currently acceptable → let the payment method decide.
|
|
490
|
+
return null
|
|
406
491
|
}
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
492
|
+
|
|
493
|
+
let resolved: ProvenPayer
|
|
494
|
+
try {
|
|
495
|
+
resolved = await provenPayer!(details)
|
|
496
|
+
} catch {
|
|
497
|
+
return null
|
|
410
498
|
}
|
|
411
499
|
|
|
412
|
-
|
|
413
|
-
if (!
|
|
500
|
+
const payer = normalizeProvenPayer(resolved)
|
|
501
|
+
if (!payer) return null
|
|
414
502
|
|
|
415
|
-
|
|
416
|
-
const key = cacheKey(wallet, conditions)
|
|
503
|
+
const key = cacheKey(payer.address, conditions)
|
|
417
504
|
const cached = cache.get(key)
|
|
418
505
|
if (cached && cached.expiresAt > Date.now()) {
|
|
419
|
-
if (cached.pass)
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
}
|
|
506
|
+
if (!cached.pass) return null
|
|
507
|
+
return {
|
|
508
|
+
method: server.name,
|
|
509
|
+
reference: `condition-gate:free:${cached.attestationId}`,
|
|
510
|
+
status: 'success' as const,
|
|
511
|
+
timestamp: new Date().toISOString(),
|
|
426
512
|
}
|
|
427
|
-
// Cached non-holder → fall through
|
|
428
|
-
return originalVerify(params)
|
|
429
513
|
}
|
|
430
514
|
|
|
431
|
-
// Call InsumerAPI
|
|
432
515
|
try {
|
|
433
|
-
const result = await callAttest(
|
|
516
|
+
const result = await callAttest(payer.address, payer.type, conditions, options)
|
|
434
517
|
const attestation = result.data.attestation
|
|
435
518
|
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
} else {
|
|
441
|
-
// "any" — at least one condition met
|
|
442
|
-
pass = attestation.results.some((r) => r.met)
|
|
443
|
-
}
|
|
519
|
+
const pass =
|
|
520
|
+
matchMode === 'all'
|
|
521
|
+
? attestation.pass
|
|
522
|
+
: attestation.results.some((r) => r.met)
|
|
444
523
|
|
|
445
|
-
// Cache the result
|
|
446
524
|
cache.set(key, {
|
|
447
525
|
pass,
|
|
448
526
|
attestationId: attestation.id,
|
|
449
527
|
expiresAt: Date.now() + cacheTtlSeconds * 1000,
|
|
450
528
|
})
|
|
451
529
|
|
|
452
|
-
if (pass)
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
}
|
|
530
|
+
if (!pass) return null
|
|
531
|
+
return {
|
|
532
|
+
method: server.name,
|
|
533
|
+
reference: `condition-gate:free:${attestation.id}`,
|
|
534
|
+
status: 'success' as const,
|
|
535
|
+
timestamp: new Date().toISOString(),
|
|
459
536
|
}
|
|
460
537
|
} catch {
|
|
461
|
-
// Attestation
|
|
538
|
+
// Attestation error → paid path.
|
|
539
|
+
return null
|
|
462
540
|
}
|
|
463
|
-
|
|
464
|
-
return originalVerify(params)
|
|
465
541
|
}
|
|
466
542
|
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
543
|
+
const wrapped = { ...server } as unknown as Record<string, unknown>
|
|
544
|
+
|
|
545
|
+
if (typeof originalBroadcast === 'function') {
|
|
546
|
+
wrapped.broadcast = async (params: any) =>
|
|
547
|
+
(await tryFree(params)) ?? originalBroadcast(params)
|
|
470
548
|
}
|
|
549
|
+
|
|
550
|
+
wrapped.verify = async (params: any) =>
|
|
551
|
+
(await tryFree(params)) ?? originalVerify(params)
|
|
552
|
+
|
|
553
|
+
return wrapped as unknown as Method.AnyServer
|
|
471
554
|
}
|