@campnetwork/origin 1.2.3 → 1.2.4

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.
@@ -193,17 +193,17 @@ interface TransactionResult {
193
193
  receipt?: any;
194
194
  }
195
195
  /**
196
- * Settles an X402 payment response by purchasing access if needed.
196
+ * Settles a payment intent response by purchasing access if needed.
197
197
  * This method checks if the user already has access to the item, and if not,
198
- * it calls buyAccess with the parameters from the X402 response.
198
+ * it calls buyAccess with the parameters from the payment intent response.
199
199
  * Supports viem WalletClient, ethers Signer, and custom signer implementations.
200
200
  *
201
- * @param x402Response - The response from getDataWithX402 containing payment details.
201
+ * @param paymentIntentResponse - The response from getDataWithIntent containing payment details.
202
202
  * @param signer - Optional signer object used to interact with the blockchain. If not provided, uses the connected wallet client.
203
203
  * @returns A promise that resolves with the transaction hash and receipt, or null if access already exists.
204
204
  * @throws {Error} If the response doesn't contain marketplace action or if the method is not buyAccess.
205
205
  */
206
- declare function settleX402(this: Origin, x402Response: X402Response, signer?: any): Promise<TransactionResult | null>;
206
+ declare function settlePaymentIntent(this: Origin, paymentIntentResponse: X402Response, signer?: any): Promise<TransactionResult | null>;
207
207
 
208
208
  /**
209
209
  * Fetch data with X402 payment handling.
@@ -212,7 +212,7 @@ declare function settleX402(this: Origin, x402Response: X402Response, signer?: a
212
212
  * @returns {Promise<any>} A promise that resolves with the fetched data.
213
213
  * @throws {Error} Throws an error if the data cannot be fetched or if no signer/wallet client is provided.
214
214
  */
215
- declare function getDataWithX402(this: Origin, tokenId: bigint, signer?: any): Promise<any>;
215
+ declare function getDataWithIntent(this: Origin, tokenId: bigint, signer?: any, decide?: (terms: any) => Promise<boolean>): Promise<any>;
216
216
 
