prividium 0.0.1-beta → 0.0.2-beta
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 +58 -3
- package/dist/index.d.ts +1 -1
- package/dist/prividium-chain.d.ts +10 -0
- package/dist/prividium-chain.js +102 -0
- package/dist/types.d.ts +26 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# Prividium SDK
|
|
2
2
|
|
|
3
|
-
A TypeScript SDK for integrating with Prividium
|
|
4
|
-
communication for blockchain applications.
|
|
3
|
+
A TypeScript SDK for integrating with the Prividium authorization system, providing seamless authentication and secure
|
|
4
|
+
RPC communication for blockchain applications.
|
|
5
5
|
|
|
6
6
|
## Features
|
|
7
7
|
|
|
@@ -33,6 +33,8 @@ const prividiumChain = defineChain({
|
|
|
33
33
|
});
|
|
34
34
|
|
|
35
35
|
// Create SDK instance
|
|
36
|
+
// Note: replace the URLs and clientId with your actual values
|
|
37
|
+
// Make sure clientId is registered in User Panel (OAUTH_CLIENTS)
|
|
36
38
|
const prividium = createPrividiumChain({
|
|
37
39
|
clientId: 'your-client-id',
|
|
38
40
|
chain: prividiumChain,
|
|
@@ -70,7 +72,60 @@ const balance = await client.getBalance({
|
|
|
70
72
|
});
|
|
71
73
|
```
|
|
72
74
|
|
|
73
|
-
### 4.
|
|
75
|
+
### 4. Sending Transactions with Injected Wallets (MetaMask)
|
|
76
|
+
|
|
77
|
+
Before sending transactions through injected wallets, you need to add the Prividium network and enable wallet RPC for
|
|
78
|
+
each transaction.
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
import { createWalletClient, custom, encodeFunctionData } from 'viem';
|
|
82
|
+
|
|
83
|
+
// Add Prividium network to MetaMask
|
|
84
|
+
await prividium.addNetworkToWallet();
|
|
85
|
+
|
|
86
|
+
// Create wallet client for MetaMask
|
|
87
|
+
const walletClient = createWalletClient({
|
|
88
|
+
chain: prividium.chain,
|
|
89
|
+
transport: custom(window.ethereum)
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
const [address] = await walletClient.getAddresses();
|
|
93
|
+
|
|
94
|
+
// Prepare transaction with viem
|
|
95
|
+
const greeterContract = '0x...';
|
|
96
|
+
const request = await walletClient.prepareTransactionRequest({
|
|
97
|
+
account: address,
|
|
98
|
+
to: greeterContract,
|
|
99
|
+
data: encodeFunctionData({
|
|
100
|
+
abi: [
|
|
101
|
+
{
|
|
102
|
+
name: 'setGreeting',
|
|
103
|
+
type: 'function',
|
|
104
|
+
inputs: [{ name: 'greeting', type: 'string' }],
|
|
105
|
+
outputs: []
|
|
106
|
+
}
|
|
107
|
+
],
|
|
108
|
+
functionName: 'setGreeting',
|
|
109
|
+
args: ['Hello, Prividium!']
|
|
110
|
+
})
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
// Enable wallet token for this transaction
|
|
114
|
+
await prividium.enableWalletToken({
|
|
115
|
+
walletAddress: address,
|
|
116
|
+
contractAddress: greeterContract,
|
|
117
|
+
nonce: Number(request.nonce),
|
|
118
|
+
calldata: request.data
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
// Send transaction through MetaMask
|
|
122
|
+
const hash = await walletClient.sendTransaction(request);
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
**Note:** Call `enableWalletToken(...)` before each transaction. Permission is transaction-specific and expires after 1
|
|
126
|
+
hour.
|
|
127
|
+
|
|
128
|
+
### 5. Setup OAuth Callback Page
|
|
74
129
|
|
|
75
130
|
The SDK requires a callback page to complete the authentication flow securely using `postMessage`. Create a callback
|
|
76
131
|
page at the `redirectUrl` you configured:
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { createPrividiumChain } from './prividium-chain.js';
|
|
2
|
-
export type { PrividiumConfig, PrividiumChain, PopupOptions, Storage, TokenData, UserProfile, UserRole } from './types.js';
|
|
2
|
+
export type { PrividiumConfig, PrividiumChain, PopupOptions, Storage, TokenData, UserProfile, UserRole, AddNetworkParams } from './types.js';
|
|
3
3
|
export { AUTH_ERRORS, STORAGE_KEYS } from './types.js';
|
|
4
4
|
export { LocalStorage, TokenManager } from './storage.js';
|
|
5
5
|
export { parseToken, isTokenExpired, generateRandomState } from './token-utils.js';
|
|
@@ -1,2 +1,12 @@
|
|
|
1
1
|
import { type PrividiumConfig, type PrividiumChain } from './types.js';
|
|
2
|
+
declare global {
|
|
3
|
+
interface Window {
|
|
4
|
+
ethereum?: {
|
|
5
|
+
request: (args: {
|
|
6
|
+
method: string;
|
|
7
|
+
params?: unknown[];
|
|
8
|
+
}) => Promise<unknown>;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
}
|
|
2
12
|
export declare function createPrividiumChain(config: PrividiumConfig): PrividiumChain;
|
package/dist/prividium-chain.js
CHANGED
|
@@ -93,6 +93,108 @@ export function createPrividiumChain(config) {
|
|
|
93
93
|
roles: userData.roles,
|
|
94
94
|
walletAddresses: userData.walletAddresses
|
|
95
95
|
};
|
|
96
|
+
},
|
|
97
|
+
async getWalletToken() {
|
|
98
|
+
const headers = getAuthHeaders();
|
|
99
|
+
if (!headers) {
|
|
100
|
+
throw new Error('Authentication required. Please call authorize() first.');
|
|
101
|
+
}
|
|
102
|
+
const response = await fetch(`${config.permissionsApiBaseUrl}/api/wallet/personal-rpc-token`, {
|
|
103
|
+
method: 'GET',
|
|
104
|
+
headers: {
|
|
105
|
+
'Content-Type': 'application/json',
|
|
106
|
+
...headers
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
if (response.status === 403) {
|
|
110
|
+
tokenManager.clearToken();
|
|
111
|
+
config.onAuthExpiry?.();
|
|
112
|
+
throw new Error('Authentication required. Please call authorize() first.');
|
|
113
|
+
}
|
|
114
|
+
if (!response.ok) {
|
|
115
|
+
throw new Error(`Failed to fetch wallet token: ${response.status} ${response.statusText}`);
|
|
116
|
+
}
|
|
117
|
+
const data = (await response.json());
|
|
118
|
+
return data.token;
|
|
119
|
+
},
|
|
120
|
+
async getWalletRpcUrl() {
|
|
121
|
+
const walletToken = await this.getWalletToken();
|
|
122
|
+
// Extract base URL without /api path
|
|
123
|
+
const baseUrl = config.rpcUrl.replace(/\/rpc.*$/, '');
|
|
124
|
+
return `${baseUrl}/wallet/${walletToken}`;
|
|
125
|
+
},
|
|
126
|
+
async invalidateWalletToken() {
|
|
127
|
+
const headers = getAuthHeaders();
|
|
128
|
+
if (!headers) {
|
|
129
|
+
throw new Error('Authentication required. Please call authorize() first.');
|
|
130
|
+
}
|
|
131
|
+
const response = await fetch(`${config.permissionsApiBaseUrl}/api/wallet/invalidate`, {
|
|
132
|
+
method: 'POST',
|
|
133
|
+
headers: {
|
|
134
|
+
'Content-Type': 'application/json',
|
|
135
|
+
...headers
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
if (!response.ok) {
|
|
139
|
+
throw new Error(`Failed to invalidate wallet token: ${response.status} ${response.statusText}`);
|
|
140
|
+
}
|
|
141
|
+
const result = (await response.json());
|
|
142
|
+
return result.newWalletToken;
|
|
143
|
+
},
|
|
144
|
+
async enableWalletToken(params) {
|
|
145
|
+
const headers = getAuthHeaders();
|
|
146
|
+
if (!headers) {
|
|
147
|
+
throw new Error('Authentication required. Please call authorize() first.');
|
|
148
|
+
}
|
|
149
|
+
const response = await fetch(`${config.permissionsApiBaseUrl}/api/wallet/enable`, {
|
|
150
|
+
method: 'POST',
|
|
151
|
+
headers: {
|
|
152
|
+
'Content-Type': 'application/json',
|
|
153
|
+
...headers
|
|
154
|
+
},
|
|
155
|
+
body: JSON.stringify(params)
|
|
156
|
+
});
|
|
157
|
+
if (response.status === 403) {
|
|
158
|
+
tokenManager.clearToken();
|
|
159
|
+
config.onAuthExpiry?.();
|
|
160
|
+
throw new Error('Authentication required. Please call authorize() first.');
|
|
161
|
+
}
|
|
162
|
+
if (!response.ok) {
|
|
163
|
+
throw new Error(`Failed to enable wallet token: ${response.status} ${response.statusText}`);
|
|
164
|
+
}
|
|
165
|
+
const result = (await response.json());
|
|
166
|
+
return result;
|
|
167
|
+
},
|
|
168
|
+
async addNetworkToWallet(params) {
|
|
169
|
+
if (typeof window === 'undefined' || !window.ethereum) {
|
|
170
|
+
throw new Error('Wallet not detected. Please install a wallet extension to add network.');
|
|
171
|
+
}
|
|
172
|
+
const walletRpcUrl = await this.getWalletRpcUrl();
|
|
173
|
+
const chainIdHex = `0x${config.chain.id.toString(16)}`;
|
|
174
|
+
const networkParams = {
|
|
175
|
+
chainId: params?.chainId || chainIdHex,
|
|
176
|
+
chainName: params?.chainName || config.chain.name,
|
|
177
|
+
nativeCurrency: params?.nativeCurrency || {
|
|
178
|
+
name: config.chain.nativeCurrency?.name || 'Ether',
|
|
179
|
+
symbol: config.chain.nativeCurrency?.symbol || 'ETH',
|
|
180
|
+
decimals: config.chain.nativeCurrency?.decimals || 18
|
|
181
|
+
},
|
|
182
|
+
rpcUrls: [walletRpcUrl],
|
|
183
|
+
blockExplorerUrls: params?.blockExplorerUrls ||
|
|
184
|
+
(config.chain.blockExplorers?.default?.url ? [config.chain.blockExplorers.default.url] : undefined)
|
|
185
|
+
};
|
|
186
|
+
try {
|
|
187
|
+
if (!window.ethereum) {
|
|
188
|
+
throw new Error('Wallet not detected');
|
|
189
|
+
}
|
|
190
|
+
await window.ethereum.request({
|
|
191
|
+
method: 'wallet_addEthereumChain',
|
|
192
|
+
params: [networkParams]
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
catch (error) {
|
|
196
|
+
throw new Error(`Failed to add network to wallet: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
197
|
+
}
|
|
96
198
|
}
|
|
97
199
|
};
|
|
98
200
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Chain, type Transport } from 'viem';
|
|
1
|
+
import { type Chain, type Transport, type Address, type Hex } from 'viem';
|
|
2
2
|
import { type OauthScope } from './popup-auth.js';
|
|
3
3
|
export interface Storage {
|
|
4
4
|
getItem(key: string): string | null;
|
|
@@ -27,6 +27,26 @@ export interface UserProfile {
|
|
|
27
27
|
roles: UserRole[];
|
|
28
28
|
walletAddresses: string[];
|
|
29
29
|
}
|
|
30
|
+
export interface AddNetworkParams {
|
|
31
|
+
chainName?: string;
|
|
32
|
+
chainId: string;
|
|
33
|
+
nativeCurrency?: {
|
|
34
|
+
name: string;
|
|
35
|
+
symbol: string;
|
|
36
|
+
decimals: number;
|
|
37
|
+
};
|
|
38
|
+
blockExplorerUrls?: string[];
|
|
39
|
+
}
|
|
40
|
+
export interface EnableWalletTokenParams {
|
|
41
|
+
walletAddress: Address;
|
|
42
|
+
contractAddress: Address;
|
|
43
|
+
nonce: number;
|
|
44
|
+
calldata: Hex;
|
|
45
|
+
}
|
|
46
|
+
export interface EnableWalletTokenResponse {
|
|
47
|
+
message: string;
|
|
48
|
+
activeUntil: string;
|
|
49
|
+
}
|
|
30
50
|
export interface PrividiumChain {
|
|
31
51
|
chain: Chain;
|
|
32
52
|
transport: Transport;
|
|
@@ -40,6 +60,11 @@ export interface PrividiumChain {
|
|
|
40
60
|
isAuthorized(): boolean;
|
|
41
61
|
getAuthHeaders(): Record<string, string> | null;
|
|
42
62
|
fetchUser(): Promise<UserProfile>;
|
|
63
|
+
getWalletToken(): Promise<string>;
|
|
64
|
+
getWalletRpcUrl(): Promise<string>;
|
|
65
|
+
invalidateWalletToken(): Promise<string>;
|
|
66
|
+
enableWalletToken(params: EnableWalletTokenParams): Promise<EnableWalletTokenResponse>;
|
|
67
|
+
addNetworkToWallet(params?: AddNetworkParams): Promise<void>;
|
|
43
68
|
}
|
|
44
69
|
export interface TokenData {
|
|
45
70
|
rawToken: string;
|