@campnetwork/origin 1.2.0-1 → 1.2.0-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/README.md CHANGED
@@ -17,9 +17,21 @@ The Origin SDK currently exposes the following modules:
17
17
  - `"@campnetwork/origin"` - The main entry point for the SDK, exposes the following classes:
18
18
  - `TwitterAPI` - For fetching user Twitter data from Origin
19
19
  - `SpotifyAPI` - For fetching user Spotify data from Origin
20
- - `Auth` - For authenticating users with the Origin SDK
20
+ - `Auth` - For authenticating users with the Origin SDK (browser and Node.js)
21
+ - Signer adapters and utilities for Node.js support (ethers, viem, custom signers)
22
+ - Camp Network chain configurations (`campMainnet`, `campTestnet`)
23
+ - Origin utilities (`createLicenseTerms`, `LicenseTerms`, `DataStatus`)
21
24
  - `"@campnetwork/origin/react"` - Exposes the CampProvider and CampContext, as well as React components and hooks for authentication and fetching user data via Origin
22
25
 
26
+ ## Features
27
+
28
+ - **Browser & Node.js Support** - Use in client-side and server-side applications
29
+ - **Multiple Signer Types** - Works with ethers, viem, or custom signers
30
+ - **Social Account Linking** - Connect Twitter, Spotify, and TikTok
31
+ - **React Components** - Pre-built UI components and hooks
32
+ - **TypeScript Support** - Full type definitions included
33
+ - **Flexible Storage** - Custom storage adapters for session persistence
34
+
23
35
  # Installation
24
36
 
25
37
  ```bash
@@ -262,8 +274,6 @@ const video = await tiktok.fetchVideo("jack", "1234567890");
262
274
 
263
275
  The Auth class is the entry point for authenticating users with the Origin SDK. It requires a clientId to be instantiated.
264
276
 
265
- **Note: The Auth class is only to be used on the client side.**
266
-
267
277
  ### Constructor
268
278
 
269
279
  - `clientId` - The client ID of your app. This is required to authenticate users with the Origin SDK.
@@ -430,6 +440,8 @@ After the user has authenticated, the following methods can be used to link and
430
440
  When linking a social account, the user will be redirected to the OAuth flow for that social platform.
431
441
  Afterwards, the user will be redirected back to the `redirectUri` specified in the Auth constructor.
432
442
 
443
+ **Note: Linking socials is only available in a browser environment**
444
+
433
445
  #### linkTwitter
434
446
 
435
447
  `linkTwitter() => void`
@@ -492,6 +504,191 @@ The `unlinkTikTok` method unlinks the user's TikTok account from Origin.
492
504
  await auth.unlinkTikTok();
493
505
  ```
494
506
 
