payid 0.4.1 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-5ZEKI5Y2.js +18 -0
- package/dist/chunk-6VPSJFO4.js +53 -0
- package/dist/chunk-7U3P7XJE.js +67 -0
- package/dist/chunk-ANG3SJGI.js +324 -0
- package/dist/chunk-AOKLY2QN.js +24 -0
- package/dist/chunk-GB3FSF7K.js +84 -0
- package/dist/chunk-GG34PNTF.js +19 -0
- package/dist/chunk-ILV7Z3IN.js +30 -0
- package/dist/chunk-MXKZJKXE.js +33 -0
- package/dist/chunk-R5U7XKVJ.js +16 -0
- package/dist/chunk-RCXMRX4F.js +54 -0
- package/dist/chunk-Y75PSD7U.js +47 -0
- package/dist/chunk-YUAYDVGX.js +134 -0
- package/dist/context/index.d.ts +3 -0
- package/dist/context/index.js +8 -0
- package/dist/core/client/index.d.ts +5 -0
- package/dist/core/client/index.js +12 -0
- package/dist/core/server/index.d.ts +4 -0
- package/dist/core/server/index.js +9 -0
- package/dist/index-2JCvey4-.d.ts +23 -0
- package/dist/index-2O3usHUn.d.ts +109 -0
- package/dist/index-BEvnPzzt.d.ts +160 -0
- package/dist/index-BQQnMG2H.d.ts +114 -0
- package/dist/index-C1DHMQA0.d.ts +154 -0
- package/dist/index-DerQdZmf.d.ts +110 -0
- package/dist/index.d.ts +17 -685
- package/dist/index.js +36 -773
- package/dist/issuer/index.d.ts +3 -0
- package/dist/issuer/index.js +16 -0
- package/dist/rule/index.d.ts +2 -0
- package/dist/rule/index.js +15 -0
- package/dist/sessionPolicy/index.d.ts +4 -0
- package/dist/sessionPolicy/index.js +13 -0
- package/dist/types-B8pJQdMQ.d.ts +26 -0
- package/dist/types-BmMf7udp.d.ts +13 -0
- package/package.json +2 -2
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import {
|
|
2
|
+
issueEnvContext,
|
|
3
|
+
issueOracleContext,
|
|
4
|
+
issueRiskContext,
|
|
5
|
+
issueStateContext
|
|
6
|
+
} from "./chunk-7U3P7XJE.js";
|
|
7
|
+
import {
|
|
8
|
+
__export
|
|
9
|
+
} from "./chunk-R5U7XKVJ.js";
|
|
10
|
+
|
|
11
|
+
// src/context/index.ts
|
|
12
|
+
var context_exports = {};
|
|
13
|
+
__export(context_exports, {
|
|
14
|
+
buildContextV2: () => buildContextV2
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
// src/context/contextV2.ts
|
|
18
|
+
async function buildContextV2(params) {
|
|
19
|
+
const ctx = {
|
|
20
|
+
...params.baseContext
|
|
21
|
+
};
|
|
22
|
+
if (params.env) {
|
|
23
|
+
ctx.env = await issueEnvContext(
|
|
24
|
+
params.env.issuer
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
if (params.state) {
|
|
28
|
+
ctx.state = await issueStateContext(
|
|
29
|
+
params.state.issuer,
|
|
30
|
+
params.state.spentToday,
|
|
31
|
+
params.state.period
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
if (params.oracle) {
|
|
35
|
+
ctx.oracle = await issueOracleContext(
|
|
36
|
+
params.oracle.issuer,
|
|
37
|
+
params.oracle.data
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
if (params.risk) {
|
|
41
|
+
ctx.risk = await issueRiskContext(
|
|
42
|
+
params.risk.issuer,
|
|
43
|
+
params.risk.score,
|
|
44
|
+
params.risk.category,
|
|
45
|
+
params.risk.modelHash
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
return ctx;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export {
|
|
52
|
+
buildContextV2,
|
|
53
|
+
context_exports
|
|
54
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import {
|
|
2
|
+
decodeSessionPolicy
|
|
3
|
+
} from "./chunk-MXKZJKXE.js";
|
|
4
|
+
import {
|
|
5
|
+
canonicalizeRuleSet
|
|
6
|
+
} from "./chunk-6VPSJFO4.js";
|
|
7
|
+
import {
|
|
8
|
+
randomHex
|
|
9
|
+
} from "./chunk-5ZEKI5Y2.js";
|
|
10
|
+
import {
|
|
11
|
+
__export
|
|
12
|
+
} from "./chunk-R5U7XKVJ.js";
|
|
13
|
+
|
|
14
|
+
// src/sessionPolicy/index.ts
|
|
15
|
+
var sessionPolicy_exports = {};
|
|
16
|
+
__export(sessionPolicy_exports, {
|
|
17
|
+
createSessionPolicyPayload: () => createSessionPolicyPayload,
|
|
18
|
+
decodeSessionPolicy: () => decodeSessionPolicy
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
// src/sessionPolicy/create.ts
|
|
22
|
+
import { ethers } from "ethers";
|
|
23
|
+
async function createSessionPolicyPayload(params) {
|
|
24
|
+
const issuedAt = Math.floor(Date.now() / 1e3);
|
|
25
|
+
const nonce = randomHex(16);
|
|
26
|
+
const payload = {
|
|
27
|
+
version: "payid.session.policy.v1",
|
|
28
|
+
receiver: params.receiver,
|
|
29
|
+
rule: canonicalizeRuleSet(params.rule),
|
|
30
|
+
issuedAt,
|
|
31
|
+
expiresAt: params.expiresAt,
|
|
32
|
+
nonce
|
|
33
|
+
};
|
|
34
|
+
const message = ethers.keccak256(
|
|
35
|
+
ethers.toUtf8Bytes(JSON.stringify(payload))
|
|
36
|
+
);
|
|
37
|
+
const signature = await params.signer.signMessage(message);
|
|
38
|
+
return {
|
|
39
|
+
...payload,
|
|
40
|
+
signature
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export {
|
|
45
|
+
createSessionPolicyPayload,
|
|
46
|
+
sessionPolicy_exports
|
|
47
|
+
};
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import {
|
|
2
|
+
evaluate,
|
|
3
|
+
generateDecisionProof,
|
|
4
|
+
resolveRule
|
|
5
|
+
} from "./chunk-ANG3SJGI.js";
|
|
6
|
+
import {
|
|
7
|
+
__export
|
|
8
|
+
} from "./chunk-R5U7XKVJ.js";
|
|
9
|
+
|
|
10
|
+
// src/core/server/index.ts
|
|
11
|
+
var server_exports = {};
|
|
12
|
+
__export(server_exports, {
|
|
13
|
+
createPayID: () => createPayID
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
// src/erc4337/build.ts
|
|
17
|
+
import { ethers } from "ethers";
|
|
18
|
+
var PAY_WITH_PAYID_ABI = [
|
|
19
|
+
// ETH payment — attestationUIDs adalah EAS UIDs, pass [] jika tidak perlu
|
|
20
|
+
"function payETH((bytes32 version, bytes32 payId, address payer, address receiver, address asset, uint256 amount, bytes32 contextHash, bytes32 ruleSetHash, address ruleAuthority, uint64 issuedAt, uint64 expiresAt, bytes32 nonce, bool requiresAttestation) d, bytes sig, bytes32[] attestationUIDs) payable",
|
|
21
|
+
// ERC20 payment
|
|
22
|
+
"function payERC20((bytes32 version, bytes32 payId, address payer, address receiver, address asset, uint256 amount, bytes32 contextHash, bytes32 ruleSetHash, address ruleAuthority, uint64 issuedAt, uint64 expiresAt, bytes32 nonce, bool requiresAttestation) d, bytes sig, bytes32[] attestationUIDs)"
|
|
23
|
+
];
|
|
24
|
+
function buildPayETHCallData(contractAddress, proof, attestationUIDs = []) {
|
|
25
|
+
const iface = new ethers.Interface(PAY_WITH_PAYID_ABI);
|
|
26
|
+
return iface.encodeFunctionData("payETH", [
|
|
27
|
+
proof.payload,
|
|
28
|
+
proof.signature,
|
|
29
|
+
attestationUIDs
|
|
30
|
+
]);
|
|
31
|
+
}
|
|
32
|
+
function buildPayERC20CallData(contractAddress, proof, attestationUIDs = []) {
|
|
33
|
+
const iface = new ethers.Interface(PAY_WITH_PAYID_ABI);
|
|
34
|
+
return iface.encodeFunctionData("payERC20", [
|
|
35
|
+
proof.payload,
|
|
36
|
+
proof.signature,
|
|
37
|
+
attestationUIDs
|
|
38
|
+
]);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// src/erc4337/userop.ts
|
|
42
|
+
function buildUserOperation(params) {
|
|
43
|
+
return {
|
|
44
|
+
sender: params.sender,
|
|
45
|
+
nonce: params.nonce,
|
|
46
|
+
initCode: params.initCode ?? "0x",
|
|
47
|
+
callData: params.callData,
|
|
48
|
+
callGasLimit: params.gas.callGasLimit,
|
|
49
|
+
verificationGasLimit: params.gas.verificationGasLimit,
|
|
50
|
+
preVerificationGas: params.gas.preVerificationGas,
|
|
51
|
+
maxFeePerGas: params.gas.maxFeePerGas,
|
|
52
|
+
maxPriorityFeePerGas: params.gas.maxPriorityFeePerGas,
|
|
53
|
+
paymasterAndData: params.paymasterAndData ?? "0x",
|
|
54
|
+
signature: "0x"
|
|
55
|
+
// signed later by smart account
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// src/core/server/server.ts
|
|
60
|
+
function isRuleSource(rule) {
|
|
61
|
+
return typeof rule === "object" && rule !== null && "uri" in rule;
|
|
62
|
+
}
|
|
63
|
+
var PayIDServer = class {
|
|
64
|
+
constructor(signer, trustedIssuers, debugTrace, wasm) {
|
|
65
|
+
this.signer = signer;
|
|
66
|
+
this.trustedIssuers = trustedIssuers;
|
|
67
|
+
this.debugTrace = debugTrace;
|
|
68
|
+
this.wasm = wasm;
|
|
69
|
+
}
|
|
70
|
+
async evaluateAndProve(params) {
|
|
71
|
+
const authorityConfig = isRuleSource(params.authorityRule) ? (await resolveRule(params.authorityRule)).config : params.authorityRule;
|
|
72
|
+
const evalConfig = params.evaluationRule ?? authorityConfig;
|
|
73
|
+
const result = await evaluate(
|
|
74
|
+
params.context,
|
|
75
|
+
evalConfig,
|
|
76
|
+
{
|
|
77
|
+
debug: this.debugTrace,
|
|
78
|
+
trustedIssuers: this.trustedIssuers
|
|
79
|
+
},
|
|
80
|
+
this.wasm
|
|
81
|
+
);
|
|
82
|
+
if (result.decision !== "ALLOW") {
|
|
83
|
+
return { result, proof: null };
|
|
84
|
+
}
|
|
85
|
+
const proof = await generateDecisionProof({
|
|
86
|
+
payId: params.payId,
|
|
87
|
+
payer: params.payer,
|
|
88
|
+
receiver: params.receiver,
|
|
89
|
+
asset: params.asset,
|
|
90
|
+
amount: params.amount,
|
|
91
|
+
context: params.context,
|
|
92
|
+
ruleConfig: authorityConfig,
|
|
93
|
+
signer: this.signer,
|
|
94
|
+
verifyingContract: params.verifyingContract,
|
|
95
|
+
ruleAuthority: params.ruleAuthority,
|
|
96
|
+
chainId: params.chainId ?? params.context?.tx?.chainId,
|
|
97
|
+
ttlSeconds: params.ttlSeconds,
|
|
98
|
+
blockTimestamp: params.blockTimestamp
|
|
99
|
+
});
|
|
100
|
+
return { result, proof };
|
|
101
|
+
}
|
|
102
|
+
buildUserOperation(params) {
|
|
103
|
+
const callData = buildPayERC20CallData(
|
|
104
|
+
params.targetContract,
|
|
105
|
+
params.proof,
|
|
106
|
+
params.attestationUIDs ?? []
|
|
107
|
+
);
|
|
108
|
+
return buildUserOperation({
|
|
109
|
+
sender: params.smartAccount,
|
|
110
|
+
nonce: params.nonce,
|
|
111
|
+
callData,
|
|
112
|
+
gas: params.gas,
|
|
113
|
+
paymasterAndData: params.paymasterAndData
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
// src/core/server/index.ts
|
|
119
|
+
function createPayID(params) {
|
|
120
|
+
return new PayIDServer(
|
|
121
|
+
params.signer,
|
|
122
|
+
params.trustedIssuers,
|
|
123
|
+
params.debugTrace ?? false,
|
|
124
|
+
params.wasm
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export {
|
|
129
|
+
buildPayETHCallData,
|
|
130
|
+
buildPayERC20CallData,
|
|
131
|
+
buildUserOperation,
|
|
132
|
+
createPayID,
|
|
133
|
+
server_exports
|
|
134
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createPayID
|
|
3
|
+
} from "../../chunk-GB3FSF7K.js";
|
|
4
|
+
import "../../chunk-GG34PNTF.js";
|
|
5
|
+
import "../../chunk-MXKZJKXE.js";
|
|
6
|
+
import "../../chunk-6VPSJFO4.js";
|
|
7
|
+
import "../../chunk-ANG3SJGI.js";
|
|
8
|
+
import "../../chunk-5ZEKI5Y2.js";
|
|
9
|
+
import "../../chunk-R5U7XKVJ.js";
|
|
10
|
+
export {
|
|
11
|
+
createPayID
|
|
12
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { EnvContext, OracleContext, RiskContext, StateContext, Attestation } from 'payid-types';
|
|
2
|
+
import { Wallet } from 'ethers';
|
|
3
|
+
|
|
4
|
+
declare function issueEnvContext(wallet: Wallet): Promise<EnvContext>;
|
|
5
|
+
|
|
6
|
+
declare function issueOracleContext(wallet: Wallet, data: Record<string, string | number>): Promise<OracleContext>;
|
|
7
|
+
|
|
8
|
+
declare function issueRiskContext(wallet: Wallet, score: number, category: string, modelHash: string): Promise<RiskContext>;
|
|
9
|
+
|
|
10
|
+
declare function issueStateContext(wallet: Wallet, spentToday: string, period: string): Promise<StateContext>;
|
|
11
|
+
|
|
12
|
+
declare function signAttestation(issuerWallet: Wallet, payload: object, ttlSeconds?: number): Promise<Attestation>;
|
|
13
|
+
|
|
14
|
+
declare const index_issueEnvContext: typeof issueEnvContext;
|
|
15
|
+
declare const index_issueOracleContext: typeof issueOracleContext;
|
|
16
|
+
declare const index_issueRiskContext: typeof issueRiskContext;
|
|
17
|
+
declare const index_issueStateContext: typeof issueStateContext;
|
|
18
|
+
declare const index_signAttestation: typeof signAttestation;
|
|
19
|
+
declare namespace index {
|
|
20
|
+
export { index_issueEnvContext as issueEnvContext, index_issueOracleContext as issueOracleContext, index_issueRiskContext as issueRiskContext, index_issueStateContext as issueStateContext, index_signAttestation as signAttestation };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { issueEnvContext as a, issueOracleContext as b, issueRiskContext as c, issueStateContext as d, index as i, signAttestation as s };
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { RuleContext, RuleConfig, RuleResult } from 'payid-types';
|
|
2
|
+
import { ethers } from 'ethers';
|
|
3
|
+
import { R as RuleSource, D as DecisionProof } from './types-B8pJQdMQ.js';
|
|
4
|
+
import { P as PayIDSessionPolicyPayloadV1 } from './types-BmMf7udp.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @class PayIDClient
|
|
8
|
+
* @description Client-side PayID engine.
|
|
9
|
+
*
|
|
10
|
+
* Fully serverless — aman dipakai di browser, mobile, edge.
|
|
11
|
+
* Tidak butuh issuer wallet, tidak butuh server.
|
|
12
|
+
*
|
|
13
|
+
* Untuk attestation, gunakan EAS UIDs yang di-fetch via `eas.EASClient`.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const client = new PayIDClient(wasmBinary)
|
|
18
|
+
*
|
|
19
|
+
* // 1. Evaluate rule
|
|
20
|
+
* const result = await client.evaluate(context, ruleConfig)
|
|
21
|
+
*
|
|
22
|
+
* // 2. Evaluate + generate proof (payer sign sendiri)
|
|
23
|
+
* const { result, proof } = await client.evaluateAndProve({
|
|
24
|
+
* context,
|
|
25
|
+
* authorityRule: ruleConfig,
|
|
26
|
+
* payId: "pay.id/merchant",
|
|
27
|
+
* payer: await signer.getAddress(),
|
|
28
|
+
* receiver: "0xRECEIVER",
|
|
29
|
+
* asset: USDT_ADDRESS,
|
|
30
|
+
* amount: parseUnits("100", 6),
|
|
31
|
+
* signer,
|
|
32
|
+
* verifyingContract: PAYID_VERIFIER_ADDRESS,
|
|
33
|
+
* ruleAuthority: RULE_AUTHORITY_ADDRESS,
|
|
34
|
+
* })
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
declare class PayIDClient {
|
|
38
|
+
private readonly debugTrace?;
|
|
39
|
+
private readonly wasm?;
|
|
40
|
+
constructor(debugTrace?: boolean | undefined, wasm?: Uint8Array | undefined);
|
|
41
|
+
evaluate(context: RuleContext, rule: RuleConfig | RuleSource): Promise<RuleResult>;
|
|
42
|
+
evaluateAndProve(params: {
|
|
43
|
+
context: RuleContext;
|
|
44
|
+
authorityRule: RuleConfig | RuleSource;
|
|
45
|
+
evaluationRule?: RuleConfig;
|
|
46
|
+
sessionPolicy?: PayIDSessionPolicyPayloadV1;
|
|
47
|
+
payId: string;
|
|
48
|
+
payer: string;
|
|
49
|
+
receiver: string;
|
|
50
|
+
asset: string;
|
|
51
|
+
amount: bigint;
|
|
52
|
+
signer: ethers.Signer;
|
|
53
|
+
verifyingContract: string;
|
|
54
|
+
ruleAuthority: string;
|
|
55
|
+
ttlSeconds?: number;
|
|
56
|
+
chainId: number;
|
|
57
|
+
blockTimestamp: number;
|
|
58
|
+
}): Promise<{
|
|
59
|
+
result: RuleResult;
|
|
60
|
+
proof: DecisionProof | null;
|
|
61
|
+
}>;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Create a PayID policy engine instance backed by a WASM rule evaluator.
|
|
66
|
+
*
|
|
67
|
+
* ## Responsibility
|
|
68
|
+
*
|
|
69
|
+
* - Holds the WASM binary used for rule execution
|
|
70
|
+
* - Defines the trust boundary for context attestation verification
|
|
71
|
+
* - Acts as the primary entry point for PayID rule evaluation
|
|
72
|
+
*
|
|
73
|
+
* ## Trust model
|
|
74
|
+
*
|
|
75
|
+
* - If `trustedIssuers` is provided, Context V2 attestation
|
|
76
|
+
* verification is ENFORCED.
|
|
77
|
+
* - If `trustedIssuers` is omitted, the engine runs in
|
|
78
|
+
* legacy (Context V1) mode without cryptographic verification.
|
|
79
|
+
*
|
|
80
|
+
* ## Environment
|
|
81
|
+
*
|
|
82
|
+
* This class is safe to instantiate in:
|
|
83
|
+
* - Browsers
|
|
84
|
+
* - Mobile apps
|
|
85
|
+
* - Edge runtimes
|
|
86
|
+
* - Backend services
|
|
87
|
+
*
|
|
88
|
+
* @param wasm
|
|
89
|
+
* Compiled PayID WASM rule engine binary.
|
|
90
|
+
*
|
|
91
|
+
* @param debugTrace
|
|
92
|
+
* Optional flag to enable decision trace generation for debugging.
|
|
93
|
+
* @example
|
|
94
|
+
* ```ts
|
|
95
|
+
*
|
|
96
|
+
* const payid = new PayID(wasmBinary, debugTrace);
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
declare function createPayID(params: {
|
|
100
|
+
wasm?: Uint8Array;
|
|
101
|
+
debugTrace?: boolean;
|
|
102
|
+
}): PayIDClient;
|
|
103
|
+
|
|
104
|
+
declare const index_createPayID: typeof createPayID;
|
|
105
|
+
declare namespace index {
|
|
106
|
+
export { index_createPayID as createPayID };
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export { createPayID as c, index as i };
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { Wallet } from 'ethers';
|
|
2
|
+
import { ContextV1, ContextV2 } from 'payid-types';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Build an attested Context V2 object from a base execution context
|
|
6
|
+
* and a set of optional attestation issuers.
|
|
7
|
+
*
|
|
8
|
+
* ## Purpose
|
|
9
|
+
*
|
|
10
|
+
* This function assembles **Context V2**, which extends a raw
|
|
11
|
+
* execution context (Context V1) with **cryptographically attested
|
|
12
|
+
* facts** such as:
|
|
13
|
+
* - Environment data (time, runtime conditions)
|
|
14
|
+
* - Stateful data (daily spend, quotas)
|
|
15
|
+
* - Oracle data (country, FX rate, KYC attributes)
|
|
16
|
+
* - Risk signals (ML score, risk category)
|
|
17
|
+
*
|
|
18
|
+
* The resulting context is suitable for:
|
|
19
|
+
* - Deterministic rule evaluation
|
|
20
|
+
* - Context V2 verification via `preprocessContextV2`
|
|
21
|
+
* - Off-chain decision proof generation
|
|
22
|
+
* - On-chain attestation verification
|
|
23
|
+
*
|
|
24
|
+
* ## Trust model
|
|
25
|
+
*
|
|
26
|
+
* - Each context domain (`env`, `state`, `oracle`, `risk`) MUST be
|
|
27
|
+
* issued and signed by a trusted issuer.
|
|
28
|
+
* - The rule engine does NOT trust raw values; it only trusts
|
|
29
|
+
* verified attestations.
|
|
30
|
+
* - Which domains are required is determined by `ruleConfig.requires`.
|
|
31
|
+
*
|
|
32
|
+
* ## Responsibility
|
|
33
|
+
*
|
|
34
|
+
* This function:
|
|
35
|
+
* - Calls the appropriate issuer functions to generate attestations
|
|
36
|
+
* - Aggregates all issued contexts into a single Context V2 object
|
|
37
|
+
* - Does NOT evaluate rules
|
|
38
|
+
* - Does NOT perform attestation verification
|
|
39
|
+
*
|
|
40
|
+
* ## Environment
|
|
41
|
+
*
|
|
42
|
+
* This function may be called from:
|
|
43
|
+
* - Backend services
|
|
44
|
+
* - Relayers / bundlers
|
|
45
|
+
* - Edge runtimes
|
|
46
|
+
*
|
|
47
|
+
* It SHOULD NOT be called directly from untrusted clients unless
|
|
48
|
+
* issuer keys are properly secured.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ### Minimal Context V2 (env + state only)
|
|
52
|
+
*
|
|
53
|
+
* ```ts
|
|
54
|
+
* const contextV2 = await buildContextV2({
|
|
55
|
+
* baseContext: {
|
|
56
|
+
* tx,
|
|
57
|
+
* payId
|
|
58
|
+
* },
|
|
59
|
+
* env: {
|
|
60
|
+
* issuer: envIssuer
|
|
61
|
+
* },
|
|
62
|
+
* state: {
|
|
63
|
+
* issuer: stateIssuer,
|
|
64
|
+
* spentToday: "2500000",
|
|
65
|
+
* period: "DAY"
|
|
66
|
+
* }
|
|
67
|
+
* });
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ### Full Context V2 (env + state + oracle + risk)
|
|
72
|
+
*
|
|
73
|
+
* ```ts
|
|
74
|
+
* const contextV2 = await buildContextV2({
|
|
75
|
+
* baseContext: {
|
|
76
|
+
* tx,
|
|
77
|
+
* payId
|
|
78
|
+
* },
|
|
79
|
+
* env: {
|
|
80
|
+
* issuer: envIssuer
|
|
81
|
+
* },
|
|
82
|
+
* state: {
|
|
83
|
+
* issuer: stateIssuer,
|
|
84
|
+
* spentToday: "2500000",
|
|
85
|
+
* period: "DAY"
|
|
86
|
+
* },
|
|
87
|
+
* oracle: {
|
|
88
|
+
* issuer: oracleIssuer,
|
|
89
|
+
* data: {
|
|
90
|
+
* country: "ID",
|
|
91
|
+
* fxRate: 15600
|
|
92
|
+
* }
|
|
93
|
+
* },
|
|
94
|
+
* risk: {
|
|
95
|
+
* issuer: riskIssuer,
|
|
96
|
+
* score: 72,
|
|
97
|
+
* category: "MEDIUM",
|
|
98
|
+
* modelHash: "0xmodelhash123"
|
|
99
|
+
* }
|
|
100
|
+
* });
|
|
101
|
+
* ```
|
|
102
|
+
*
|
|
103
|
+
* @param params
|
|
104
|
+
* Context assembly parameters.
|
|
105
|
+
*
|
|
106
|
+
* @param params.baseContext
|
|
107
|
+
* Base execution context (Context V1), containing transaction
|
|
108
|
+
* and PayID-related fields.
|
|
109
|
+
*
|
|
110
|
+
* @param params.env
|
|
111
|
+
* Optional environment attestation.
|
|
112
|
+
* Typically used for time-based or runtime constraints.
|
|
113
|
+
*
|
|
114
|
+
* @param params.state
|
|
115
|
+
* Optional state attestation.
|
|
116
|
+
* Used for cumulative values such as daily spend or quota tracking.
|
|
117
|
+
*
|
|
118
|
+
* @param params.oracle
|
|
119
|
+
* Optional oracle attestation.
|
|
120
|
+
* Used for external facts such as country, FX rate, or KYC signals.
|
|
121
|
+
*
|
|
122
|
+
* @param params.risk
|
|
123
|
+
* Optional risk attestation.
|
|
124
|
+
* Used for ML-based risk scoring and categorization.
|
|
125
|
+
*
|
|
126
|
+
* @returns
|
|
127
|
+
* A fully assembled Context V2 object containing the base context
|
|
128
|
+
* and all requested attested sub-contexts.
|
|
129
|
+
*
|
|
130
|
+
* @throws
|
|
131
|
+
* May throw if attestation issuance fails for any domain.
|
|
132
|
+
*/
|
|
133
|
+
declare function buildContextV2(params: {
|
|
134
|
+
baseContext: ContextV1;
|
|
135
|
+
env?: {
|
|
136
|
+
issuer: Wallet;
|
|
137
|
+
};
|
|
138
|
+
state?: {
|
|
139
|
+
issuer: Wallet;
|
|
140
|
+
spentToday: string;
|
|
141
|
+
period: string;
|
|
142
|
+
};
|
|
143
|
+
oracle?: {
|
|
144
|
+
issuer: Wallet;
|
|
145
|
+
data: Record<string, string | number>;
|
|
146
|
+
};
|
|
147
|
+
risk?: {
|
|
148
|
+
issuer: Wallet;
|
|
149
|
+
score: number;
|
|
150
|
+
category: string;
|
|
151
|
+
modelHash: string;
|
|
152
|
+
};
|
|
153
|
+
}): Promise<ContextV2>;
|
|
154
|
+
|
|
155
|
+
declare const index_buildContextV2: typeof buildContextV2;
|
|
156
|
+
declare namespace index {
|
|
157
|
+
export { index_buildContextV2 as buildContextV2 };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export { buildContextV2 as b, index as i };
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { ethers } from 'ethers';
|
|
2
|
+
import { RuleConfig } from 'payid-types';
|
|
3
|
+
import { P as PayIDSessionPolicyPayloadV1 } from './types-BmMf7udp.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Create and sign an ephemeral PayID session policy payload.
|
|
7
|
+
*
|
|
8
|
+
* A session policy represents a **temporary, off-chain consent**
|
|
9
|
+
* granted by the receiver to apply additional rule constraints
|
|
10
|
+
* during rule evaluation (e.g. session limits, QR payments,
|
|
11
|
+
* intent-scoped conditions).
|
|
12
|
+
*
|
|
13
|
+
* ## Security model
|
|
14
|
+
*
|
|
15
|
+
* - The session policy is signed by the receiver.
|
|
16
|
+
* - The signature proves **explicit consent** for the included rule.
|
|
17
|
+
* - This policy does NOT establish on-chain authority and MUST NOT
|
|
18
|
+
* be registered or referenced in any on-chain rule registry.
|
|
19
|
+
*
|
|
20
|
+
* ## Canonicalization
|
|
21
|
+
*
|
|
22
|
+
* - The rule set is canonicalized BEFORE signing to ensure
|
|
23
|
+
* deterministic hashing and signature verification.
|
|
24
|
+
* - The exact payload signed here MUST be used verbatim during
|
|
25
|
+
* policy verification.
|
|
26
|
+
*
|
|
27
|
+
* ## Lifecycle
|
|
28
|
+
*
|
|
29
|
+
* - Session policies are valid only until `expiresAt`.
|
|
30
|
+
* - Expired policies MUST be rejected by the verifier.
|
|
31
|
+
*
|
|
32
|
+
* @param params
|
|
33
|
+
* @param params.receiver
|
|
34
|
+
* Address of the receiver granting the session policy.
|
|
35
|
+
*
|
|
36
|
+
* @param params.rule
|
|
37
|
+
* Rule configuration to be applied as an **off-chain evaluation
|
|
38
|
+
* override** during the session.
|
|
39
|
+
*
|
|
40
|
+
* @param params.expiresAt
|
|
41
|
+
* UNIX timestamp (seconds) indicating when the session policy
|
|
42
|
+
* becomes invalid.
|
|
43
|
+
*
|
|
44
|
+
* @param params.signer
|
|
45
|
+
* Signer controlling the receiver address, used to sign the
|
|
46
|
+
* session policy payload.
|
|
47
|
+
*
|
|
48
|
+
* @returns
|
|
49
|
+
* A signed `PayIDSessionPolicyPayloadV1` that may be transmitted
|
|
50
|
+
* to clients and verified using `decodeSessionPolicy`.
|
|
51
|
+
*
|
|
52
|
+
* @throws
|
|
53
|
+
* May throw if signing fails or the signer is misconfigured.
|
|
54
|
+
*/
|
|
55
|
+
declare function createSessionPolicyPayload(params: {
|
|
56
|
+
receiver: string;
|
|
57
|
+
rule: RuleConfig;
|
|
58
|
+
expiresAt: number;
|
|
59
|
+
signer: ethers.Signer;
|
|
60
|
+
}): Promise<PayIDSessionPolicyPayloadV1>;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Decode and verify an ephemeral PayID session policy.
|
|
64
|
+
*
|
|
65
|
+
* This function validates that a session policy:
|
|
66
|
+
* - Uses a supported policy version
|
|
67
|
+
* - Has not expired
|
|
68
|
+
* - Was cryptographically signed by the declared receiver
|
|
69
|
+
*
|
|
70
|
+
* If all checks pass, the embedded rule configuration is returned
|
|
71
|
+
* and may be used as an **off-chain evaluation override**
|
|
72
|
+
* (e.g. combined with an authoritative on-chain rule).
|
|
73
|
+
*
|
|
74
|
+
* ## Security model
|
|
75
|
+
*
|
|
76
|
+
* - The session policy signature represents **explicit consent**
|
|
77
|
+
* from the receiver for temporary rule constraints.
|
|
78
|
+
* - This policy does NOT establish on-chain authority and MUST NOT
|
|
79
|
+
* be used to derive `ruleSetHash` or interact with rule registries.
|
|
80
|
+
*
|
|
81
|
+
* ## Invariants
|
|
82
|
+
*
|
|
83
|
+
* - The payload verified here MUST match exactly the payload that was signed.
|
|
84
|
+
* - No canonicalization or mutation is performed during verification.
|
|
85
|
+
* - Expired or invalidly signed policies are rejected immediately.
|
|
86
|
+
*
|
|
87
|
+
* @export
|
|
88
|
+
*
|
|
89
|
+
* @param sessionPolicy
|
|
90
|
+
* A signed session policy payload created by
|
|
91
|
+
* `createSessionPolicyPayload`.
|
|
92
|
+
*
|
|
93
|
+
* @param now
|
|
94
|
+
* Current UNIX timestamp (seconds) used to validate policy expiry.
|
|
95
|
+
*
|
|
96
|
+
* @returns
|
|
97
|
+
* A `RuleConfig` representing the session's evaluation rule.
|
|
98
|
+
*
|
|
99
|
+
* @throws
|
|
100
|
+
* Throws if:
|
|
101
|
+
* - The policy version is unsupported
|
|
102
|
+
* - The policy has expired
|
|
103
|
+
* - The signature does not match the receiver
|
|
104
|
+
*/
|
|
105
|
+
declare function decodeSessionPolicy(sessionPolicy: PayIDSessionPolicyPayloadV1, now: number): RuleConfig;
|
|
106
|
+
|
|
107
|
+
declare const index_PayIDSessionPolicyPayloadV1: typeof PayIDSessionPolicyPayloadV1;
|
|
108
|
+
declare const index_createSessionPolicyPayload: typeof createSessionPolicyPayload;
|
|
109
|
+
declare const index_decodeSessionPolicy: typeof decodeSessionPolicy;
|
|
110
|
+
declare namespace index {
|
|
111
|
+
export { index_PayIDSessionPolicyPayloadV1 as PayIDSessionPolicyPayloadV1, index_createSessionPolicyPayload as createSessionPolicyPayload, index_decodeSessionPolicy as decodeSessionPolicy };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export { createSessionPolicyPayload as c, decodeSessionPolicy as d, index as i };
|