ryt-sdk 1.0.8 → 1.0.10

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/wallet.js CHANGED
@@ -1,81 +1,129 @@
1
1
  import { ethers } from "ethers";
2
2
  /**
3
- * RYTWallet is a wallet instance that supports signing transactions and interacting with the blockchain.
3
+ * Unified wallet abstraction for RYT SDK.
4
+ *
5
+ * Supports:
6
+ * - Private key wallets (Node.js / backend)
7
+ * - Browser injected wallets (MetaMask, Rabby, etc.)
8
+ *
9
+ * Automatically adapts behavior based on runtime environment.
4
10
  */
5
11
  export default class RYTWallet {
6
12
  /**
7
- * Create a new RYTWallet instance
13
+ * Create a new wallet instance.
8
14
  *
9
- * @param privateKey - Ethereum private key (0x...)
10
- * @param provider - Optional RYTProvider instance for network access
15
+ * The wallet can operate in two modes:
11
16
  *
12
- * @example
13
- * ```ts
17
+ * 1. Backend mode (private key provided)
18
+ * - Full signing capability
19
+ * - Used in Node.js scripts, servers, bots
20
+ *
21
+ * 2. Browser mode (injected wallet)
22
+ * - Uses connected wallet (MetaMask, etc.)
23
+ * - No private key required or allowed
24
+ *
25
+ * @param signerOrPrivateKey
26
+ * - string → private key (backend mode)
27
+ * - signer → injected browser signer
28
+ *
29
+ * @param provider Optional provider instance for network access
30
+ *
31
+ * @throws Error if invalid input is provided
32
+ *
33
+ * @example Backend:
14
34
  * const wallet = new RYTWallet(PRIVATE_KEY, provider);
15
- * ```
35
+ *
36
+ * @example Browser:
37
+ * const wallet = new RYTWallet(signer);
16
38
  */
17
- constructor(privateKey, provider) {
18
- if (!privateKey) {
19
- throw new Error("Private key is required");
39
+ constructor(signerOrPrivateKey, provider) {
40
+ if (!signerOrPrivateKey) {
41
+ throw new Error("Wallet signer or private key is required");
20
42
  }
21
- // Ensure private key format is valid
22
- if (!privateKey.startsWith("0x")) {
23
- throw new Error("Invalid private key format: must start with 0x");
43
+ /**
44
+ * -----------------------------
45
+ * Backend mode (private key)
46
+ * -----------------------------
47
+ */
48
+ if (typeof signerOrPrivateKey === "string") {
49
+ if (!signerOrPrivateKey.startsWith("0x")) {
50
+ throw new Error("Invalid private key format (must start with 0x)");
51
+ }
52
+ const rpcProvider = provider?.getProvider();
53
+ this.signer = rpcProvider
54
+ ? new ethers.Wallet(signerOrPrivateKey, rpcProvider)
55
+ : new ethers.Wallet(signerOrPrivateKey);
56
+ return;
24
57
  }
25
- const ethersProvider = provider?.getProvider();
26
- this.wallet = ethersProvider
27
- ? new ethers.Wallet(privateKey, ethersProvider)
28
- : new ethers.Wallet(privateKey);
58
+ /**
59
+ * -----------------------------
60
+ * Browser mode (injected signer)
61
+ * -----------------------------
62
+ */
63
+ this.signer = signerOrPrivateKey;
29
64
  }
30
65
  /**
31
- * Returns the wallet address
66
+ * Get wallet address.
32
67
  *
33
68
  * @returns Wallet public address
34
69
  *
35
70
  * @example
36
- * ```ts
37
- * const address = wallet.getAddress();
38
- * ```
71
+ * const address = await wallet.getAddress();
39
72
  */
40
- getAddress() {
41
- return this.wallet.address;
73
+ async getAddress() {
74
+ return await this.signer.getAddress();
42
75
  }
43
76
  /**
44
- * Get wallet balance in wei
77
+ * Get wallet balance in wei.
45
78
  *
46
- * @returns Promise resolving to balance (bigint)
79
+ * Requires connected provider (RPC or injected).
47
80
  *
48
- * @throws Error if provider is not connected
81
+ * @returns Balance in wei
49
82
  *
50
- * @example
51
- * ```ts
52
- * const balance = await wallet.getBalance();
53
- * ```
83
+ * @throws Error if provider is not available
54
84
  */
55
85
  async getBalance() {
56
- if (!this.wallet.provider) {
57
- throw new Error("Provider not initialized. Attach a provider to fetch balance.");
86
+ if (!this.signer.provider) {
87
+ throw new Error("No provider available. Connect wallet or attach provider.");
58
88
  }
59
- return await this.wallet.provider.getBalance(this.wallet.address);
89
+ return await this.signer.provider.getBalance(await this.signer.getAddress());
60
90
  }
61
91
  /**
62
- * Send a signed transaction
92
+ * Send a blockchain transaction.
93
+ *
94
+ * Works in both:
95
+ * - Backend (signed via private key)
96
+ * - Browser (signed via MetaMask / injected wallet)
63
97
  *
64
- * @param tx - Transaction object (to, value, data, etc.)
65
- * @returns Promise resolving to transaction response
98
+ * @param tx Transaction request object
99
+ * @returns Transaction response
66
100
  *
67
101
  * @example
68
- * ```ts
69
102
  * await wallet.sendTransaction({
70
- * to: "0xabc...",
71
- * value: ethers.parseEther("0.01")
103
+ * to: "0x...",
104
+ * value: "0.01"
72
105
  * });
73
- * ```
74
106
  */
75
107
  async sendTransaction(tx) {
76
- if (!this.wallet) {
108
+ if (!this.signer) {
77
109
  throw new Error("Wallet not initialized");
78
110
  }
79
- return await this.wallet.sendTransaction(tx);
111
+ return await this.signer.sendTransaction(tx);
112
+ }
113
+ /**
114
+ * Check if wallet is connected to a provider.
115
+ *
116
+ * @returns true if provider is available
117
+ */
118
+ isConnected() {
119
+ return !!this.signer.provider;
120
+ }
121
+ /**
122
+ * Get underlying signer instance.
123
+ *
124
+ * Useful for advanced contract interactions.
125
+ */
126
+ getSigner() {
127
+ return this.signer;
80
128
  }
81
129
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "ryt-sdk",
3
3
  "type": "module",
4
- "version": "1.0.8",
4
+ "version": "1.0.10",
5
5
  "description": "RYT blockchain SDK for provider, wallet, and smart contract interactions",
6
6
  "author": "Muhammad Hammad Mubeen",
7
7
  "license": "MIT",
@@ -59,12 +59,5 @@
59
59
  "publishConfig": {
60
60
  "access": "public"
61
61
  },
62
- "repository": {
63
- "type": "git",
64
- "url": "https://github.com/Hammad-Mubeen/ryt-sdk.git"
65
- },
66
- "homepage": "https://ryt-docs.vercel.app",
67
- "bugs": {
68
- "url": "https://github.com/Hammad-Mubeen/ryt-sdk/issues"
69
- }
62
+ "homepage": "https://ryt-docs.vercel.app"
70
63
  }