@campnetwork/origin 1.2.1 → 1.2.3
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/core.cjs +154 -101
- package/dist/core.d.ts +190 -131
- package/dist/core.esm.d.ts +190 -131
- package/dist/core.esm.js +161 -117
- package/dist/react/index.esm.d.ts +57 -3
- package/dist/react/index.esm.js +455 -174
- package/package.json +1 -1
package/dist/react/index.esm.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
import React, { createContext, useState, useContext, useEffect, useLayoutEffect, useRef, useSyncExternalStore } from 'react';
|
|
3
|
-
import { custom, createWalletClient, createPublicClient, http,
|
|
3
|
+
import { custom, createWalletClient, createPublicClient, http, encodeFunctionData, checksumAddress, zeroAddress, keccak256, toBytes, erc20Abi, getAbiItem, formatEther, formatUnits, parseEther } from 'viem';
|
|
4
4
|
import { toAccount } from 'viem/accounts';
|
|
5
5
|
import { createSiweMessage } from 'viem/siwe';
|
|
6
6
|
import axios from 'axios';
|
|
@@ -3457,28 +3457,270 @@ function subscriptionExpiry(tokenId, user) {
|
|
|
3457
3457
|
}
|
|
3458
3458
|
|
|
3459
3459
|
/**
|
|
3460
|
-
*
|
|
3461
|
-
* If the current allowance is less than the specified amount, it will perform the approval.
|
|
3462
|
-
* @param {ApproveParams} params - The parameters for the approval.
|
|
3460
|
+
* Adapter for viem WalletClient
|
|
3463
3461
|
*/
|
|
3464
|
-
|
|
3465
|
-
|
|
3466
|
-
|
|
3467
|
-
|
|
3468
|
-
|
|
3469
|
-
|
|
3470
|
-
|
|
3462
|
+
class ViemSignerAdapter {
|
|
3463
|
+
constructor(signer) {
|
|
3464
|
+
this.type = "viem";
|
|
3465
|
+
this.signer = signer;
|
|
3466
|
+
}
|
|
3467
|
+
getAddress() {
|
|
3468
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
3469
|
+
if (this.signer.account) {
|
|
3470
|
+
return this.signer.account.address;
|
|
3471
|
+
}
|
|
3472
|
+
const accounts = yield this.signer.request({
|
|
3473
|
+
method: "eth_requestAccounts",
|
|
3474
|
+
params: [],
|
|
3475
|
+
});
|
|
3476
|
+
if (!accounts || accounts.length === 0) {
|
|
3477
|
+
throw new Error("No accounts found in viem wallet client");
|
|
3478
|
+
}
|
|
3479
|
+
return accounts[0];
|
|
3471
3480
|
});
|
|
3472
|
-
|
|
3473
|
-
|
|
3474
|
-
|
|
3475
|
-
|
|
3476
|
-
|
|
3477
|
-
|
|
3478
|
-
|
|
3479
|
-
|
|
3481
|
+
}
|
|
3482
|
+
signMessage(message) {
|
|
3483
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
3484
|
+
const address = yield this.getAddress();
|
|
3485
|
+
return yield this.signer.signMessage({
|
|
3486
|
+
account: address,
|
|
3487
|
+
message,
|
|
3488
|
+
});
|
|
3489
|
+
});
|
|
3490
|
+
}
|
|
3491
|
+
signTypedData(domain, types, value) {
|
|
3492
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
3493
|
+
throw new Error("Viem WalletClient does not support signTypedData");
|
|
3494
|
+
});
|
|
3495
|
+
}
|
|
3496
|
+
getChainId() {
|
|
3497
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
3498
|
+
var _a;
|
|
3499
|
+
return ((_a = this.signer.chain) === null || _a === void 0 ? void 0 : _a.id) || 1;
|
|
3500
|
+
});
|
|
3501
|
+
}
|
|
3502
|
+
}
|
|
3503
|
+
/**
|
|
3504
|
+
* Adapter for ethers Signer (v5 and v6)
|
|
3505
|
+
*/
|
|
3506
|
+
class EthersSignerAdapter {
|
|
3507
|
+
constructor(signer) {
|
|
3508
|
+
this.type = "ethers";
|
|
3509
|
+
this.signer = signer;
|
|
3510
|
+
}
|
|
3511
|
+
getAddress() {
|
|
3512
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
3513
|
+
// Works for both ethers v5 and v6
|
|
3514
|
+
if (typeof this.signer.getAddress === "function") {
|
|
3515
|
+
return yield this.signer.getAddress();
|
|
3516
|
+
}
|
|
3517
|
+
if (this.signer.address) {
|
|
3518
|
+
return this.signer.address;
|
|
3519
|
+
}
|
|
3520
|
+
throw new Error("Unable to get address from ethers signer");
|
|
3521
|
+
});
|
|
3522
|
+
}
|
|
3523
|
+
signMessage(message) {
|
|
3524
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
3525
|
+
if (typeof this.signer.signMessage !== "function") {
|
|
3526
|
+
throw new Error("Signer does not support signMessage");
|
|
3527
|
+
}
|
|
3528
|
+
return yield this.signer.signMessage(message);
|
|
3529
|
+
});
|
|
3530
|
+
}
|
|
3531
|
+
signTypedData(domain, types, value) {
|
|
3532
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
3533
|
+
if (typeof this.signer._signTypedData === "function") {
|
|
3534
|
+
return yield this.signer._signTypedData(domain, types, value);
|
|
3535
|
+
}
|
|
3536
|
+
if (typeof this.signer.signTypedData !== "function") {
|
|
3537
|
+
throw new Error("Signer does not support signTypedData or _signTypedData");
|
|
3538
|
+
}
|
|
3539
|
+
return yield this.signer.signTypedData(domain, types, value);
|
|
3540
|
+
});
|
|
3541
|
+
}
|
|
3542
|
+
getChainId() {
|
|
3543
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
3544
|
+
// Try ethers v6 first
|
|
3545
|
+
if (this.signer.provider &&
|
|
3546
|
+
typeof this.signer.provider.getNetwork === "function") {
|
|
3547
|
+
const network = yield this.signer.provider.getNetwork();
|
|
3548
|
+
// ethers v6 returns bigint, v5 returns number
|
|
3549
|
+
return typeof network.chainId === "bigint"
|
|
3550
|
+
? Number(network.chainId)
|
|
3551
|
+
: network.chainId;
|
|
3552
|
+
}
|
|
3553
|
+
// Fallback for ethers v5
|
|
3554
|
+
if (typeof this.signer.getChainId === "function") {
|
|
3555
|
+
return yield this.signer.getChainId();
|
|
3556
|
+
}
|
|
3557
|
+
// Default to mainnet if we can't determine
|
|
3558
|
+
return 484;
|
|
3559
|
+
});
|
|
3560
|
+
}
|
|
3561
|
+
}
|
|
3562
|
+
/**
|
|
3563
|
+
* Adapter for custom signer implementations
|
|
3564
|
+
*/
|
|
3565
|
+
class CustomSignerAdapter {
|
|
3566
|
+
constructor(signer) {
|
|
3567
|
+
this.type = "custom";
|
|
3568
|
+
this.signer = signer;
|
|
3569
|
+
}
|
|
3570
|
+
getAddress() {
|
|
3571
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
3572
|
+
if (typeof this.signer.getAddress === "function") {
|
|
3573
|
+
return yield this.signer.getAddress();
|
|
3574
|
+
}
|
|
3575
|
+
if (this.signer.address) {
|
|
3576
|
+
return this.signer.address;
|
|
3577
|
+
}
|
|
3578
|
+
throw new Error("Custom signer must implement getAddress() or have address property");
|
|
3579
|
+
});
|
|
3580
|
+
}
|
|
3581
|
+
signMessage(message) {
|
|
3582
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
3583
|
+
if (typeof this.signer.signMessage !== "function") {
|
|
3584
|
+
throw new Error("Custom signer must implement signMessage()");
|
|
3585
|
+
}
|
|
3586
|
+
return yield this.signer.signMessage(message);
|
|
3587
|
+
});
|
|
3588
|
+
}
|
|
3589
|
+
signTypedData(domain, types, value) {
|
|
3590
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
3591
|
+
if (typeof this.signer.signTypedData !== "function") {
|
|
3592
|
+
throw new Error("Custom signer must implement signTypedData()");
|
|
3593
|
+
}
|
|
3594
|
+
return yield this.signer.signTypedData(domain, types, value);
|
|
3595
|
+
});
|
|
3596
|
+
}
|
|
3597
|
+
getChainId() {
|
|
3598
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
3599
|
+
if (typeof this.signer.getChainId === "function") {
|
|
3600
|
+
const chainId = yield this.signer.getChainId();
|
|
3601
|
+
return typeof chainId === "bigint" ? Number(chainId) : chainId;
|
|
3602
|
+
}
|
|
3603
|
+
if (this.signer.chainId !== undefined) {
|
|
3604
|
+
return typeof this.signer.chainId === "bigint"
|
|
3605
|
+
? Number(this.signer.chainId)
|
|
3606
|
+
: this.signer.chainId;
|
|
3607
|
+
}
|
|
3608
|
+
// Default to mainnet
|
|
3609
|
+
return 484;
|
|
3610
|
+
});
|
|
3611
|
+
}
|
|
3612
|
+
}
|
|
3613
|
+
/**
|
|
3614
|
+
* Factory function to create appropriate adapter based on signer type
|
|
3615
|
+
*/
|
|
3616
|
+
function createSignerAdapter(signer) {
|
|
3617
|
+
// Check for viem WalletClient
|
|
3618
|
+
if (signer.transport &&
|
|
3619
|
+
signer.chain &&
|
|
3620
|
+
typeof signer.signMessage === "function") {
|
|
3621
|
+
return new ViemSignerAdapter(signer);
|
|
3622
|
+
}
|
|
3623
|
+
// Check for ethers signer (v5 or v6)
|
|
3624
|
+
if (signer._isSigner ||
|
|
3625
|
+
(signer.provider && typeof signer.signMessage === "function")) {
|
|
3626
|
+
return new EthersSignerAdapter(signer);
|
|
3627
|
+
}
|
|
3628
|
+
// Try custom adapter
|
|
3629
|
+
return new CustomSignerAdapter(signer);
|
|
3630
|
+
}
|
|
3631
|
+
|
|
3632
|
+
/**
|
|
3633
|
+
* Settles an X402 payment response by purchasing access if needed.
|
|
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.
|
|
3636
|
+
* Supports viem WalletClient, ethers Signer, and custom signer implementations.
|
|
3637
|
+
*
|
|
3638
|
+
* @param x402Response - The response from getDataWithX402 containing payment details.
|
|
3639
|
+
* @param signer - Optional signer object used to interact with the blockchain. If not provided, uses the connected wallet client.
|
|
3640
|
+
* @returns A promise that resolves with the transaction hash and receipt, or null if access already exists.
|
|
3641
|
+
* @throws {Error} If the response doesn't contain marketplace action or if the method is not buyAccess.
|
|
3642
|
+
*/
|
|
3643
|
+
function settleX402(x402Response, signer) {
|
|
3644
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
3645
|
+
if (!x402Response.marketplaceAction) {
|
|
3646
|
+
throw new Error("No marketplace action found in X402 response");
|
|
3647
|
+
}
|
|
3648
|
+
const { marketplaceAction } = x402Response;
|
|
3649
|
+
if (marketplaceAction.method !== "buyAccess") {
|
|
3650
|
+
throw new Error(`Unsupported marketplace action method: ${marketplaceAction.method}`);
|
|
3651
|
+
}
|
|
3652
|
+
const tokenId = BigInt(marketplaceAction.tokenId);
|
|
3653
|
+
const payerAddress = marketplaceAction.payer;
|
|
3654
|
+
const alreadyHasAccess = yield this.hasAccess(payerAddress, tokenId);
|
|
3655
|
+
if (alreadyHasAccess) {
|
|
3656
|
+
console.log("User already has access to this item");
|
|
3657
|
+
return null;
|
|
3658
|
+
}
|
|
3659
|
+
const expectedPrice = BigInt(marketplaceAction.amount);
|
|
3660
|
+
const expectedDuration = BigInt(marketplaceAction.duration);
|
|
3661
|
+
const expectedPaymentToken = marketplaceAction.asset;
|
|
3662
|
+
const isNativeToken = expectedPaymentToken === "0x0000000000000000000000000000000000000000";
|
|
3663
|
+
const value = isNativeToken ? expectedPrice : BigInt(0);
|
|
3664
|
+
if (signer) {
|
|
3665
|
+
const signerAdapter = createSignerAdapter(signer);
|
|
3666
|
+
const marketplaceAddress = this.environment
|
|
3667
|
+
.MARKETPLACE_CONTRACT_ADDRESS;
|
|
3668
|
+
const abi = this.environment.MARKETPLACE_ABI;
|
|
3669
|
+
const data = encodeFunctionData({
|
|
3670
|
+
abi,
|
|
3671
|
+
functionName: "buyAccess",
|
|
3672
|
+
args: [
|
|
3673
|
+
payerAddress,
|
|
3674
|
+
tokenId,
|
|
3675
|
+
expectedPrice,
|
|
3676
|
+
expectedDuration,
|
|
3677
|
+
expectedPaymentToken,
|
|
3678
|
+
],
|
|
3480
3679
|
});
|
|
3680
|
+
if (signerAdapter.type === "viem") {
|
|
3681
|
+
const viemSigner = signerAdapter.signer;
|
|
3682
|
+
const txHash = yield viemSigner.sendTransaction({
|
|
3683
|
+
to: marketplaceAddress,
|
|
3684
|
+
data,
|
|
3685
|
+
value,
|
|
3686
|
+
account: (yield signerAdapter.getAddress()),
|
|
3687
|
+
});
|
|
3688
|
+
const receipt = yield viemSigner.waitForTransactionReceipt({
|
|
3689
|
+
hash: txHash,
|
|
3690
|
+
});
|
|
3691
|
+
return { txHash, receipt };
|
|
3692
|
+
}
|
|
3693
|
+
else if (signerAdapter.type === "ethers") {
|
|
3694
|
+
const ethersSigner = signerAdapter.signer;
|
|
3695
|
+
const tx = yield ethersSigner.sendTransaction({
|
|
3696
|
+
to: marketplaceAddress,
|
|
3697
|
+
data,
|
|
3698
|
+
value: value.toString(),
|
|
3699
|
+
});
|
|
3700
|
+
const receipt = yield tx.wait();
|
|
3701
|
+
return { txHash: tx.hash, receipt };
|
|
3702
|
+
}
|
|
3703
|
+
else {
|
|
3704
|
+
const customSigner = signerAdapter.signer;
|
|
3705
|
+
if (typeof customSigner.sendTransaction !== "function") {
|
|
3706
|
+
throw new Error("Custom signer must implement sendTransaction() method");
|
|
3707
|
+
}
|
|
3708
|
+
const tx = yield customSigner.sendTransaction({
|
|
3709
|
+
to: marketplaceAddress,
|
|
3710
|
+
data,
|
|
3711
|
+
value: value.toString(),
|
|
3712
|
+
});
|
|
3713
|
+
if (tx.wait && typeof tx.wait === "function") {
|
|
3714
|
+
const receipt = yield tx.wait();
|
|
3715
|
+
return { txHash: tx.hash, receipt };
|
|
3716
|
+
}
|
|
3717
|
+
return { txHash: tx.hash || tx };
|
|
3718
|
+
}
|
|
3481
3719
|
}
|
|
3720
|
+
if (!this.viemClient) {
|
|
3721
|
+
throw new Error("No signer or wallet client provided for settleX402");
|
|
3722
|
+
}
|
|
3723
|
+
return yield this.buyAccess(payerAddress, tokenId, expectedPrice, expectedDuration, expectedPaymentToken, isNativeToken ? value : undefined);
|
|
3482
3724
|
});
|
|
3483
3725
|
}
|
|
3484
3726
|
|
|
@@ -3521,6 +3763,180 @@ const createLicenseTerms = (price, duration, royaltyBps, paymentToken) => {
|
|
|
3521
3763
|
paymentToken,
|
|
3522
3764
|
};
|
|
3523
3765
|
};
|
|
3766
|
+
/**
|
|
3767
|
+
* Defines the EIP-712 typed data structure for X402 Intent signatures.
|
|
3768
|
+
*/
|
|
3769
|
+
const X402_INTENT_TYPES = {
|
|
3770
|
+
X402Intent: [
|
|
3771
|
+
{ name: "payer", type: "address" },
|
|
3772
|
+
{ name: "asset", type: "address" },
|
|
3773
|
+
{ name: "amount", type: "uint256" },
|
|
3774
|
+
{ name: "httpMethod", type: "string" },
|
|
3775
|
+
{ name: "payTo", type: "address" },
|
|
3776
|
+
{ name: "tokenId", type: "uint256" },
|
|
3777
|
+
{ name: "duration", type: "uint32" },
|
|
3778
|
+
{ name: "expiresAt", type: "uint256" },
|
|
3779
|
+
{ name: "nonce", type: "bytes32" },
|
|
3780
|
+
],
|
|
3781
|
+
};
|
|
3782
|
+
|
|
3783
|
+
/**
|
|
3784
|
+
* Fetch data with X402 payment handling.
|
|
3785
|
+
* @param {bigint} tokenId The token ID to fetch data for.
|
|
3786
|
+
* @param {any} [signer] Optional signer object for signing the X402 intent.
|
|
3787
|
+
* @returns {Promise<any>} A promise that resolves with the fetched data.
|
|
3788
|
+
* @throws {Error} Throws an error if the data cannot be fetched or if no signer/wallet client is provided.
|
|
3789
|
+
*/
|
|
3790
|
+
function getDataWithX402(tokenId, signer) {
|
|
3791
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
3792
|
+
var _a;
|
|
3793
|
+
const viemClient = this.viemClient;
|
|
3794
|
+
if (!signer && !viemClient) {
|
|
3795
|
+
throw new Error("No signer or wallet client provided for X402 intent.");
|
|
3796
|
+
}
|
|
3797
|
+
const initialResponse = yield fetch(`${this.environment.AUTH_HUB_BASE_API}/${this.environment.AUTH_ENDPOINT}/origin/data/${tokenId}`, {
|
|
3798
|
+
method: "GET",
|
|
3799
|
+
});
|
|
3800
|
+
if (initialResponse.status !== 402) {
|
|
3801
|
+
if (!initialResponse.ok) {
|
|
3802
|
+
throw new Error("Failed to fetch data");
|
|
3803
|
+
}
|
|
3804
|
+
return initialResponse.json();
|
|
3805
|
+
}
|
|
3806
|
+
const sig = viemClient || createSignerAdapter(signer);
|
|
3807
|
+
const walletAddress = viemClient
|
|
3808
|
+
? yield getCurrentAccount.call(this)
|
|
3809
|
+
: yield sig.getAddress();
|
|
3810
|
+
const intentData = yield initialResponse.json();
|
|
3811
|
+
if (intentData.error) {
|
|
3812
|
+
throw new Error(intentData.error);
|
|
3813
|
+
}
|
|
3814
|
+
const requirements = intentData.accepts[0];
|
|
3815
|
+
const x402Payload = yield buildX402Payload.call(this, requirements, checksumAddress(walletAddress), sig);
|
|
3816
|
+
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
|
+
},
|
|
3823
|
+
});
|
|
3824
|
+
if (retryResponse.status === 402) {
|
|
3825
|
+
// subscription required
|
|
3826
|
+
return retryResponse.json();
|
|
3827
|
+
}
|
|
3828
|
+
if (!retryResponse.ok) {
|
|
3829
|
+
throw new Error("Failed to fetch data after X402 payment");
|
|
3830
|
+
}
|
|
3831
|
+
const res = yield retryResponse.json();
|
|
3832
|
+
return {
|
|
3833
|
+
error: null,
|
|
3834
|
+
data: (_a = res.data) !== null && _a !== void 0 ? _a : res,
|
|
3835
|
+
};
|
|
3836
|
+
});
|
|
3837
|
+
}
|
|
3838
|
+
/**
|
|
3839
|
+
* Build the X402 payment payload.
|
|
3840
|
+
* @private
|
|
3841
|
+
*/
|
|
3842
|
+
function buildX402Payload(requirements, payer, signer) {
|
|
3843
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
3844
|
+
const asset = requirements.asset === "native" ? zeroAddress : requirements.asset;
|
|
3845
|
+
const amount = BigInt(requirements.maxAmountRequired || 0);
|
|
3846
|
+
const duration = requirements.extra.duration;
|
|
3847
|
+
const domain = makeX402IntentDomain.call(this);
|
|
3848
|
+
const types = X402_INTENT_TYPES;
|
|
3849
|
+
const nonce = crypto.randomUUID();
|
|
3850
|
+
const nonceBytes32 = keccak256(toBytes(nonce));
|
|
3851
|
+
const payment = {
|
|
3852
|
+
payer: payer,
|
|
3853
|
+
asset: asset,
|
|
3854
|
+
amount: amount.toString(),
|
|
3855
|
+
httpMethod: "GET",
|
|
3856
|
+
payTo: checksumAddress(this.environment.MARKETPLACE_CONTRACT_ADDRESS),
|
|
3857
|
+
tokenId: requirements.extra.tokenId,
|
|
3858
|
+
duration: duration,
|
|
3859
|
+
expiresAt: Math.floor(Date.now() / 1000) + requirements.maxTimeoutSeconds,
|
|
3860
|
+
nonce: nonceBytes32,
|
|
3861
|
+
};
|
|
3862
|
+
const signerAdapter = createSignerAdapter(signer);
|
|
3863
|
+
const signature = yield signerAdapter.signTypedData(domain, types, payment);
|
|
3864
|
+
const x402Payload = {
|
|
3865
|
+
x402Version: 1,
|
|
3866
|
+
scheme: "exact",
|
|
3867
|
+
network: requirements.network,
|
|
3868
|
+
payload: Object.assign(Object.assign({}, payment), { sigType: "eip712", signature: signature, license: {
|
|
3869
|
+
tokenId: requirements.extra.tokenId,
|
|
3870
|
+
duration: duration,
|
|
3871
|
+
} }),
|
|
3872
|
+
};
|
|
3873
|
+
return x402Payload;
|
|
3874
|
+
});
|
|
3875
|
+
}
|
|
3876
|
+
/**
|
|
3877
|
+
* Create the X402 Intent domain for EIP-712 signing.
|
|
3878
|
+
* @private
|
|
3879
|
+
*/
|
|
3880
|
+
function makeX402IntentDomain() {
|
|
3881
|
+
return {
|
|
3882
|
+
name: "Origin X402 Intent",
|
|
3883
|
+
version: "1",
|
|
3884
|
+
chainId: this.environment.CHAIN.id,
|
|
3885
|
+
verifyingContract: this.environment
|
|
3886
|
+
.MARKETPLACE_CONTRACT_ADDRESS,
|
|
3887
|
+
};
|
|
3888
|
+
}
|
|
3889
|
+
/**
|
|
3890
|
+
* Get the current account address.
|
|
3891
|
+
* @private
|
|
3892
|
+
*/
|
|
3893
|
+
function getCurrentAccount() {
|
|
3894
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
3895
|
+
const viemClient = this.viemClient;
|
|
3896
|
+
if (!viemClient) {
|
|
3897
|
+
throw new Error("WalletClient not connected. Please connect a wallet.");
|
|
3898
|
+
}
|
|
3899
|
+
// If account is already set on the client, return it directly
|
|
3900
|
+
if (viemClient.account) {
|
|
3901
|
+
return viemClient.account.address;
|
|
3902
|
+
}
|
|
3903
|
+
// Otherwise request accounts (browser wallet flow)
|
|
3904
|
+
const accounts = yield viemClient.request({
|
|
3905
|
+
method: "eth_requestAccounts",
|
|
3906
|
+
params: [],
|
|
3907
|
+
});
|
|
3908
|
+
if (!accounts || accounts.length === 0) {
|
|
3909
|
+
throw new Error("No accounts found in connected wallet.");
|
|
3910
|
+
}
|
|
3911
|
+
return accounts[0];
|
|
3912
|
+
});
|
|
3913
|
+
}
|
|
3914
|
+
|
|
3915
|
+
/**
|
|
3916
|
+
* Approves a spender to spend a specified amount of tokens on behalf of the owner.
|
|
3917
|
+
* If the current allowance is less than the specified amount, it will perform the approval.
|
|
3918
|
+
* @param {ApproveParams} params - The parameters for the approval.
|
|
3919
|
+
*/
|
|
3920
|
+
function approveIfNeeded(_a) {
|
|
3921
|
+
return __awaiter(this, arguments, void 0, function* ({ walletClient, publicClient, tokenAddress, owner, spender, amount, }) {
|
|
3922
|
+
const allowance = yield publicClient.readContract({
|
|
3923
|
+
address: tokenAddress,
|
|
3924
|
+
abi: erc20Abi,
|
|
3925
|
+
functionName: "allowance",
|
|
3926
|
+
args: [owner, spender],
|
|
3927
|
+
});
|
|
3928
|
+
if (allowance < amount) {
|
|
3929
|
+
yield walletClient.writeContract({
|
|
3930
|
+
address: tokenAddress,
|
|
3931
|
+
account: owner,
|
|
3932
|
+
abi: erc20Abi,
|
|
3933
|
+
functionName: "approve",
|
|
3934
|
+
args: [spender, amount],
|
|
3935
|
+
chain: testnet,
|
|
3936
|
+
});
|
|
3937
|
+
}
|
|
3938
|
+
});
|
|
3939
|
+
}
|
|
3524
3940
|
|
|
3525
3941
|
var _Origin_instances, _Origin_generateURL, _Origin_setOriginStatus, _Origin_uploadFile, _Origin_waitForTxReceipt, _Origin_ensureChainId, _Origin_getCurrentAccount, _Origin_resolveWalletAddress;
|
|
3526
3942
|
/**
|
|
@@ -3528,11 +3944,17 @@ var _Origin_instances, _Origin_generateURL, _Origin_setOriginStatus, _Origin_upl
|
|
|
3528
3944
|
* Handles the upload of files to Origin, as well as querying the user's stats
|
|
3529
3945
|
*/
|
|
3530
3946
|
class Origin {
|
|
3531
|
-
constructor(
|
|
3947
|
+
constructor(environment, jwt, viemClient, baseParentId) {
|
|
3532
3948
|
_Origin_instances.add(this);
|
|
3533
|
-
|
|
3949
|
+
if (jwt) {
|
|
3950
|
+
this.jwt = jwt;
|
|
3951
|
+
}
|
|
3952
|
+
else {
|
|
3953
|
+
console.warn("JWT not provided. Some features may be unavailable.");
|
|
3954
|
+
}
|
|
3534
3955
|
this.viemClient = viemClient;
|
|
3535
|
-
this.environment =
|
|
3956
|
+
this.environment =
|
|
3957
|
+
typeof environment === "string" ? ENVIRONMENTS[environment] : environment;
|
|
3536
3958
|
this.baseParentId = baseParentId;
|
|
3537
3959
|
// DataNFT methods
|
|
3538
3960
|
this.mintWithSignature = mintWithSignature.bind(this);
|
|
@@ -3554,6 +3976,8 @@ class Origin {
|
|
|
3554
3976
|
this.buyAccess = buyAccess.bind(this);
|
|
3555
3977
|
this.hasAccess = hasAccess.bind(this);
|
|
3556
3978
|
this.subscriptionExpiry = subscriptionExpiry.bind(this);
|
|
3979
|
+
this.settleX402 = settleX402.bind(this);
|
|
3980
|
+
this.getDataWithX402 = getDataWithX402.bind(this);
|
|
3557
3981
|
}
|
|
3558
3982
|
getJwt() {
|
|
3559
3983
|
return this.jwt;
|
|
@@ -3764,6 +4188,12 @@ class Origin {
|
|
|
3764
4188
|
return this.buyAccess(account, tokenId, totalCost, duration, paymentToken);
|
|
3765
4189
|
});
|
|
3766
4190
|
}
|
|
4191
|
+
/**
|
|
4192
|
+
* Fetch the underlying data associated with a specific token ID.
|
|
4193
|
+
* @param {bigint} tokenId - The token ID to fetch data for.
|
|
4194
|
+
* @returns {Promise<any>} A promise that resolves with the fetched data.
|
|
4195
|
+
* @throws {Error} Throws an error if the data cannot be fetched.
|
|
4196
|
+
*/
|
|
3767
4197
|
getData(tokenId) {
|
|
3768
4198
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3769
4199
|
const response = yield fetch(`${this.environment.AUTH_HUB_BASE_API}/${this.environment.AUTH_ENDPOINT}/origin/data/${tokenId}`, {
|
|
@@ -4138,155 +4568,6 @@ _Origin_instances = new WeakSet(), _Origin_generateURL = function _Origin_genera
|
|
|
4138
4568
|
});
|
|
4139
4569
|
};
|
|
4140
4570
|
|
|
4141
|
-
/**
|
|
4142
|
-
* Adapter for viem WalletClient
|
|
4143
|
-
*/
|
|
4144
|
-
class ViemSignerAdapter {
|
|
4145
|
-
constructor(signer) {
|
|
4146
|
-
this.type = "viem";
|
|
4147
|
-
this.signer = signer;
|
|
4148
|
-
}
|
|
4149
|
-
getAddress() {
|
|
4150
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
4151
|
-
if (this.signer.account) {
|
|
4152
|
-
return this.signer.account.address;
|
|
4153
|
-
}
|
|
4154
|
-
const accounts = yield this.signer.request({
|
|
4155
|
-
method: "eth_requestAccounts",
|
|
4156
|
-
params: [],
|
|
4157
|
-
});
|
|
4158
|
-
if (!accounts || accounts.length === 0) {
|
|
4159
|
-
throw new Error("No accounts found in viem wallet client");
|
|
4160
|
-
}
|
|
4161
|
-
return accounts[0];
|
|
4162
|
-
});
|
|
4163
|
-
}
|
|
4164
|
-
signMessage(message) {
|
|
4165
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
4166
|
-
const address = yield this.getAddress();
|
|
4167
|
-
return yield this.signer.signMessage({
|
|
4168
|
-
account: address,
|
|
4169
|
-
message,
|
|
4170
|
-
});
|
|
4171
|
-
});
|
|
4172
|
-
}
|
|
4173
|
-
getChainId() {
|
|
4174
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
4175
|
-
var _a;
|
|
4176
|
-
return ((_a = this.signer.chain) === null || _a === void 0 ? void 0 : _a.id) || 1;
|
|
4177
|
-
});
|
|
4178
|
-
}
|
|
4179
|
-
}
|
|
4180
|
-
/**
|
|
4181
|
-
* Adapter for ethers Signer (v5 and v6)
|
|
4182
|
-
*/
|
|
4183
|
-
class EthersSignerAdapter {
|
|
4184
|
-
constructor(signer) {
|
|
4185
|
-
this.type = "ethers";
|
|
4186
|
-
this.signer = signer;
|
|
4187
|
-
}
|
|
4188
|
-
getAddress() {
|
|
4189
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
4190
|
-
// Works for both ethers v5 and v6
|
|
4191
|
-
if (typeof this.signer.getAddress === "function") {
|
|
4192
|
-
return yield this.signer.getAddress();
|
|
4193
|
-
}
|
|
4194
|
-
if (this.signer.address) {
|
|
4195
|
-
return this.signer.address;
|
|
4196
|
-
}
|
|
4197
|
-
throw new Error("Unable to get address from ethers signer");
|
|
4198
|
-
});
|
|
4199
|
-
}
|
|
4200
|
-
signMessage(message) {
|
|
4201
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
4202
|
-
if (typeof this.signer.signMessage !== "function") {
|
|
4203
|
-
throw new Error("Signer does not support signMessage");
|
|
4204
|
-
}
|
|
4205
|
-
return yield this.signer.signMessage(message);
|
|
4206
|
-
});
|
|
4207
|
-
}
|
|
4208
|
-
getChainId() {
|
|
4209
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
4210
|
-
// Try ethers v6 first
|
|
4211
|
-
if (this.signer.provider &&
|
|
4212
|
-
typeof this.signer.provider.getNetwork === "function") {
|
|
4213
|
-
const network = yield this.signer.provider.getNetwork();
|
|
4214
|
-
// ethers v6 returns bigint, v5 returns number
|
|
4215
|
-
return typeof network.chainId === "bigint"
|
|
4216
|
-
? Number(network.chainId)
|
|
4217
|
-
: network.chainId;
|
|
4218
|
-
}
|
|
4219
|
-
// Fallback for ethers v5
|
|
4220
|
-
if (typeof this.signer.getChainId === "function") {
|
|
4221
|
-
return yield this.signer.getChainId();
|
|
4222
|
-
}
|
|
4223
|
-
// Default to mainnet if we can't determine
|
|
4224
|
-
return 484;
|
|
4225
|
-
});
|
|
4226
|
-
}
|
|
4227
|
-
}
|
|
4228
|
-
/**
|
|
4229
|
-
* Adapter for custom signer implementations
|
|
4230
|
-
*/
|
|
4231
|
-
class CustomSignerAdapter {
|
|
4232
|
-
constructor(signer) {
|
|
4233
|
-
this.type = "custom";
|
|
4234
|
-
this.signer = signer;
|
|
4235
|
-
}
|
|
4236
|
-
getAddress() {
|
|
4237
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
4238
|
-
if (typeof this.signer.getAddress === "function") {
|
|
4239
|
-
return yield this.signer.getAddress();
|
|
4240
|
-
}
|
|
4241
|
-
if (this.signer.address) {
|
|
4242
|
-
return this.signer.address;
|
|
4243
|
-
}
|
|
4244
|
-
throw new Error("Custom signer must implement getAddress() or have address property");
|
|
4245
|
-
});
|
|
4246
|
-
}
|
|
4247
|
-
signMessage(message) {
|
|
4248
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
4249
|
-
if (typeof this.signer.signMessage !== "function") {
|
|
4250
|
-
throw new Error("Custom signer must implement signMessage()");
|
|
4251
|
-
}
|
|
4252
|
-
return yield this.signer.signMessage(message);
|
|
4253
|
-
});
|
|
4254
|
-
}
|
|
4255
|
-
getChainId() {
|
|
4256
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
4257
|
-
if (typeof this.signer.getChainId === "function") {
|
|
4258
|
-
const chainId = yield this.signer.getChainId();
|
|
4259
|
-
return typeof chainId === "bigint" ? Number(chainId) : chainId;
|
|
4260
|
-
}
|
|
4261
|
-
if (this.signer.chainId !== undefined) {
|
|
4262
|
-
return typeof this.signer.chainId === "bigint"
|
|
4263
|
-
? Number(this.signer.chainId)
|
|
4264
|
-
: this.signer.chainId;
|
|
4265
|
-
}
|
|
4266
|
-
// Default to mainnet
|
|
4267
|
-
return 484;
|
|
4268
|
-
});
|
|
4269
|
-
}
|
|
4270
|
-
}
|
|
4271
|
-
/**
|
|
4272
|
-
* Factory function to create appropriate adapter based on signer type
|
|
4273
|
-
*/
|
|
4274
|
-
function createSignerAdapter(signer) {
|
|
4275
|
-
// Check for viem WalletClient
|
|
4276
|
-
if (signer.transport &&
|
|
4277
|
-
signer.chain &&
|
|
4278
|
-
typeof signer.signMessage === "function") {
|
|
4279
|
-
return new ViemSignerAdapter(signer);
|
|
4280
|
-
}
|
|
4281
|
-
// Check for ethers signer (v5 or v6)
|
|
4282
|
-
if (signer._isSigner ||
|
|
4283
|
-
(signer.provider && typeof signer.signMessage === "function")) {
|
|
4284
|
-
return new EthersSignerAdapter(signer);
|
|
4285
|
-
}
|
|
4286
|
-
// Try custom adapter
|
|
4287
|
-
return new CustomSignerAdapter(signer);
|
|
4288
|
-
}
|
|
4289
|
-
|
|
4290
4571
|
/**
|
|
4291
4572
|
* Browser localStorage adapter
|
|
4292
4573
|
*/
|
|
@@ -4629,7 +4910,7 @@ class Auth {
|
|
|
4629
4910
|
this.isAuthenticated = true;
|
|
4630
4911
|
this.userId = res.userId;
|
|
4631
4912
|
this.jwt = res.token;
|
|
4632
|
-
this.origin = new Origin(this.
|
|
4913
|
+
this.origin = new Origin(this.environment, this.jwt, this.viem, this.baseParentId);
|
|
4633
4914
|
yield __classPrivateFieldGet(this, _Auth_storage, "f").setItem("camp-sdk:jwt", this.jwt);
|
|
4634
4915
|
yield __classPrivateFieldGet(this, _Auth_storage, "f").setItem("camp-sdk:wallet-address", this.walletAddress);
|
|
4635
4916
|
yield __classPrivateFieldGet(this, _Auth_storage, "f").setItem("camp-sdk:user-id", this.userId);
|
|
@@ -4691,7 +4972,7 @@ class Auth {
|
|
|
4691
4972
|
this.isAuthenticated = true;
|
|
4692
4973
|
this.userId = res.userId;
|
|
4693
4974
|
this.jwt = res.token;
|
|
4694
|
-
this.origin = new Origin(this.
|
|
4975
|
+
this.origin = new Origin(this.environment, this.jwt, this.viem, this.baseParentId);
|
|
4695
4976
|
yield __classPrivateFieldGet(this, _Auth_storage, "f").setItem("camp-sdk:jwt", this.jwt);
|
|
4696
4977
|
yield __classPrivateFieldGet(this, _Auth_storage, "f").setItem("camp-sdk:wallet-address", this.walletAddress);
|
|
4697
4978
|
yield __classPrivateFieldGet(this, _Auth_storage, "f").setItem("camp-sdk:user-id", this.userId);
|
|
@@ -5080,7 +5361,7 @@ _Auth_triggers = new WeakMap(), _Auth_isNodeEnvironment = new WeakMap(), _Auth_s
|
|
|
5080
5361
|
this.walletAddress = walletAddress;
|
|
5081
5362
|
this.userId = userId;
|
|
5082
5363
|
this.jwt = jwt;
|
|
5083
|
-
this.origin = new Origin(this.
|
|
5364
|
+
this.origin = new Origin(this.environment, this.jwt, this.viem, this.baseParentId);
|
|
5084
5365
|
this.isAuthenticated = true;
|
|
5085
5366
|
if (provider) {
|
|
5086
5367
|
this.setProvider({
|