@opendatalabs/vana-sdk 3.4.1 → 3.5.1-pr.159.2d90789
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 +116 -0
- package/dist/account/personal-server-lite-owner-binding.cjs +3 -3
- package/dist/account/personal-server-lite-owner-binding.cjs.map +1 -1
- package/dist/account/personal-server-lite-owner-binding.d.ts +2 -2
- package/dist/account/personal-server-lite-owner-binding.js +1 -1
- package/dist/account/personal-server-lite-owner-binding.js.map +1 -1
- package/dist/direct/access-request-client.cjs +104 -0
- package/dist/direct/access-request-client.cjs.map +1 -0
- package/dist/direct/access-request-client.d.ts +51 -0
- package/dist/direct/access-request-client.js +79 -0
- package/dist/direct/access-request-client.js.map +1 -0
- package/dist/direct/access-request-client.test.d.ts +1 -0
- package/dist/direct/connect-flow.cjs +152 -0
- package/dist/direct/connect-flow.cjs.map +1 -0
- package/dist/direct/connect-flow.d.ts +85 -0
- package/dist/direct/connect-flow.js +128 -0
- package/dist/direct/connect-flow.js.map +1 -0
- package/dist/direct/connect-flow.test.d.ts +1 -0
- package/dist/direct/controller.cjs +129 -0
- package/dist/direct/controller.cjs.map +1 -0
- package/dist/direct/controller.d.ts +152 -0
- package/dist/direct/controller.js +109 -0
- package/dist/direct/controller.js.map +1 -0
- package/dist/direct/controller.test.d.ts +1 -0
- package/dist/direct/endpoints.cjs +45 -0
- package/dist/direct/endpoints.cjs.map +1 -0
- package/dist/direct/endpoints.d.ts +22 -0
- package/dist/direct/endpoints.js +19 -0
- package/dist/direct/endpoints.js.map +1 -0
- package/dist/direct/errors.cjs +65 -0
- package/dist/direct/errors.cjs.map +1 -0
- package/dist/direct/errors.d.ts +44 -0
- package/dist/direct/errors.js +38 -0
- package/dist/direct/errors.js.map +1 -0
- package/dist/direct/escrow-payment.cjs +96 -0
- package/dist/direct/escrow-payment.cjs.map +1 -0
- package/dist/direct/escrow-payment.d.ts +81 -0
- package/dist/direct/escrow-payment.js +72 -0
- package/dist/direct/escrow-payment.js.map +1 -0
- package/dist/direct/escrow-payment.test.d.ts +1 -0
- package/dist/direct/personal-server-read.cjs +149 -0
- package/dist/direct/personal-server-read.cjs.map +1 -0
- package/dist/direct/personal-server-read.d.ts +103 -0
- package/dist/direct/personal-server-read.js +124 -0
- package/dist/direct/personal-server-read.js.map +1 -0
- package/dist/direct/personal-server-read.test.d.ts +1 -0
- package/dist/direct/types.cjs +35 -0
- package/dist/direct/types.cjs.map +1 -0
- package/dist/direct/types.d.ts +205 -0
- package/dist/direct/types.js +11 -0
- package/dist/direct/types.js.map +1 -0
- package/dist/direct/use-direct-vana-connect.cjs +68 -0
- package/dist/direct/use-direct-vana-connect.cjs.map +1 -0
- package/dist/direct/use-direct-vana-connect.d.ts +45 -0
- package/dist/direct/use-direct-vana-connect.js +46 -0
- package/dist/direct/use-direct-vana-connect.js.map +1 -0
- package/dist/index.browser.d.ts +9 -4
- package/dist/index.browser.js +644 -178
- package/dist/index.browser.js.map +4 -4
- package/dist/index.node.cjs +672 -183
- package/dist/index.node.cjs.map +4 -4
- package/dist/index.node.d.ts +9 -4
- package/dist/index.node.js +644 -178
- package/dist/index.node.js.map +4 -4
- package/dist/personal-server-lite/owner-binding.cjs +93 -0
- package/dist/personal-server-lite/owner-binding.cjs.map +1 -0
- package/dist/personal-server-lite/owner-binding.d.ts +44 -0
- package/dist/personal-server-lite/owner-binding.js +65 -0
- package/dist/personal-server-lite/owner-binding.js.map +1 -0
- package/dist/personal-server-lite/owner-binding.test.d.ts +1 -0
- package/dist/protocol/data-point-status.cjs +80 -0
- package/dist/protocol/data-point-status.cjs.map +1 -0
- package/dist/protocol/data-point-status.d.ts +34 -0
- package/dist/protocol/data-point-status.js +51 -0
- package/dist/protocol/data-point-status.js.map +1 -0
- package/dist/protocol/data-point-status.test.d.ts +1 -0
- package/dist/protocol/eip712.cjs +53 -31
- package/dist/protocol/eip712.cjs.map +1 -1
- package/dist/protocol/eip712.d.ts +98 -43
- package/dist/protocol/eip712.js +47 -27
- package/dist/protocol/eip712.js.map +1 -1
- package/dist/protocol/escrow-deposit.cjs +89 -0
- package/dist/protocol/escrow-deposit.cjs.map +1 -0
- package/dist/protocol/escrow-deposit.d.ts +47 -0
- package/dist/protocol/escrow-deposit.js +60 -0
- package/dist/protocol/escrow-deposit.js.map +1 -0
- package/dist/protocol/escrow-deposit.test.d.ts +1 -0
- package/dist/protocol/escrow-flow.test.d.ts +21 -0
- package/dist/protocol/escrow.cjs +146 -0
- package/dist/protocol/escrow.cjs.map +1 -0
- package/dist/protocol/escrow.d.ts +336 -0
- package/dist/protocol/escrow.js +118 -0
- package/dist/protocol/escrow.js.map +1 -0
- package/dist/protocol/escrow.test.d.ts +1 -0
- package/dist/protocol/fee-registry.cjs +116 -0
- package/dist/protocol/fee-registry.cjs.map +1 -0
- package/dist/protocol/fee-registry.d.ts +151 -0
- package/dist/protocol/fee-registry.js +89 -0
- package/dist/protocol/fee-registry.js.map +1 -0
- package/dist/protocol/fee-registry.test.d.ts +1 -0
- package/dist/protocol/gateway.cjs +107 -37
- package/dist/protocol/gateway.cjs.map +1 -1
- package/dist/protocol/gateway.d.ts +223 -57
- package/dist/protocol/gateway.js +107 -37
- package/dist/protocol/gateway.js.map +1 -1
- package/dist/protocol/grants.cjs +27 -64
- package/dist/protocol/grants.cjs.map +1 -1
- package/dist/protocol/grants.d.ts +6 -13
- package/dist/protocol/grants.js +27 -63
- package/dist/protocol/grants.js.map +1 -1
- package/dist/protocol/personal-server-data.cjs +71 -0
- package/dist/protocol/personal-server-data.cjs.map +1 -0
- package/dist/protocol/personal-server-data.d.ts +16 -0
- package/dist/protocol/personal-server-data.js +47 -0
- package/dist/protocol/personal-server-data.js.map +1 -0
- package/dist/protocol/personal-server-data.test.d.ts +1 -0
- package/dist/protocol/personal-server-registration.cjs +16 -4
- package/dist/protocol/personal-server-registration.cjs.map +1 -1
- package/dist/protocol/personal-server-registration.d.ts +5 -2
- package/dist/protocol/personal-server-registration.js +16 -4
- package/dist/protocol/personal-server-registration.js.map +1 -1
- package/dist/react.cjs +32 -0
- package/dist/react.cjs.map +1 -0
- package/dist/react.d.ts +33 -0
- package/dist/react.js +11 -0
- package/dist/react.js.map +1 -0
- package/dist/server.cjs +73 -0
- package/dist/server.cjs.map +1 -0
- package/dist/server.d.ts +32 -0
- package/dist/server.js +55 -0
- package/dist/server.js.map +1 -0
- package/dist/storage/providers/vana-storage.cjs +75 -17
- package/dist/storage/providers/vana-storage.cjs.map +1 -1
- package/dist/storage/providers/vana-storage.js +75 -17
- package/dist/storage/providers/vana-storage.js.map +1 -1
- package/package.json +20 -1
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import {
|
|
2
|
+
GENERIC_PAYMENT_TYPES,
|
|
3
|
+
NATIVE_ASSET_ADDRESS,
|
|
4
|
+
genericPaymentDomain
|
|
5
|
+
} from "../protocol/escrow.js";
|
|
6
|
+
const GRANT_OP_TYPE = "grant";
|
|
7
|
+
function toDirectFeeBreakdown(breakdown) {
|
|
8
|
+
return {
|
|
9
|
+
registrationFee: breakdown.registrationFee,
|
|
10
|
+
dataAccessFee: breakdown.dataAccessFee,
|
|
11
|
+
registrationPaid: breakdown.registrationPaid
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
function toDirectPaymentReceipt(result) {
|
|
15
|
+
return {
|
|
16
|
+
opType: result.opType,
|
|
17
|
+
opId: result.opId,
|
|
18
|
+
asset: result.asset,
|
|
19
|
+
amount: result.amount,
|
|
20
|
+
paymentNonce: result.paymentNonce,
|
|
21
|
+
breakdown: toDirectFeeBreakdown(result.breakdown),
|
|
22
|
+
paidAt: result.paidAt
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function createDefaultNonceSource() {
|
|
26
|
+
const counters = /* @__PURE__ */ new Map();
|
|
27
|
+
return (payerAddress) => {
|
|
28
|
+
const key = payerAddress.toLowerCase();
|
|
29
|
+
const next = (counters.get(key) ?? 0n) + 1n;
|
|
30
|
+
counters.set(key, next);
|
|
31
|
+
return next;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
async function authorizeGrantPayment(params) {
|
|
35
|
+
const { payerAddress, required, config } = params;
|
|
36
|
+
const nonceSource = config.nonceSource ?? createDefaultNonceSource();
|
|
37
|
+
const paymentNonce = BigInt(await nonceSource(payerAddress));
|
|
38
|
+
const asset = required.asset || NATIVE_ASSET_ADDRESS;
|
|
39
|
+
const opId = required.grantId;
|
|
40
|
+
const amount = BigInt(required.amount);
|
|
41
|
+
const signature = await config.signTypedData({
|
|
42
|
+
domain: genericPaymentDomain(config.chainId, config.escrowContract),
|
|
43
|
+
types: GENERIC_PAYMENT_TYPES,
|
|
44
|
+
primaryType: "GenericPayment",
|
|
45
|
+
message: {
|
|
46
|
+
payerAddress,
|
|
47
|
+
opType: GRANT_OP_TYPE,
|
|
48
|
+
opId,
|
|
49
|
+
asset,
|
|
50
|
+
amount,
|
|
51
|
+
paymentNonce
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
const result = await config.client.payForOp({
|
|
55
|
+
payerAddress,
|
|
56
|
+
opType: GRANT_OP_TYPE,
|
|
57
|
+
opId,
|
|
58
|
+
asset,
|
|
59
|
+
amount: amount.toString(),
|
|
60
|
+
paymentNonce: paymentNonce.toString(),
|
|
61
|
+
signature
|
|
62
|
+
});
|
|
63
|
+
return toDirectPaymentReceipt(result);
|
|
64
|
+
}
|
|
65
|
+
export {
|
|
66
|
+
GRANT_OP_TYPE,
|
|
67
|
+
authorizeGrantPayment,
|
|
68
|
+
createDefaultNonceSource,
|
|
69
|
+
toDirectFeeBreakdown,
|
|
70
|
+
toDirectPaymentReceipt
|
|
71
|
+
};
|
|
72
|
+
//# sourceMappingURL=escrow-payment.js.map
|
|
@@ -0,0 +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 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\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\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 nonceSource = config.nonceSource ?? createDefaultNonceSource();\n const paymentNonce = BigInt(await nonceSource(payerAddress));\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 signature = await config.signTypedData({\n domain: genericPaymentDomain(config.chainId, config.escrowContract),\n types: GENERIC_PAYMENT_TYPES,\n primaryType: \"GenericPayment\",\n message: {\n payerAddress,\n opType: GRANT_OP_TYPE,\n opId,\n asset,\n amount,\n paymentNonce,\n },\n });\n\n const result = await config.client.payForOp({\n payerAddress,\n opType: GRANT_OP_TYPE,\n opId,\n asset,\n amount: amount.toString(),\n paymentNonce: paymentNonce.toString(),\n signature,\n });\n\n return toDirectPaymentReceipt(result);\n}\n"],"mappings":"AAqBA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAIK;AAQA,MAAM,gBAAgB;AA+CtB,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;AASA,eAAsB,sBAAsB,QAIV;AAChC,QAAM,EAAE,cAAc,UAAU,OAAO,IAAI;AAC3C,QAAM,cAAc,OAAO,eAAe,yBAAyB;AACnE,QAAM,eAAe,OAAO,MAAM,YAAY,YAAY,CAAC;AAC3D,QAAM,QAAS,SAAS,SAAS;AACjC,QAAM,OAAO,SAAS;AACtB,QAAM,SAAS,OAAO,SAAS,MAAM;AAErC,QAAM,YAAY,MAAM,OAAO,cAAc;AAAA,IAC3C,QAAQ,qBAAqB,OAAO,SAAS,OAAO,cAAc;AAAA,IAClE,OAAO;AAAA,IACP,aAAa;AAAA,IACb,SAAS;AAAA,MACP;AAAA,MACA,QAAQ;AAAA,MACR;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,EACF,CAAC;AAED,QAAM,SAAS,MAAM,OAAO,OAAO,SAAS;AAAA,IAC1C;AAAA,IACA,QAAQ;AAAA,IACR;AAAA,IACA;AAAA,IACA,QAAQ,OAAO,SAAS;AAAA,IACxB,cAAc,aAAa,SAAS;AAAA,IACpC;AAAA,EACF,CAAC;AAED,SAAO,uBAAuB,MAAM;AACtC;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var personal_server_read_exports = {};
|
|
20
|
+
__export(personal_server_read_exports, {
|
|
21
|
+
buildPersonalServerDataReadRequest: () => buildPersonalServerDataReadRequest,
|
|
22
|
+
dataPathForScope: () => dataPathForScope,
|
|
23
|
+
parsePersonalServerPaymentRequired: () => parsePersonalServerPaymentRequired,
|
|
24
|
+
readPersonalServerData: () => readPersonalServerData
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(personal_server_read_exports);
|
|
27
|
+
var import_web3_signed_builder = require("../auth/web3-signed-builder");
|
|
28
|
+
var import_escrow = require("../protocol/escrow");
|
|
29
|
+
var import_escrow_payment = require("./escrow-payment");
|
|
30
|
+
var import_errors = require("./errors");
|
|
31
|
+
function stripTrailingSlash(url) {
|
|
32
|
+
return url.replace(/\/+$/, "");
|
|
33
|
+
}
|
|
34
|
+
function dataPathForScope(scope) {
|
|
35
|
+
return `/v1/data/${encodeURIComponent(scope)}`;
|
|
36
|
+
}
|
|
37
|
+
async function buildPersonalServerDataReadRequest(params) {
|
|
38
|
+
const base = stripTrailingSlash(params.personalServerUrl);
|
|
39
|
+
const path = dataPathForScope(params.scope);
|
|
40
|
+
const authorization = await (0, import_web3_signed_builder.buildWeb3SignedHeader)({
|
|
41
|
+
signMessage: params.signMessage,
|
|
42
|
+
aud: base,
|
|
43
|
+
method: "GET",
|
|
44
|
+
uri: path,
|
|
45
|
+
grantId: params.grantId
|
|
46
|
+
});
|
|
47
|
+
const headers = {
|
|
48
|
+
Authorization: authorization,
|
|
49
|
+
Accept: "application/json"
|
|
50
|
+
};
|
|
51
|
+
return { url: `${base}${path}`, method: "GET", path, headers };
|
|
52
|
+
}
|
|
53
|
+
async function parsePersonalServerPaymentRequired(res, grantId) {
|
|
54
|
+
let raw = void 0;
|
|
55
|
+
try {
|
|
56
|
+
raw = await res.json();
|
|
57
|
+
} catch {
|
|
58
|
+
raw = void 0;
|
|
59
|
+
}
|
|
60
|
+
const body = raw ?? {};
|
|
61
|
+
const resolvedGrantId = typeof body.grantId === "string" ? body.grantId : typeof body.opId === "string" ? body.opId : grantId;
|
|
62
|
+
const amountValue = typeof body.amount === "string" ? body.amount : typeof body.maxAmountRequired === "string" ? body.maxAmountRequired : "0";
|
|
63
|
+
return {
|
|
64
|
+
grantId: resolvedGrantId,
|
|
65
|
+
asset: typeof body.asset === "string" ? body.asset : import_escrow.NATIVE_ASSET_ADDRESS,
|
|
66
|
+
amount: amountValue,
|
|
67
|
+
raw
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
async function readPersonalServerData(params) {
|
|
71
|
+
const fetchFn = params.fetchFn ?? globalThis.fetch;
|
|
72
|
+
if (!fetchFn) {
|
|
73
|
+
throw new import_errors.PersonalServerReadError(
|
|
74
|
+
"No fetch implementation available for Personal Server read"
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
const initial = await buildPersonalServerDataReadRequest({
|
|
78
|
+
personalServerUrl: params.personalServerUrl,
|
|
79
|
+
scope: params.scope,
|
|
80
|
+
grantId: params.grantId,
|
|
81
|
+
signMessage: params.signMessage
|
|
82
|
+
});
|
|
83
|
+
let res = await fetchFn(initial.url, {
|
|
84
|
+
method: initial.method,
|
|
85
|
+
headers: initial.headers
|
|
86
|
+
});
|
|
87
|
+
let payment;
|
|
88
|
+
if (res.status === 402) {
|
|
89
|
+
const required = await parsePersonalServerPaymentRequired(
|
|
90
|
+
res,
|
|
91
|
+
params.grantId
|
|
92
|
+
);
|
|
93
|
+
if (!params.escrow) {
|
|
94
|
+
throw new import_errors.PaymentRequiredError(
|
|
95
|
+
"Personal Server requires payment but no escrow config is set",
|
|
96
|
+
{
|
|
97
|
+
scope: params.scope,
|
|
98
|
+
grantId: required.grantId,
|
|
99
|
+
asset: required.asset,
|
|
100
|
+
amount: required.amount
|
|
101
|
+
}
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
payment = await (0, import_escrow_payment.authorizeGrantPayment)({
|
|
105
|
+
payerAddress: params.payerAddress,
|
|
106
|
+
required,
|
|
107
|
+
config: params.escrow
|
|
108
|
+
});
|
|
109
|
+
const retry = await buildPersonalServerDataReadRequest({
|
|
110
|
+
personalServerUrl: params.personalServerUrl,
|
|
111
|
+
scope: params.scope,
|
|
112
|
+
grantId: params.grantId,
|
|
113
|
+
signMessage: params.signMessage
|
|
114
|
+
});
|
|
115
|
+
res = await fetchFn(retry.url, {
|
|
116
|
+
method: retry.method,
|
|
117
|
+
headers: retry.headers
|
|
118
|
+
});
|
|
119
|
+
if (res.status === 402) {
|
|
120
|
+
throw new import_errors.PaymentRequiredError(
|
|
121
|
+
"Personal Server still requires payment after escrow settlement",
|
|
122
|
+
{
|
|
123
|
+
scope: params.scope,
|
|
124
|
+
grantId: required.grantId,
|
|
125
|
+
asset: required.asset,
|
|
126
|
+
amount: required.amount,
|
|
127
|
+
payment
|
|
128
|
+
}
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (!res.ok) {
|
|
133
|
+
const detail = await res.text().catch(() => "");
|
|
134
|
+
throw new import_errors.PersonalServerReadError(
|
|
135
|
+
`Personal Server read failed: ${res.status} ${res.statusText}`,
|
|
136
|
+
res.status,
|
|
137
|
+
{ scope: params.scope, body: detail.slice(0, 500) }
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
return { data: await res.json(), payment };
|
|
141
|
+
}
|
|
142
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
143
|
+
0 && (module.exports = {
|
|
144
|
+
buildPersonalServerDataReadRequest,
|
|
145
|
+
dataPathForScope,
|
|
146
|
+
parsePersonalServerPaymentRequired,
|
|
147
|
+
readPersonalServerData
|
|
148
|
+
});
|
|
149
|
+
//# sourceMappingURL=personal-server-read.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/direct/personal-server-read.ts"],"sourcesContent":["/**\n * Personal Server data-read request builder and the 402 -> escrow-pay -> retry loop.\n *\n * @remarks\n * The read targets the Personal Server data path (`/v1/data/{scope}`),\n * authenticates with a Web3Signed header (built on {@link buildWeb3SignedHeader}),\n * and — on `402 Payment Required` — settles the grant's data-access fee through\n * the DPv2 escrow gateway and retries once.\n *\n * The 402 body is parsed into a {@link PersonalServerPaymentRequired} (grant id,\n * asset, and amount owed), which drives the escrow settlement.\n *\n * @category Direct\n * @module direct/personal-server-read\n */\n\nimport { buildWeb3SignedHeader } from \"../auth/web3-signed-builder\";\nimport type { Web3SignedSignFn } from \"../auth/web3-signed-builder\";\nimport { NATIVE_ASSET_ADDRESS } from \"../protocol/escrow\";\nimport {\n authorizeGrantPayment,\n type EscrowPaymentConfig,\n} from \"./escrow-payment\";\nimport { PaymentRequiredError, PersonalServerReadError } from \"./errors\";\nimport type {\n DirectPaymentReceipt,\n PersonalServerPaymentRequired,\n} from \"./types\";\n\n/** Minimal `Response`-like shape so the read loop is testable without a DOM. */\nexport interface FetchResponseLike {\n ok: boolean;\n status: number;\n statusText: string;\n headers: { get(name: string): string | null };\n json(): Promise<unknown>;\n text(): Promise<string>;\n}\n\n/** Minimal `fetch` signature accepted by {@link readPersonalServerData}. */\nexport type PersonalServerFetch = (\n input: string,\n init: {\n method: string;\n headers: Record<string, string>;\n },\n) => Promise<FetchResponseLike>;\n\n/** A built, ready-to-send Personal Server data read request. */\nexport interface PersonalServerDataReadRequest {\n /** Absolute URL of the read endpoint. */\n url: string;\n /** HTTP method (always `\"GET\"`). */\n method: \"GET\";\n /** Request path used in the Web3Signed `uri` claim (e.g. `/v1/data/{scope}`). */\n path: string;\n /** Headers including the Web3Signed `Authorization` value. */\n headers: Record<string, string>;\n}\n\n/** Outcome of {@link readPersonalServerData}: the payload plus optional receipt. */\nexport interface PersonalServerReadResult {\n /** The decoded JSON payload returned by the Personal Server. */\n data: unknown;\n /** Present only when this read required (and settled) a payment. */\n payment?: DirectPaymentReceipt;\n}\n\nfunction stripTrailingSlash(url: string): string {\n return url.replace(/\\/+$/, \"\");\n}\n\n/** Compute the data path for a scope (`/v1/data/{scope}`). */\nexport function dataPathForScope(scope: string): string {\n return `/v1/data/${encodeURIComponent(scope)}`;\n}\n\n/**\n * Build a Web3Signed-authenticated Personal Server data read request.\n *\n * @param params - Personal Server URL, scope, grant id, and an EIP-191 signer.\n * @returns The request URL, method, path, and headers (including `Authorization`).\n */\nexport async function buildPersonalServerDataReadRequest(params: {\n /** Base URL of the user's Personal Server. */\n personalServerUrl: string;\n /** Scope to read (e.g. `\"icloud_notes.notes\"`). */\n scope: string;\n /** Grant id authorizing the read. */\n grantId: string;\n /** EIP-191 signer for the Web3Signed header (the app key). */\n signMessage: Web3SignedSignFn;\n}): Promise<PersonalServerDataReadRequest> {\n const base = stripTrailingSlash(params.personalServerUrl);\n const path = dataPathForScope(params.scope);\n const authorization = await buildWeb3SignedHeader({\n signMessage: params.signMessage,\n aud: base,\n method: \"GET\",\n uri: path,\n grantId: params.grantId,\n });\n const headers: Record<string, string> = {\n Authorization: authorization,\n Accept: \"application/json\",\n };\n return { url: `${base}${path}`, method: \"GET\", path, headers };\n}\n\n/**\n * Parse a `402 Payment Required` body into a {@link PersonalServerPaymentRequired}.\n *\n * @remarks\n * Accepts a few field spellings and falls back to the read's own grantId and the\n * native asset when a field is absent.\n *\n * @param res - The 402 response.\n * @param grantId - The grant id of the read (default `opId`).\n * @returns The parsed payment requirement.\n */\nexport async function parsePersonalServerPaymentRequired(\n res: FetchResponseLike,\n grantId: string,\n): Promise<PersonalServerPaymentRequired> {\n let raw: unknown = undefined;\n try {\n raw = await res.json();\n } catch {\n raw = undefined;\n }\n const body = (raw ?? {}) as {\n grantId?: unknown;\n opId?: unknown;\n asset?: unknown;\n amount?: unknown;\n maxAmountRequired?: unknown;\n };\n const resolvedGrantId =\n typeof body.grantId === \"string\"\n ? body.grantId\n : typeof body.opId === \"string\"\n ? body.opId\n : grantId;\n const amountValue =\n typeof body.amount === \"string\"\n ? body.amount\n : typeof body.maxAmountRequired === \"string\"\n ? body.maxAmountRequired\n : \"0\";\n return {\n grantId: resolvedGrantId,\n asset: typeof body.asset === \"string\" ? body.asset : NATIVE_ASSET_ADDRESS,\n amount: amountValue,\n raw,\n };\n}\n\n/**\n * Read approved data from a Personal Server, settling a 402 via escrow.\n *\n * @remarks\n * Sends a Web3Signed-authenticated `GET /v1/data/{scope}`. On `402`, parses what\n * is owed, authorizes an escrow payment for the grant via `escrow`, and retries\n * once. If escrow is not configured, throws {@link PaymentRequiredError} carrying\n * the parsed requirement so callers can debug amount/asset.\n *\n * @param params - Connection details, app signer, optional escrow config and fetch.\n * @returns `{ data, payment? }`.\n */\nexport async function readPersonalServerData(params: {\n personalServerUrl: string;\n scope: string;\n grantId: string;\n payerAddress: `0x${string}`;\n signMessage: Web3SignedSignFn;\n escrow?: EscrowPaymentConfig;\n fetchFn?: PersonalServerFetch;\n}): Promise<PersonalServerReadResult> {\n const fetchFn =\n params.fetchFn ?? (globalThis.fetch as unknown as PersonalServerFetch);\n if (!fetchFn) {\n throw new PersonalServerReadError(\n \"No fetch implementation available for Personal Server read\",\n );\n }\n\n const initial = await buildPersonalServerDataReadRequest({\n personalServerUrl: params.personalServerUrl,\n scope: params.scope,\n grantId: params.grantId,\n signMessage: params.signMessage,\n });\n\n let res = await fetchFn(initial.url, {\n method: initial.method,\n headers: initial.headers,\n });\n\n let payment: DirectPaymentReceipt | undefined;\n\n if (res.status === 402) {\n const required = await parsePersonalServerPaymentRequired(\n res,\n params.grantId,\n );\n if (!params.escrow) {\n throw new PaymentRequiredError(\n \"Personal Server requires payment but no escrow config is set\",\n {\n scope: params.scope,\n grantId: required.grantId,\n asset: required.asset,\n amount: required.amount,\n },\n );\n }\n\n payment = await authorizeGrantPayment({\n payerAddress: params.payerAddress,\n required,\n config: params.escrow,\n });\n\n // Re-sign and retry — the settled payment unlocks the read.\n const retry = await buildPersonalServerDataReadRequest({\n personalServerUrl: params.personalServerUrl,\n scope: params.scope,\n grantId: params.grantId,\n signMessage: params.signMessage,\n });\n res = await fetchFn(retry.url, {\n method: retry.method,\n headers: retry.headers,\n });\n\n if (res.status === 402) {\n throw new PaymentRequiredError(\n \"Personal Server still requires payment after escrow settlement\",\n {\n scope: params.scope,\n grantId: required.grantId,\n asset: required.asset,\n amount: required.amount,\n payment,\n },\n );\n }\n }\n\n if (!res.ok) {\n const detail = await res.text().catch(() => \"\");\n throw new PersonalServerReadError(\n `Personal Server read failed: ${res.status} ${res.statusText}`,\n res.status,\n { scope: params.scope, body: detail.slice(0, 500) },\n );\n }\n\n return { data: await res.json(), payment };\n}\n"],"mappings":"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","names":[]}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Personal Server data-read request builder and the 402 -> escrow-pay -> retry loop.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* The read targets the Personal Server data path (`/v1/data/{scope}`),
|
|
6
|
+
* authenticates with a Web3Signed header (built on {@link buildWeb3SignedHeader}),
|
|
7
|
+
* and — on `402 Payment Required` — settles the grant's data-access fee through
|
|
8
|
+
* the DPv2 escrow gateway and retries once.
|
|
9
|
+
*
|
|
10
|
+
* The 402 body is parsed into a {@link PersonalServerPaymentRequired} (grant id,
|
|
11
|
+
* asset, and amount owed), which drives the escrow settlement.
|
|
12
|
+
*
|
|
13
|
+
* @category Direct
|
|
14
|
+
* @module direct/personal-server-read
|
|
15
|
+
*/
|
|
16
|
+
import type { Web3SignedSignFn } from "../auth/web3-signed-builder";
|
|
17
|
+
import { type EscrowPaymentConfig } from "./escrow-payment";
|
|
18
|
+
import type { DirectPaymentReceipt, PersonalServerPaymentRequired } from "./types";
|
|
19
|
+
/** Minimal `Response`-like shape so the read loop is testable without a DOM. */
|
|
20
|
+
export interface FetchResponseLike {
|
|
21
|
+
ok: boolean;
|
|
22
|
+
status: number;
|
|
23
|
+
statusText: string;
|
|
24
|
+
headers: {
|
|
25
|
+
get(name: string): string | null;
|
|
26
|
+
};
|
|
27
|
+
json(): Promise<unknown>;
|
|
28
|
+
text(): Promise<string>;
|
|
29
|
+
}
|
|
30
|
+
/** Minimal `fetch` signature accepted by {@link readPersonalServerData}. */
|
|
31
|
+
export type PersonalServerFetch = (input: string, init: {
|
|
32
|
+
method: string;
|
|
33
|
+
headers: Record<string, string>;
|
|
34
|
+
}) => Promise<FetchResponseLike>;
|
|
35
|
+
/** A built, ready-to-send Personal Server data read request. */
|
|
36
|
+
export interface PersonalServerDataReadRequest {
|
|
37
|
+
/** Absolute URL of the read endpoint. */
|
|
38
|
+
url: string;
|
|
39
|
+
/** HTTP method (always `"GET"`). */
|
|
40
|
+
method: "GET";
|
|
41
|
+
/** Request path used in the Web3Signed `uri` claim (e.g. `/v1/data/{scope}`). */
|
|
42
|
+
path: string;
|
|
43
|
+
/** Headers including the Web3Signed `Authorization` value. */
|
|
44
|
+
headers: Record<string, string>;
|
|
45
|
+
}
|
|
46
|
+
/** Outcome of {@link readPersonalServerData}: the payload plus optional receipt. */
|
|
47
|
+
export interface PersonalServerReadResult {
|
|
48
|
+
/** The decoded JSON payload returned by the Personal Server. */
|
|
49
|
+
data: unknown;
|
|
50
|
+
/** Present only when this read required (and settled) a payment. */
|
|
51
|
+
payment?: DirectPaymentReceipt;
|
|
52
|
+
}
|
|
53
|
+
/** Compute the data path for a scope (`/v1/data/{scope}`). */
|
|
54
|
+
export declare function dataPathForScope(scope: string): string;
|
|
55
|
+
/**
|
|
56
|
+
* Build a Web3Signed-authenticated Personal Server data read request.
|
|
57
|
+
*
|
|
58
|
+
* @param params - Personal Server URL, scope, grant id, and an EIP-191 signer.
|
|
59
|
+
* @returns The request URL, method, path, and headers (including `Authorization`).
|
|
60
|
+
*/
|
|
61
|
+
export declare function buildPersonalServerDataReadRequest(params: {
|
|
62
|
+
/** Base URL of the user's Personal Server. */
|
|
63
|
+
personalServerUrl: string;
|
|
64
|
+
/** Scope to read (e.g. `"icloud_notes.notes"`). */
|
|
65
|
+
scope: string;
|
|
66
|
+
/** Grant id authorizing the read. */
|
|
67
|
+
grantId: string;
|
|
68
|
+
/** EIP-191 signer for the Web3Signed header (the app key). */
|
|
69
|
+
signMessage: Web3SignedSignFn;
|
|
70
|
+
}): Promise<PersonalServerDataReadRequest>;
|
|
71
|
+
/**
|
|
72
|
+
* Parse a `402 Payment Required` body into a {@link PersonalServerPaymentRequired}.
|
|
73
|
+
*
|
|
74
|
+
* @remarks
|
|
75
|
+
* Accepts a few field spellings and falls back to the read's own grantId and the
|
|
76
|
+
* native asset when a field is absent.
|
|
77
|
+
*
|
|
78
|
+
* @param res - The 402 response.
|
|
79
|
+
* @param grantId - The grant id of the read (default `opId`).
|
|
80
|
+
* @returns The parsed payment requirement.
|
|
81
|
+
*/
|
|
82
|
+
export declare function parsePersonalServerPaymentRequired(res: FetchResponseLike, grantId: string): Promise<PersonalServerPaymentRequired>;
|
|
83
|
+
/**
|
|
84
|
+
* Read approved data from a Personal Server, settling a 402 via escrow.
|
|
85
|
+
*
|
|
86
|
+
* @remarks
|
|
87
|
+
* Sends a Web3Signed-authenticated `GET /v1/data/{scope}`. On `402`, parses what
|
|
88
|
+
* is owed, authorizes an escrow payment for the grant via `escrow`, and retries
|
|
89
|
+
* once. If escrow is not configured, throws {@link PaymentRequiredError} carrying
|
|
90
|
+
* the parsed requirement so callers can debug amount/asset.
|
|
91
|
+
*
|
|
92
|
+
* @param params - Connection details, app signer, optional escrow config and fetch.
|
|
93
|
+
* @returns `{ data, payment? }`.
|
|
94
|
+
*/
|
|
95
|
+
export declare function readPersonalServerData(params: {
|
|
96
|
+
personalServerUrl: string;
|
|
97
|
+
scope: string;
|
|
98
|
+
grantId: string;
|
|
99
|
+
payerAddress: `0x${string}`;
|
|
100
|
+
signMessage: Web3SignedSignFn;
|
|
101
|
+
escrow?: EscrowPaymentConfig;
|
|
102
|
+
fetchFn?: PersonalServerFetch;
|
|
103
|
+
}): Promise<PersonalServerReadResult>;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { buildWeb3SignedHeader } from "../auth/web3-signed-builder.js";
|
|
2
|
+
import { NATIVE_ASSET_ADDRESS } from "../protocol/escrow.js";
|
|
3
|
+
import {
|
|
4
|
+
authorizeGrantPayment
|
|
5
|
+
} from "./escrow-payment.js";
|
|
6
|
+
import { PaymentRequiredError, PersonalServerReadError } from "./errors.js";
|
|
7
|
+
function stripTrailingSlash(url) {
|
|
8
|
+
return url.replace(/\/+$/, "");
|
|
9
|
+
}
|
|
10
|
+
function dataPathForScope(scope) {
|
|
11
|
+
return `/v1/data/${encodeURIComponent(scope)}`;
|
|
12
|
+
}
|
|
13
|
+
async function buildPersonalServerDataReadRequest(params) {
|
|
14
|
+
const base = stripTrailingSlash(params.personalServerUrl);
|
|
15
|
+
const path = dataPathForScope(params.scope);
|
|
16
|
+
const authorization = await buildWeb3SignedHeader({
|
|
17
|
+
signMessage: params.signMessage,
|
|
18
|
+
aud: base,
|
|
19
|
+
method: "GET",
|
|
20
|
+
uri: path,
|
|
21
|
+
grantId: params.grantId
|
|
22
|
+
});
|
|
23
|
+
const headers = {
|
|
24
|
+
Authorization: authorization,
|
|
25
|
+
Accept: "application/json"
|
|
26
|
+
};
|
|
27
|
+
return { url: `${base}${path}`, method: "GET", path, headers };
|
|
28
|
+
}
|
|
29
|
+
async function parsePersonalServerPaymentRequired(res, grantId) {
|
|
30
|
+
let raw = void 0;
|
|
31
|
+
try {
|
|
32
|
+
raw = await res.json();
|
|
33
|
+
} catch {
|
|
34
|
+
raw = void 0;
|
|
35
|
+
}
|
|
36
|
+
const body = raw ?? {};
|
|
37
|
+
const resolvedGrantId = typeof body.grantId === "string" ? body.grantId : typeof body.opId === "string" ? body.opId : grantId;
|
|
38
|
+
const amountValue = typeof body.amount === "string" ? body.amount : typeof body.maxAmountRequired === "string" ? body.maxAmountRequired : "0";
|
|
39
|
+
return {
|
|
40
|
+
grantId: resolvedGrantId,
|
|
41
|
+
asset: typeof body.asset === "string" ? body.asset : NATIVE_ASSET_ADDRESS,
|
|
42
|
+
amount: amountValue,
|
|
43
|
+
raw
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
async function readPersonalServerData(params) {
|
|
47
|
+
const fetchFn = params.fetchFn ?? globalThis.fetch;
|
|
48
|
+
if (!fetchFn) {
|
|
49
|
+
throw new PersonalServerReadError(
|
|
50
|
+
"No fetch implementation available for Personal Server read"
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
const initial = await buildPersonalServerDataReadRequest({
|
|
54
|
+
personalServerUrl: params.personalServerUrl,
|
|
55
|
+
scope: params.scope,
|
|
56
|
+
grantId: params.grantId,
|
|
57
|
+
signMessage: params.signMessage
|
|
58
|
+
});
|
|
59
|
+
let res = await fetchFn(initial.url, {
|
|
60
|
+
method: initial.method,
|
|
61
|
+
headers: initial.headers
|
|
62
|
+
});
|
|
63
|
+
let payment;
|
|
64
|
+
if (res.status === 402) {
|
|
65
|
+
const required = await parsePersonalServerPaymentRequired(
|
|
66
|
+
res,
|
|
67
|
+
params.grantId
|
|
68
|
+
);
|
|
69
|
+
if (!params.escrow) {
|
|
70
|
+
throw new PaymentRequiredError(
|
|
71
|
+
"Personal Server requires payment but no escrow config is set",
|
|
72
|
+
{
|
|
73
|
+
scope: params.scope,
|
|
74
|
+
grantId: required.grantId,
|
|
75
|
+
asset: required.asset,
|
|
76
|
+
amount: required.amount
|
|
77
|
+
}
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
payment = await authorizeGrantPayment({
|
|
81
|
+
payerAddress: params.payerAddress,
|
|
82
|
+
required,
|
|
83
|
+
config: params.escrow
|
|
84
|
+
});
|
|
85
|
+
const retry = await buildPersonalServerDataReadRequest({
|
|
86
|
+
personalServerUrl: params.personalServerUrl,
|
|
87
|
+
scope: params.scope,
|
|
88
|
+
grantId: params.grantId,
|
|
89
|
+
signMessage: params.signMessage
|
|
90
|
+
});
|
|
91
|
+
res = await fetchFn(retry.url, {
|
|
92
|
+
method: retry.method,
|
|
93
|
+
headers: retry.headers
|
|
94
|
+
});
|
|
95
|
+
if (res.status === 402) {
|
|
96
|
+
throw new PaymentRequiredError(
|
|
97
|
+
"Personal Server still requires payment after escrow settlement",
|
|
98
|
+
{
|
|
99
|
+
scope: params.scope,
|
|
100
|
+
grantId: required.grantId,
|
|
101
|
+
asset: required.asset,
|
|
102
|
+
amount: required.amount,
|
|
103
|
+
payment
|
|
104
|
+
}
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (!res.ok) {
|
|
109
|
+
const detail = await res.text().catch(() => "");
|
|
110
|
+
throw new PersonalServerReadError(
|
|
111
|
+
`Personal Server read failed: ${res.status} ${res.statusText}`,
|
|
112
|
+
res.status,
|
|
113
|
+
{ scope: params.scope, body: detail.slice(0, 500) }
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
return { data: await res.json(), payment };
|
|
117
|
+
}
|
|
118
|
+
export {
|
|
119
|
+
buildPersonalServerDataReadRequest,
|
|
120
|
+
dataPathForScope,
|
|
121
|
+
parsePersonalServerPaymentRequired,
|
|
122
|
+
readPersonalServerData
|
|
123
|
+
};
|
|
124
|
+
//# sourceMappingURL=personal-server-read.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/direct/personal-server-read.ts"],"sourcesContent":["/**\n * Personal Server data-read request builder and the 402 -> escrow-pay -> retry loop.\n *\n * @remarks\n * The read targets the Personal Server data path (`/v1/data/{scope}`),\n * authenticates with a Web3Signed header (built on {@link buildWeb3SignedHeader}),\n * and — on `402 Payment Required` — settles the grant's data-access fee through\n * the DPv2 escrow gateway and retries once.\n *\n * The 402 body is parsed into a {@link PersonalServerPaymentRequired} (grant id,\n * asset, and amount owed), which drives the escrow settlement.\n *\n * @category Direct\n * @module direct/personal-server-read\n */\n\nimport { buildWeb3SignedHeader } from \"../auth/web3-signed-builder\";\nimport type { Web3SignedSignFn } from \"../auth/web3-signed-builder\";\nimport { NATIVE_ASSET_ADDRESS } from \"../protocol/escrow\";\nimport {\n authorizeGrantPayment,\n type EscrowPaymentConfig,\n} from \"./escrow-payment\";\nimport { PaymentRequiredError, PersonalServerReadError } from \"./errors\";\nimport type {\n DirectPaymentReceipt,\n PersonalServerPaymentRequired,\n} from \"./types\";\n\n/** Minimal `Response`-like shape so the read loop is testable without a DOM. */\nexport interface FetchResponseLike {\n ok: boolean;\n status: number;\n statusText: string;\n headers: { get(name: string): string | null };\n json(): Promise<unknown>;\n text(): Promise<string>;\n}\n\n/** Minimal `fetch` signature accepted by {@link readPersonalServerData}. */\nexport type PersonalServerFetch = (\n input: string,\n init: {\n method: string;\n headers: Record<string, string>;\n },\n) => Promise<FetchResponseLike>;\n\n/** A built, ready-to-send Personal Server data read request. */\nexport interface PersonalServerDataReadRequest {\n /** Absolute URL of the read endpoint. */\n url: string;\n /** HTTP method (always `\"GET\"`). */\n method: \"GET\";\n /** Request path used in the Web3Signed `uri` claim (e.g. `/v1/data/{scope}`). */\n path: string;\n /** Headers including the Web3Signed `Authorization` value. */\n headers: Record<string, string>;\n}\n\n/** Outcome of {@link readPersonalServerData}: the payload plus optional receipt. */\nexport interface PersonalServerReadResult {\n /** The decoded JSON payload returned by the Personal Server. */\n data: unknown;\n /** Present only when this read required (and settled) a payment. */\n payment?: DirectPaymentReceipt;\n}\n\nfunction stripTrailingSlash(url: string): string {\n return url.replace(/\\/+$/, \"\");\n}\n\n/** Compute the data path for a scope (`/v1/data/{scope}`). */\nexport function dataPathForScope(scope: string): string {\n return `/v1/data/${encodeURIComponent(scope)}`;\n}\n\n/**\n * Build a Web3Signed-authenticated Personal Server data read request.\n *\n * @param params - Personal Server URL, scope, grant id, and an EIP-191 signer.\n * @returns The request URL, method, path, and headers (including `Authorization`).\n */\nexport async function buildPersonalServerDataReadRequest(params: {\n /** Base URL of the user's Personal Server. */\n personalServerUrl: string;\n /** Scope to read (e.g. `\"icloud_notes.notes\"`). */\n scope: string;\n /** Grant id authorizing the read. */\n grantId: string;\n /** EIP-191 signer for the Web3Signed header (the app key). */\n signMessage: Web3SignedSignFn;\n}): Promise<PersonalServerDataReadRequest> {\n const base = stripTrailingSlash(params.personalServerUrl);\n const path = dataPathForScope(params.scope);\n const authorization = await buildWeb3SignedHeader({\n signMessage: params.signMessage,\n aud: base,\n method: \"GET\",\n uri: path,\n grantId: params.grantId,\n });\n const headers: Record<string, string> = {\n Authorization: authorization,\n Accept: \"application/json\",\n };\n return { url: `${base}${path}`, method: \"GET\", path, headers };\n}\n\n/**\n * Parse a `402 Payment Required` body into a {@link PersonalServerPaymentRequired}.\n *\n * @remarks\n * Accepts a few field spellings and falls back to the read's own grantId and the\n * native asset when a field is absent.\n *\n * @param res - The 402 response.\n * @param grantId - The grant id of the read (default `opId`).\n * @returns The parsed payment requirement.\n */\nexport async function parsePersonalServerPaymentRequired(\n res: FetchResponseLike,\n grantId: string,\n): Promise<PersonalServerPaymentRequired> {\n let raw: unknown = undefined;\n try {\n raw = await res.json();\n } catch {\n raw = undefined;\n }\n const body = (raw ?? {}) as {\n grantId?: unknown;\n opId?: unknown;\n asset?: unknown;\n amount?: unknown;\n maxAmountRequired?: unknown;\n };\n const resolvedGrantId =\n typeof body.grantId === \"string\"\n ? body.grantId\n : typeof body.opId === \"string\"\n ? body.opId\n : grantId;\n const amountValue =\n typeof body.amount === \"string\"\n ? body.amount\n : typeof body.maxAmountRequired === \"string\"\n ? body.maxAmountRequired\n : \"0\";\n return {\n grantId: resolvedGrantId,\n asset: typeof body.asset === \"string\" ? body.asset : NATIVE_ASSET_ADDRESS,\n amount: amountValue,\n raw,\n };\n}\n\n/**\n * Read approved data from a Personal Server, settling a 402 via escrow.\n *\n * @remarks\n * Sends a Web3Signed-authenticated `GET /v1/data/{scope}`. On `402`, parses what\n * is owed, authorizes an escrow payment for the grant via `escrow`, and retries\n * once. If escrow is not configured, throws {@link PaymentRequiredError} carrying\n * the parsed requirement so callers can debug amount/asset.\n *\n * @param params - Connection details, app signer, optional escrow config and fetch.\n * @returns `{ data, payment? }`.\n */\nexport async function readPersonalServerData(params: {\n personalServerUrl: string;\n scope: string;\n grantId: string;\n payerAddress: `0x${string}`;\n signMessage: Web3SignedSignFn;\n escrow?: EscrowPaymentConfig;\n fetchFn?: PersonalServerFetch;\n}): Promise<PersonalServerReadResult> {\n const fetchFn =\n params.fetchFn ?? (globalThis.fetch as unknown as PersonalServerFetch);\n if (!fetchFn) {\n throw new PersonalServerReadError(\n \"No fetch implementation available for Personal Server read\",\n );\n }\n\n const initial = await buildPersonalServerDataReadRequest({\n personalServerUrl: params.personalServerUrl,\n scope: params.scope,\n grantId: params.grantId,\n signMessage: params.signMessage,\n });\n\n let res = await fetchFn(initial.url, {\n method: initial.method,\n headers: initial.headers,\n });\n\n let payment: DirectPaymentReceipt | undefined;\n\n if (res.status === 402) {\n const required = await parsePersonalServerPaymentRequired(\n res,\n params.grantId,\n );\n if (!params.escrow) {\n throw new PaymentRequiredError(\n \"Personal Server requires payment but no escrow config is set\",\n {\n scope: params.scope,\n grantId: required.grantId,\n asset: required.asset,\n amount: required.amount,\n },\n );\n }\n\n payment = await authorizeGrantPayment({\n payerAddress: params.payerAddress,\n required,\n config: params.escrow,\n });\n\n // Re-sign and retry — the settled payment unlocks the read.\n const retry = await buildPersonalServerDataReadRequest({\n personalServerUrl: params.personalServerUrl,\n scope: params.scope,\n grantId: params.grantId,\n signMessage: params.signMessage,\n });\n res = await fetchFn(retry.url, {\n method: retry.method,\n headers: retry.headers,\n });\n\n if (res.status === 402) {\n throw new PaymentRequiredError(\n \"Personal Server still requires payment after escrow settlement\",\n {\n scope: params.scope,\n grantId: required.grantId,\n asset: required.asset,\n amount: required.amount,\n payment,\n },\n );\n }\n }\n\n if (!res.ok) {\n const detail = await res.text().catch(() => \"\");\n throw new PersonalServerReadError(\n `Personal Server read failed: ${res.status} ${res.statusText}`,\n res.status,\n { scope: params.scope, body: detail.slice(0, 500) },\n );\n }\n\n return { data: await res.json(), payment };\n}\n"],"mappings":"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","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var types_exports = {};
|
|
20
|
+
__export(types_exports, {
|
|
21
|
+
DirectOpType: () => DirectOpType
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(types_exports);
|
|
24
|
+
const DirectOpType = {
|
|
25
|
+
GrantRegistration: "grant_registration",
|
|
26
|
+
DataAccess: "data_access",
|
|
27
|
+
DataRegistration: "data_registration",
|
|
28
|
+
ServerRegistration: "server_registration",
|
|
29
|
+
BuilderRegistration: "builder_registration"
|
|
30
|
+
};
|
|
31
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
32
|
+
0 && (module.exports = {
|
|
33
|
+
DirectOpType
|
|
34
|
+
});
|
|
35
|
+
//# sourceMappingURL=types.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/direct/types.ts"],"sourcesContent":["/**\n * Shared types for the Direct Data Controller and the browser connect helper.\n *\n * @remarks\n * These types describe the \"two-tab\" Data Portability flow documented in the\n * builder guide: a backend controller creates an access request, the browser\n * opens Vana for user approval, and the backend reads the approved data from\n * the user's Personal Server (handling 402 Payment Required).\n *\n * @category Direct\n * @module direct/types\n */\n\n/**\n * Target environment for a {@link DirectDataController}.\n *\n * - `\"production\"` — Vana mainnet stack (default service URLs).\n * - `\"dev\"` — Vana internal dev/testnet stack. Use only when testing against\n * Vana's dev infrastructure.\n */\nexport type DirectEnv = \"dev\" | \"production\";\n\n/**\n * App identity advertised to users during approval and attributed in Builder\n * League activity reports.\n */\nexport interface DirectAppConfig {\n /** Stable, human-readable app id (e.g. `\"notes-lens\"`). */\n id: string;\n /** Display name shown to the user in the Vana approval UI. */\n name: string;\n /** Public homepage URL for the app. */\n homepageUrl: string;\n}\n\n/**\n * Resolved app identity: the configured {@link DirectAppConfig} plus the app's\n * derived on-chain address (the address to fund and inspect).\n */\nexport interface AppIdentity extends DirectAppConfig {\n /** The app's `0x`-prefixed on-chain address (derived from `appPrivateKey`). */\n address: string;\n}\n\n/**\n * Resolved service URLs for a given {@link DirectEnv}.\n *\n * @remarks\n * Centralizes the per-environment base URLs the controller talks to. Each can\n * be overridden via {@link DirectDataControllerConfig.endpoints} when pointing\n * at a non-standard deployment.\n */\nexport interface DirectServiceEndpoints {\n /** Vana chain id for this environment (1480 mainnet, 14800 moksha). */\n chainId: number;\n /** Base URL of the Vana Account access-request API that issues `dcr_*` ids. */\n accessRequestBaseUrl: string;\n /** Base URL users are sent to for approval (the Vana app). */\n approvalAppBaseUrl: string;\n}\n\n/** Result of {@link DirectDataController.createAccessRequest}. */\nexport interface AccessRequest {\n /** Opaque request id (e.g. `\"dcr_123\"`). */\n requestId: string;\n /** URL the browser opens so the user can approve the requested scopes. */\n approvalUrl: string;\n /** On-chain address of the (registered or reused) app. */\n appAddress: string;\n}\n\n/** Lifecycle status of an access request. */\nexport type AccessRequestStatusValue =\n | \"pending\"\n | \"approved\"\n | \"denied\"\n | \"expired\";\n\n/** Result of {@link DirectDataController.getAccessRequestStatus}. */\nexport interface AccessRequestStatus {\n /** Current lifecycle status of the request. */\n status: AccessRequestStatusValue;\n /** Personal Server base URL — present once `status === \"approved\"`. */\n personalServerUrl?: string;\n /** Grant id covering the approved scope — present once approved. */\n grantId?: string;\n /** The approved scope — present once approved. */\n scope?: string;\n}\n\n/** Result of {@link DirectDataController.readApprovedData}. */\nexport interface ApprovedDataResult<T = unknown> {\n /** The scope the data was read for. */\n scope: string;\n /** The decoded payload returned by the Personal Server. */\n data: T;\n /**\n * Payment receipt — present only when this read required (and settled) a\n * payment. Lets builders inspect the amount, asset, and fee breakdown without\n * digging into the underlying 402/escrow exchange. Reads served from a paid-up\n * grant omit this field.\n */\n payment?: DirectPaymentReceipt;\n}\n\n/**\n * Client for the Vana Account access-request API — the service that turns a\n * registered app + scopes into a `dcr_*` id and approval URL.\n *\n * @remarks\n * The controller uses a default client against the Vana Account endpoints. You\n * can inject your own implementation to point at a custom deployment or to\n * supply a test double.\n */\nexport interface AccessRequestClient {\n /**\n * Create an access request for the given app + scopes.\n *\n * @param input - App identity, source, scopes, and the post-approval return URL.\n * @returns The created {@link AccessRequest}.\n */\n createAccessRequest(input: {\n appAddress: string;\n app: DirectAppConfig;\n source: string;\n scopes: string[];\n returnUrl: string;\n }): Promise<AccessRequest>;\n\n /**\n * Fetch the current status of a previously created access request.\n *\n * @param requestId - The `dcr_*` id returned by {@link AccessRequestClient.createAccessRequest}.\n * @returns The current {@link AccessRequestStatus}.\n */\n getAccessRequestStatus(requestId: string): Promise<AccessRequestStatus>;\n}\n\n/**\n * Op-type vocabulary used by the DPv2 escrow payment surface.\n *\n * @remarks\n * These are the operations the gateway prices and settles via\n * `POST /v1/escrow/pay` (`opType` field of the `GenericPayment` message). A\n * direct data read settles the {@link DirectOpType.DataAccess} op for the\n * approved grant; the other op types are listed here for completeness and to\n * give builders a typed vocabulary when inspecting fee breakdowns.\n *\n * Note: the escrow `GenericPayment` `opType` is currently `\"grant\"` on the wire\n * for grant lifecycle payments; this enum names the higher-level fee categories\n * the gateway reports in a {@link PaymentBreakdown}.\n */\nexport const DirectOpType = {\n GrantRegistration: \"grant_registration\",\n DataAccess: \"data_access\",\n DataRegistration: \"data_registration\",\n ServerRegistration: \"server_registration\",\n BuilderRegistration: \"builder_registration\",\n} as const;\n\n/** A direct-flow op type (see {@link DirectOpType}). */\nexport type DirectOpTypeValue =\n (typeof DirectOpType)[keyof typeof DirectOpType];\n\n/**\n * What a Personal Server `402 Payment Required` tells the controller is owed for\n * a data read.\n *\n * @remarks\n * The PS read 402 body identifies the grant to settle and the amount/asset. The\n * controller settles it via the DPv2 escrow gateway (`/v1/escrow/pay`). The full\n * unmodified body is preserved under {@link PersonalServerPaymentRequired.raw}.\n */\nexport interface PersonalServerPaymentRequired {\n /** Grant id to settle (the escrow `opId`). Defaults to the read's grantId. */\n grantId: string;\n /** Asset address owed (zero address = native VANA). */\n asset: string;\n /** Amount owed, as a decimal base-unit string (preserves uint256 precision). */\n amount: string;\n /** The full, unmodified 402 response body. */\n raw: unknown;\n}\n\n/**\n * Structured payment metadata attached to a successful paid read.\n *\n * @remarks\n * Derived from the gateway's {@link EscrowPayResult}. Lets builders debug the\n * amount, asset, and per-op fee breakdown without re-deriving anything from the\n * raw 402/payment exchange.\n */\nexport interface DirectPaymentReceipt {\n /** Op type settled (the gateway `opType`, e.g. `\"grant\"`). */\n opType: string;\n /** Op id settled (the grant id). */\n opId: string;\n /** Asset paid in (zero address = native VANA). */\n asset: string;\n /** Total amount paid, as a decimal base-unit string. */\n amount: string;\n /** Payment nonce used for this settlement. */\n paymentNonce: string;\n /** Fee breakdown reported by the gateway (registration vs data-access fee). */\n breakdown: DirectFeeBreakdown;\n /** ISO timestamp the gateway recorded the payment. */\n paidAt: string;\n}\n\n/**\n * Per-op fee breakdown reported by the gateway.\n *\n * @remarks\n * Mirrors the escrow {@link PaymentBreakdown}: a one-time registration fee plus\n * the per-read data-access fee, and whether this settlement covered the\n * registration fee.\n */\nexport interface DirectFeeBreakdown {\n /** One-time registration fee for the op, as a decimal base-unit string. */\n registrationFee: string;\n /** Per-read data-access fee, as a decimal base-unit string. */\n dataAccessFee: string;\n /** True when this settlement paid the registration fee. */\n registrationPaid: boolean;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAwJO,MAAM,eAAe;AAAA,EAC1B,mBAAmB;AAAA,EACnB,YAAY;AAAA,EACZ,kBAAkB;AAAA,EAClB,oBAAoB;AAAA,EACpB,qBAAqB;AACvB;","names":[]}
|