507
+ ## Node.js Support
508
+
509
+ The Origin SDK supports Node.js environments, allowing you to authenticate and interact with Origin using server-side signers like ethers or viem.
510
+
511
+ ### Installation for Node.js
512
+
513
+ ```bash
514
+ # With ethers
515
+ npm install @campnetwork/origin ethers
516
+
517
+ # With viem
518
+ npm install @campnetwork/origin viem
519
+ ```
520
+
521
+ ### Key Differences from Browser Usage
522
+
523
+ 1. **No Browser Provider Detection**: In Node.js, you explicitly provide a signer instead of detecting browser wallets
524
+ 2. **Storage**: By default, Node.js uses in-memory storage (not persisted). You can provide a custom storage adapter
525
+ 3. **OAuth Social Linking**: Social account linking requires browser environment for OAuth flow
526
+ 4. **SIWE Domain/URI**: You must provide domain and URI for SIWE messages
527
+
528
+ ### Using with ethers
529
+
530
+ ```js
531
+ import { Auth, campMainnet } from "@campnetwork/origin";
532
+ import { ethers } from "ethers";
533
+
534
+ // Setup ethers provider and signer
535
+ const provider = new ethers.JsonRpcProvider(
536
+ process.env.RPC_URL || campMainnet.rpcUrls.default.http[0]
537
+ );
538
+ const signer = new ethers.Wallet(process.env.PRIVATE_KEY, provider);
539
+
540
+ // Create Auth instance
541
+ const auth = new Auth({
542
+ clientId: process.env.CLIENT_ID,
543
+ redirectUri: "https://myapp.com/callback",
544
+ environment: "PRODUCTION",
545
+ });
546
+
547
+ // Connect using ethers signer
548
+ const result = await auth.connectWithSigner(signer, {
549
+ domain: "myapp.com",
550
+ uri: "https://myapp.com",
551
+ });
552
+
553
+ console.log("Connected!", result.walletAddress);
554
+
555
+ // Use origin methods
556
+ if (auth.origin) {
557
+ const terms = await auth.origin.getTerms(tokenId);
558
+ console.log("Terms:", terms);
559
+ }
560
+ ```
561
+
562
+ ### Using with viem
563
+
564
+ ```js
565
+ import { Auth, createNodeWalletClient, campMainnet } from "@campnetwork/origin";
566
+ import { privateKeyToAccount } from "viem/accounts";
567
+
568
+ // Create viem account from private key
569
+ const account = privateKeyToAccount(process.env.PRIVATE_KEY);
570
+
571
+ // Create wallet client for Node.js using Camp Network chain
572
+ const client = createNodeWalletClient(
573
+ account,
574
+ campMainnet,
575
+ process.env.RPC_URL || campMainnet.rpcUrls.default.http[0]
576
+ );
577
+
578
+ // Create Auth instance
579
+ const auth = new Auth({
580
+ clientId: process.env.CLIENT_ID,
581
+ redirectUri: "https://myapp.com/callback",
582
+ environment: "PRODUCTION",
583
+ });
584
+
585
+ // Connect using viem client
586
+ await auth.connectWithSigner(client, {
587
+ domain: "myapp.com",
588
+ uri: "https://myapp.com",
589
+ });
590
+
591
+ console.log("Authenticated:", auth.isAuthenticated);
592
+ ```
593
+
594
+ ### Exported Chain Configurations
595
+
596
+ The SDK exports Camp Network chain configurations for easy use:
597
+
598
+ ```js
599
+ import { campMainnet, campTestnet } from "@campnetwork/origin";
600
+
601
+ // campMainnet - Chain ID: 484 (Production)
602
+ // campTestnet - Chain ID: 123420001114 (Basecamp testnet)
603
+
604
+ console.log(campMainnet.rpcUrls.default.http[0]); // RPC URL
605
+ console.log(campMainnet.blockExplorers.default.url); // Block explorer
606
+ ```
607
+
608
+ ### Custom Storage Adapter
609
+
610
+ By default, Node.js uses in-memory storage. You can provide a custom storage adapter for persistence:
611
+
612
+ ```js
613
+ import { Auth, MemoryStorage } from "@campnetwork/origin";
614
+
615
+ // Custom file-based storage
616
+ class FileStorage {
617
+ async getItem(key) {
618
+ /* read from file */
619
+ }
620
+ async setItem(key, value) {
621
+ /* write to file */
622
+ }
623
+ async removeItem(key) {
624
+ /* delete from file */
625
+ }
626
+ }
627
+
628
+ const auth = new Auth({
629
+ clientId: process.env.CLIENT_ID,
630
+ redirectUri: "https://myapp.com/callback",
631
+ environment: "PRODUCTION",
632
+ storage: new FileStorage(), // Custom storage
633
+ });
634
+ ```
635
+
636
+ ### Methods
637
+
638
+ #### connectWithSigner
639
+
640
+ `connectWithSigner(signer: any, options?: { domain?: string, uri?: string }) => Promise<{ success: boolean, message: string, walletAddress: string }>`
641
+
642
+ Connect with a custom signer (viem WalletClient, ethers Signer, or custom signer implementation).
643
+
644
+ ```js
645
+ await auth.connectWithSigner(signer, {
646
+ domain: "myapp.com", // Required: Your application domain
647
+ uri: "https://myapp.com", // Required: Your application URI
648
+ });
649
+ ```
650
+
651
+ **Supported Signer Types:**
652
+
653
+ - **viem WalletClient** - Automatically detected and used
654
+ - **ethers Signer** (v5 or v6) - Works with both versions
655
+ - **Custom Signer** - Must implement `getAddress()`, `signMessage()`, and `getChainId()` methods
656
+
657
+ ### Exported Types and Utilities
658
+
659
+ ```js
660
+ import {
661
+ // Auth class
662
+ Auth,
663
+
664
+ // Signer adapters
665
+ ViemSignerAdapter,
666
+ EthersSignerAdapter,
667
+ CustomSignerAdapter,
668
+ createSignerAdapter,
669
+
670
+ // Storage adapters
671
+ BrowserStorage,
672
+ MemoryStorage,
673
+
674
+ // Viem helpers
675
+ createNodeWalletClient,
676
+
677
+ // Chain configs
678
+ campMainnet,
679
+ campTestnet,
680
+ } from "@campnetwork/origin";
681
+ ```
682
+
683
+ ### Examples
684
+
685
+ See the [examples/server-side](./examples/server-side) directory for complete Node.js examples including:
686
+
687
+ - `connect-with-ethers.js` - Using ethers v6 Signer
688
+ - `connect-with-viem.js` - Using viem WalletClient
689
+ - `connect-with-custom-signer.js` - Custom signer implementation
690
+ - `query-origin-data.js` - Querying blockchain data
691
+
495
692
  # React
