-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArchiver.ts
More file actions
380 lines (350 loc) · 12.6 KB
/
Copy pathArchiver.ts
File metadata and controls
380 lines (350 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import { rpc, Transaction } from '@stellar/stellar-sdk'
import { xdr } from '@stellar/stellar-sdk'
import { ArchivedLedgerEntry, SimulateResponse } from './types.js'
import { asXdrBase64, asContractIdHex, type ContractIdHex } from './branded-types.js'
/**
* Type guard — returns true if the simulation response indicates archived
* ledger entries that need restoration.
*
* @param response - A {@link SimulateResponse} from `simulateTransaction`.
* @returns `true`, narrowing `response` to
* `SimulateTransactionRestoreResponse`, if a restore is required.
* @see {@link extractArchivedKeys} to get the archived keys once this
* returns `true`.
*/
export function isRestoreResponse(
response: SimulateResponse,
): response is rpc.Api.SimulateTransactionRestoreResponse {
return rpc.Api.isSimulationRestore(response)
}
/**
* Type guard — returns true if the simulation response indicates a
* successful simulation with no restore required.
*
* @param response - A {@link SimulateResponse} from `simulateTransaction`.
* @returns `true`, narrowing `response` to
* `SimulateTransactionSuccessResponse`, if simulation succeeded and no
* restore is needed.
* @see {@link extractFootprintFromSuccess} to read the footprint from a
* success response.
*/
export function isSuccessResponse(
response: SimulateResponse,
): response is rpc.Api.SimulateTransactionSuccessResponse {
return rpc.Api.isSimulationSuccess(response)
}
/**
* Type guard — returns true if the simulation response indicates an error.
*
* @param response - A {@link SimulateResponse} from `simulateTransaction`.
* @returns `true`, narrowing `response` to
* `SimulateTransactionErrorResponse`, if simulation failed.
*/
export function isErrorResponse(
response: SimulateResponse,
): response is rpc.Api.SimulateTransactionErrorResponse {
return rpc.Api.isSimulationError(response)
}
/**
* Extracts the list of archived ledger keys from a restore simulation response.
* The read-write entries in the transaction footprint represent the keys that
* need to be restored.
*
* @param response - A restore simulation response, as narrowed by
* {@link isRestoreResponse}.
* @returns Array of {@link ArchivedLedgerEntry}. Empty if the response has
* `_parsed: false` or the footprint could not be read (a warning is
* logged via `console.warn` in the former case).
*
* @example
* ```ts
* const sim = await server.simulateTransaction(tx)
* if (isRestoreResponse(sim)) {
* const archived = extractArchivedKeys(sim)
* }
* ```
*/
export function extractArchivedKeys(
response: rpc.Api.SimulateTransactionRestoreResponse,
): ArchivedLedgerEntry[] {
const keys: ArchivedLedgerEntry[] = []
if (!response._parsed) {
console.warn(
'SorobanResurrect: restore simulation response has _parsed=false, cannot extract archived keys',
)
return keys
}
try {
const footprint = response.transactionData.getFootprint()
const readWrite = footprint.readWrite()
for (const ledgerKey of readWrite) {
const keyBase64 = asXdrBase64(ledgerKey.toXDR('base64'))
keys.push({
key: ledgerKey,
keyBase64,
})
}
} catch {
return keys
}
return keys
}
/**
* Extracts the read-only and read-write ledger keys from a success simulation
* response footprint.
*
* @param response - A successful simulation response, as narrowed by
* {@link isSuccessResponse}.
* @returns `{ readOnly, readWrite }` ledger key arrays. Both are empty if
* the response has `_parsed: false` or the footprint could not be read.
* @see {@link detectArchivedEntries}, which typically consumes the
* `readWrite` keys returned here.
*/
export function extractFootprintFromSuccess(response: rpc.Api.SimulateTransactionSuccessResponse): {
readOnly: xdr.LedgerKey[]
readWrite: xdr.LedgerKey[]
} {
if (!response._parsed) {
console.warn(
'SorobanResurrect: success simulation response has _parsed=false, cannot extract footprint',
)
return { readOnly: [], readWrite: [] }
}
try {
const footprint = response.transactionData.getFootprint()
return {
readOnly: footprint.readOnly() || [],
readWrite: footprint.readWrite() || [],
}
} catch {
return { readOnly: [], readWrite: [] }
}
}
/**
* Queries the Soroban RPC server to determine which of the given ledger keys
* correspond to archived (non-existent / expired) entries.
*
* Keys are fetched in chunks of 50. If a chunk request fails (network error,
* rate-limit, etc.), every key in that chunk is conservatively treated as
* archived to avoid false negatives.
*
* @param server - Soroban RPC server instance.
* @param ledgerKeys - Ledger keys to check (typically the read-write
* footprint of a transaction).
* @returns Array of {@link ArchivedLedgerEntry} for keys that are missing
* from `getLedgerEntries` results (i.e. archived), or that could not be
* verified due to a request error.
* @see {@link detectArchivedKeysViaDirect}, which wraps this with the
* simulate → extract-footprint steps.
*/
export async function detectArchivedEntries(
server: ISorobanRpcClient,
ledgerKeys: xdr.LedgerKey[],
): Promise<ArchivedLedgerEntry[]> {
const archived: ArchivedLedgerEntry[] = []
const chunkSize = 50
for (let i = 0; i < ledgerKeys.length; i += chunkSize) {
const chunk = ledgerKeys.slice(i, i + chunkSize)
try {
const result = await server.getLedgerEntries(...chunk)
// Build a set of returned entry keys to identify archived ones
const knownKeys = new Set<string>()
if (result.entries) {
for (const entry of result.entries) {
knownKeys.add(entry.key.toXDR('base64'))
}
}
// Check each key in the chunk; if not in returned entries, it's archived
for (const key of chunk) {
const keyXdr = key.toXDR('base64')
if (!knownKeys.has(keyXdr)) {
archived.push({
key,
keyBase64: asXdrBase64(keyXdr),
})
}
}
} catch {
// On network error, conservatively treat all keys in chunk as archived
archived.push(
...chunk.map((key) => ({
key,
keyBase64: asXdrBase64(key.toXDR('base64')),
})),
)
}
}
return archived
}
/**
* Detects archived keys by simulating the transaction and extracting
* archived entries from the footprint.
*
* @param server - Soroban RPC server instance.
* @param transaction - The transaction to simulate.
* @returns Array of {@link ArchivedLedgerEntry} — empty if the simulation
* does not indicate a restore is needed.
* @see {@link detectArchivedKeysViaDirect} for the alternative
* direct-ledger-query strategy.
*/
export async function detectArchivedKeysViaSimulation(
server: ISorobanRpcClient,
transaction: Transaction,
): Promise<ArchivedLedgerEntry[]> {
const response = await server.simulateTransaction(transaction)
if (isRestoreResponse(response)) {
return extractArchivedKeys(response)
}
return []
}
/**
* Detects archived keys by querying the ledger directly for keys that
* appear in a success simulation footprint.
*
* This approach first simulates the transaction in success mode (no restore
* needed), extracts the footprint keys, then queries the ledger to find
* which ones are archived.
*
* @param server - Soroban RPC server instance.
* @param transaction - The transaction to simulate and check.
* @returns Array of {@link ArchivedLedgerEntry} found via direct ledger
* lookup.
* @throws {Error} If the simulation itself fails, or if the simulation
* already indicates a restore is needed (the simulation-based `restore`
* response is a stronger signal — call {@link detectArchivedKeysViaSimulation}
* or {@link isRestoreResponse} first).
* @see {@link detectArchivedKeysViaSimulation} for the default,
* simulation-based strategy (`archiveDetectionMethod: 'simulation'`).
*/
export async function detectArchivedKeysViaDirect(
server: ISorobanRpcClient,
transaction: Transaction,
): Promise<ArchivedLedgerEntry[]> {
const response = await server.simulateTransaction(transaction)
if (isErrorResponse(response)) {
throw new Error(`Simulation error: ${response.error}`)
}
if (isRestoreResponse(response)) {
throw new Error('Archived entries already detected via simulation restore response')
}
if (!isSuccessResponse(response)) {
throw new Error('Unexpected simulation response type')
}
const { readWrite } = extractFootprintFromSuccess(response)
if (readWrite.length === 0) {
return []
}
return detectArchivedEntries(server, readWrite)
}
/**
* Builds a ContractData ledger key for a given contract ID and storage key.
* This is used to query specific contract data entries on the ledger.
*
* @param contractId - The Stellar contract ID as a hex string (e.g. `"deadbeef..."`).
* Use {@link asContractIdHex} to cast from a plain string at the call site.
* @param key - The storage key as an xdr.ScVal.
* @param keyType - The durability of the storage entry (persistent or temporary).
* @returns An xdr.LedgerKey for the ContractData entry.
*/
export function buildContractDataKey(
contractId: ContractIdHex | string,
key: xdr.ScVal,
keyType: 'persistent' | 'temporary' = 'persistent',
): xdr.LedgerKey {
// Convert hex contract ID string to bytes
const contractBytes = new Uint8Array(
(contractId.match(/.{1,2}/g) ?? []).map((b) => parseInt(b, 16)),
)
const contractAddress = {
switch: () => xdr.ScAddressType.scAddressTypeContract(),
contractId: contractBytes,
} as unknown as xdr.ScAddress
const contractData = {
contract: contractAddress,
key,
durability:
keyType === 'temporary'
? xdr.ContractDataDurability.temporary()
: xdr.ContractDataDurability.persistent(),
} as unknown as xdr.LedgerKeyContractData
return {
type: xdr.LedgerEntryType.contractData(),
contractData,
} as unknown as xdr.LedgerKey
}
/**
* Checks whether a specific contract data entry is archived (expired / not found
* on the ledger). This is a targeted utility for dApp developers who want to
* check specific storage slots without simulating a full transaction.
*
* @param server - Soroban RPC server instance.
* @param contractId - The Stellar contract ID as a hex string.
* Use {@link asContractIdHex} to cast from a plain string at the call site.
* @param key - The storage key as an xdr.ScVal.
* @param keyType - The durability of the storage entry (persistent or temporary).
* @returns `true` if the entry is archived (not found), `false` if it exists.
*
* @example
* ```ts
* import { xdr } from '@stellar/stellar-sdk'
* import { asContractIdHex } from '@soroban-resurrect/sdk'
* const isArchived = await checkArchivedContractData(
* server,
* asContractIdHex('CCJZ5DGASBWQXR5G4GXEJM2Q4FI5L3QJ6TQ3QFJTQH7GJ6KJ3J2Q2K2Q'),
* xdr.ScVal.scvSymbol('Balance'),
* 'persistent',
* )
* ```
*/
export async function checkArchivedContractData(
server: rpc.Server,
contractId: ContractIdHex | string,
key: xdr.ScVal,
keyType: 'persistent' | 'temporary' = 'persistent',
): Promise<boolean> {
const ledgerKey = buildContractDataKey(contractId, key, keyType)
const archived = await detectArchivedEntries(server, [ledgerKey])
return archived.length > 0
}
/**
* Retrieves a specific contract data entry from the ledger.
* Returns the ledger entry data if it exists, or `null` if archived / not found.
*
* @param server - Soroban RPC server instance.
* @param contractId - The Stellar contract ID as a hex string.
* Use {@link asContractIdHex} to cast from a plain string at the call site.
* @param key - The storage key as an xdr.ScVal.
* @param keyType - The durability of the storage entry (persistent or temporary).
* @returns The ledger entry if found, otherwise `null`.
*
* @example
* ```ts
* import { xdr } from '@stellar/stellar-sdk'
* import { asContractIdHex } from '@soroban-resurrect/sdk'
* const entry = await getContractDataEntry(
* server,
* asContractIdHex('CCJZ5DGASBWQXR5G4GXEJM2Q4FI5L3QJ6TQ3QFJTQH7GJ6KJ3J2Q2K2Q'),
* xdr.ScVal.scvSymbol('Balance'),
* )
* if (entry) {
* console.log('Entry exists:', entry.key.toXDR('base64'))
* }
* ```
*/
export async function getContractDataEntry(
server: rpc.Server,
contractId: ContractIdHex | string,
key: xdr.ScVal,
keyType: 'persistent' | 'temporary' = 'persistent',
): Promise<rpc.Api.LedgerEntryResult | null> {
const ledgerKey = buildContractDataKey(contractId, key, keyType)
try {
const result = await server.getLedgerEntries(ledgerKey)
if (result.entries && result.entries.length > 0) {
return result.entries[0]
}
return null
} catch {
return null
}
}