@dynamic-labs/solana 0.0.0-exp20240808.0

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.
Files changed (91) hide show
  1. package/CHANGELOG.md +3746 -0
  2. package/LICENSE +21 -0
  3. package/README.md +0 -0
  4. package/_virtual/_tslib.cjs +36 -0
  5. package/_virtual/_tslib.js +32 -0
  6. package/package.json +48 -0
  7. package/src/CoinbaseSolana.cjs +52 -0
  8. package/src/CoinbaseSolana.d.ts +9 -0
  9. package/src/CoinbaseSolana.js +48 -0
  10. package/src/Phantom.cjs +44 -0
  11. package/src/Phantom.d.ts +13 -0
  12. package/src/Phantom.js +40 -0
  13. package/src/SolanaWalletStandardConnector/SolanaWalletStandardConnector.cjs +100 -0
  14. package/src/SolanaWalletStandardConnector/SolanaWalletStandardConnector.d.ts +21 -0
  15. package/src/SolanaWalletStandardConnector/SolanaWalletStandardConnector.js +96 -0
  16. package/src/SolanaWalletStandardConnector/index.d.ts +1 -0
  17. package/src/SolanaWalletStandardConnector/utils/createSolanaSignerFromWalletStandard/createSolanaSignerFromWalletStandard.cjs +117 -0
  18. package/src/SolanaWalletStandardConnector/utils/createSolanaSignerFromWalletStandard/createSolanaSignerFromWalletStandard.d.ts +4 -0
  19. package/src/SolanaWalletStandardConnector/utils/createSolanaSignerFromWalletStandard/createSolanaSignerFromWalletStandard.js +113 -0
  20. package/src/SolanaWalletStandardConnector/utils/createSolanaSignerFromWalletStandard/index.d.ts +1 -0
  21. package/src/Solflare.cjs +63 -0
  22. package/src/Solflare.d.ts +9 -0
  23. package/src/Solflare.js +59 -0
  24. package/src/contants.cjs +8 -0
  25. package/src/contants.d.ts +1 -0
  26. package/src/contants.js +4 -0
  27. package/src/errors/SignMessageNotSupportedError.cjs +17 -0
  28. package/src/errors/SignMessageNotSupportedError.d.ts +4 -0
  29. package/src/errors/SignMessageNotSupportedError.js +13 -0
  30. package/src/index.cjs +30 -0
  31. package/src/index.d.ts +7 -0
  32. package/src/index.js +22 -0
  33. package/src/injected/BackpackSol.cjs +46 -0
  34. package/src/injected/BackpackSol.d.ts +9 -0
  35. package/src/injected/BackpackSol.js +42 -0
  36. package/src/injected/FallbackSolanaConnector.cjs +22 -0
  37. package/src/injected/FallbackSolanaConnector.d.ts +9 -0
  38. package/src/injected/FallbackSolanaConnector.js +18 -0
  39. package/src/injected/InjectedWalletBase.cjs +118 -0
  40. package/src/injected/InjectedWalletBase.d.ts +21 -0
  41. package/src/injected/InjectedWalletBase.js +114 -0
  42. package/src/injected/PhantomInjected.cjs +37 -0
  43. package/src/injected/PhantomInjected.d.ts +8 -0
  44. package/src/injected/PhantomInjected.js +33 -0
  45. package/src/injected/UnknownInjected.cjs +21 -0
  46. package/src/injected/UnknownInjected.d.ts +8 -0
  47. package/src/injected/UnknownInjected.js +17 -0
  48. package/src/injected/fetchInjectedWalletConnectors.cjs +63 -0
  49. package/src/injected/fetchInjectedWalletConnectors.d.ts +6 -0
  50. package/src/injected/fetchInjectedWalletConnectors.js +58 -0
  51. package/src/injected/index.d.ts +3 -0
  52. package/src/phantomRedirect/PhantomRedirect.cjs +337 -0
  53. package/src/phantomRedirect/PhantomRedirect.d.ts +37 -0
  54. package/src/phantomRedirect/PhantomRedirect.js +328 -0
  55. package/src/phantomRedirect/storage.cjs +96 -0
  56. package/src/phantomRedirect/storage.d.ts +40 -0
  57. package/src/phantomRedirect/storage.js +91 -0
  58. package/src/phantomRedirect/types.d.ts +1 -0
  59. package/src/phantomRedirect/utils.cjs +56 -0
  60. package/src/phantomRedirect/utils.d.ts +4 -0
  61. package/src/phantomRedirect/utils.js +44 -0
  62. package/src/solProviderHelper.cjs +173 -0
  63. package/src/solProviderHelper.d.ts +36 -0
  64. package/src/solProviderHelper.js +169 -0
  65. package/src/solWalletConnector.cjs +174 -0
  66. package/src/solWalletConnector.d.ts +37 -0
  67. package/src/solWalletConnector.js +170 -0
  68. package/src/types.d.ts +68 -0
  69. package/src/utils/extractNonce.cjs +14 -0
  70. package/src/utils/extractNonce.d.ts +1 -0
  71. package/src/utils/extractNonce.js +10 -0
  72. package/src/utils/findWalletProviderFromWalletStandard/findWalletProviderFromWalletStandard.cjs +29 -0
  73. package/src/utils/findWalletProviderFromWalletStandard/findWalletProviderFromWalletStandard.d.ts +3 -0
  74. package/src/utils/findWalletProviderFromWalletStandard/findWalletProviderFromWalletStandard.js +25 -0
  75. package/src/utils/findWalletProviderFromWalletStandard/index.d.ts +1 -0
  76. package/src/utils/getGenesisHashLSKey.cjs +10 -0
  77. package/src/utils/getGenesisHashLSKey.d.ts +1 -0
  78. package/src/utils/getGenesisHashLSKey.js +6 -0
  79. package/src/utils/isBackpackSolanaSigner.cjs +8 -0
  80. package/src/utils/isBackpackSolanaSigner.d.ts +2 -0
  81. package/src/utils/isBackpackSolanaSigner.js +4 -0
  82. package/src/utils/isSignedMessage.cjs +8 -0
  83. package/src/utils/isSignedMessage.d.ts +2 -0
  84. package/src/utils/isSignedMessage.js +4 -0
  85. package/src/wallet/SolanaWallet.cjs +33 -0
  86. package/src/wallet/SolanaWallet.d.ts +17 -0
  87. package/src/wallet/SolanaWallet.js +29 -0
  88. package/src/wallet/index.d.ts +2 -0
  89. package/src/wallet/isSolanaWallet.cjs +8 -0
  90. package/src/wallet/isSolanaWallet.d.ts +3 -0
  91. package/src/wallet/isSolanaWallet.js +4 -0
