@opendatalabs/vana-sdk 3.13.2 → 3.13.4-pr.184.80a4b1f
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/account/personal-server-lite-owner-binding.d.ts +1 -1
- package/dist/account/personal-server-registration.d.ts +1 -1
- package/dist/auth/errors.d.ts +1 -1
- package/dist/auth/oauth-client.d.ts +1 -1
- package/dist/browser.d.ts +1 -1
- package/dist/chains/index.d.ts +2 -2
- package/dist/chains.browser.d.ts +2 -2
- package/dist/chains.d.ts +2 -2
- package/dist/chains.node.d.ts +2 -2
- package/dist/contracts/contractController.d.ts +4 -4
- package/dist/core/client.d.ts +1 -1
- package/dist/crypto/ecies/base.d.ts +1 -1
- package/dist/crypto/ecies/browser.d.ts +1 -1
- package/dist/crypto/ecies/index.d.ts +2 -2
- package/dist/crypto/ecies/node.d.ts +1 -1
- package/dist/crypto/services/WalletKeyEncryptionService.d.ts +1 -1
- package/dist/direct/access-request-client.cjs +1 -0
- package/dist/direct/access-request-client.cjs.map +1 -1
- package/dist/direct/access-request-client.d.ts +2 -2
- package/dist/direct/access-request-client.js +1 -0
- package/dist/direct/access-request-client.js.map +1 -1
- package/dist/direct/connect-flow.cjs +1 -1
- package/dist/direct/connect-flow.cjs.map +1 -1
- package/dist/direct/connect-flow.d.ts +1 -1
- package/dist/direct/connect-flow.js +1 -1
- package/dist/direct/connect-flow.js.map +1 -1
- package/dist/direct/controller.cjs.map +1 -1
- package/dist/direct/controller.d.ts +8 -8
- package/dist/direct/controller.js.map +1 -1
- package/dist/direct/endpoints.d.ts +1 -1
- package/dist/direct/errors.d.ts +1 -1
- package/dist/direct/escrow-payment.cjs +142 -11
- package/dist/direct/escrow-payment.cjs.map +1 -1
- package/dist/direct/escrow-payment.d.ts +65 -12
- package/dist/direct/escrow-payment.js +138 -11
- package/dist/direct/escrow-payment.js.map +1 -1
- package/dist/direct/personal-server-read.cjs +156 -23
- package/dist/direct/personal-server-read.cjs.map +1 -1
- package/dist/direct/personal-server-read.d.ts +28 -18
- package/dist/direct/personal-server-read.js +159 -25
- package/dist/direct/personal-server-read.js.map +1 -1
- package/dist/direct/types.cjs.map +1 -1
- package/dist/direct/types.d.ts +61 -22
- package/dist/direct/types.js.map +1 -1
- package/dist/direct/use-direct-vana-connect.d.ts +1 -1
- package/dist/generated/abi/index.d.ts +25 -25
- package/dist/index.browser.d.ts +44 -44
- package/dist/index.browser.js.map +2 -2
- package/dist/index.node.cjs +480 -0
- package/dist/index.node.cjs.map +4 -4
- package/dist/index.node.d.ts +48 -45
- package/dist/index.node.js +468 -0
- package/dist/index.node.js.map +4 -4
- package/dist/node.d.ts +1 -1
- package/dist/platform/browser-only.d.ts +3 -3
- package/dist/platform/browser-safe.d.ts +3 -3
- package/dist/platform/browser.d.ts +1 -1
- package/dist/platform/index.d.ts +5 -5
- package/dist/platform/node.d.ts +1 -1
- package/dist/platform/ports/openpgp-port.d.ts +1 -1
- package/dist/platform/utils.d.ts +1 -1
- package/dist/platform.browser.d.ts +4 -4
- package/dist/platform.d.ts +5 -5
- package/dist/platform.node.d.ts +5 -5
- package/dist/protocol/data-point-status.d.ts +1 -1
- package/dist/protocol/eip712.cjs.map +1 -1
- package/dist/protocol/eip712.js.map +1 -1
- package/dist/protocol/escrow-deposit.d.ts +1 -1
- package/dist/protocol/escrow.cjs.map +1 -1
- package/dist/protocol/escrow.d.ts +11 -4
- package/dist/protocol/escrow.js.map +1 -1
- package/dist/protocol/fee-registry.d.ts +1 -1
- package/dist/protocol/gateway.cjs.map +1 -1
- package/dist/protocol/gateway.js.map +1 -1
- package/dist/protocol/grants.d.ts +1 -1
- package/dist/protocol/personal-server-data.d.ts +2 -2
- package/dist/protocol/personal-server-registration.d.ts +2 -2
- package/dist/react.d.ts +3 -3
- package/dist/server.cjs +10 -0
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.ts +8 -8
- package/dist/server.js +10 -0
- package/dist/server.js.map +1 -1
- package/dist/session-relay/client.d.ts +1 -1
- package/dist/session-relay/endpoints.d.ts +1 -1
- package/dist/session-relay/errors.d.ts +1 -1
- package/dist/session-relay/index.d.ts +5 -5
- package/dist/session-relay/signing.d.ts +1 -1
- package/dist/session-relay/types.d.ts +1 -1
- package/dist/session-relay.d.ts +5 -5
- package/dist/storage/default.d.ts +2 -2
- package/dist/storage/index.d.ts +16 -16
- package/dist/storage/manager.d.ts +1 -1
- package/dist/storage/providers/callback-storage.d.ts +2 -2
- package/dist/storage/providers/dropbox.d.ts +1 -1
- package/dist/storage/providers/google-drive.d.ts +1 -1
- package/dist/storage/providers/ipfs.d.ts +1 -1
- package/dist/storage/providers/pinata.d.ts +1 -1
- package/dist/storage/providers/r2.d.ts +1 -1
- package/dist/storage/providers/vana-storage.d.ts +3 -3
- package/dist/types/config.d.ts +1 -1
- package/dist/types/index.d.ts +6 -6
- package/dist/types.d.ts +1 -1
- package/package.json +13 -1
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
genericPaymentDomain
|
|
5
5
|
} from "../protocol/escrow.js";
|
|
6
6
|
const GRANT_OP_TYPE = "grant";
|
|
7
|
+
const DATA_ACCESS_OP_TYPE = "data_access";
|
|
7
8
|
function toDirectFeeBreakdown(breakdown) {
|
|
8
9
|
return {
|
|
9
10
|
registrationFee: breakdown.registrationFee,
|
|
@@ -32,6 +33,58 @@ function createDefaultNonceSource() {
|
|
|
32
33
|
};
|
|
33
34
|
}
|
|
34
35
|
const processLocalNonceSource = createDefaultNonceSource();
|
|
36
|
+
const UINT256_MAX = (1n << 256n) - 1n;
|
|
37
|
+
const ADDRESS_RE = /^0x[0-9a-fA-F]{40}$/;
|
|
38
|
+
const BYTES32_RE = /^0x[0-9a-fA-F]{64}$/;
|
|
39
|
+
const SIGNATURE_RE = /^0x[0-9a-fA-F]{130}$/;
|
|
40
|
+
function isUint256Decimal(value, allowZero) {
|
|
41
|
+
const pattern = allowZero ? /^(0|[1-9]\d*)$/ : /^[1-9]\d*$/;
|
|
42
|
+
return value.length <= UINT256_MAX.toString().length && pattern.test(value) && BigInt(value) <= UINT256_MAX;
|
|
43
|
+
}
|
|
44
|
+
function isValidAccessRecord(record) {
|
|
45
|
+
return BYTES32_RE.test(record.dataPointId) && isUint256Decimal(record.version, false) && ADDRESS_RE.test(record.accessor) && BYTES32_RE.test(record.recordId) && SIGNATURE_RE.test(record.signature);
|
|
46
|
+
}
|
|
47
|
+
function validateSigningOperation(payerAddress, required) {
|
|
48
|
+
if (!ADDRESS_RE.test(payerAddress)) {
|
|
49
|
+
throw new Error("Payment payer must be a 20-byte EVM address");
|
|
50
|
+
}
|
|
51
|
+
if (!BYTES32_RE.test(required.opId)) {
|
|
52
|
+
throw new Error("Payment operation id must be a 32-byte hex value");
|
|
53
|
+
}
|
|
54
|
+
if (!ADDRESS_RE.test(required.asset || NATIVE_ASSET_ADDRESS)) {
|
|
55
|
+
throw new Error("Payment asset must be a 20-byte EVM address");
|
|
56
|
+
}
|
|
57
|
+
if (!isUint256Decimal(required.amount, true)) {
|
|
58
|
+
throw new Error("Payment amount must be a canonical uint256 decimal");
|
|
59
|
+
}
|
|
60
|
+
if (required.paymentNonce !== void 0 && !isUint256Decimal(required.paymentNonce, false)) {
|
|
61
|
+
throw new Error("Payment nonce must be a positive uint256 decimal");
|
|
62
|
+
}
|
|
63
|
+
const accessRecord = required.accessRecord;
|
|
64
|
+
if (required.opType === DATA_ACCESS_OP_TYPE) {
|
|
65
|
+
if (!accessRecord || !isValidAccessRecord(accessRecord)) {
|
|
66
|
+
throw new Error("Data-access payment requires a valid access record");
|
|
67
|
+
}
|
|
68
|
+
if (required.opId.toLowerCase() !== accessRecord.recordId.toLowerCase()) {
|
|
69
|
+
throw new Error(
|
|
70
|
+
"Data-access payment operation id must equal the access record id"
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
if (accessRecord.accessor.toLowerCase() !== payerAddress.toLowerCase()) {
|
|
74
|
+
throw new Error(
|
|
75
|
+
"Data-access payment accessor must equal the payment payer address"
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if (required.amount === "0") {
|
|
81
|
+
if (!accessRecord || !isValidAccessRecord(accessRecord) || accessRecord.accessor.toLowerCase() !== payerAddress.toLowerCase()) {
|
|
82
|
+
throw new Error(
|
|
83
|
+
"Zero-amount grant payments require a valid access record for the payer"
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
35
88
|
function base64EncodeJson(value) {
|
|
36
89
|
const bytes = new TextEncoder().encode(JSON.stringify(value));
|
|
37
90
|
let binary = "";
|
|
@@ -43,18 +96,25 @@ function base64DecodeJson(value) {
|
|
|
43
96
|
const bytes = Uint8Array.from(binary, (char) => char.charCodeAt(0));
|
|
44
97
|
return JSON.parse(new TextDecoder().decode(bytes));
|
|
45
98
|
}
|
|
46
|
-
async function
|
|
99
|
+
async function signEscrowPayment(params) {
|
|
47
100
|
const { payerAddress, required, config } = params;
|
|
101
|
+
validateSigningOperation(payerAddress, required);
|
|
48
102
|
const nonceSource = config.nonceSource ?? processLocalNonceSource;
|
|
49
103
|
const paymentNonce = BigInt(
|
|
50
104
|
required.paymentNonce ?? await nonceSource(payerAddress)
|
|
51
105
|
);
|
|
52
106
|
const asset = required.asset || NATIVE_ASSET_ADDRESS;
|
|
53
|
-
const opId = required.
|
|
107
|
+
const opId = required.opId;
|
|
54
108
|
const amount = BigInt(required.amount);
|
|
109
|
+
if (amount < 0n || amount > UINT256_MAX) {
|
|
110
|
+
throw new Error("Payment amount must be a uint256");
|
|
111
|
+
}
|
|
112
|
+
if (paymentNonce <= 0n || paymentNonce > UINT256_MAX) {
|
|
113
|
+
throw new Error("Payment nonce must be a positive uint256");
|
|
114
|
+
}
|
|
55
115
|
const message = {
|
|
56
116
|
payerAddress,
|
|
57
|
-
opType:
|
|
117
|
+
opType: required.opType,
|
|
58
118
|
opId,
|
|
59
119
|
asset,
|
|
60
120
|
amount,
|
|
@@ -76,8 +136,8 @@ async function signGrantPayment(params) {
|
|
|
76
136
|
...required.accessRecord ? { accessRecord: required.accessRecord } : {}
|
|
77
137
|
};
|
|
78
138
|
}
|
|
79
|
-
async function
|
|
80
|
-
const signed = await
|
|
139
|
+
async function buildEscrowPaymentHeader(params) {
|
|
140
|
+
const signed = await signEscrowPayment(params);
|
|
81
141
|
const payment = {
|
|
82
142
|
x402Version: 1,
|
|
83
143
|
scheme: "vana-escrow-grant",
|
|
@@ -86,35 +146,102 @@ async function buildGrantPaymentHeader(params) {
|
|
|
86
146
|
};
|
|
87
147
|
return base64EncodeJson(payment);
|
|
88
148
|
}
|
|
89
|
-
function
|
|
149
|
+
async function buildGrantPaymentHeader(params) {
|
|
150
|
+
return buildEscrowPaymentHeader({
|
|
151
|
+
...params,
|
|
152
|
+
required: {
|
|
153
|
+
...params.required,
|
|
154
|
+
opType: GRANT_OP_TYPE,
|
|
155
|
+
opId: params.required.grantId
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
function asRecord(value) {
|
|
160
|
+
return value && typeof value === "object" && !Array.isArray(value) ? value : void 0;
|
|
161
|
+
}
|
|
162
|
+
function stringField(value, key) {
|
|
163
|
+
const field = value?.[key];
|
|
164
|
+
return typeof field === "string" ? field : void 0;
|
|
165
|
+
}
|
|
166
|
+
function isCanonicalIsoTimestamp(value) {
|
|
167
|
+
try {
|
|
168
|
+
return new Date(value).toISOString() === value;
|
|
169
|
+
} catch {
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
function paymentResponseMetadataFromHeader(header) {
|
|
90
174
|
if (!header) return void 0;
|
|
91
175
|
try {
|
|
92
|
-
|
|
176
|
+
const result = asRecord(base64DecodeJson(header));
|
|
177
|
+
const breakdown = asRecord(result?.breakdown);
|
|
178
|
+
const opType = stringField(result, "opType");
|
|
179
|
+
const opId = stringField(result, "opId");
|
|
180
|
+
const payerAddress = stringField(result, "payerAddress");
|
|
181
|
+
const asset = stringField(result, "asset");
|
|
182
|
+
const amount = stringField(result, "amount");
|
|
183
|
+
const paymentNonce = stringField(result, "paymentNonce");
|
|
184
|
+
const registrationFee = stringField(breakdown, "registrationFee");
|
|
185
|
+
const dataAccessFee = stringField(breakdown, "dataAccessFee");
|
|
186
|
+
const paidAt = stringField(result, "paidAt");
|
|
187
|
+
if (result?.success !== true || !opType || !opId || !BYTES32_RE.test(opId) || !payerAddress || !ADDRESS_RE.test(payerAddress) || !asset || !ADDRESS_RE.test(asset) || !amount || !isUint256Decimal(amount, true) || !paymentNonce || !isUint256Decimal(paymentNonce, false) || !registrationFee || !isUint256Decimal(registrationFee, true) || !dataAccessFee || !isUint256Decimal(dataAccessFee, true) || typeof breakdown?.registrationPaid !== "boolean" || !paidAt || !isCanonicalIsoTimestamp(paidAt)) {
|
|
188
|
+
return void 0;
|
|
189
|
+
}
|
|
190
|
+
return {
|
|
191
|
+
opType,
|
|
192
|
+
opId,
|
|
193
|
+
asset,
|
|
194
|
+
amount,
|
|
195
|
+
paymentNonce,
|
|
196
|
+
breakdown: {
|
|
197
|
+
registrationFee,
|
|
198
|
+
dataAccessFee,
|
|
199
|
+
registrationPaid: breakdown.registrationPaid
|
|
200
|
+
},
|
|
201
|
+
paidAt
|
|
202
|
+
};
|
|
93
203
|
} catch {
|
|
94
204
|
return void 0;
|
|
95
205
|
}
|
|
96
206
|
}
|
|
207
|
+
function paymentReceiptFromHeader(header) {
|
|
208
|
+
return paymentResponseMetadataFromHeader(header);
|
|
209
|
+
}
|
|
97
210
|
async function authorizeGrantPayment(params) {
|
|
98
|
-
|
|
99
|
-
|
|
211
|
+
return authorizeEscrowPayment({
|
|
212
|
+
...params,
|
|
213
|
+
required: {
|
|
214
|
+
...params.required,
|
|
215
|
+
opType: GRANT_OP_TYPE,
|
|
216
|
+
opId: params.required.grantId
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
async function authorizeEscrowPayment(params) {
|
|
221
|
+
const { payerAddress, config } = params;
|
|
222
|
+
const signed = await signEscrowPayment(params);
|
|
100
223
|
const result = await config.client.payForOp({
|
|
101
224
|
payerAddress,
|
|
102
|
-
opType:
|
|
225
|
+
opType: signed.message.opType,
|
|
103
226
|
opId: signed.message.opId,
|
|
104
227
|
asset: signed.message.asset,
|
|
105
228
|
amount: signed.message.amount,
|
|
106
229
|
paymentNonce: signed.message.paymentNonce,
|
|
107
230
|
signature: signed.signature,
|
|
108
|
-
accessRecord:
|
|
231
|
+
accessRecord: signed.accessRecord
|
|
109
232
|
});
|
|
110
233
|
return toDirectPaymentReceipt(result);
|
|
111
234
|
}
|
|
112
235
|
export {
|
|
236
|
+
DATA_ACCESS_OP_TYPE,
|
|
113
237
|
GRANT_OP_TYPE,
|
|
238
|
+
authorizeEscrowPayment,
|
|
114
239
|
authorizeGrantPayment,
|
|
240
|
+
buildEscrowPaymentHeader,
|
|
115
241
|
buildGrantPaymentHeader,
|
|
116
242
|
createDefaultNonceSource,
|
|
117
243
|
paymentReceiptFromHeader,
|
|
244
|
+
paymentResponseMetadataFromHeader,
|
|
118
245
|
toDirectFeeBreakdown,
|
|
119
246
|
toDirectPaymentReceipt
|
|
120
247
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/direct/escrow-payment.ts"],"sourcesContent":["/**\n * Escrow-backed payment authorization for the Direct Data Controller.\n *\n * @remarks\n * Builds on the DPv2 escrow surface added in `protocol/escrow`. When a Personal\n * Server read returns `402 Payment Required`, the controller settles the\n * grant's data-access fee through the escrow gateway:\n *\n * 1. Sign a `GenericPayment` EIP-712 message (op `\"grant\"`, opId = grantId)\n * with the app key.\n * 2. POST it to the gateway's `/v1/escrow/pay` via {@link EscrowGatewayClient}.\n * 3. Map the gateway's {@link EscrowPayResult} into a typed\n * {@link DirectPaymentReceipt} for the caller to inspect.\n *\n * This module adapts the escrow `payForOp` flow to the direct-read use case; it\n * does not define its own payment scheme.\n *\n * @category Direct\n * @module direct/escrow-payment\n */\n\nimport {\n GENERIC_PAYMENT_TYPES,\n NATIVE_ASSET_ADDRESS,\n genericPaymentDomain,\n type EscrowAccessRecord,\n type EscrowGatewayClient,\n type EscrowPayResult,\n type PaymentBreakdown,\n} from \"../protocol/escrow\";\nimport type {\n DirectFeeBreakdown,\n DirectPaymentReceipt,\n PersonalServerPaymentRequired,\n} from \"./types\";\n\n/** The escrow `GenericPayment.opType` used for grant-lifecycle payments. */\nexport const GRANT_OP_TYPE = \"grant\" as const;\n\n/**\n * EIP-712 typed-data signer (e.g. viem `account.signTypedData`).\n *\n * @remarks\n * Kept structurally minimal so any viem account/wallet client satisfies it\n * without the SDK depending on viem's exact `signTypedData` overload set.\n */\nexport type SignTypedDataFn = (args: {\n domain: ReturnType<typeof genericPaymentDomain>;\n types: typeof GENERIC_PAYMENT_TYPES;\n primaryType: \"GenericPayment\";\n message: {\n payerAddress: `0x${string}`;\n opType: string;\n opId: `0x${string}`;\n asset: `0x${string}`;\n amount: bigint;\n paymentNonce: bigint;\n };\n}) => Promise<`0x${string}`>;\n\n/** Supplies a monotonically-increasing payment nonce per payer. */\nexport type PaymentNonceSource = (\n payerAddress: string,\n) => Promise<bigint> | bigint;\n\ninterface GrantPaymentMessage {\n payerAddress: `0x${string}`;\n opType: typeof GRANT_OP_TYPE;\n opId: `0x${string}`;\n asset: `0x${string}`;\n amount: string;\n paymentNonce: string;\n}\n\ninterface SignedGrantPayment {\n message: GrantPaymentMessage;\n signature: `0x${string}`;\n accessRecord?: EscrowAccessRecord;\n}\n\ninterface X402PaymentHeader {\n x402Version: 1;\n scheme: \"vana-escrow-grant\";\n network: string;\n payload: SignedGrantPayment;\n}\n\n/** Escrow settlement configuration for the controller. */\nexport interface EscrowPaymentConfig {\n /** Client for the gateway escrow endpoints (`/v1/escrow/*`). */\n client: EscrowGatewayClient;\n /** Deployed `DataPortabilityEscrow` contract address. */\n escrowContract: `0x${string}`;\n /** Chain id for the EIP-712 domain (1480 mainnet, 14800 moksha). */\n chainId: number;\n /** App EIP-712 signer. */\n signTypedData: SignTypedDataFn;\n /**\n * Supplies the next payment nonce for a payer. Defaults to a process-local\n * monotonic counter seeded at 1. Provide a durable source in production so\n * nonces survive restarts (the gateway rejects reused (payer, nonce) pairs).\n */\n nonceSource?: PaymentNonceSource;\n}\n\n/** Map the gateway {@link PaymentBreakdown} into the public {@link DirectFeeBreakdown}. */\nexport function toDirectFeeBreakdown(\n breakdown: PaymentBreakdown,\n): DirectFeeBreakdown {\n return {\n registrationFee: breakdown.registrationFee,\n dataAccessFee: breakdown.dataAccessFee,\n registrationPaid: breakdown.registrationPaid,\n };\n}\n\n/** Map a gateway {@link EscrowPayResult} into the public {@link DirectPaymentReceipt}. */\nexport function toDirectPaymentReceipt(\n result: EscrowPayResult,\n): DirectPaymentReceipt {\n return {\n opType: result.opType,\n opId: result.opId,\n asset: result.asset,\n amount: result.amount,\n paymentNonce: result.paymentNonce,\n breakdown: toDirectFeeBreakdown(result.breakdown),\n paidAt: result.paidAt,\n };\n}\n\n/** Default in-process monotonic nonce counter (seeded at 1 per payer). */\nexport function createDefaultNonceSource(): PaymentNonceSource {\n const counters = new Map<string, bigint>();\n return (payerAddress: string): bigint => {\n const key = payerAddress.toLowerCase();\n const next = (counters.get(key) ?? 0n) + 1n;\n counters.set(key, next);\n return next;\n };\n}\n\nconst processLocalNonceSource = createDefaultNonceSource();\n\nfunction base64EncodeJson(value: unknown): string {\n const bytes = new TextEncoder().encode(JSON.stringify(value));\n let binary = \"\";\n for (const byte of bytes) binary += String.fromCharCode(byte);\n return btoa(binary);\n}\n\nfunction base64DecodeJson(value: string): unknown {\n const binary = atob(value);\n const bytes = Uint8Array.from(binary, (char) => char.charCodeAt(0));\n return JSON.parse(new TextDecoder().decode(bytes));\n}\n\nasync function signGrantPayment(params: {\n payerAddress: `0x${string}`;\n required: PersonalServerPaymentRequired;\n config: EscrowPaymentConfig;\n}): Promise<SignedGrantPayment> {\n const { payerAddress, required, config } = params;\n const nonceSource = config.nonceSource ?? processLocalNonceSource;\n const paymentNonce = BigInt(\n required.paymentNonce ?? (await nonceSource(payerAddress)),\n );\n const asset = (required.asset || NATIVE_ASSET_ADDRESS) as `0x${string}`;\n const opId = required.grantId as `0x${string}`;\n const amount = BigInt(required.amount);\n\n const message = {\n payerAddress,\n opType: GRANT_OP_TYPE,\n opId,\n asset,\n amount,\n paymentNonce,\n };\n\n const signature = await config.signTypedData({\n domain: genericPaymentDomain(config.chainId, config.escrowContract),\n types: GENERIC_PAYMENT_TYPES,\n primaryType: \"GenericPayment\",\n message,\n });\n\n return {\n message: {\n ...message,\n amount: amount.toString(),\n paymentNonce: paymentNonce.toString(),\n },\n signature,\n ...(required.accessRecord ? { accessRecord: required.accessRecord } : {}),\n };\n}\n\nexport async function buildGrantPaymentHeader(params: {\n payerAddress: `0x${string}`;\n required: PersonalServerPaymentRequired;\n config: EscrowPaymentConfig;\n}): Promise<string> {\n const signed = await signGrantPayment(params);\n const payment: X402PaymentHeader = {\n x402Version: 1,\n scheme: \"vana-escrow-grant\",\n network: params.required.network ?? `vana:${params.config.chainId}`,\n payload: signed,\n };\n return base64EncodeJson(payment);\n}\n\nexport function paymentReceiptFromHeader(\n header: string | null | undefined,\n): DirectPaymentReceipt | undefined {\n if (!header) return undefined;\n try {\n return toDirectPaymentReceipt(base64DecodeJson(header) as EscrowPayResult);\n } catch {\n return undefined;\n }\n}\n\n/**\n * Authorize an escrow payment for a grant data-access fee.\n *\n * @param params - The payment requirement, the payer address, and escrow config.\n * @returns The gateway's {@link EscrowPayResult} as a typed\n * {@link DirectPaymentReceipt}.\n */\nexport async function authorizeGrantPayment(params: {\n payerAddress: `0x${string}`;\n required: PersonalServerPaymentRequired;\n config: EscrowPaymentConfig;\n}): Promise<DirectPaymentReceipt> {\n const { payerAddress, required, config } = params;\n const signed = await signGrantPayment(params);\n\n const result = await config.client.payForOp({\n payerAddress,\n opType: GRANT_OP_TYPE,\n opId: signed.message.opId,\n asset: signed.message.asset,\n amount: signed.message.amount,\n paymentNonce: signed.message.paymentNonce,\n signature: signed.signature,\n accessRecord: required.accessRecord,\n });\n\n return toDirectPaymentReceipt(result);\n}\n"],"mappings":"AAqBA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAKK;AAQA,MAAM,gBAAgB;AAqEtB,SAAS,qBACd,WACoB;AACpB,SAAO;AAAA,IACL,iBAAiB,UAAU;AAAA,IAC3B,eAAe,UAAU;AAAA,IACzB,kBAAkB,UAAU;AAAA,EAC9B;AACF;AAGO,SAAS,uBACd,QACsB;AACtB,SAAO;AAAA,IACL,QAAQ,OAAO;AAAA,IACf,MAAM,OAAO;AAAA,IACb,OAAO,OAAO;AAAA,IACd,QAAQ,OAAO;AAAA,IACf,cAAc,OAAO;AAAA,IACrB,WAAW,qBAAqB,OAAO,SAAS;AAAA,IAChD,QAAQ,OAAO;AAAA,EACjB;AACF;AAGO,SAAS,2BAA+C;AAC7D,QAAM,WAAW,oBAAI,IAAoB;AACzC,SAAO,CAAC,iBAAiC;AACvC,UAAM,MAAM,aAAa,YAAY;AACrC,UAAM,QAAQ,SAAS,IAAI,GAAG,KAAK,MAAM;AACzC,aAAS,IAAI,KAAK,IAAI;AACtB,WAAO;AAAA,EACT;AACF;AAEA,MAAM,0BAA0B,yBAAyB;AAEzD,SAAS,iBAAiB,OAAwB;AAChD,QAAM,QAAQ,IAAI,YAAY,EAAE,OAAO,KAAK,UAAU,KAAK,CAAC;AAC5D,MAAI,SAAS;AACb,aAAW,QAAQ,MAAO,WAAU,OAAO,aAAa,IAAI;AAC5D,SAAO,KAAK,MAAM;AACpB;AAEA,SAAS,iBAAiB,OAAwB;AAChD,QAAM,SAAS,KAAK,KAAK;AACzB,QAAM,QAAQ,WAAW,KAAK,QAAQ,CAAC,SAAS,KAAK,WAAW,CAAC,CAAC;AAClE,SAAO,KAAK,MAAM,IAAI,YAAY,EAAE,OAAO,KAAK,CAAC;AACnD;AAEA,eAAe,iBAAiB,QAIA;AAC9B,QAAM,EAAE,cAAc,UAAU,O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C;AAAA,IACA,QAAQ;AAAA,IACR,MAAM,OAAO,QAAQ;AAAA,IACrB,OAAO,OAAO,QAAQ;AAAA,IACtB,QAAQ,OAAO,QAAQ;AAAA,IACvB,cAAc,OAAO,QAAQ;AAAA,IAC7B,WAAW,OAAO;AAAA,IAClB,cAAc,SAAS;AAAA,EACzB,CAAC;AAED,SAAO,uBAAuB,MAAM;AACtC;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/direct/escrow-payment.ts"],"sourcesContent":["/**\n * Escrow-backed payment authorization for the Direct Data Controller.\n *\n * @remarks\n * Builds on the DPv2 escrow surface added in `protocol/escrow`. When a Personal\n * Server read returns `402 Payment Required`, the controller settles the\n * challenged operation through the escrow gateway:\n *\n * 1. Sign the challenge's `GenericPayment` EIP-712 message with the app key.\n * 2. POST it to the gateway's `/v1/escrow/pay` via {@link EscrowGatewayClient}.\n * 3. Map the gateway's {@link EscrowPayResult} into a typed\n * {@link DirectPaymentReceipt} for the caller to inspect.\n *\n * This module supports legacy `\"grant\"` operations and receipt-bound\n * `\"data_access\"` operations. It adapts the escrow `payForOp` flow to the\n * direct-read use case; it does not define its own payment scheme.\n *\n * @category Direct\n * @module direct/escrow-payment\n */\n\nimport {\n GENERIC_PAYMENT_TYPES,\n NATIVE_ASSET_ADDRESS,\n genericPaymentDomain,\n type EscrowAccessRecord,\n type EscrowGatewayClient,\n type EscrowPayResult,\n type PaymentBreakdown,\n} from \"../protocol/escrow\";\nimport type {\n DirectFeeBreakdown,\n DirectPaymentReceipt,\n DirectPaymentResponseMetadata,\n PersonalServerPaymentOperation,\n PersonalServerPaymentRequired,\n} from \"./types\";\n\n/** The escrow `GenericPayment.opType` used for grant-lifecycle payments. */\nexport const GRANT_OP_TYPE = \"grant\" as const;\n/** The escrow `GenericPayment.opType` used for receipt-bound data access. */\nexport const DATA_ACCESS_OP_TYPE = \"data_access\" as const;\n\n/**\n * EIP-712 typed-data signer (e.g. viem `account.signTypedData`).\n *\n * @remarks\n * Kept structurally minimal so any viem account/wallet client satisfies it\n * without the SDK depending on viem's exact `signTypedData` overload set.\n */\nexport type SignTypedDataFn = (args: {\n domain: ReturnType<typeof genericPaymentDomain>;\n types: typeof GENERIC_PAYMENT_TYPES;\n primaryType: \"GenericPayment\";\n message: {\n payerAddress: `0x${string}`;\n opType: string;\n opId: `0x${string}`;\n asset: `0x${string}`;\n amount: bigint;\n paymentNonce: bigint;\n };\n}) => Promise<`0x${string}`>;\n\n/** Supplies a monotonically-increasing payment nonce per payer. */\nexport type PaymentNonceSource = (\n payerAddress: string,\n) => Promise<bigint> | bigint;\n\ninterface EscrowPaymentMessage {\n payerAddress: `0x${string}`;\n opType: typeof GRANT_OP_TYPE | typeof DATA_ACCESS_OP_TYPE;\n opId: `0x${string}`;\n asset: `0x${string}`;\n amount: string;\n paymentNonce: string;\n}\n\ninterface SignedEscrowPayment {\n message: EscrowPaymentMessage;\n signature: `0x${string}`;\n accessRecord?: EscrowAccessRecord;\n}\n\ninterface X402PaymentHeader {\n x402Version: 1;\n scheme: \"vana-escrow-grant\";\n network: string;\n payload: SignedEscrowPayment;\n}\n\n/** Configuration required to sign an escrow X-PAYMENT header. */\nexport interface EscrowPaymentHeaderConfig {\n /** Deployed `DataPortabilityEscrow` contract address. */\n escrowContract: `0x${string}`;\n /** Chain id for the EIP-712 domain (1480 mainnet, 14800 moksha). */\n chainId: number;\n /** App EIP-712 signer. */\n signTypedData: SignTypedDataFn;\n /**\n * Supplies the next payment nonce for a payer. Defaults to a process-local\n * monotonic counter seeded at 1. Provide a durable source in production so\n * nonces survive restarts (the gateway rejects reused (payer, nonce) pairs).\n */\n nonceSource?: PaymentNonceSource;\n}\n\n/**\n * Escrow settlement configuration for gateway authorization.\n *\n * @remarks\n * Extends the header-signing boundary with the gateway client used by\n * {@link authorizeEscrowPayment}. Existing controller and legacy wrapper\n * callers can continue to provide this full configuration.\n */\nexport interface EscrowPaymentConfig extends EscrowPaymentHeaderConfig {\n /** Client for the gateway escrow endpoints (`/v1/escrow/*`). */\n client: EscrowGatewayClient;\n}\n\n/** Map the gateway {@link PaymentBreakdown} into the public {@link DirectFeeBreakdown}. */\nexport function toDirectFeeBreakdown(\n breakdown: PaymentBreakdown,\n): DirectFeeBreakdown {\n return {\n registrationFee: breakdown.registrationFee,\n dataAccessFee: breakdown.dataAccessFee,\n registrationPaid: breakdown.registrationPaid,\n };\n}\n\n/** Map a gateway {@link EscrowPayResult} into the public {@link DirectPaymentReceipt}. */\nexport function toDirectPaymentReceipt(\n result: EscrowPayResult,\n): DirectPaymentReceipt {\n return {\n opType: result.opType,\n opId: result.opId,\n asset: result.asset,\n amount: result.amount,\n paymentNonce: result.paymentNonce,\n breakdown: toDirectFeeBreakdown(result.breakdown),\n paidAt: result.paidAt,\n };\n}\n\n/** Default in-process monotonic nonce counter (seeded at 1 per payer). */\nexport function createDefaultNonceSource(): PaymentNonceSource {\n const counters = new Map<string, bigint>();\n return (payerAddress: string): bigint => {\n const key = payerAddress.toLowerCase();\n const next = (counters.get(key) ?? 0n) + 1n;\n counters.set(key, next);\n return next;\n };\n}\n\nconst processLocalNonceSource = createDefaultNonceSource();\nconst UINT256_MAX = (1n << 256n) - 1n;\nconst ADDRESS_RE = /^0x[0-9a-fA-F]{40}$/;\nconst BYTES32_RE = /^0x[0-9a-fA-F]{64}$/;\nconst SIGNATURE_RE = /^0x[0-9a-fA-F]{130}$/;\n\nfunction isUint256Decimal(value: string, allowZero: boolean): boolean {\n const pattern = allowZero ? /^(0|[1-9]\\d*)$/ : /^[1-9]\\d*$/;\n return (\n value.length <= UINT256_MAX.toString().length &&\n pattern.test(value) &&\n BigInt(value) <= UINT256_MAX\n );\n}\n\nfunction isValidAccessRecord(record: EscrowAccessRecord): boolean {\n return (\n BYTES32_RE.test(record.dataPointId) &&\n isUint256Decimal(record.version, false) &&\n ADDRESS_RE.test(record.accessor) &&\n BYTES32_RE.test(record.recordId) &&\n SIGNATURE_RE.test(record.signature)\n );\n}\n\nfunction validateSigningOperation(\n payerAddress: `0x${string}`,\n required: PersonalServerPaymentOperation,\n): void {\n if (!ADDRESS_RE.test(payerAddress)) {\n throw new Error(\"Payment payer must be a 20-byte EVM address\");\n }\n if (!BYTES32_RE.test(required.opId)) {\n throw new Error(\"Payment operation id must be a 32-byte hex value\");\n }\n if (!ADDRESS_RE.test(required.asset || NATIVE_ASSET_ADDRESS)) {\n throw new Error(\"Payment asset must be a 20-byte EVM address\");\n }\n if (!isUint256Decimal(required.amount, true)) {\n throw new Error(\"Payment amount must be a canonical uint256 decimal\");\n }\n if (\n required.paymentNonce !== undefined &&\n !isUint256Decimal(required.paymentNonce, false)\n ) {\n throw new Error(\"Payment nonce must be a positive uint256 decimal\");\n }\n\n const accessRecord = required.accessRecord;\n if (required.opType === DATA_ACCESS_OP_TYPE) {\n if (!accessRecord || !isValidAccessRecord(accessRecord)) {\n throw new Error(\"Data-access payment requires a valid access record\");\n }\n if (required.opId.toLowerCase() !== accessRecord.recordId.toLowerCase()) {\n throw new Error(\n \"Data-access payment operation id must equal the access record id\",\n );\n }\n if (accessRecord.accessor.toLowerCase() !== payerAddress.toLowerCase()) {\n throw new Error(\n \"Data-access payment accessor must equal the payment payer address\",\n );\n }\n return;\n }\n\n if (required.amount === \"0\") {\n if (\n !accessRecord ||\n !isValidAccessRecord(accessRecord) ||\n accessRecord.accessor.toLowerCase() !== payerAddress.toLowerCase()\n ) {\n throw new Error(\n \"Zero-amount grant payments require a valid access record for the payer\",\n );\n }\n }\n}\n\nfunction base64EncodeJson(value: unknown): string {\n const bytes = new TextEncoder().encode(JSON.stringify(value));\n let binary = \"\";\n for (const byte of bytes) binary += String.fromCharCode(byte);\n return btoa(binary);\n}\n\nfunction base64DecodeJson(value: string): unknown {\n const binary = atob(value);\n const bytes = Uint8Array.from(binary, (char) => char.charCodeAt(0));\n return JSON.parse(new TextDecoder().decode(bytes));\n}\n\nasync function signEscrowPayment(params: {\n payerAddress: `0x${string}`;\n required: PersonalServerPaymentOperation;\n config: EscrowPaymentHeaderConfig;\n}): Promise<SignedEscrowPayment> {\n const { payerAddress, required, config } = params;\n validateSigningOperation(payerAddress, required);\n const nonceSource = config.nonceSource ?? processLocalNonceSource;\n const paymentNonce = BigInt(\n required.paymentNonce ?? (await nonceSource(payerAddress)),\n );\n const asset = (required.asset || NATIVE_ASSET_ADDRESS) as `0x${string}`;\n const opId = required.opId as `0x${string}`;\n const amount = BigInt(required.amount);\n if (amount < 0n || amount > UINT256_MAX) {\n throw new Error(\"Payment amount must be a uint256\");\n }\n if (paymentNonce <= 0n || paymentNonce > UINT256_MAX) {\n throw new Error(\"Payment nonce must be a positive uint256\");\n }\n\n const message = {\n payerAddress,\n opType: required.opType,\n opId,\n asset,\n amount,\n paymentNonce,\n };\n\n const signature = await config.signTypedData({\n domain: genericPaymentDomain(config.chainId, config.escrowContract),\n types: GENERIC_PAYMENT_TYPES,\n primaryType: \"GenericPayment\",\n message,\n });\n\n return {\n message: {\n ...message,\n amount: amount.toString(),\n paymentNonce: paymentNonce.toString(),\n },\n signature,\n ...(required.accessRecord ? { accessRecord: required.accessRecord } : {}),\n };\n}\n\n/**\n * Build the canonical X-PAYMENT header for a validated escrow operation.\n *\n * @remarks\n * Supports both legacy grant payments and receipt-bound data-access payments.\n * Signing is injected through {@link EscrowPaymentHeaderConfig.signTypedData}.\n */\nexport async function buildEscrowPaymentHeader(params: {\n /** Address whose escrow balance pays for the operation. */\n payerAddress: `0x${string}`;\n /** Validated operation parsed from the Personal Server challenge. */\n required: PersonalServerPaymentOperation;\n /** Escrow contract, chain, signer, and nonce configuration. */\n config: EscrowPaymentHeaderConfig;\n}): Promise<string> {\n const signed = await signEscrowPayment(params);\n const payment: X402PaymentHeader = {\n x402Version: 1,\n scheme: \"vana-escrow-grant\",\n network: params.required.network ?? `vana:${params.config.chainId}`,\n payload: signed,\n };\n return base64EncodeJson(payment);\n}\n\n/** Build a legacy grant X-PAYMENT header. */\nexport async function buildGrantPaymentHeader(params: {\n payerAddress: `0x${string}`;\n required: PersonalServerPaymentRequired;\n config: EscrowPaymentConfig;\n}): Promise<string> {\n return buildEscrowPaymentHeader({\n ...params,\n required: {\n ...params.required,\n opType: GRANT_OP_TYPE,\n opId: params.required.grantId,\n },\n });\n}\n\nfunction asRecord(value: unknown): Record<string, unknown> | undefined {\n return value && typeof value === \"object\" && !Array.isArray(value)\n ? (value as Record<string, unknown>)\n : undefined;\n}\n\nfunction stringField(\n value: Record<string, unknown> | undefined,\n key: string,\n): string | undefined {\n const field = value?.[key];\n return typeof field === \"string\" ? field : undefined;\n}\n\nfunction isCanonicalIsoTimestamp(value: string): boolean {\n try {\n return new Date(value).toISOString() === value;\n } catch {\n return false;\n }\n}\n\n/**\n * Parse shape-validated payment response metadata echoed by a Personal Server.\n *\n * @remarks\n * This metadata is not authenticated by the gateway. It is suitable for\n * display and debugging, not as proof that a payment occurred.\n */\nexport function paymentResponseMetadataFromHeader(\n header: string | null | undefined,\n): DirectPaymentResponseMetadata | undefined {\n if (!header) return undefined;\n try {\n const result = asRecord(base64DecodeJson(header));\n const breakdown = asRecord(result?.breakdown);\n const opType = stringField(result, \"opType\");\n const opId = stringField(result, \"opId\");\n const payerAddress = stringField(result, \"payerAddress\");\n const asset = stringField(result, \"asset\");\n const amount = stringField(result, \"amount\");\n const paymentNonce = stringField(result, \"paymentNonce\");\n const registrationFee = stringField(breakdown, \"registrationFee\");\n const dataAccessFee = stringField(breakdown, \"dataAccessFee\");\n const paidAt = stringField(result, \"paidAt\");\n if (\n result?.success !== true ||\n !opType ||\n !opId ||\n !BYTES32_RE.test(opId) ||\n !payerAddress ||\n !ADDRESS_RE.test(payerAddress) ||\n !asset ||\n !ADDRESS_RE.test(asset) ||\n !amount ||\n !isUint256Decimal(amount, true) ||\n !paymentNonce ||\n !isUint256Decimal(paymentNonce, false) ||\n !registrationFee ||\n !isUint256Decimal(registrationFee, true) ||\n !dataAccessFee ||\n !isUint256Decimal(dataAccessFee, true) ||\n typeof breakdown?.registrationPaid !== \"boolean\" ||\n !paidAt ||\n !isCanonicalIsoTimestamp(paidAt)\n ) {\n return undefined;\n }\n return {\n opType,\n opId,\n asset,\n amount,\n paymentNonce,\n breakdown: {\n registrationFee,\n dataAccessFee,\n registrationPaid: breakdown.registrationPaid,\n },\n paidAt,\n };\n } catch {\n return undefined;\n }\n}\n\n/**\n * @deprecated Use {@link paymentResponseMetadataFromHeader}. A Personal\n * Server response header is untrusted metadata, not a gateway-authenticated\n * receipt.\n */\nexport function paymentReceiptFromHeader(\n header: string | null | undefined,\n): DirectPaymentResponseMetadata | undefined {\n return paymentResponseMetadataFromHeader(header);\n}\n\n/**\n * Authorize an escrow payment for a grant data-access fee.\n *\n * @param params - The payment requirement, the payer address, and escrow config.\n * @returns The gateway's {@link EscrowPayResult} as a typed\n * {@link DirectPaymentReceipt}.\n */\nexport async function authorizeGrantPayment(params: {\n payerAddress: `0x${string}`;\n required: PersonalServerPaymentRequired;\n config: EscrowPaymentConfig;\n}): Promise<DirectPaymentReceipt> {\n return authorizeEscrowPayment({\n ...params,\n required: {\n ...params.required,\n opType: GRANT_OP_TYPE,\n opId: params.required.grantId,\n },\n });\n}\n\n/**\n * Authorize a validated grant or data-access operation through the escrow\n * gateway.\n */\nexport async function authorizeEscrowPayment(params: {\n /** Address whose escrow balance pays for the operation. */\n payerAddress: `0x${string}`;\n /** Validated operation to authorize. */\n required: PersonalServerPaymentOperation;\n /** Escrow gateway and signing configuration. */\n config: EscrowPaymentConfig;\n}): Promise<DirectPaymentReceipt> {\n const { payerAddress, config } = params;\n const signed = await signEscrowPayment(params);\n\n const result = await config.client.payForOp({\n payerAddress,\n opType: signed.message.opType,\n opId: signed.message.opId,\n asset: signed.message.asset,\n amount: signed.message.amount,\n paymentNonce: signed.message.paymentNonce,\n signature: signed.signature,\n accessRecord: signed.accessRecord,\n });\n\n return toDirectPaymentReceipt(result);\n}\n"],"mappings":"AAqBA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAKK;AAUA,MAAM,gBAAgB;AAEtB,MAAM,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9a-fA-F]{64}$/.test(value);
|
|
100
100
|
}
|
|
101
|
-
|
|
101
|
+
const UINT256_MAX = (1n << 256n) - 1n;
|
|
102
|
+
function isAddressHex(value) {
|
|
103
|
+
return /^0x[0-9a-fA-F]{40}$/.test(value);
|
|
104
|
+
}
|
|
105
|
+
function isUint256Decimal(value, allowZero) {
|
|
106
|
+
const pattern = allowZero ? /^(0|[1-9]\d*)$/ : /^[1-9]\d*$/;
|
|
107
|
+
return value.length <= UINT256_MAX.toString().length && pattern.test(value) && BigInt(value) <= UINT256_MAX;
|
|
108
|
+
}
|
|
109
|
+
function isValidDataAccessRecord(record) {
|
|
110
|
+
return isBytes32Hex(record.dataPointId) && isUint256Decimal(record.version, false) && isAddressHex(record.accessor) && isBytes32Hex(record.recordId) && /^0x[0-9a-fA-F]{130}$/.test(record.signature);
|
|
111
|
+
}
|
|
112
|
+
function parseLegacyGrantOperation(params) {
|
|
102
113
|
const { challengeGrantId, challengeOpId, challengeOpType, grantId } = params;
|
|
103
|
-
|
|
114
|
+
const opType = challengeOpType ?? import_escrow_payment.GRANT_OP_TYPE;
|
|
115
|
+
if (opType !== import_escrow_payment.GRANT_OP_TYPE) {
|
|
104
116
|
throw new import_errors.PersonalServerReadError(
|
|
105
117
|
"Personal Server payment challenge used an unsupported escrow op type",
|
|
106
118
|
402,
|
|
107
119
|
{ opType: challengeOpType }
|
|
108
120
|
);
|
|
109
121
|
}
|
|
110
|
-
const
|
|
111
|
-
if (!
|
|
112
|
-
if (!isBytes32Hex(challengedGrantId)) {
|
|
122
|
+
const opId = challengeOpId ?? challengeGrantId ?? grantId;
|
|
123
|
+
if (!opId || !isBytes32Hex(opId)) {
|
|
113
124
|
throw new import_errors.PersonalServerReadError(
|
|
114
125
|
"Personal Server payment challenge used an invalid escrow op id",
|
|
115
126
|
402,
|
|
116
|
-
{ opId
|
|
127
|
+
{ opId }
|
|
117
128
|
);
|
|
118
129
|
}
|
|
119
|
-
if (
|
|
130
|
+
if (opId.toLowerCase() !== grantId.toLowerCase()) {
|
|
120
131
|
throw new import_errors.PersonalServerReadError(
|
|
121
132
|
"Personal Server payment challenge did not match the requested grant",
|
|
122
133
|
402,
|
|
123
|
-
{ opId
|
|
134
|
+
{ opId, grantId }
|
|
124
135
|
);
|
|
125
136
|
}
|
|
137
|
+
return { opType: import_escrow_payment.GRANT_OP_TYPE, opId };
|
|
138
|
+
}
|
|
139
|
+
function hasDataAccessMarker(body) {
|
|
140
|
+
if (stringField(body, "opType") === import_escrow_payment.DATA_ACCESS_OP_TYPE) return true;
|
|
141
|
+
return Array.isArray(body.accepts) && body.accepts.some((value) => {
|
|
142
|
+
const accept = asRecord(value);
|
|
143
|
+
return stringField(asRecord(accept?.message), "opType") === import_escrow_payment.DATA_ACCESS_OP_TYPE;
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
function parseCanonicalDataAccessAccept(value) {
|
|
147
|
+
const accept = asRecord(value);
|
|
148
|
+
const message = asRecord(accept?.message);
|
|
149
|
+
if (stringField(accept, "scheme") !== "vana-escrow-grant" || stringField(message, "opType") !== import_escrow_payment.DATA_ACCESS_OP_TYPE) {
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
const network = stringField(accept, "network");
|
|
153
|
+
const payerAddress = stringField(message, "payerAddress");
|
|
154
|
+
const opId = stringField(message, "opId");
|
|
155
|
+
const asset = stringField(message, "asset");
|
|
156
|
+
const amount = stringField(message, "amount");
|
|
157
|
+
const paymentNonce = stringField(message, "paymentNonce");
|
|
158
|
+
const accessRecord = parseAccessRecord(accept?.accessRecord);
|
|
159
|
+
const acceptAsset = stringField(accept, "asset");
|
|
160
|
+
const acceptAmount = stringField(accept, "amount");
|
|
161
|
+
if (!network || !payerAddress || !isAddressHex(payerAddress) || !opId || !isBytes32Hex(opId) || !asset || !isAddressHex(asset) || !amount || !isUint256Decimal(amount, true) || !paymentNonce || !isUint256Decimal(paymentNonce, false) || !accessRecord || !isValidDataAccessRecord(accessRecord) || payerAddress.toLowerCase() !== accessRecord.accessor.toLowerCase() || opId.toLowerCase() !== accessRecord.recordId.toLowerCase() || !acceptAsset || acceptAsset.toLowerCase() !== asset.toLowerCase() || acceptAmount !== amount) {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
return {
|
|
165
|
+
opType: import_escrow_payment.DATA_ACCESS_OP_TYPE,
|
|
166
|
+
opId,
|
|
167
|
+
network,
|
|
168
|
+
paymentNonce,
|
|
169
|
+
accessRecord,
|
|
170
|
+
asset,
|
|
171
|
+
amount
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
function parseCanonicalDataAccessPayment(params) {
|
|
175
|
+
const { body, grantId, raw } = params;
|
|
176
|
+
if (body.x402Version !== 1 || body.error !== "PAYMENT_REQUIRED") {
|
|
177
|
+
throw new import_errors.PersonalServerReadError(
|
|
178
|
+
"Personal Server data-access payment response was not a canonical x402 challenge",
|
|
179
|
+
402
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
const operation = Array.isArray(body.accepts) ? body.accepts.map(parseCanonicalDataAccessAccept).find((candidate) => candidate !== null) : void 0;
|
|
183
|
+
if (!operation) {
|
|
184
|
+
throw new import_errors.PersonalServerReadError(
|
|
185
|
+
"Personal Server data-access payment challenge was untrusted or incomplete",
|
|
186
|
+
402
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
return { grantId, raw, ...operation };
|
|
190
|
+
}
|
|
191
|
+
function preferredLegacyAccept(body) {
|
|
192
|
+
if (!Array.isArray(body.accepts)) return void 0;
|
|
193
|
+
return body.accepts.map(asRecord).find((accept) => {
|
|
194
|
+
const message = asRecord(accept?.message);
|
|
195
|
+
return stringField(accept, "scheme") === "vana-escrow-grant" && stringField(message, "opType") === import_escrow_payment.GRANT_OP_TYPE;
|
|
196
|
+
});
|
|
126
197
|
}
|
|
127
198
|
async function buildPersonalServerDataReadRequest(params) {
|
|
128
199
|
const base = stripTrailingSlash(params.personalServerUrl);
|
|
@@ -148,32 +219,92 @@ async function parsePersonalServerPaymentRequired(res, grantId) {
|
|
|
148
219
|
raw = void 0;
|
|
149
220
|
}
|
|
150
221
|
const body = asRecord(raw) ?? {};
|
|
151
|
-
|
|
152
|
-
|
|
222
|
+
if (hasDataAccessMarker(body)) {
|
|
223
|
+
return parseCanonicalDataAccessPayment({ body, grantId, raw });
|
|
224
|
+
}
|
|
225
|
+
const hasAcceptsEnvelope = Object.prototype.hasOwnProperty.call(
|
|
226
|
+
body,
|
|
227
|
+
"accepts"
|
|
228
|
+
);
|
|
229
|
+
if (hasAcceptsEnvelope && (body.x402Version !== 1 || body.error !== "PAYMENT_REQUIRED")) {
|
|
230
|
+
throw new import_errors.PersonalServerReadError(
|
|
231
|
+
"Personal Server grant payment response was not a canonical x402 challenge",
|
|
232
|
+
402
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
const accept = preferredLegacyAccept(body);
|
|
236
|
+
if (hasAcceptsEnvelope && !accept) {
|
|
237
|
+
throw new import_errors.PersonalServerReadError(
|
|
238
|
+
"Personal Server grant payment challenge had no compatible escrow offer",
|
|
239
|
+
402
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
if (accept) {
|
|
243
|
+
const message = asRecord(accept.message);
|
|
244
|
+
const network = stringField(accept, "network");
|
|
245
|
+
const payerAddress = stringField(message, "payerAddress");
|
|
246
|
+
const opId = stringField(message, "opId");
|
|
247
|
+
const asset = stringField(message, "asset");
|
|
248
|
+
const amount = stringField(message, "amount");
|
|
249
|
+
const paymentNonce = stringField(message, "paymentNonce");
|
|
250
|
+
const acceptAsset = stringField(accept, "asset");
|
|
251
|
+
const acceptAmount = stringField(accept, "amount");
|
|
252
|
+
const accessRecord2 = parseAccessRecord(accept.accessRecord);
|
|
253
|
+
const hasAccessRecord = accept.accessRecord !== void 0;
|
|
254
|
+
if (!network || !payerAddress || !isAddressHex(payerAddress) || !opId || !isBytes32Hex(opId) || !asset || !isAddressHex(asset) || !amount || !isUint256Decimal(amount, true) || !paymentNonce || !isUint256Decimal(paymentNonce, false) || !acceptAsset || acceptAsset.toLowerCase() !== asset.toLowerCase() || acceptAmount !== amount || hasAccessRecord && (!accessRecord2 || !isValidDataAccessRecord(accessRecord2) || accessRecord2.accessor.toLowerCase() !== payerAddress.toLowerCase()) || amount === "0" && !accessRecord2) {
|
|
255
|
+
throw new import_errors.PersonalServerReadError(
|
|
256
|
+
"Personal Server grant payment challenge was untrusted or incomplete",
|
|
257
|
+
402
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
const operation2 = parseLegacyGrantOperation({
|
|
261
|
+
challengeOpId: opId,
|
|
262
|
+
challengeOpType: stringField(message, "opType"),
|
|
263
|
+
grantId
|
|
264
|
+
});
|
|
265
|
+
return {
|
|
266
|
+
grantId,
|
|
267
|
+
...operation2,
|
|
268
|
+
network,
|
|
269
|
+
paymentNonce,
|
|
270
|
+
asset,
|
|
271
|
+
amount,
|
|
272
|
+
raw,
|
|
273
|
+
...accessRecord2 ? { accessRecord: accessRecord2 } : {}
|
|
274
|
+
};
|
|
275
|
+
}
|
|
153
276
|
const challengeGrantId = stringField(body, "grantId");
|
|
154
|
-
const challengeOpId = stringField(
|
|
155
|
-
const challengeOpType = stringField(
|
|
156
|
-
|
|
277
|
+
const challengeOpId = stringField(body, "opId");
|
|
278
|
+
const challengeOpType = stringField(body, "opType");
|
|
279
|
+
const accessRecord = parseAccessRecord(body.accessRecord);
|
|
280
|
+
const operation = parseLegacyGrantOperation({
|
|
157
281
|
challengeGrantId,
|
|
158
282
|
challengeOpId,
|
|
159
283
|
challengeOpType,
|
|
160
284
|
grantId
|
|
161
285
|
});
|
|
162
|
-
const amountValue = stringField(
|
|
163
|
-
|
|
286
|
+
const amountValue = stringField(body, "amount") ?? stringField(body, "maxAmountRequired") ?? "0";
|
|
287
|
+
const payment = {
|
|
164
288
|
grantId,
|
|
165
|
-
network: stringField(
|
|
166
|
-
paymentNonce: stringField(
|
|
167
|
-
|
|
168
|
-
asset: stringField(message, "asset") ?? stringField(body, "asset") ?? import_escrow.NATIVE_ASSET_ADDRESS,
|
|
289
|
+
network: stringField(body, "network"),
|
|
290
|
+
paymentNonce: stringField(body, "paymentNonce"),
|
|
291
|
+
asset: stringField(body, "asset") ?? import_escrow.NATIVE_ASSET_ADDRESS,
|
|
169
292
|
amount: amountValue,
|
|
170
293
|
raw
|
|
171
294
|
};
|
|
295
|
+
return {
|
|
296
|
+
...payment,
|
|
297
|
+
...operation,
|
|
298
|
+
...accessRecord ? { accessRecord } : {}
|
|
299
|
+
};
|
|
172
300
|
}
|
|
173
301
|
function hasPositiveAmount(amount) {
|
|
174
302
|
if (!/^\d+$/.test(amount)) return false;
|
|
175
303
|
return BigInt(amount) > 0n;
|
|
176
304
|
}
|
|
305
|
+
function isReceiptOnlyGrantAcknowledgment(required, payerAddress) {
|
|
306
|
+
return required.opType === import_escrow_payment.GRANT_OP_TYPE && required.amount === "0" && required.accessRecord !== void 0 && isValidDataAccessRecord(required.accessRecord) && required.accessRecord.accessor.toLowerCase() === payerAddress.toLowerCase();
|
|
307
|
+
}
|
|
177
308
|
async function readPersonalServerData(params) {
|
|
178
309
|
const fetchFn = params.fetchFn ?? globalThis.fetch;
|
|
179
310
|
if (!fetchFn) {
|
|
@@ -213,9 +344,9 @@ async function readPersonalServerData(params) {
|
|
|
213
344
|
}
|
|
214
345
|
);
|
|
215
346
|
}
|
|
216
|
-
if (!hasPositiveAmount(required.amount)) {
|
|
347
|
+
if (required.opType === import_escrow_payment.GRANT_OP_TYPE && !hasPositiveAmount(required.amount) && !isReceiptOnlyGrantAcknowledgment(required, params.payerAddress)) {
|
|
217
348
|
throw new import_errors.PaymentRequiredError(
|
|
218
|
-
"Personal Server payment challenge
|
|
349
|
+
"Personal Server payment challenge included neither a positive amount nor a valid access receipt",
|
|
219
350
|
{
|
|
220
351
|
scope: params.scope,
|
|
221
352
|
grantId: required.grantId,
|
|
@@ -224,7 +355,7 @@ async function readPersonalServerData(params) {
|
|
|
224
355
|
}
|
|
225
356
|
);
|
|
226
357
|
}
|
|
227
|
-
const paymentHeader = await (0, import_escrow_payment.
|
|
358
|
+
const paymentHeader = await (0, import_escrow_payment.buildEscrowPaymentHeader)({
|
|
228
359
|
payerAddress: params.payerAddress,
|
|
229
360
|
required,
|
|
230
361
|
config: params.escrow
|
|
@@ -261,7 +392,9 @@ async function readPersonalServerData(params) {
|
|
|
261
392
|
{ scope: params.scope, body: detail.slice(0, 500) }
|
|
262
393
|
);
|
|
263
394
|
}
|
|
264
|
-
payment = (0, import_escrow_payment.
|
|
395
|
+
payment = (0, import_escrow_payment.paymentResponseMetadataFromHeader)(
|
|
396
|
+
res.headers.get("X-PAYMENT-RESPONSE")
|
|
397
|
+
);
|
|
265
398
|
return { data: await res.json(), payment };
|
|
266
399
|
}
|
|
267
400
|
// Annotate the CommonJS export names for ESM import in node:
|