@thru/wallet 0.2.22

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 (69) hide show
  1. package/README.md +67 -0
  2. package/android/build.gradle +37 -0
  3. package/android/src/main/AndroidManifest.xml +1 -0
  4. package/android/src/main/java/org/thru/walletnative/ThruWebViewBridgeModule.kt +77 -0
  5. package/app.plugin.cjs +101 -0
  6. package/dist/BrowserSDK-CpRFiJsW.d.ts +409 -0
  7. package/dist/index.d.ts +23 -0
  8. package/dist/index.js +941 -0
  9. package/dist/index.js.map +1 -0
  10. package/dist/native/react.d.ts +109 -0
  11. package/dist/native/react.js +2381 -0
  12. package/dist/native/react.js.map +1 -0
  13. package/dist/native.d.ts +329 -0
  14. package/dist/native.js +1126 -0
  15. package/dist/native.js.map +1 -0
  16. package/dist/react-ui.d.ts +5 -0
  17. package/dist/react-ui.js +266 -0
  18. package/dist/react-ui.js.map +1 -0
  19. package/dist/react.d.ts +66 -0
  20. package/dist/react.js +1151 -0
  21. package/dist/react.js.map +1 -0
  22. package/expo-module.config.json +6 -0
  23. package/package.json +114 -0
  24. package/src/BrowserSDK.ts +315 -0
  25. package/src/index.ts +27 -0
  26. package/src/interfaces/IThruChain.ts +37 -0
  27. package/src/interfaces/accounts.ts +61 -0
  28. package/src/interfaces/index.ts +9 -0
  29. package/src/interfaces/types.ts +95 -0
  30. package/src/native/NativeSDK.test.ts +819 -0
  31. package/src/native/NativeSDK.ts +773 -0
  32. package/src/native/index.ts +39 -0
  33. package/src/native/provider/NativeProvider.ts +363 -0
  34. package/src/native/provider/WebViewBridge.test.ts +339 -0
  35. package/src/native/provider/WebViewBridge.ts +339 -0
  36. package/src/native/provider/chains/ThruChain.ts +85 -0
  37. package/src/native/provider/shell.html +88 -0
  38. package/src/native/provider/shell.test.ts +56 -0
  39. package/src/native/provider/shell.ts +111 -0
  40. package/src/native/provider/shims-html.d.ts +4 -0
  41. package/src/native/react/ThruContext.ts +37 -0
  42. package/src/native/react/ThruProvider.tsx +168 -0
  43. package/src/native/react/ThruWalletSheet.tsx +1162 -0
  44. package/src/native/react/android-webauthn.ts +37 -0
  45. package/src/native/react/hooks/useAccounts.ts +35 -0
  46. package/src/native/react/hooks/useThru.ts +11 -0
  47. package/src/native/react/hooks/useWallet.ts +71 -0
  48. package/src/native/react/hooks/useWalletAvailability.ts +31 -0
  49. package/src/native/react/hooks/waitForWallet.ts +21 -0
  50. package/src/native/react/index.ts +29 -0
  51. package/src/protocol/index.ts +2 -0
  52. package/src/protocol/postMessage.ts +283 -0
  53. package/src/protocol/walletState.ts +12 -0
  54. package/src/provider/EmbeddedProvider.ts +330 -0
  55. package/src/provider/IframeManager.ts +438 -0
  56. package/src/provider/chains/ThruChain.ts +86 -0
  57. package/src/provider/index.ts +17 -0
  58. package/src/provider/types/messages.ts +37 -0
  59. package/src/react/ThruContext.ts +31 -0
  60. package/src/react/ThruProvider.tsx +169 -0
  61. package/src/react/hooks/useAccounts.ts +38 -0
  62. package/src/react/hooks/useThru.ts +11 -0
  63. package/src/react/hooks/useWallet.ts +81 -0
  64. package/src/react/index.ts +30 -0
  65. package/src/react-ui/ThruAccountSwitcher.tsx +187 -0
  66. package/src/react-ui/custom.d.ts +8 -0
  67. package/src/react-ui/index.ts +1 -0
  68. package/src/static/logo.png +0 -0
  69. package/src/static/logomark_red.svg +11 -0
