@metamask-previews/keyring-controller 28.2.0-preview-56cdd31f1 → 28.2.0-preview-4a8d4a2

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,247 @@
1
+ import type { EthKeyring } from '@metamask/keyring-internal-api';
2
+ import type { CachedEncryptionKey, KeyringEntry, KeyringMetadata, SerializedKeyring } from './KeyringController.js';
3
+ /**
4
+ * The scope of a vault transaction: what it owns — and therefore what it
5
+ * restores on failure. Everything outside the scope is untouched, even on
6
+ * failure: instances, references, and all.
7
+ */
8
+ export type TransactionScope = WalletTransactionScope | KeyringTransactionScope;
9
+ /**
10
+ * The wallet transaction scope: the transaction owns the whole wallet —
11
+ * all keyrings, the unsupported keyrings, and the encryption key.
12
+ */
13
+ export type WalletTransactionScope = {
14
+ type: 'wallet';
15
+ /**
16
+ * Decides, when the transaction commits, whether the vault is written —
17
+ * possibly preparing the write first (e.g. re-deriving the encryption
18
+ * key). The session-change status is given as a lazy check, so that
19
+ * policies that ignore it never pay for it.
20
+ */
21
+ persist: VaultPersistPolicy;
22
+ };
23
+ /**
24
+ * The wallet-scope persist policy: answers, when the transaction commits,
25
+ * whether the vault is written.
26
+ *
27
+ * The standard policy persists only when the session state changed since
28
+ * `begin()`; other flows force the write, decline it, or gate it on their
29
+ * own criterion.
30
+ */
31
+ export type VaultPersistPolicy = (hasChanged: () => Promise<boolean>) => boolean | Promise<boolean>;
32
+ /**
33
+ * The keyring transaction scope: the transaction owns a single keyring
34
+ * entry, selected or created before the transaction began.
35
+ */
36
+ export type KeyringTransactionScope = {
37
+ type: 'keyring';
38
+ /**
39
+ * The operated keyring entry.
40
+ */
41
+ entry: KeyringEntry;
42
+ /**
43
+ * Whether the transaction created the operated keyring entry.
44
+ */
45
+ created: boolean;
46
+ };
47
+ /**
48
+ * The live vault session state the transactions operate on: what the
49
+ * transaction snapshots at `begin()`, mutates through, and restores on
50
+ * `rollback()`. The controller owns the real state and implements this
51
+ * interface; every member reads or rewrites the state it owns.
52
+ */
53
+ export type VaultSession = {
54
+ /**
55
+ * The keyrings currently registered, as a fresh array: mutations go
56
+ * through the other members.
57
+ */
58
+ getKeyrings(): KeyringEntry[];
59
+ /**
60
+ * Replace the registered keyrings wholesale.
61
+ */
62
+ replaceKeyrings(keyrings: KeyringEntry[]): void;
63
+ /**
64
+ * Remove the given entry from the registered keyrings, without touching
65
+ * the others. A no-op for an entry that is not registered: a missing
66
+ * entry must never fall back to another position.
67
+ *
68
+ * @returns Whether the entry was registered.
69
+ */
70
+ removeKeyring(entry: KeyringEntry): boolean;
71
+ /**
72
+ * Insert the given entry at the given position in the registered
73
+ * keyrings.
74
+ */
75
+ insertKeyringAt(index: number, entry: KeyringEntry): void;
76
+ /**
77
+ * The unsupported keyrings currently parked, as a fresh array.
78
+ */
79
+ getUnsupportedKeyrings(): SerializedKeyring[];
80
+ /**
81
+ * Replace the unsupported keyrings wholesale.
82
+ */
83
+ setUnsupportedKeyrings(keyrings: SerializedKeyring[]): void;
84
+ /**
85
+ * Park the given serialized keyring in the unsupported keyrings.
86
+ */
87
+ parkUnsupportedKeyring(keyring: SerializedKeyring): void;
88
+ /**
89
+ * The cached encryption key, if any.
90
+ */
91
+ getEncryptionKey(): CachedEncryptionKey | undefined;
92
+ /**
93
+ * Set, or clear, the cached encryption key.
94
+ */
95
+ setEncryptionKey(key: CachedEncryptionKey | undefined): void;
96
+ };
97
+ /**
98
+ * The controller-owned primitives the transactions drive: building new
99
+ * keyring entries, and enforcing the no-duplicate-accounts invariant on an
100
+ * explicit list of keyrings.
101
+ */
102
+ export type VaultOperations = {
103
+ /**
104
+ * Build a keyring entry from its serialized parts, without registering
105
+ * it.
106
+ */
107
+ createKeyring(type: string, data?: unknown, metadata?: KeyringMetadata): Promise<KeyringEntry>;
108
+ /**
109
+ * Assert that the given keyrings hold no duplicate account.
110
+ *
111
+ * @throws If the keyrings hold a duplicate account.
112
+ */
113
+ assertNoDuplicateAccounts(keyrings: EthKeyring[]): Promise<void>;
114
+ /**
115
+ * Update the vault with the current session state.
116
+ */
117
+ updateVault(): Promise<boolean>;
118
+ };
119
+ /**
120
+ * The context of an in-flight vault transaction.
121
+ *
122
+ * Every mutation of the vault session runs inside a transaction, created
123
+ * before the mutation begins. The transaction states what it owns — its
124
+ * scope — and tracks what the mutation did to what it owns: `begin()`
125
+ * takes the snapshot that defines the restore point, `rollback()` restores
126
+ * exactly what the scope owns on failure, `hasChanged()` answers whether
127
+ * the scope changed, and `commit()` persists what it owns when its scope
128
+ * says so — the wallet scope's persist policy decides, the keyring scope
129
+ * persists iff the operated keyring changed.
130
+ *
131
+ * ## Contract
132
+ *
133
+ * 1. Every mutation runs inside a transaction. The transaction snapshots
134
+ * exactly what it owns, before any mutation.
135
+ * 2. On failure, the transaction restores exactly what it owns.
136
+ * Everything outside its scope is untouched — instances, references,
137
+ * and all.
138
+ * 3. Mutations made outside a transaction (e.g. through instances handed
139
+ * out by deprecated direct-access getters) are undefined behavior — a
140
+ * bug in the caller, not an input the transaction defends against. The
141
+ * transaction does not reconcile them, and must not amplify them.
142
+ * 4. On success, nothing is destroyed or rebuilt; instances survive in
143
+ * place and references remain valid.
144
+ * 5. When a rebuild is necessary (failure path only), the stale instance
145
+ * is always destroyed before its replacement is created, as it must
146
+ * release the resources shared with the replacement (e.g. the SDK
147
+ * singleton of hardware keyrings) before the replacement is
148
+ * initialized.
149
+ *
150
+ * The transaction is also the home of the data it accumulates about
151
+ * itself, which must not outlive it: the keyring instances it destroyed,
152
+ * including partially destroyed ones — destruction releases resources
153
+ * that `serialize()` does not capture (e.g. the bridge of a hardware
154
+ * keyring), so a rollback must rebuild a destroyed instance even when its
155
+ * serialized state still matches its snapshot.
156
+ */
157
+ export declare class VaultTransaction {
158
+ #private;
159
+ /**
160
+ * @param scope - What the transaction owns.
161
+ * @param session - The vault session state the transaction operates on.
162
+ * @param operations - The controller-owned primitives the transaction
163
+ * drives.
164
+ */
165
+ constructor(scope: TransactionScope, session: VaultSession, operations: VaultOperations);
166
+ /**
167
+ * What the transaction owns — and therefore what it restores on failure.
168
+ *
169
+ * @returns The transaction scope.
170
+ */
171
+ get scope(): TransactionScope;
172
+ /**
173
+ * Take the pre-mutation snapshot that defines the restore point: every
174
+ * keyring of the wallet for the wallet scope, the operated keyring for
175
+ * the keyring scope (skipped when the transaction created it), plus the
176
+ * wholesale-captured session state.
177
+ *
178
+ * Must be called once, before the transaction's first mutation; a
179
+ * failure leaves the session untouched.
180
+ */
181
+ begin(): Promise<void>;
182
+ /**
183
+ * Check whether what the scope owns has changed since `begin()`.
184
+ *
185
+ * For the keyring scope, the operated keyring is resolved by identity:
186
+ * another keyring sharing its id must not affect the decision. For the
187
+ * wallet scope, the whole session state — the serialized keyrings, the
188
+ * unsupported keyrings, and the encryption key — is compared against the
189
+ * state captured at `begin()`.
190
+ *
191
+ * @returns Whether the scope has changed.
192
+ */
193
+ hasChanged(): Promise<boolean>;
194
+ /**
195
+ * Persist what the scope owns, iff it changed: the keyring scope
196
+ * persists the vault when the operated keyring changed, and the wallet
197
+ * scope when its persist policy says so.
198
+ *
199
+ * Must be called while the controller mutex is held, after `begin()` and
200
+ * a successful operation.
201
+ */
202
+ commit(): Promise<void>;
203
+ /**
204
+ * Restore what the scope owns from the snapshot taken at `begin()`:
205
+ * every keyring of the wallet, plus the unsupported keyrings and the
206
+ * encryption key, for the wallet scope; the single operated keyring for
207
+ * the keyring scope. Everything outside the scope is untouched.
208
+ *
209
+ * Must be called while the controller mutex is held, and only after
210
+ * `begin()`.
211
+ */
212
+ rollback(): Promise<void>;
213
+ /**
214
+ * Destroy the given keyring entry's instances, marking the keyring as
215
+ * destroyed by this transaction — even when the destruction fails
216
+ * partway, as the rollback must rebuild it rather than keep it, whatever
217
+ * resources it already released.
218
+ *
219
+ * @param entry - The keyring entry to destroy.
220
+ * @param options - Destroy options.
221
+ * @param options.ignoreErrors - Whether to log destruction errors
222
+ * instead of throwing them: a rollback must not let them mask the
223
+ * transaction's own error.
224
+ */
225
+ destroy(entry: KeyringEntry, { ignoreErrors }?: {
226
+ ignoreErrors: boolean;
227
+ }): Promise<void>;
228
+ /**
229
+ * Check whether the given keyring entry's keyring was destroyed during
230
+ * this transaction, including partially: such a keyring must be treated
231
+ * as changed by the transaction, and rebuilt on rollback, even when its
232
+ * serialized state still matches its snapshot.
233
+ *
234
+ * @param entry - The keyring entry to check.
235
+ * @returns Whether the entry's keyring was destroyed.
236
+ */
237
+ isDestroyed(entry: KeyringEntry): boolean;
238
+ /**
239
+ * Remove the given keyring entry from the session keyrings, destroying
240
+ * its instances. A no-op for an entry that is not registered: a missing
241
+ * entry must never fall back to another position.
242
+ *
243
+ * @param entry - The keyring entry to remove.
244
+ */
245
+ remove(entry: KeyringEntry): Promise<void>;
246
+ }
247
+ //# sourceMappingURL=VaultTransaction.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"VaultTransaction.d.ts","sourceRoot":"","sources":["../src/VaultTransaction.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gCAAgC,CAAC;AAGjE,OAAO,KAAK,EACV,mBAAmB,EACnB,YAAY,EACZ,eAAe,EACf,iBAAiB,EAClB,MAAM,wBAAwB,CAAC;AAEhC;;;;GAIG;AACH,MAAM,MAAM,gBAAgB,GAAG,sBAAsB,GAAG,uBAAuB,CAAC;AAEhF;;;GAGG;AACH,MAAM,MAAM,sBAAsB,GAAG;IACnC,IAAI,EAAE,QAAQ,CAAC;IACf;;;;;OAKG;IACH,OAAO,EAAE,kBAAkB,CAAC;CAC7B,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,MAAM,kBAAkB,GAAG,CAC/B,UAAU,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,KAC/B,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;AAEhC;;;GAGG;AACH,MAAM,MAAM,uBAAuB,GAAG;IACpC,IAAI,EAAE,SAAS,CAAC;IAChB;;OAEG;IACH,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