forked from ancore-org/ancore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-reference.yaml
More file actions
740 lines (707 loc) · 25.3 KB
/
Copy pathapi-reference.yaml
File metadata and controls
740 lines (707 loc) · 25.3 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
##
# Ancore Interface Specification
# Issue #287 — OpenAPI-style contract/client interface documentation
#
# This file is the canonical reference for all contract entrypoints and SDK
# wrapper methods. Teams MUST keep this in sync when changing signatures.
#
# Version history:
# 0.1.0 — initial spec (2026-04-24)
##
spec_version: "0.1.0"
updated: "2026-04-24"
# ---------------------------------------------------------------------------
# Shared type definitions
# ---------------------------------------------------------------------------
types:
Address:
description: Stellar account or contract address (G… or C… format)
type: string
pattern: "^[GC][A-Z0-9]{55}$"
PublicKey:
description: Ed25519 public key — Stellar G… address
type: string
pattern: "^G[A-Z0-9]{55}$"
BytesN32:
description: 32-byte value encoded as hex or base64 (contract layer)
type: string
U64:
description: Unsigned 64-bit integer (unix timestamp in seconds or nonce)
type: integer
minimum: 0
maximum: 18446744073709551615
SessionPermission:
description: |
Permission bit granted to a session key. Maps to Vec<u32> in the contract.
Values are additive — pass multiple to grant multiple permissions.
type: integer
enum:
- value: 0
name: SEND_PAYMENT
description: Authorize payment operations
- value: 1
name: MANAGE_DATA
description: Authorize manage-data operations
- value: 2
name: INVOKE_CONTRACT
description: Authorize arbitrary contract invocations
note: >
PERMISSION_EXECUTE (contract constant = 1) must be present in the
permissions array for execute() to succeed via the session-key path.
PERMISSION_EXECUTE is a separate internal constant from SessionPermission —
it is NOT the same as SessionPermission.MANAGE_DATA (also value 1).
Always include it explicitly when creating session keys that need to call execute().
SessionKey:
description: A delegated signing key with scoped permissions and expiry
fields:
publicKey:
type: PublicKey
required: true
permissions:
type: array
items: SessionPermission
required: true
expiresAt:
type: U64
description: >
Expiry timestamp. The @ancore/types interface uses unix milliseconds (ms).
The contract stores and compares in unix seconds — the SDK's extend_session_key_ttl
helper auto-detects ms vs seconds (values > 100_000_000_000 are treated as ms).
Pass unix seconds to the contract directly; pass unix ms to the TypeScript SDK types.
required: true
label:
type: string
description: Optional human-readable label (off-chain only, not stored on-chain)
required: false
TransactionResult:
description: Result of a submitted Stellar transaction
fields:
status:
type: string
enum: [success, failure, pending]
required: true
hash:
type: string
description: Transaction hash (present on success)
required: false
ledger:
type: integer
description: Ledger sequence number (present on success)
required: false
error:
type: string
description: Error message (present on failure)
required: false
timestamp:
type: integer
description: Unix timestamp (ms) when the result was recorded
required: true
InvocationArgs:
description: |
Encoded contract invocation — method name + XDR ScVal arguments.
Returned by SDK builder methods; pass to your transaction builder.
fields:
method:
type: string
required: true
args:
type: array
items: xdr.ScVal
required: true
# ---------------------------------------------------------------------------
# Contract entrypoints (contracts/account/src/lib.rs)
# ---------------------------------------------------------------------------
contract_methods:
initialize:
description: |
Initialize the account contract with an owner address.
Must be called exactly once after deployment.
auth: none
parameters:
- name: owner
type: Address
required: true
description: Stellar address that will own this account
returns:
success: "void"
errors:
- code: 1
name: AlreadyInitialized
condition: Contract storage already contains an owner key
state_changes:
- Writes Owner → owner address to instance storage
- Writes Nonce → 0 to instance storage
- Writes Version → 1 to instance storage
- Extends instance TTL (30-day bump)
events:
- topic: initialized
data: "(owner: Address)"
get_owner:
description: Return the owner address stored in the contract.
auth: none
parameters: []
returns:
success: Address
errors:
- code: 2
name: NotInitialized
condition: Contract has not been initialized
get_nonce:
description: Return the current replay-protection nonce.
auth: none
parameters: []
returns:
success: U64
errors: []
note: Returns 0 if nonce key is absent (pre-initialization safe read).
get_version:
description: Return the current contract version number.
auth: none
parameters: []
returns:
success: "u32"
errors: []
execute:
description: |
Execute a cross-contract call with replay protection.
Supports two auth paths: owner direct auth or session-key signature.
auth: owner OR valid session key with PERMISSION_EXECUTE
parameters:
- name: _caller
type: CallerIdentity
required: true
description: "Enum: Owner | SessionKey(BytesN<32>)"
- name: to
type: Address
required: true
description: Target contract address
- name: function
type: Symbol
required: true
description: Function name to invoke on the target contract
- name: args
type: "Vec<Val>"
required: true
description: Arguments to pass to the target function
- name: expected_nonce
type: U64
required: true
description: Must equal the current nonce; prevents replay attacks
- name: session_pub_key
type: "Option<BytesN<32>>"
required: false
description: Session key public key (session-key path only)
- name: signature
type: "Option<BytesN<64>>"
required: false
description: Ed25519 signature over signature_payload (session-key path only)
- name: signature_payload
type: "Option<Bytes>"
required: false
description: |
Canonical payload that was signed. Must equal the contract-computed
payload: XDR(to) ++ XDR(function) ++ XDR(args) ++ XDR(nonce).
returns:
success: Val
description: Return value from the invoked contract function
errors:
- code: 2
name: NotInitialized
condition: Contract has not been initialized
- code: 3
name: Unauthorized
condition: Owner path — require_auth() failed
- code: 4
name: InvalidNonce
condition: expected_nonce does not match current nonce
- code: 5
name: SessionKeyNotFound
condition: session_pub_key provided but no matching key in storage
- code: 6
name: SessionKeyExpired
condition: "ledger.timestamp() >= session_key.expires_at"
- code: 7
name: InsufficientPermission
condition: Session key does not have PERMISSION_EXECUTE (bit 1) set
- code: 9
name: InvalidSignature
condition: signature or signature_payload missing, or payload mismatch
state_changes:
- Increments Nonce by 1 (before invocation — checks-effects-interactions)
- Extends instance TTL (30-day bump)
events:
- topic: executed
data: "(to: Address, function: Symbol, nonce: u64)"
security_notes:
- Nonce is incremented before the cross-contract call to prevent reentrancy nonce manipulation
- Signature payload binds to exact (to, function, args, nonce) — no partial replay possible
add_session_key:
description: |
Register a new session key with scoped permissions and an expiry timestamp.
Only the account owner may call this.
auth: owner (require_auth) — called AFTER the expires_at == 0 guard
parameters:
- name: public_key
type: "BytesN<32>"
required: true
description: Ed25519 public key of the session key
- name: expires_at
type: U64
required: true
description: Unix timestamp (seconds) when the key expires; must be > 0
- name: permissions
type: "Vec<u32>"
required: true
description: Permission bits granted to this key (see SessionPermission)
returns:
success: "void"
errors:
- code: 11
name: InvalidExpiration
condition: expires_at == 0 (checked first, before auth)
- code: 2
name: NotInitialized
condition: Contract has not been initialized (checked during get_owner)
- code: 3
name: Unauthorized
condition: Caller is not the owner
state_changes:
- Writes SessionKey(public_key) → SessionKey struct to persistent storage
- Extends session key TTL proportional to expires_at
- Extends instance TTL (30-day bump)
events:
- topic: session_key_added
data: "(public_key: BytesN<32>, expires_at: u64)"
revoke_session_key:
description: |
Remove a session key from storage. Immediately invalidates it.
Only the account owner may call this.
auth: owner (require_auth)
parameters:
- name: public_key
type: "BytesN<32>"
required: true
description: Ed25519 public key of the session key to revoke
returns:
success: "void"
errors:
- code: 2
name: NotInitialized
condition: Contract has not been initialized
- code: 3
name: Unauthorized
condition: Caller is not the owner
state_changes:
- Removes SessionKey(public_key) from persistent storage
- Extends instance TTL (30-day bump)
events:
- topic: session_key_revoked
data: "(public_key: BytesN<32>)"
get_session_key:
description: Return the SessionKey struct for a given public key, or None.
auth: none
parameters:
- name: public_key
type: "BytesN<32>"
required: true
returns:
success: "Option<SessionKey>"
errors: []
has_session_key:
description: Return true if a session key exists in storage (regardless of expiry).
auth: none
parameters:
- name: public_key
type: "BytesN<32>"
required: true
returns:
success: bool
errors: []
is_session_key_active:
description: Return true if a session key exists AND has not expired.
auth: none
parameters:
- name: public_key
type: "BytesN<32>"
required: true
returns:
success: bool
errors: []
refresh_session_key_ttl:
description: |
Extend the Soroban storage TTL of an existing session key so it is not
evicted before its logical expiry. Does not change expires_at.
auth: none
parameters:
- name: public_key
type: "BytesN<32>"
required: true
returns:
success: "void"
errors:
- code: 5
name: SessionKeyNotFound
condition: No session key found for the given public key
state_changes:
- Extends persistent storage TTL for SessionKey(public_key)
- Extends instance TTL (30-day bump)
events:
- topic: session_key_ttl_refreshed
data: "(public_key: BytesN<32>, expires_at: u64)"
upgrade:
description: |
Replace the contract WASM with a new hash. Increments the version counter.
Only the account owner may call this.
auth: owner (require_auth)
parameters:
- name: new_wasm_hash
type: "BytesN<32>"
required: true
description: Hash of the new WASM blob; must not be all-zero
returns:
success: "void"
errors:
- code: 2
name: NotInitialized
condition: Contract has not been initialized
- code: 3
name: Unauthorized
condition: Caller is not the owner
- code: 10
name: InvalidWasmHash
condition: new_wasm_hash is all-zero bytes
state_changes:
- Increments Version by 1
- Calls env.deployer().update_current_contract_wasm(new_wasm_hash)
- Extends instance TTL (30-day bump)
events:
- topic: upgraded
data: "(new_wasm_hash: BytesN<32>)"
migrate:
description: |
Update the stored version number for a migration. Version must be
strictly increasing. Only the account owner may call this.
auth: owner (require_auth)
parameters:
- name: new_version
type: "u32"
required: true
description: New version number; must be > current version
returns:
success: "void"
errors:
- code: 2
name: NotInitialized
condition: Contract has not been initialized
- code: 3
name: Unauthorized
condition: Caller is not the owner
- code: 8
name: InvalidVersion
condition: new_version <= current_version
state_changes:
- Writes Version → new_version to instance storage
- Extends instance TTL (30-day bump)
events:
- topic: migrated
data: "(old_version: u32, new_version: u32)"
# ---------------------------------------------------------------------------
# SDK wrapper methods (packages/core-sdk / packages/account-abstraction)
# ---------------------------------------------------------------------------
sdk_methods:
AncoreClient.addSessionKey:
package: "@ancore/core-sdk"
description: |
Build the InvocationArgs for an add_session_key contract call.
Returns encoded args — caller is responsible for building and submitting
the transaction.
Delegates to the standalone addSessionKey() function from add-session-key.ts.
signature: "addSessionKey(params: AddSessionKeyParams): InvocationArgs"
parameters:
- name: params.publicKey
type: string
required: true
description: Ed25519 public key (G… Stellar address)
validation: non-empty string
- name: params.permissions
type: "SessionPermission[]"
required: true
description: Array of permission enum values
validation: must be an array
- name: params.expiresAt
type: number
required: true
description: >
Expiry timestamp in unix seconds (passed directly to the contract).
Note: @ancore/types SessionKey.expiresAt is unix ms — convert before
passing to addSessionKey if reading from a stored SessionKey.
validation: finite number
returns:
success: InvocationArgs
errors:
- name: BuilderValidationError
code: BUILDER_VALIDATION
condition: Any required parameter is missing or invalid
- name: SessionKeyManagementError
code: SESSION_KEY_MANAGEMENT_FAILED
condition: AccountContract layer throws an unexpected error
async: false
AncoreClient.revokeSessionKey:
package: "@ancore/core-sdk"
description: |
Build the InvocationArgs for a revoke_session_key contract call.
Delegates to the standalone revokeSessionKey() function from revoke-session-key.ts.
signature: "revokeSessionKey(params: RevokeSessionKeyParams): InvocationArgs"
parameters:
- name: params.publicKey
type: string
required: true
description: Ed25519 public key of the key to revoke
validation: non-empty string
returns:
success: InvocationArgs
errors:
- name: BuilderValidationError
code: BUILDER_VALIDATION
condition: publicKey is missing or empty
- name: SessionKeyManagementError
code: SESSION_KEY_REVOKE_FAILED
condition: AccountContract layer throws an unexpected error
async: false
executeWithSessionKey:
package: "@ancore/core-sdk"
exported_from: "execute-with-session-key.ts"
description: |
Standalone exported function — NOT a method on AncoreClient.
Execute a cross-contract call authenticated by a session key.
Validates inputs, builds the execute invocation via an AccountContract instance,
delegates to the provided executionLayer for signing and submission, and returns
the result.
The AncoreClient class in ancore-client.ts does NOT expose this method —
use it directly from the module export.
signature: >
executeWithSessionKey<TResult, TArgs>(
params: ExecuteWithSessionKeyParams<TArgs>
): Promise<ExecuteWithSessionKeyResult<TResult>>
note: >
This function is used via an AncoreClient instance that is constructed with
{ accountContract, executionLayer } — a different shape from the AncoreClient
exported by ancore-client.ts. See execute-with-session-key.ts for the full
AncoreClient class that wraps this function.
parameters:
- name: params.target
type: string
required: true
description: Target contract address (G… or C…)
validation: valid Stellar Address
- name: params.function
type: string
required: true
description: Function name to invoke on the target contract
validation: non-empty string
- name: params.args
type: "xdr.ScVal[]"
required: true
description: XDR-encoded arguments for the target function
validation: must be an array
- name: params.expectedNonce
type: number
required: true
description: Current contract nonce (fetch via AccountContract.getNonce)
validation: non-negative integer
- name: params.signer.publicKey
type: string
required: true
description: Session key public key (G… address)
validation: valid Ed25519 public key (StrKey.isValidEd25519PublicKey)
- name: params.signer.signAuthEntryXdr
type: "(authEntryXdr: string) => Promise<string> | string"
required: true
description: Callback that signs an auth entry XDR and returns signed XDR
validation: must be a function
returns:
success: "ExecuteWithSessionKeyResult<TResult>"
fields:
result: "TResult — decoded return value from the contract"
transactionHash: "string | undefined — hash of the submitted transaction"
errors:
- name: SessionKeyExecutionValidationError
code: SESSION_KEY_EXECUTION_VALIDATION
condition: Any input parameter fails validation
- name: SessionKeyExecutionError
code: SESSION_KEY_EXECUTION_UNAUTHORIZED
condition: Contract returns Unauthorized (session key not authorized)
- name: SessionKeyExecutionError
code: SESSION_KEY_EXECUTION_INVALID_NONCE
condition: Contract returns InvalidNonce
- name: SessionKeyExecutionError
code: SESSION_KEY_EXECUTION_NOT_INITIALIZED
condition: Contract returns NotInitialized
- name: SessionKeyExecutionError
code: SESSION_KEY_EXECUTION_CONTRACT
condition: Any other AccountContractError
- name: SessionKeyExecutionError
code: SESSION_KEY_EXECUTION_FAILED
condition: Unknown or unexpected error
async: true
sendPayment:
package: "@ancore/core-sdk"
description: |
Build, sign, and submit a Stellar payment transaction.
Internally uses AccountTransactionBuilder (simulate + assemble) then
submits via StellarClient.
signature: >
sendPayment(
params: SendPaymentParams,
deps: SendPaymentDeps
): Promise<TransactionResult>
parameters:
- name: params.to
type: string
required: true
description: Destination Stellar address (G…)
validation: non-empty string
- name: params.amount
type: string
required: true
description: Amount as a decimal string (e.g. "10.5000000")
validation: positive numeric string
- name: params.asset
type: "{ code: string; issuer: string } | 'native'"
required: false
default: "'native'"
description: Asset to send; omit or pass 'native' for XLM
- name: params.signer
type: PaymentSigner
required: true
description: "Implements sign(xdr: string): Promise<string> | string"
validation: must have a sign function
- name: deps.sourceAccount
type: "Account"
required: true
description: Loaded Stellar Account object (provides sequence number)
- name: deps.builderOptions
type: AccountTransactionBuilderOptions
required: true
description: Network passphrase, RPC server, fee, etc.
- name: deps.stellarClient
type: StellarClient
required: true
description: "@ancore/stellar StellarClient for network submission"
returns:
success: TransactionResult
errors:
- name: BuilderValidationError
code: BUILDER_VALIDATION
condition: Invalid params (missing to, non-positive amount, missing signer)
- name: SimulationFailedError
code: SIMULATION_FAILED
condition: Soroban simulation returns an error
- name: SimulationExpiredError
code: SIMULATION_EXPIRED
condition: Simulation result requires ledger restoration
- name: TransactionSubmissionError
code: SUBMISSION_FAILED
condition: Signing fails or network submission fails
async: true
AccountContract.getOwner:
package: "@ancore/account-abstraction"
description: Simulate get_owner and return the decoded owner address.
signature: "getOwner(options: AccountContractReadOptions): Promise<string>"
parameters:
- name: options.server
type: SorobanRpcServer
required: true
- name: options.sourceAccount
type: string
required: true
- name: options.networkPassphrase
type: string
required: false
returns:
success: string
errors:
- name: AccountContractError
condition: Simulation fails or contract not initialized
async: true
AccountContract.getNonce:
package: "@ancore/account-abstraction"
description: Simulate get_nonce and return the current nonce as a number.
signature: "getNonce(options: AccountContractReadOptions): Promise<number>"
parameters:
- name: options.server
type: SorobanRpcServer
required: true
- name: options.sourceAccount
type: string
required: true
- name: options.networkPassphrase
type: string
required: false
returns:
success: number
errors:
- name: AccountContractError
condition: Simulation fails or contract not initialized
async: true
AccountContract.getSessionKey:
package: "@ancore/account-abstraction"
description: Simulate get_session_key and return the decoded SessionKey or null.
signature: >
getSessionKey(
publicKey: string | Uint8Array,
options: AccountContractReadOptions
): Promise<SessionKey | null>
parameters:
- name: publicKey
type: "string | Uint8Array"
required: true
- name: options
type: AccountContractReadOptions
required: true
returns:
success: "SessionKey | null"
errors:
- name: AccountContractError
condition: Simulation fails
async: true
# ---------------------------------------------------------------------------
# Contract error code reference
# ---------------------------------------------------------------------------
error_codes:
1: { name: AlreadyInitialized, sdk_class: AlreadyInitializedError }
2: { name: NotInitialized, sdk_class: NotInitializedError }
3: { name: Unauthorized, sdk_class: UnauthorizedError }
4: { name: InvalidNonce, sdk_class: InvalidNonceError }
5: { name: SessionKeyNotFound, sdk_class: SessionKeyNotFoundError }
6: { name: SessionKeyExpired, sdk_class: SessionKeyExpiredError }
7: { name: InsufficientPermission, sdk_class: InsufficientPermissionError }
8: { name: InvalidVersion, sdk_class: ContractInvocationError }
9: { name: InvalidSignature, sdk_class: ContractInvocationError }
10: { name: InvalidWasmHash, sdk_class: ContractInvocationError }
11: { name: InvalidExpiration, sdk_class: ContractInvocationError }
# ---------------------------------------------------------------------------
# Contract events reference
# ---------------------------------------------------------------------------
events:
initialized:
data: "(owner: Address)"
emitted_by: [initialize]
executed:
data: "(to: Address, function: Symbol, nonce: u64)"
emitted_by: [execute]
session_key_added:
data: "(public_key: BytesN<32>, expires_at: u64)"
emitted_by: [add_session_key]
session_key_revoked:
data: "(public_key: BytesN<32>)"
emitted_by: [revoke_session_key]
session_key_ttl_refreshed:
data: "(public_key: BytesN<32>, expires_at: u64)"
emitted_by: [refresh_session_key_ttl]
upgraded:
data: "(new_wasm_hash: BytesN<32>)"
emitted_by: [upgrade]
migrated:
data: "(old_version: u32, new_version: u32)"
emitted_by: [migrate]