@relayprotocol/relay-lighter-wallet-adapter 0.1.1

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.
@@ -0,0 +1,181 @@
1
+ import { type AdaptedWallet } from '@relayprotocol/relay-sdk';
2
+ export declare const LIGHTER_CHAIN_ID = 3586256;
3
+ /**
4
+ * Minimal L1 signer shape the adapter passes into `transfer()`. The real
5
+ * Lighter SDK accepts `string | ethers.Signer`; we only ever use
6
+ * `signMessage`, so that's all we require structurally.
7
+ */
8
+ export type LighterEthSigner = {
9
+ signMessage: (message: string) => Promise<string>;
10
+ };
11
+ export type LighterTransferParams = {
12
+ toAccountIndex: number;
13
+ assetIndex: number;
14
+ fromRouteType: number;
15
+ toRouteType: number;
16
+ amount: number;
17
+ usdcFee: number;
18
+ memo: string;
19
+ /**
20
+ * L1 signer used for the per-transfer authorization. Only omitted when
21
+ * the caller's `signerClient` handles L1 signing internally — the stock
22
+ * `@relay-protocol/lighter-ts-sdk` `SignerClient` requires it.
23
+ */
24
+ ethSigner?: LighterEthSigner;
25
+ nonce?: number;
26
+ };
27
+ /** Subset of the SDK's `Transaction` shape that the adapter actually reads. */
28
+ export type LighterTransaction = {
29
+ hash: string;
30
+ status: number | 'pending' | 'confirmed' | 'failed';
31
+ block_height?: number;
32
+ message?: string;
33
+ };
34
+ /**
35
+ * Minimal Lighter signer contract the adapter depends on. Integrators
36
+ * passing a pre-built signer only need to implement these two methods;
37
+ * the full `@relay-protocol/lighter-ts-sdk` `SignerClient` satisfies this
38
+ * structurally and can be passed directly.
39
+ */
40
+ export type LighterSigner = {
41
+ transfer: (params: LighterTransferParams) => Promise<[unknown, string, string | null]>;
42
+ getTransaction: (txHash: string) => Promise<LighterTransaction>;
43
+ };
44
+ /**
45
+ * Optional per-origin persistent storage for the generated API key. If
46
+ * omitted the adapter regenerates a fresh key on every page load (the
47
+ * conservative default — key never leaves memory). If you want to skip the
48
+ * per-session `changeApiKey` signature prompt, plug in something like
49
+ * `localStorage` or IndexedDB.
50
+ */
51
+ export type LighterKeyStorage = {
52
+ get: (storageKey: string) => Promise<string | null> | string | null;
53
+ set: (storageKey: string, value: string) => Promise<void> | void;
54
+ };
55
+ /** Callback that signs a plain-text L1 authorization message. */
56
+ export type LighterSignL1Message = (message: string) => Promise<string>;
57
+ type AdaptLighterWalletBaseOptions = {
58
+ /**
59
+ * The user's L1 (EVM) wallet address. Used to resolve their Lighter
60
+ * account index and to key the optional persistent storage.
61
+ */
62
+ l1Address: `0x${string}`;
63
+ /**
64
+ * Callback that signs an L1 authorization message with the user's
65
+ * connected wallet. Typically a thin wrapper around a viem
66
+ * `WalletClient.signMessage` call.
67
+ *
68
+ * Required unless `signerClient` is supplied — when the integrator
69
+ * provides their own signer, it's assumed to handle L1 signing
70
+ * internally (or to accept `signL1Message` as a separate concern).
71
+ *
72
+ * When both are supplied, `signL1Message` is still forwarded to the
73
+ * signer's `transfer()` calls via the `ethSigner` param.
74
+ *
75
+ * Not invoked for the bootstrap step when `accountApiKey` or
76
+ * `signerClient` is supplied — only for per-transfer authorization.
77
+ */
78
+ signL1Message?: LighterSignL1Message;
79
+ apiUrl?: string;
80
+ apiKeyIndex?: number;
81
+ /** Paths to the Lighter WASM signer + Go runtime shim. Default: jsDelivr CDN. */
82
+ wasmConfig?: {
83
+ wasmPath?: string;
84
+ wasmExecPath?: string;
85
+ };
86
+ /**
87
+ * Pre-registered Lighter account API key (hex-encoded, with or without
88
+ * `0x` prefix). This is the Lighter protocol's session key — not a
89
+ * wallet private key. When supplied, the adapter skips the
90
+ * `generateAPIKey` + `changeApiKey` bootstrap entirely (no signature
91
+ * prompt, no on-chain key-rotation wait) and uses this key directly for
92
+ * the `SignerClient`.
93
+ *
94
+ * The caller is responsible for:
95
+ * - Having already registered the corresponding public key on the
96
+ * user's Lighter account at `apiKeyIndex`
97
+ * - Storing the key securely
98
+ *
99
+ * Use this when you've built your own API-key lifecycle (e.g. a
100
+ * backend service that provisions keys, or a wallet-level integration
101
+ * that manages keys outside this adapter). Mutually exclusive with
102
+ * `storage` — if both are set, `accountApiKey` wins and `storage` is
103
+ * ignored.
104
+ */
105
+ accountApiKey?: string;
106
+ /**
107
+ * Optional API-key persistence. When provided, the adapter reuses the
108
+ * stored key across sessions instead of re-running `changeApiKey`.
109
+ * Ignored when `accountApiKey` or `signerClient` is supplied.
110
+ */
111
+ storage?: LighterKeyStorage;
112
+ /** Confirmation poll interval. Default: 2000ms. */
113
+ pollIntervalMs?: number;
114
+ /** Confirmation timeout. Default: 120000ms. */
115
+ timeoutMs?: number;
116
+ };
117
+ /**
118
+ * Pre-built-signer path. `signerClient` and `accountIndex` must be paired
119
+ * — the adapter can't pull account index out of a `SignerClient` instance
120
+ * (the SDK's config is private), and it needs the value for
121
+ * `AdaptedWallet.address()`. Pairing them lets the adapter run with
122
+ * zero runtime dependency on `@relay-protocol/lighter-ts-sdk`.
123
+ */
124
+ type AdaptLighterWalletPreBuiltOptions = AdaptLighterWalletBaseOptions & {
125
+ /**
126
+ * Pre-built Lighter signer. When supplied, the adapter bypasses the
127
+ * `generateAPIKey` / `changeApiKey` bootstrap entirely and uses this
128
+ * signer for all `transfer()` and `getTransaction()` calls.
129
+ *
130
+ * The caller is responsible for ensuring the signer is ready to use
131
+ * (initialized, WASM loaded, API key registered) and for caching it
132
+ * if desired. A full `SignerClient` from `@relay-protocol/lighter-ts-sdk`
133
+ * satisfies `LighterSigner` structurally and can be passed directly.
134
+ *
135
+ * When set, `apiUrl`, `apiKeyIndex`, `wasmConfig`, `accountApiKey`,
136
+ * and `storage` are all ignored (baked into the signer).
137
+ */
138
+ signerClient: LighterSigner;
139
+ /** The user's Lighter account index. Required when `signerClient` is set. */
140
+ accountIndex: number;
141
+ };
142
+ /** Bootstrap path — the adapter owns the signer lifecycle. */
143
+ type AdaptLighterWalletBootstrapOptions = AdaptLighterWalletBaseOptions & {
144
+ signerClient?: never;
145
+ accountIndex?: never;
146
+ };
147
+ export type AdaptLighterWalletOptions = AdaptLighterWalletBootstrapOptions | AdaptLighterWalletPreBuiltOptions;
148
+ /**
149
+ * Adapts a Lighter wallet to work with the Relay SDK.
150
+ *
151
+ * The adapter owns the full Lighter session lifecycle so integrators only
152
+ * have to provide the user's L1 address and a message-signing callback:
153
+ *
154
+ * ```ts
155
+ * const wallet = adaptLighterWallet({
156
+ * l1Address: account.address,
157
+ * signL1Message: (msg) =>
158
+ * walletClient.signMessage({ account, message: msg })
159
+ * })
160
+ * ```
161
+ *
162
+ * On construction the adapter does nothing interactive — no network calls,
163
+ * no signature prompts. The first call into `handleSendTransactionStep`
164
+ * triggers the lazy bootstrap, which:
165
+ * 1. Resolves the user's Lighter `accountIndex` from their L1 address.
166
+ * 2. Either loads a persisted API key (if `storage` is provided) or
167
+ * generates a fresh keypair in memory.
168
+ * 3. For a fresh key, prompts the user to sign an L1 authorization and
169
+ * calls `changeApiKey` to register it against the account.
170
+ * 4. Waits for the key rotation to execute on Lighter.
171
+ * 5. Constructs the real `SignerClient` backed by the new key.
172
+ *
173
+ * Subsequent transactions reuse the cached session (in-memory at minimum).
174
+ *
175
+ * Passing `signerClient` skips the entire bootstrap and avoids any runtime
176
+ * dependency on `@relay-protocol/lighter-ts-sdk`.
177
+ *
178
+ */
179
+ export declare const adaptLighterWallet: (options: AdaptLighterWalletOptions) => AdaptedWallet;
180
+ export {};
181
+ //# sourceMappingURL=adapter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,aAAa,EAEnB,MAAM,0BAA0B,CAAA;AAEjC,eAAO,MAAM,gBAAgB,UAAU,CAAA;AA8BvC;;;;GAIG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B,WAAW,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAAA;CAClD,CAAA;AAED,MAAM,MAAM,qBAAqB,GAAG;IAClC,c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;;;OAGG;IACH,SAAS,EAAE,KAAK,MAAM,EAAE,CAAA;IACxB;;;;;;;;;;;;;;OAcG;IACH,aAAa,CAAC,EAAE,oBAAoB,CAAA;IAEpC,MAAM,CAAC,EAAE,MAAM,CAAA;IAEf,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,iFAAiF;IACjF,UAAU,CAAC,EAAE;QACX,QAAQ,CAAC,EAAE,MAAM,CAAA;QACjB,YAAY,CAAC,EAAE,MAAM,CAAA;KACtB,CAAA;IACD;;;;;;;;;;;;;;;;;;OAkBG;IACH,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB;;;;OAIG;IACH,OAAO,CAAC,EAAE,iBAAiB,CAAA;IAC3B,mDAAmD;IACnD,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,+CAA+C;IAC/C,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB,CAAA;AAED;;;;;;GAMG;AACH,KAAK,iCAAiC,GAAG,6BAA6B,GAAG;IACvE;;;;;;;;;;;;OAYG;IACH,YAAY,EAAE,aAAa,CAAA;IAC3B,6EAA6E;IAC7E,YAAY,EAAE,MAAM,CAAA;CACrB,CAAA;AAED,8DAA8D;AAC9D,KAAK,kCAAkC,GAAG,6BAA6B,GAAG;IACxE,YAAY,CAAC,EAAE,KAAK,CAAA;IACpB,YAAY,CAAC,EAAE,KAAK,CAAA;CACrB,CAAA;AAED,MAAM,MAAM,yBAAyB,GACjC,kCAAkC,GAClC,iCAAiC,CAAA;AAErC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,eAAO,MAAM,kBAAkB,YACpB,yBAAyB,KACjC,aAyQF,CAAA"}
@@ -0,0 +1,2 @@
1
+ export * from './adapter.js';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA"}