@@ -0,0 +1,329 @@
1
+ import { Thru } from '@thru/sdk/client';
2
+
3
+ declare const AddressType: {
4
+ readonly THRU: "thru";
5
+ };
6
+ type AddressType = (typeof AddressType)[keyof typeof AddressType];
7
+ interface WalletAccount {
8
+ accountType: AddressType;
9
+ address: string;
10
+ label: string;
11
+ }
12
+ interface AppMetadata {
13
+ appId: string;
14
+ appName: string;
15
+ appUrl: string;
16
+ imageUrl?: string;
17
+ }
18
+ interface ConnectResult {
19
+ walletId?: string;
20
+ accounts: WalletAccount[];
21
+ selectedAccount?: WalletAccount | null;
22
+ status?: "pending" | "completed";
23
+ metadata?: AppMetadata;
24
+ }
25
+ declare const ThruTransactionEncoding: {
26
+ readonly SIGNING_PAYLOAD_BASE64: "signing_payload_base64";
27
+ readonly RAW_TRANSACTION_BASE64: "raw_transaction_base64";
28
+ };
29
+ type ThruTransactionEncoding = (typeof ThruTransactionEncoding)[keyof typeof ThruTransactionEncoding];
30
+ interface ThruSigningContext {
31
+ mode: "managed_fee_payer";
32
+ selectedAccountPublicKey: string | null;
33
+ feePayerPublicKey: string;
34
+ signerPublicKey: string;
35
+ acceptedInputEncodings: ThruTransactionEncoding[];
36
+ outputEncoding: typeof ThruTransactionEncoding.RAW_TRANSACTION_BASE64;
37
+ }
38
+ interface ThruTransactionReviewSimulation {
39
+ before?: string;
40
+ after?: string;
41
+ }
42
+ interface ThruTransactionReviewAbiReflection {
43
+ label?: string;
44
+ kind?: string | null;
45
+ typeName?: string;
46
+ value?: unknown;
47
+ rawHex?: string;
48
+ source?: string;
49
+ error?: string;
50
+ }
51
+ interface ThruTransactionReviewPayload {
52
+ appName?: string;
53
+ programAddress?: string;
54
+ abiName?: string;
55
+ instruction?: string;
56
+ simulation?: ThruTransactionReviewSimulation;
57
+ abiReflection?: ThruTransactionReviewAbiReflection;
58
+ }
59
+ interface ThruTransactionIntent {
60
+ walletAddress?: string;
61
+ programAddress: string;
62
+ instructionData: string;
63
+ readWriteAddresses?: string[];
64
+ readOnlyAddresses?: string[];
65
+ review?: ThruTransactionReviewPayload;
66
+ }
67
+ interface SignMessageParams {
68
+ message: string | Uint8Array;
69
+ networkId: string;
70
+ }
71
+ interface SignMessageResult {
72
+ signature: Uint8Array;
73
+ publicKey: string;
74
+ }
75
+
76
+ /**
77
+ * Minimal Thru chain interface exposed to SDK consumers.
78
+ * The concrete implementation will evolve as the Thru transaction
79
+ * flow is fleshed out, but maintaining a dedicated contract now
80
+ * keeps the Surface area aligned with other chain adapters.
81
+ */
82
+ interface IThruChain {
83
+ /** Indicates whether the wallet has approved a Thru connection. */
84
+ readonly connected: boolean;
85
+ /**
86
+ * Initiate a Thru connection flow. Resolves with the connected address once
87
+ * the user has approved the request.
88
+ */
89
+ connect(): Promise<{
90
+ publicKey: string;
91
+ }>;
92
+ /** Disconnect the currently connected Thru account. */
93
+ disconnect(): Promise<void>;
94
+ /**
95
+ * Return the current embedded signing contract for Thru transactions.
96
+ *
97
+ * The selected account is the managed wallet account shown to the user.
98
+ * The fee payer / signer can differ when the wallet routes transactions
99
+ * through an embedded manager profile.
100
+ */
101
+ getSigningContext(): Promise<ThruSigningContext>;
102
+ /**
103
+ * Sign a wallet-managed transaction intent and return canonical raw
104
+ * transaction bytes encoded as base64. The wallet owns fee payer choice,
105
+ * account ordering, headers, nonces, and final wire layout.
106
+ */
107
+ signTransaction(transaction: ThruTransactionIntent): Promise<string>;
108
+ }
109
+
110
+ declare const POST_MESSAGE_REQUEST_TYPES: {
111
+ readonly CONNECT: "connect";
112
+ readonly DISCONNECT: "disconnect";
113
+ readonly SIGN_MESSAGE: "signMessage";
114
+ readonly SIGN_TRANSACTION: "signTransaction";
115
+ readonly GET_ACCOUNTS: "getAccounts";
116
+ readonly GET_CONNECTION_STATE: "getConnectionState";
117
+ readonly GET_SIGNING_CONTEXT: "getSigningContext";
118
+ readonly SELECT_ACCOUNT: "selectAccount";
119
+ readonly MANAGE_ACCOUNTS: "manageAccounts";
120
+ };
121
+ declare const EMBEDDED_PROVIDER_EVENTS: {
122
+ readonly CONNECT_START: "connect_start";
123
+ readonly CONNECT: "connect";
124
+ readonly DISCONNECT: "disconnect";
125
+ readonly CONNECT_ERROR: "connect_error";
126
+ readonly ERROR: "error";
127
+ readonly LOCK: "lock";
128
+ readonly UI_SHOW: "ui_show";
129
+ readonly ACCOUNT_CHANGED: "account_changed";
130
+ };
131
+ interface GetConnectionStateResult {
132
+ isAuthorized: boolean;
133
+ isConnected: boolean;
134
+ isUnlocked: boolean;
135
+ hasPasskey: boolean;
136
+ hasWalletAccount: boolean;
137
+ accounts: WalletAccount[];
138
+ selectedAccount: WalletAccount | null;
139
+ metadata: AppMetadata | null;
140
+ }
141
+ interface ManageAccountsResult {
142
+ accounts: WalletAccount[];
143
+ selectedAccount: WalletAccount | null;
144
+ }
145
+ declare const ErrorCode: {
146
+ readonly USER_REJECTED: "USER_REJECTED";
147
+ readonly WALLET_LOCKED: "WALLET_LOCKED";
148
+ readonly INVALID_PASSWORD: "INVALID_PASSWORD";
149
+ readonly ALREADY_CONNECTED: "ALREADY_CONNECTED";
150
+ readonly ACCOUNT_NOT_FOUND: "ACCOUNT_NOT_FOUND";
151
+ readonly ACCOUNT_CHANGED: "ACCOUNT_CHANGED";
152
+ readonly INVALID_TRANSACTION: "INVALID_TRANSACTION";
153
+ readonly TRANSACTION_FAILED: "TRANSACTION_FAILED";
154
+ readonly INSUFFICIENT_FUNDS: "INSUFFICIENT_FUNDS";
155
+ readonly NETWORK_ERROR: "NETWORK_ERROR";
156
+ readonly TIMEOUT: "TIMEOUT";
157
+ readonly UNKNOWN_ERROR: "UNKNOWN_ERROR";
158
+ };
159
+ type ErrorCode = (typeof ErrorCode)[keyof typeof ErrorCode];
160
+ type ConnectMetadataInput = Partial<AppMetadata>;
161
+ type ConnectIntent = "default" | "switch-account";
162
+ interface ConnectRequestPayload {
163
+ metadata?: ConnectMetadataInput;
164
+ preferredAccountAddress?: string;
165
+ intent?: ConnectIntent;
166
+ }
167
+
168
+ interface WebViewRefLike {
169
+ injectJavaScript: (script: string) => void;
170
+ }
171
+ interface WebViewMessageEventLike {
172
+ nativeEvent: {
173
+ data: string;
174
+ };
175
+ }
176
+
177
+ type IosWebViewMode = "direct" | "shell-iframe";
178
+ type WalletAvailability = {
179
+ status: "checking";
180
+ isAuthorized: false;
181
+ isConnected: false;
182
+ isUnlocked: false;
183
+ hasPasskey: false;
184
+ hasWalletAccount: false;
185
+ accounts: WalletAccount[];
186
+ selectedAccount: null;
187
+ metadata: null;
188
+ error: null;
189
+ } | {
190
+ status: "ready";
191
+ isAuthorized: boolean;
192
+ isConnected: boolean;
193
+ isUnlocked: boolean;
194
+ hasPasskey: boolean;
195
+ hasWalletAccount: boolean;
196
+ accounts: WalletAccount[];
197
+ selectedAccount: WalletAccount | null;
198
+ metadata: AppMetadata | null;
199
+ error: null;
200
+ } | {
201
+ status: "error";
202
+ isAuthorized: false;
203
+ isConnected: false;
204
+ isUnlocked: false;
205
+ hasPasskey: false;
206
+ hasWalletAccount: false;
207
+ accounts: WalletAccount[];
208
+ selectedAccount: null;
209
+ metadata: null;
210
+ error: Error;
211
+ };
212
+ interface NativeSDKConfig {
213
+ walletUrl?: string;
214
+ /** Stamped on every postMessage so wallet's ConnectedAppsStorage can
215
+ scope per-host. Default: 'thru-mobile://app'. */
216
+ origin?: string;
217
+ rpcUrl?: string;
218
+ addressTypes?: AddressType[];
219
+ /** iOS-only host mode. Shell iframe is the default; direct is kept
220
+ as an escape hatch for real-device passkey/WebAuthn comparisons. */
221
+ iosWebViewMode?: IosWebViewMode;
222
+ /** Optional host-provided persistent storage (SecureStore,
223
+ AsyncStorage, localStorage-compatible adapter, etc.). */
224
+ storage?: NativeSDKStorage;
225
+ /** Override the legacy connection snapshot key cleared from `storage`. */
226
+ storageKey?: string;
227
+ /** Override the key used to remember the app-local selected account. */
228
+ selectedAccountStorageKey?: string;
229
+ }
230
+ interface SignInOptions {
231
+ app_id: string;
232
+ app_display_name: string;
233
+ app_url?: string;
234
+ image_url?: string;
235
+ intent?: ConnectOptions["intent"];
236
+ }
237
+ interface ConnectOptions {
238
+ metadata?: ConnectMetadataInput;
239
+ preferredAccountAddress?: string;
240
+ intent?: ConnectRequestPayload["intent"];
241
+ }
242
+ interface RestoreConnectionOptions {
243
+ hydrate?: boolean;
244
+ }
245
+ type SDKEvent = "connect" | "disconnect" | "lock" | "error" | "accountChanged" | "availabilityChanged";
246
+ type EventCallback = (...args: any[]) => void;
247
+ interface NativeSDKStorage {
248
+ getItem: (key: string) => string | null | Promise<string | null>;
249
+ setItem: (key: string, value: string) => void | Promise<void>;
250
+ removeItem: (key: string) => void | Promise<void>;
251
+ }
252
+ interface NativeSDKUiHandlers {
253
+ onShowRequested?: () => void;
254
+ onHideRequested?: () => void;
255
+ }
256
+ /**
257
+ * NativeSDK - mobile mirror of `@thru/wallet`'s `BrowserSDK`.
258
+ * Public surface matches verbatim except `mountInline(HTMLElement)` is
259
+ * replaced by `attachWebView(WebViewRefLike)` since the host bottom
260
+ * sheet owns the WebView lifecycle.
261
+ */
262
+ declare class NativeSDK {
263
+ private provider;
264
+ private eventListeners;
265
+ private initialized;
266
+ private thruClient;
267
+ private rpcUrl;
268
+ private connectInFlight;
269
+ private lastConnectResult;
270
+ private walletAvailability;
271
+ private readonly origin;
272
+ private readonly storage?;
273
+ private readonly storageKey;
274
+ private readonly selectedAccountStorageKey;
275
+ private readonly iosWebViewMode;
276
+ constructor(config?: NativeSDKConfig);
277
+ /** Hand the WebView ref to the underlying provider/bridge. */
278
+ attachWebView(ref: WebViewRefLike): void;
279
+ /** Mark a direct top-level WebView wallet document as ready. */
280
+ markWebViewReady(): void;
281
+ /** Bind to the WebView's `onMessage` handler. */
282
+ onMessage: (event: WebViewMessageEventLike) => void;
283
+ /** Build the URL to load inside the shell <iframe>. */
284
+ getIframeSrc(): string;
285
+ /** Wallet origin (e.g. https://wallet.thru.org). */
286
+ getWalletOrigin(): string;
287
+ /** Bind host UI lifecycle handlers used by custom WebView hosts. */
288
+ setUiHandlers(handlers: NativeSDKUiHandlers): void;
289
+ clearUiHandlers(): void;
290
+ /** Reject in-flight wallet requests after a user-driven host dismiss. */
291
+ rejectPendingRequests(message?: string): void;
292
+ /** iOS WebView host mode. Non-iOS hosts should ignore this value. */
293
+ getIosWebViewMode(): IosWebViewMode;
294
+ initialize(): Promise<void>;
295
+ connect(options?: ConnectOptions): Promise<ConnectResult>;
296
+ signIn(options: SignInOptions): Promise<ConnectResult>;
297
+ disconnect(): Promise<void>;
298
+ isConnected(): boolean;
299
+ getWalletAvailability(): WalletAvailability;
300
+ restoreConnection(options?: RestoreConnectionOptions): Promise<ConnectResult | null>;
301
+ syncConnectionState(options?: ConnectOptions): Promise<GetConnectionStateResult | null>;
302
+ refreshWalletAvailability(options?: ConnectOptions): Promise<WalletAvailability>;
303
+ getAccounts(): WalletAccount[];
304
+ getSelectedAccount(): WalletAccount | null;
305
+ selectAccount(publicKey: string): Promise<WalletAccount>;
306
+ manageAccounts(): Promise<ManageAccountsResult>;
307
+ get thru(): IThruChain;
308
+ on(event: SDKEvent, callback: EventCallback): void;
309
+ off(event: SDKEvent, callback: EventCallback): void;
310
+ once(event: SDKEvent, callback: EventCallback): void;
311
+ destroy(): void;
312
+ /** Lazily-instantiated Thru chain client. */
313
+ getThru(): Thru;
314
+ private emit;
315
+ private setupEventForwarding;
316
+ private requestConnectionState;
317
+ private applyConnectionState;
318
+ private setWalletAvailability;
319
+ private clearAuthorizedAvailability;
320
+ private resolveMetadata;
321
+ private resolveSignInMetadata;
322
+ private refreshCachedAccounts;
323
+ private applyPreferredSelectedAccount;
324
+ private persistSelectedAccountAddress;
325
+ private clearPersistedConnection;
326
+ private readSelectedAccountAddress;
327
+ }
328
+
329
+ export { AddressType, type AppMetadata, type ConnectMetadataInput, type ConnectOptions, type ConnectResult, EMBEDDED_PROVIDER_EVENTS, ErrorCode, type EventCallback, type GetConnectionStateResult, type IThruChain, type IosWebViewMode, type ManageAccountsResult, NativeSDK, type NativeSDKConfig, type NativeSDKStorage, type NativeSDKUiHandlers, POST_MESSAGE_REQUEST_TYPES, type RestoreConnectionOptions, type SDKEvent, type SignInOptions, type SignMessageParams, type SignMessageResult, type ThruSigningContext, ThruTransactionEncoding, type ThruTransactionIntent, type WalletAccount, type WalletAvailability };