496
693
 
497
694
  The React components and hooks can be imported as ES6 modules. The example below shows how to set up the `CampProvider` component and subsequently use the provided hooks and components.
@@ -519,7 +716,7 @@ createRoot(document.getElementById("root")).render(
519
716
 
520
717
  ## CampProvider
521
718
 
522
- The `CampProvider` component requires a `clientId` prop to be passed in order link the users to your app.
719
+ The `CampProvider` component requires a `clientId` prop to be passed in order to link the users to your app.
523
720
  It can also take the following optional props:
524
721
 
525
722
  - `redirectUri` - `string | object` - Either a string that will be used as the redirect URI for all socials, or an object with the following optional properties: `twitter`, `spotify`. This is used to redirect the user to different pages after they have completed the OAuth flow for a social.
@@ -955,6 +1152,36 @@ When minting or updating an IpNFT, the following constraints apply to the `Licen
955
1152
  - The royaltyBps must be between `1` and `10000` (0.01% to 100%).
956
1153
  - The duration must be between `86400` seconds and `2628000` seconds (1 day to 30 days).
957
1154
 
1155
+ ### `createLicenseTerms(price, duration, royaltyBps, paymentToken)`
1156
+
1157
+ A utility function to create properly validated license terms for minting and updating IpNFTs.
1158
+
1159
+ - `price`: Price in wei (bigint)
1160
+ - `duration`: Duration in seconds (number)
1161
+ - `royaltyBps`: Royalty in basis points (number)
1162
+ - `paymentToken`: Payment token address (Address) - use `zeroAddress` from viem for native currency
1163
+ - **Returns:** A validated `LicenseTerms` object
1164
+ - **Throws:** Error if any parameter violates the constraints
1165
+
1166
+ **Example:**
1167
+
1168
+ ```typescript
1169
+ import { createLicenseTerms } from "@campnetwork/origin";
1170
+ import { zeroAddress } from "viem";
1171
+
1172
+ // Create license terms with validation
1173
+ const license = createLicenseTerms(
1174
+ BigInt("1000000000000000"), // 0.001 CAMP in wei
1175
+ 86400, // 1 day in seconds
1176
+ 1000, // 10% royalty (1000 basis points)
1177
+ zeroAddress // Native currency (CAMP)
1178
+ );
1179
+
1180
+ // Use with minting functions
1181
+ await auth.origin.mintFile(file, metadata, license);
1182
+ await auth.origin.mintSocial("twitter", metadata, license);
1183
+ ```
1184
+
958
1185
  ### File Upload & Minting
959
1186
 
960
1187
  #### `mintFile(file, metadata, license, parents?, options?)`