@@ -0,0 +1,328 @@
1
+ 'use client'
2
+ import { __awaiter } from '../../_virtual/_tslib.js';
3
+ import nacl from 'tweetnacl';
4
+ import bs58 from 'bs58';
5
+ import { PublicKey, Transaction } from '@solana/web3.js';
6
+ import { SolWalletConnector } from '../solWalletConnector.js';
7
+ import { fetchCluster, buildUrl, decryptPayload, encryptPayload } from './utils.js';
8
+ import { storage, clearStorage } from './storage.js';
9
+
10
+ class PhantomRedirect extends SolWalletConnector {
11
+ constructor(props) {
12
+ super(Object.assign({}, props));
13
+ this.name = 'Phantom';
14
+ this.overrideKey = 'phantom';
15
+ }
16
+ getMethod() {
17
+ throw new Error('Method not implemented.');
18
+ }
19
+ getAddress() {
20
+ return __awaiter(this, void 0, void 0, function* () {
21
+ const address = storage.address.get();
22
+ if (address) {
23
+ return address;
24
+ }
25
+ yield this.connect();
26
+ return undefined;
27
+ });
28
+ }
29
+ connect() {
30
+ return __awaiter(this, void 0, void 0, function* () {
31
+ var _a, _b;
32
+ // Generate a new key pair
33
+ const keyPair = nacl.box.keyPair();
34
+ storage.encryptionPublicKey.set(keyPair.publicKey);
35
+ storage.encryptionSecretKey.set(keyPair.secretKey);
36
+ const isLocalHost = window.location.href.includes('localhost') ||
37
+ window.location.href.includes('0.0.0.0') ||
38
+ window.location.href.includes('127.0.0.1');
39
+ const rpc = (_b = (_a = this.solNetworks[0].privateCustomerRpcUrls) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : this.solNetworks[0].rpcUrls[0];
40
+ const params = new URLSearchParams({
41
+ app_url: isLocalHost ? 'https://demo.dynamic.xyz' : window.location.href,
42
+ cluster: yield fetchCluster(rpc),
43
+ dapp_encryption_public_key: bs58.encode(keyPair.publicKey),
44
+ redirect_link: window.location.href,
45
+ });
46
+ const url = buildUrl('connect', params);
47
+ window.location.href = url;
48
+ });
49
+ }
50
+ getSession() {
51
+ return __awaiter(this, void 0, void 0, function* () {
52
+ const params = new URLSearchParams(window.location.search);
53
+ const { data, nonce, phantom_encryption_public_key: phantomEncryptionPublicKey, encryptionSecretKey, } = this.getInputsOrThrow('getSession', ['data', 'nonce', 'phantom_encryption_public_key'], ['encryptionSecretKey']);
54
+ const sharedSecret = nacl.box.before(bs58.decode(phantomEncryptionPublicKey), encryptionSecretKey);
55
+ storage.sharedSecret.set(sharedSecret);
56
+ const connectData = decryptPayload(data, nonce, sharedSecret);
57
+ storage.session.set(connectData.session);
58
+ storage.address.set(new PublicKey(connectData.public_key));
59
+ params.delete('phantom_encryption_public_key');
60
+ params.delete('data');
61
+ params.delete('nonce');
62
+ history.replaceState(null, '', `${window.location.origin}${window.location.pathname}?${params.toString()}`);
63
+ return connectData.public_key;
64
+ });
65
+ }
66
+ signMessage(messageToSign) {
67
+ return __awaiter(this, void 0, void 0, function* () {
68
+ const { session, sharedSecret, encryptionPublicKey } = this.getInputsOrThrow('signMessage', [], ['session', 'sharedSecret', 'encryptionPublicKey']);
69
+ storage.message.set(messageToSign);
70
+ const payload = {
71
+ message: bs58.encode(Buffer.from(messageToSign)),
72
+ session,
73
+ };
74
+ const [nonce, encryptedPayload] = encryptPayload(payload, sharedSecret);
75
+ const params = new URLSearchParams({
76
+ dapp_encryption_public_key: bs58.encode(encryptionPublicKey),
77
+ nonce: bs58.encode(nonce),
78
+ payload: bs58.encode(encryptedPayload),
79
+ redirect_link: window.location.href,
80
+ });
81
+ const url = buildUrl('signMessage', params);
82
+ storage.method.set('signMessage');
83
+ window.location.href = url;
84
+ // throwing this to prevent local storage from being cleared.
85
+ // when verifying signature, the SDK calls endSession when no
86
+ // signature is returned. in the case of phantom mobile, a signature
87
+ // is not returned from signMessage, so an error will always be thrown.
88
+ // this is a workaround to prevent the SDK from clearing local storage
89
+ // ideally we would figure out how to:
90
+ // 1. kick off the sign message on one tab
91
+ // 2. resume the process on that tab after the user signs in phantom
92
+ throw new Error('ignore');
93
+ });
94
+ }
95
+ extractSignature() {
96
+ const params = new URLSearchParams(window.location.search);
97
+ const { data, nonce, sharedSecret, message } = this.getInputsOrThrow('extractSignature', ['data', 'nonce'], ['sharedSecret', 'message']);
98
+ const signMessageData = decryptPayload(data, nonce, sharedSecret);
99
+ params.delete('data');
100
+ params.delete('nonce');
101
+ history.replaceState(null, '', `${window.location.origin}${window.location.pathname}?${params.toString()}`);
102
+ return {
103
+ message,
104
+ signature: signMessageData.signature,
105
+ };
106
+ }
107
+ extractTransactions() {
108
+ const params = new URLSearchParams(window.location.search);
109
+ const { data, nonce, sharedSecret } = this.getInputsOrThrow('extractTransactions', ['data', 'nonce'], ['sharedSecret']);
110
+ const signAllTransactionsData = decryptPayload(data, nonce, sharedSecret);
111
+ const decodedTransactions = signAllTransactionsData.transactions.map((t) => Transaction.from(bs58.decode(t)));
112
+ params.delete('data');
113
+ params.delete('nonce');
114
+ history.replaceState(null, '', `${window.location.origin}${window.location.pathname}?${params.toString()}`);
115
+ return decodedTransactions;
116
+ }
117
+ extractTransaction() {
118
+ const params = new URLSearchParams(window.location.search);
119
+ const { data, nonce, sharedSecret } = this.getInputsOrThrow('extractTransaction', ['data', 'nonce'], ['sharedSecret']);
120
+ const signTransactionData = decryptPayload(data, nonce, sharedSecret);
121
+ const decodedTransaction = Transaction.from(bs58.decode(signTransactionData.transaction));
122
+ params.delete('data');
123
+ params.delete('nonce');
124
+ history.replaceState(null, '', `${window.location.origin}${window.location.pathname}?${params.toString()}`);
125
+ return decodedTransaction;
126
+ }
127
+ consumeMethod() {
128
+ const method = storage.method.get();
129
+ storage.method.remove();
130
+ return method;
131
+ }
132
+ getSigner() {
133
+ return __awaiter(this, void 0, void 0, function* () {
134
+ const address = storage.address.get();
135
+ if (!address) {
136
+ return undefined;
137
+ }
138
+ return {
139
+ addListener: () => {
140
+ throw new Error('Not implemented');
141
+ },
142
+ connect: () => {
143
+ throw new Error('Not implemented');
144
+ },
145
+ disconnect: () => {
146
+ throw new Error('Not implemented');
147
+ },
148
+ emit: () => {
149
+ throw new Error('Not implemented');
150
+ },
151
+ eventNames: () => {
152
+ throw new Error('Not implemented');
153
+ },
154
+ isBackpack: false,
155
+ isBraveWallet: false,
156
+ isConnected: true,
157
+ isExodus: false,
158
+ isGlow: false,
159
+ isMagicEden: false,
160
+ isPhantom: true,
161
+ isSolflare: false,
162
+ listenerCount: () => {
163
+ throw new Error('Not implemented');
164
+ },
165
+ listeners: () => {
166
+ throw new Error('Not implemented');
167
+ },
168
+ off: () => {
169
+ throw new Error('Not implemented');
170
+ },
171
+ on: () => {
172
+ throw new Error('Not implemented');
173
+ },
174
+ once: () => {
175
+ throw new Error('Not implemented');
176
+ },
177
+ providers: [],
178
+ publicKey: new PublicKey(address),
179
+ removeAllListeners: () => {
180
+ throw new Error('Not implemented');
181
+ },
182
+ removeListener: () => {
183
+ throw new Error('Not implemented');
184
+ },
185
+ signAllTransactions: (transactions) => __awaiter(this, void 0, void 0, function* () {
186
+ const serializedTransactions = transactions.map((t) => bs58.encode(t.serialize({
187
+ requireAllSignatures: false,
188
+ })));
189
+ const { session, sharedSecret, encryptionPublicKey } = this.getInputsOrThrow('signAllTransactions', [], ['session', 'sharedSecret', 'encryptionPublicKey']);
190
+ const payload = {
191
+ session,
192
+ transactions: serializedTransactions,
193
+ };
194
+ const [nonce, encryptedPayload] = encryptPayload(payload, sharedSecret);
195
+ const params = new URLSearchParams({
196
+ dapp_encryption_public_key: bs58.encode(encryptionPublicKey),
197
+ nonce: bs58.encode(nonce),
198
+ payload: bs58.encode(encryptedPayload),
199
+ redirect_link: window.location.href,
200
+ });
201
+ const url = buildUrl('signAllTransactions', params);
202
+ window.location.href = url;
203
+ // actual signatures will be retrieved upon redirect back to dapp
204
+ return [];
205
+ }),
206
+ signAndSendTransaction: (transaction, options) => __awaiter(this, void 0, void 0, function* () {
207
+ const { session, sharedSecret, encryptionPublicKey } = this.getInputsOrThrow('signAndSendTransaction', [], ['session', 'sharedSecret', 'encryptionPublicKey']);
208
+ const payload = {
209
+ options,
210
+ session,
211
+ transaction: bs58.encode(transaction.serialize({ requireAllSignatures: false })),
212
+ };
213
+ const [nonce, encryptedPayload] = encryptPayload(payload, sharedSecret);
214
+ const params = new URLSearchParams({
215
+ dapp_encryption_public_key: bs58.encode(encryptionPublicKey),
216
+ nonce: bs58.encode(nonce),
217
+ payload: bs58.encode(encryptedPayload),
218
+ redirect_link: window.location.href,
219
+ });
220
+ const url = buildUrl('signAndSendTransaction', params);
221
+ storage.method.set('signAndSendTransaction');
222
+ window.location.href = url;
223
+ // actual signature will be retrived upon redirect back to dapp
224
+ return { signature: '' };
225
+ }),
226
+ signMessage: (message) => __awaiter(this, void 0, void 0, function* () {
227
+ const { session, sharedSecret, encryptionPublicKey } = this.getInputsOrThrow('signMessage', [], ['session', 'sharedSecret', 'encryptionPublicKey']);
228
+ const payload = {
229
+ message: bs58.encode(Buffer.from(message)),
230
+ session,
231
+ };
232
+ const [nonce, encryptedPayload] = encryptPayload(payload, sharedSecret);
233
+ const params = new URLSearchParams({
234
+ dapp_encryption_public_key: bs58.encode(encryptionPublicKey),
235
+ nonce: bs58.encode(nonce),
236
+ payload: bs58.encode(encryptedPayload),
237
+ redirect_link: window.location.href,
238
+ });
239
+ const url = buildUrl('signMessage', params);
240
+ window.location.href = url;
241
+ // actual signature will be retrived upon redirect back to dapp
242
+ return Buffer.from('');
243
+ }),
244
+ signTransaction: (transaction) => __awaiter(this, void 0, void 0, function* () {
245
+ const serializedTransaction = bs58.encode(transaction.serialize({
246
+ requireAllSignatures: false,
247
+ }));
248
+ const { session, sharedSecret, encryptionPublicKey } = this.getInputsOrThrow('signTransaction', [], ['session', 'sharedSecret', 'encryptionPublicKey']);
249
+ const payload = {
250
+ session,
251
+ transaction: serializedTransaction,
252
+ };
253
+ const [nonce, encryptedPayload] = encryptPayload(payload, sharedSecret);
254
+ const params = new URLSearchParams({
255
+ dapp_encryption_public_key: bs58.encode(encryptionPublicKey),
256
+ nonce: bs58.encode(nonce),
257
+ payload: bs58.encode(encryptedPayload),
258
+ redirect_link: window.location.href,
259
+ });
260
+ const url = buildUrl('signTransaction', params);
261
+ window.location.href = url;
262
+ return transaction;
263
+ }),
264
+ };
265
+ });
266
+ }
267
+ getConnectedAccounts() {
268
+ return __awaiter(this, void 0, void 0, function* () {
269
+ const address = storage.address.get();
270
+ return address ? [address] : [];
271
+ });
272
+ }
273
+ endSession() {
274
+ return __awaiter(this, void 0, void 0, function* () {
275
+ const address = storage.address.get();
276
+ const session = storage.session.get();
277
+ const sharedSecret = storage.sharedSecret.get();
278
+ const encryptionPublicKey = storage.encryptionPublicKey.get();
279
+ // Clear all local storage in case of any stale state
280
+ clearStorage();
281
+ // if there is no session/encrytion keys, then we don't need to disconnect
282
+ if (!address || !session || !encryptionPublicKey || !sharedSecret)
283
+ return;
284
+ const payload = { session };
285
+ const [nonce, encryptedPayload] = encryptPayload(payload, sharedSecret);
286
+ const params = new URLSearchParams({
287
+ dapp_encryption_public_key: bs58.encode(encryptionPublicKey),
288
+ nonce: bs58.encode(nonce),
289
+ payload: bs58.encode(encryptedPayload),
290
+ redirect_link: window.location.href,
291
+ });
292
+ const url = buildUrl('disconnect', params);
293
+ window.location.href = url;
294
+ });
295
+ }
296
+ /**
297
+ * Helper method to get inputs from query params and localstorage
298
+ *
299
+ * The second argument is used to read values from the query string
300
+ * e.g. ['data', 'nonce'] -> params.get('data') and params.get('nonce')
301
+ *
302
+ * The third argument is used to read values from local storage
303
+ * e.g. ['address', 'message'] -> storage.address.get() and storage.message.get()
304
+ *
305
+ * Throws an error if any of the inputs are unable to be found in their respective locations
306
+ */
307
+ getInputsOrThrow(methodName, queryParams, storageParams) {
308
+ const inputs = {};
309
+ const queryString = new URLSearchParams(window.location.search);
310
+ queryParams.forEach((param) => {
311
+ const value = queryString.get(param);
312
+ if (!value) {
313
+ throw new Error(`[PhantomRedirect] ${methodName} called, but required input '${param}' not found in query params`);
314
+ }
315
+ inputs[param] = value;
316
+ });
317
+ storageParams.forEach((storageParam) => {
318
+ const value = storage[storageParam].get();
319
+ if (!value) {
320
+ throw new Error(`[PhantomRedirect] ${methodName} called, but required input '${storageParam}' not found in local storage`);
321
+ }
322
+ inputs[storageParam] = value;
323
+ });
324
+ return inputs;
325
+ }
326
+ }
327
+
328
+ export { PhantomRedirect };
@@ -0,0 +1,96 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ const storage = {
7
+ address: {
8
+ get: () => { var _a; return (_a = localStorage.getItem('dynamic_phantom_wallet_address')) !== null && _a !== void 0 ? _a : undefined; },
9
+ remove: () => {
10
+ localStorage.removeItem('dynamic_phantom_wallet_address');
11
+ },
12
+ set: (address) => {
13
+ localStorage.setItem('dynamic_phantom_wallet_address', address.toString());
14
+ },
15
+ },
16
+ encryptionPublicKey: {
17
+ get: () => {
18
+ const rawPublicKey = localStorage.getItem('dynamic_phantom_public_key');
19
+ if (!rawPublicKey) {
20
+ return undefined;
21
+ }
22
+ return new Uint8Array(JSON.parse(rawPublicKey));
23
+ },
24
+ remove: () => {
25
+ localStorage.removeItem('dynamic_phantom_public_key');
26
+ },
27
+ set: (publicKey) => {
28
+ localStorage.setItem('dynamic_phantom_public_key', JSON.stringify([...publicKey]));
29
+ },
30
+ },
31
+ encryptionSecretKey: {
32
+ get: () => {
33
+ const rawSecretKey = localStorage.getItem('dynamic_phantom_secret_key');
34
+ if (!rawSecretKey) {
35
+ return undefined;
36
+ }
37
+ return new Uint8Array(JSON.parse(rawSecretKey));
38
+ },
39
+ remove: () => {
40
+ localStorage.removeItem('dynamic_phantom_secret_key');
41
+ },
42
+ set: (secretKey) => {
43
+ localStorage.setItem('dynamic_phantom_secret_key', JSON.stringify([...secretKey]));
44
+ },
45
+ },
46
+ message: {
47
+ get: () => { var _a; return (_a = localStorage.getItem('dynamic_phantom_message_to_sign')) !== null && _a !== void 0 ? _a : undefined; },
48
+ remove: () => {
49
+ localStorage.removeItem('dynamic_phantom_message_to_sign');
50
+ },
51
+ set: (message) => {
52
+ localStorage.setItem('dynamic_phantom_message_to_sign', message);
53
+ },
54
+ },
55
+ method: {
56
+ get: () => { var _a; return (_a = localStorage.getItem('dynamic_phantom_method')) !== null && _a !== void 0 ? _a : undefined; },
57
+ remove: () => {
58
+ localStorage.removeItem('dynamic_phantom_method');
59
+ },
60
+ set: (method) => {
61
+ localStorage.setItem('dynamic_phantom_method', method);
62
+ },
63
+ },
64
+ session: {
65
+ get: () => { var _a; return (_a = localStorage.getItem('dynamic_phantom_session')) !== null && _a !== void 0 ? _a : undefined; },
66
+ remove: () => {
67
+ localStorage.removeItem('dynamic_phantom_session');
68
+ },
69
+ set: (session) => {
70
+ localStorage.setItem('dynamic_phantom_session', session);
71
+ },
72
+ },
73
+ sharedSecret: {
74
+ get: () => {
75
+ const rawSharedSecret = localStorage.getItem('dynamic_phantom_shared_secret');
76
+ if (!rawSharedSecret) {
77
+ return undefined;
78
+ }
79
+ return new Uint8Array(JSON.parse(rawSharedSecret));
80
+ },
81
+ remove: () => {
82
+ localStorage.removeItem('dynamic_phantom_shared_secret');
83
+ },
84
+ set: (sharedSecret) => {
85
+ localStorage.setItem('dynamic_phantom_shared_secret', JSON.stringify([...sharedSecret]));
86
+ },
87
+ },
88
+ };
89
+ const clearStorage = () => {
90
+ for (const key in storage) {
91
+ storage[key].remove();
92
+ }
93
+ };
94
+
95
+ exports.clearStorage = clearStorage;
96
+ exports.storage = storage;
@@ -0,0 +1,40 @@
1
+ import { PublicKey } from '@solana/web3.js';
2
+ import { Method } from './types';
3
+ export declare const storage: {
4
+ address: {
5
+ get: () => string | undefined;
6
+ remove: () => void;
7
+ set: (address: PublicKey) => void;
8
+ };
9
+ encryptionPublicKey: {
10
+ get: () => Uint8Array | undefined;
11
+ remove: () => void;
12
+ set: (publicKey: Uint8Array) => void;
13
+ };
14
+ encryptionSecretKey: {
15
+ get: () => Uint8Array | undefined;
16
+ remove: () => void;
17
+ set: (secretKey: Uint8Array) => void;
18
+ };
19
+ message: {
20
+ get: () => string | undefined;
21
+ remove: () => void;
22
+ set: (message: string) => void;
23
+ };
24
+ method: {
25
+ get: () => Method | undefined;
26
+ remove: () => void;
27
+ set: (method: Method) => void;
28
+ };
29
+ session: {
30
+ get: () => string | undefined;
31
+ remove: () => void;
32
+ set: (session: string) => void;
33
+ };
34
+ sharedSecret: {
35
+ get: () => Uint8Array | undefined;
36
+ remove: () => void;
37
+ set: (sharedSecret: Uint8Array) => void;
38
+ };
39
+ };
40
+ export declare const clearStorage: () => void;
@@ -0,0 +1,91 @@
1
+ 'use client'
2
+ const storage = {
3
+ address: {
4
+ get: () => { var _a; return (_a = localStorage.getItem('dynamic_phantom_wallet_address')) !== null && _a !== void 0 ? _a : undefined; },
5
+ remove: () => {
6
+ localStorage.removeItem('dynamic_phantom_wallet_address');
7
+ },
8
+ set: (address) => {
9
+ localStorage.setItem('dynamic_phantom_wallet_address', address.toString());
10
+ },
11
+ },
12
+ encryptionPublicKey: {
13
+ get: () => {
14
+ const rawPublicKey = localStorage.getItem('dynamic_phantom_public_key');
15
+ if (!rawPublicKey) {
16
+ return undefined;
17
+ }
18
+ return new Uint8Array(JSON.parse(rawPublicKey));
19
+ },
20
+ remove: () => {
21
+ localStorage.removeItem('dynamic_phantom_public_key');
22
+ },
23
+ set: (publicKey) => {
24
+ localStorage.setItem('dynamic_phantom_public_key', JSON.stringify([...publicKey]));
25
+ },
26
+ },
27
+ encryptionSecretKey: {
28
+ get: () => {
29
+ const rawSecretKey = localStorage.getItem('dynamic_phantom_secret_key');
30
+ if (!rawSecretKey) {
31
+ return undefined;
32
+ }
33
+ return new Uint8Array(JSON.parse(rawSecretKey));
34
+ },
35
+ remove: () => {
36
+ localStorage.removeItem('dynamic_phantom_secret_key');
37
+ },
38
+ set: (secretKey) => {
39
+ localStorage.setItem('dynamic_phantom_secret_key', JSON.stringify([...secretKey]));
40
+ },
41
+ },
42
+ message: {
43
+ get: () => { var _a; return (_a = localStorage.getItem('dynamic_phantom_message_to_sign')) !== null && _a !== void 0 ? _a : undefined; },
44
+ remove: () => {
45
+ localStorage.removeItem('dynamic_phantom_message_to_sign');
46
+ },
47
+ set: (message) => {
48
+ localStorage.setItem('dynamic_phantom_message_to_sign', message);
49
+ },
50
+ },
51
+ method: {
52
+ get: () => { var _a; return (_a = localStorage.getItem('dynamic_phantom_method')) !== null && _a !== void 0 ? _a : undefined; },
53
+ remove: () => {
54
+ localStorage.removeItem('dynamic_phantom_method');
55
+ },
56
+ set: (method) => {
57
+ localStorage.setItem('dynamic_phantom_method', method);
58
+ },
59
+ },
60
+ session: {
61
+ get: () => { var _a; return (_a = localStorage.getItem('dynamic_phantom_session')) !== null && _a !== void 0 ? _a : undefined; },
62
+ remove: () => {
63
+ localStorage.removeItem('dynamic_phantom_session');
64
+ },
65
+ set: (session) => {
66
+ localStorage.setItem('dynamic_phantom_session', session);
67
+ },
68
+ },
69
+ sharedSecret: {
70
+ get: () => {
71
+ const rawSharedSecret = localStorage.getItem('dynamic_phantom_shared_secret');
72
+ if (!rawSharedSecret) {
73
+ return undefined;
74
+ }
75
+ return new Uint8Array(JSON.parse(rawSharedSecret));
76
+ },
77
+ remove: () => {
78
+ localStorage.removeItem('dynamic_phantom_shared_secret');
79
+ },
80
+ set: (sharedSecret) => {
81
+ localStorage.setItem('dynamic_phantom_shared_secret', JSON.stringify([...sharedSecret]));
82
+ },
83
+ },
84
+ };
85
+ const clearStorage = () => {
86
+ for (const key in storage) {
87
+ storage[key].remove();
88
+ }
89
+ };
90
+
91
+ export { clearStorage, storage };
@@ -0,0 +1 @@
1
+ export type Method = 'signMessage' | 'signAndSendTransaction';
@@ -0,0 +1,56 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var _tslib = require('../../_virtual/_tslib.cjs');
7
+ var nacl = require('tweetnacl');
8
+ var bs58 = require('bs58');
9
+
10
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
11
+
12
+ var nacl__default = /*#__PURE__*/_interopDefaultLegacy(nacl);
13
+ var bs58__default = /*#__PURE__*/_interopDefaultLegacy(bs58);
14
+
15
+ const buildUrl = (path, params) => `https://phantom.app/ul/v1/${path}?${params.toString()}`;
16
+ const decryptPayload = (data, nonce, sharedSecret) => {
17
+ const decryptedData = nacl__default["default"].box.open.after(bs58__default["default"].decode(data), bs58__default["default"].decode(nonce), sharedSecret);
18
+ if (!decryptedData) {
19
+ throw new Error('Unable to decrypt data');
20
+ }
21
+ return JSON.parse(Buffer.from(decryptedData).toString('utf8'));
22
+ };
23
+ const encryptPayload = (payload, sharedSecret) => {
24
+ const nonce = nacl__default["default"].randomBytes(24);
25
+ const encryptedPayload = nacl__default["default"].box.after(Buffer.from(JSON.stringify(payload)), nonce, sharedSecret);
26
+ return [nonce, encryptedPayload];
27
+ };
28
+ const GENESIS_HASHES = {
29
+ '4uhcVJyU9pJkvQyS88uRDiswHXSCkY3zQawwpjk2NsNY': 'testnet',
30
+ '5eykt4UsFv8P8NJdTREpY1vzqKqZKvdpKuc147dw2N9d': 'mainnet-beta',
31
+ EtWTRABZaYq6iMfeYKouRu166VU2xqa1wcaWoxPkrZBG: 'devnet',
32
+ };
33
+ const fetchCluster = (rpc) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
34
+ try {
35
+ const { result } = yield (yield fetch(rpc, {
36
+ body: JSON.stringify({
37
+ id: 1,
38
+ jsonrpc: '2.0',
39
+ method: 'getGenesisHash',
40
+ }),
41
+ headers: {
42
+ 'Content-Type': 'application/json',
43
+ },
44
+ method: 'POST',
45
+ })).json();
46
+ return GENESIS_HASHES[result];
47
+ }
48
+ catch (err) {
49
+ return 'mainnet-beta';
50
+ }
51
+ });
52
+
53
+ exports.buildUrl = buildUrl;
54
+ exports.decryptPayload = decryptPayload;
55
+ exports.encryptPayload = encryptPayload;
56
+ exports.fetchCluster = fetchCluster;
@@ -0,0 +1,4 @@
1
+ export declare const buildUrl: (path: string, params: URLSearchParams) => string;
2
+ export declare const decryptPayload: (data: string, nonce: string, sharedSecret: Uint8Array) => any;
3
+ export declare const encryptPayload: (payload: any, sharedSecret: Uint8Array) => Uint8Array[];
4
+ export declare const fetchCluster: (rpc: string) => Promise<string>;
@@ -0,0 +1,44 @@
1
+ 'use client'
2
+ import { __awaiter } from '../../_virtual/_tslib.js';
3
+ import nacl from 'tweetnacl';
4
+ import bs58 from 'bs58';
5
+
6
+ const buildUrl = (path, params) => `https://phantom.app/ul/v1/${path}?${params.toString()}`;
7
+ const decryptPayload = (data, nonce, sharedSecret) => {
8
+ const decryptedData = nacl.box.open.after(bs58.decode(data), bs58.decode(nonce), sharedSecret);
9
+ if (!decryptedData) {
10
+ throw new Error('Unable to decrypt data');
11
+ }
12
+ return JSON.parse(Buffer.from(decryptedData).toString('utf8'));
13
+ };
14
+ const encryptPayload = (payload, sharedSecret) => {
15
+ const nonce = nacl.randomBytes(24);
16
+ const encryptedPayload = nacl.box.after(Buffer.from(JSON.stringify(payload)), nonce, sharedSecret);
17
+ return [nonce, encryptedPayload];
18
+ };
19
+ const GENESIS_HASHES = {
20
+ '4uhcVJyU9pJkvQyS88uRDiswHXSCkY3zQawwpjk2NsNY': 'testnet',
21
+ '5eykt4UsFv8P8NJdTREpY1vzqKqZKvdpKuc147dw2N9d': 'mainnet-beta',
22
+ EtWTRABZaYq6iMfeYKouRu166VU2xqa1wcaWoxPkrZBG: 'devnet',
23
+ };
24
+ const fetchCluster = (rpc) => __awaiter(void 0, void 0, void 0, function* () {
25
+ try {
26
+ const { result } = yield (yield fetch(rpc, {
27
+ body: JSON.stringify({
28
+ id: 1,
29
+ jsonrpc: '2.0',
30
+ method: 'getGenesisHash',
31
+ }),
32
+ headers: {
33
+ 'Content-Type': 'application/json',
34
+ },
35
+ method: 'POST',
36
+ })).json();
37
+ return GENESIS_HASHES[result];
38
+ }
39
+ catch (err) {
40
+ return 'mainnet-beta';
41
+ }
42
+ });
43
+
44
+ export { buildUrl, decryptPayload, encryptPayload, fetchCluster };