217
217
  interface RoyaltyInfo {
218
218
  tokenBoundAccount: Address;
@@ -249,13 +249,13 @@ declare class Origin {
249
249
  buyAccess: typeof buyAccess;
250
250
  hasAccess: typeof hasAccess;
251
251
  subscriptionExpiry: typeof subscriptionExpiry;
252
- settleX402: typeof settleX402;
253
- getDataWithX402: typeof getDataWithX402;
252
+ settlePaymentIntent: typeof settlePaymentIntent;
253
+ getDataWithIntent: typeof getDataWithIntent;
254
254
  private jwt?;
255
255
  environment: Environment;
256
256
  private viemClient?;
257
257
  baseParentId?: bigint;
258
- constructor(environment: Environment | string, jwt?: string, viemClient?: WalletClient, baseParentId?: bigint);
258
+ constructor(environment?: Environment | string, jwt?: string, viemClient?: WalletClient, baseParentId?: bigint);
259
259
  getJwt(): string | undefined;
260
260
  setViemClient(client: WalletClient): void;
261
261
  mintFile(file: File, metadata: Record<string, unknown>, license: LicenseTerms, parents?: bigint[], options?: {
@@ -3630,22 +3630,22 @@ function createSignerAdapter(signer) {
3630
3630
  }
3631
3631
 
3632
3632
  /**
3633
- * Settles an X402 payment response by purchasing access if needed.
3633
+ * Settles a payment intent response by purchasing access if needed.
3634
3634
  * This method checks if the user already has access to the item, and if not,
3635
- * it calls buyAccess with the parameters from the X402 response.
3635
+ * it calls buyAccess with the parameters from the payment intent response.
3636
3636
  * Supports viem WalletClient, ethers Signer, and custom signer implementations.
3637
3637
  *
3638
- * @param x402Response - The response from getDataWithX402 containing payment details.
3638
+ * @param paymentIntentResponse - The response from getDataWithIntent containing payment details.
3639
3639
  * @param signer - Optional signer object used to interact with the blockchain. If not provided, uses the connected wallet client.
3640
3640
  * @returns A promise that resolves with the transaction hash and receipt, or null if access already exists.
3641
3641
  * @throws {Error} If the response doesn't contain marketplace action or if the method is not buyAccess.
3642
3642
  */
3643
- function settleX402(x402Response, signer) {
3643
+ function settlePaymentIntent(paymentIntentResponse, signer) {
3644
3644
  return __awaiter(this, void 0, void 0, function* () {
3645
- if (!x402Response.marketplaceAction) {
3645
+ if (!paymentIntentResponse.marketplaceAction) {
3646
3646
  throw new Error("No marketplace action found in X402 response");
3647
3647
  }
3648
- const { marketplaceAction } = x402Response;
3648
+ const { marketplaceAction } = paymentIntentResponse;
3649
3649
  if (marketplaceAction.method !== "buyAccess") {
3650
3650
  throw new Error(`Unsupported marketplace action method: ${marketplaceAction.method}`);
3651
3651
  }
@@ -3780,6 +3780,13 @@ const X402_INTENT_TYPES = {
3780
3780
  ],
3781
3781
  };
3782
3782
 
3783
+ const fetchTokenData = (origin, tokenId, headers) => __awaiter(void 0, void 0, void 0, function* () {
3784
+ const response = yield fetch(`${origin.environment.AUTH_HUB_BASE_API}/${origin.environment.AUTH_ENDPOINT}/origin/data/${tokenId}`, {
3785
+ method: "GET",
3786
+ headers: Object.assign({ "Content-Type": "application/json" }, headers),
3787
+ });
3788
+ return response;
3789
+ });
3783
3790
  /**
3784
3791
  * Fetch data with X402 payment handling.
3785
3792
  * @param {bigint} tokenId The token ID to fetch data for.
@@ -3787,16 +3794,14 @@ const X402_INTENT_TYPES = {
3787
3794
  * @returns {Promise<any>} A promise that resolves with the fetched data.
3788
3795
  * @throws {Error} Throws an error if the data cannot be fetched or if no signer/wallet client is provided.
3789
3796
  */
3790
- function getDataWithX402(tokenId, signer) {
3797
+ function getDataWithIntent(tokenId, signer, decide) {
3791
3798
  return __awaiter(this, void 0, void 0, function* () {
3792
3799
  var _a;
3793
3800
  const viemClient = this.viemClient;
3794
3801
  if (!signer && !viemClient) {
3795
3802
  throw new Error("No signer or wallet client provided for X402 intent.");
3796
3803
  }
3797
- const initialResponse = yield fetch(`${this.environment.AUTH_HUB_BASE_API}/${this.environment.AUTH_ENDPOINT}/origin/data/${tokenId}`, {
3798
- method: "GET",
3799
- });
3804
+ const initialResponse = yield fetchTokenData(this, tokenId, {});
3800
3805
  if (initialResponse.status !== 402) {
3801
3806
  if (!initialResponse.ok) {
3802
3807
  throw new Error("Failed to fetch data");
@@ -3814,16 +3819,34 @@ function getDataWithX402(tokenId, signer) {
3814
3819
  const requirements = intentData.accepts[0];
3815
3820
  const x402Payload = yield buildX402Payload.call(this, requirements, checksumAddress(walletAddress), sig);
3816
3821
  const header = btoa(JSON.stringify(x402Payload));
3817
- const retryResponse = yield fetch(`${this.environment.AUTH_HUB_BASE_API}/${this.environment.AUTH_ENDPOINT}/origin/data/${tokenId}`, {
3818
- method: "GET",
3819
- headers: {
3820
- "Content-Type": "application/json",
3821
- "X-PAYMENT": header,
3822
- },
3822
+ const retryResponse = yield fetchTokenData(this, tokenId, {
3823
+ "X-PAYMENT": header,
3823
3824
  });
3824
3825
  if (retryResponse.status === 402) {
3825
3826
  // subscription required
3826
- return retryResponse.json();
3827
+ if (decide) {
3828
+ const resJson = yield retryResponse.json();
3829
+ const accepted = yield decide(resJson.marketplaceAction);
3830
+ if (accepted) {
3831
+ const settlement = yield this.settlePaymentIntent(resJson, signer || viemClient);
3832
+ if (settlement && !settlement.txHash) {
3833
+ throw new Error(`Failed to settle payment intent for token ID ${tokenId}`);
3834
+ }
3835
+ // retry fetching data after settlement
3836
+ const finalResponse = yield this.getDataWithIntent(tokenId, signer, undefined);
3837
+ return finalResponse;
3838
+ }
3839
+ else {
3840
+ // user declined to proceed with payment
3841
+ return {
3842
+ error: "User declined to proceed with payment",
3843
+ data: null,
3844
+ };
3845
+ }
3846
+ }
3847
+ else {
3848
+ return retryResponse.json();
3849
+ }
3827
3850
  }
3828
3851
  if (!retryResponse.ok) {
3829
3852
  throw new Error("Failed to fetch data after X402 payment");
@@ -3954,7 +3977,9 @@ class Origin {
3954
3977
  }
3955
3978
  this.viemClient = viemClient;
3956
3979
  this.environment =
3957
- typeof environment === "string" ? ENVIRONMENTS[environment] : environment;
3980
+ typeof environment === "string"
3981
+ ? ENVIRONMENTS[environment]
3982
+ : environment || ENVIRONMENTS["DEVELOPMENT"];
3958
3983
  this.baseParentId = baseParentId;
3959
3984
  // DataNFT methods
3960
3985
  this.mintWithSignature = mintWithSignature.bind(this);
@@ -3976,8 +4001,8 @@ class Origin {
3976
4001
  this.buyAccess = buyAccess.bind(this);
3977
4002
  this.hasAccess = hasAccess.bind(this);
3978
4003
  this.subscriptionExpiry = subscriptionExpiry.bind(this);
3979
- this.settleX402 = settleX402.bind(this);
3980
- this.getDataWithX402 = getDataWithX402.bind(this);
4004
+ this.settlePaymentIntent = settlePaymentIntent.bind(this);
4005
+ this.getDataWithIntent = getDataWithIntent.bind(this);
3981
4006
  }
3982
4007
  getJwt() {
3983
4008
  return this.jwt;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@campnetwork/origin",
3
- "version": "1.2.3",
3
+ "version": "1.2.4",
4
4
  "main": "dist/core.cjs",
5
5
  "exports": {
6
6
  ".": {