@xoxno/sdk-js 1.0.235 → 1.0.237
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.
- package/dist/cjs/sdk/stellar/__tests__/smart-account.test.d.ts +1 -0
- package/dist/cjs/sdk/stellar/auth-policy.d.ts +4 -4
- package/dist/cjs/sdk/stellar/lending.d.ts +9 -7
- package/dist/cjs/sdk/stellar/prepare.d.ts +19 -3
- package/dist/cjs/sdk/stellar/quote.d.ts +12 -3
- package/dist/esm/sdk/stellar/auth-policy.js +28 -1
- package/dist/esm/sdk/stellar/cctp.js +1 -1
- package/dist/esm/sdk/stellar/governance.js +1 -1
- package/dist/esm/sdk/stellar/lending.js +3 -3
- package/dist/esm/sdk/stellar/prepare.js +83 -2
- package/dist/esm/sdk/stellar/quote.js +5 -2
- package/dist/esm/sdk/stellar/swap.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/sdk/stellar/__tests__/smart-account.test.d.ts +1 -0
- package/dist/sdk/stellar/auth-policy.d.ts +4 -4
- package/dist/sdk/stellar/index.cjs +1 -1
- package/dist/sdk/stellar/index.esm.js +1 -1
- package/dist/sdk/stellar/lending.d.ts +9 -7
- package/dist/sdk/stellar/prepare.d.ts +19 -3
- package/dist/sdk/stellar/quote.d.ts +12 -3
- package/package.json +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -19,11 +19,11 @@
|
|
|
19
19
|
* | controller `migrate_from_blend` | `blend_pool.submit(caller, controller, controller, [withdraw / withdraw_collateral / repay])`, at most twice | `contracts/controller/src/external/blend.rs:46-97` |
|
|
20
20
|
* | controller `borrow`, `withdraw`, `swap_debt`, `swap_collateral`, `repay_debt_with_collateral`, `flash_loan`, `flash_position`, any other | none | the controller funds these from its own custody (`strategies/legs.rs:50`, `strategies/flash_position.rs:285`) |
|
|
21
21
|
* | governance, any | none | proposer auth only |
|
|
22
|
-
* | router `execute_strategy` | `token_in.transfer(sender, router, <= total_in)` | `contracts/swap-aggregator/src/execute/mod.rs:81` |
|
|
22
|
+
* | router `execute_strategy` | `token_in.transfer(sender, router, <= total_in)`; `token_out.trust(sender)` at most once, when the sender has no trustline for a Stellar Asset Contract output (CAP-73) | `contracts/swap-aggregator/src/execute/mod.rs:81`, `:139` |
|
|
23
23
|
*/
|
|
24
24
|
import { xdr } from '@stellar/stellar-sdk';
|
|
25
25
|
import { type StellarNetwork } from './contracts.js';
|
|
26
|
-
export type StellarAuthPolicyErrorCode = 'MALFORMED_TRANSACTION' | 'INVOCATION_CHANGED' | 'UNSUPPORTED_CONTRACT' | 'CALLER_MISMATCH' | 'DUPLICATE_ENTRY' | 'UNEXPECTED_ROOT' | 'UNEXPECTED_CALL' | 'NESTED_INVOCATION' | 'UNEXPECTED_TOKEN' | 'UNEXPECTED_SENDER' | 'UNEXPECTED_RECIPIENT' | 'AMOUNT_EXCEEDED';
|
|
26
|
+
export type StellarAuthPolicyErrorCode = 'MALFORMED_TRANSACTION' | 'INVOCATION_CHANGED' | 'SOURCE_CHANGED' | 'UNSUPPORTED_CONTRACT' | 'CALLER_MISMATCH' | 'DUPLICATE_ENTRY' | 'UNEXPECTED_ROOT' | 'UNEXPECTED_CALL' | 'NESTED_INVOCATION' | 'UNEXPECTED_TOKEN' | 'UNEXPECTED_SENDER' | 'UNEXPECTED_RECIPIENT' | 'AMOUNT_EXCEEDED';
|
|
27
27
|
/** The invocation that broke the policy, in readable form. */
|
|
28
28
|
export interface StellarAuthOffender {
|
|
29
29
|
contract: string;
|
|
@@ -85,11 +85,11 @@ export interface StellarAuthDeployment {
|
|
|
85
85
|
export declare function stellarAuthPolicyForInvocation(caller: string, invocation: xdr.InvokeContractArgs, deployment: StellarAuthDeployment): StellarAuthPolicy;
|
|
86
86
|
export interface StellarPreparedTxAuthOptions {
|
|
87
87
|
network: StellarNetwork;
|
|
88
|
-
/**
|
|
88
|
+
/** Authorizing G... account or C... smart account, independently of the source. */
|
|
89
89
|
caller: string;
|
|
90
90
|
/**
|
|
91
91
|
* The unsigned builder output that was sent to `prepareTransaction`. When
|
|
92
|
-
* given, the prepared invocation
|
|
92
|
+
* given, the prepared invocation and effective source must match it.
|
|
93
93
|
*/
|
|
94
94
|
builtXdr?: string;
|
|
95
95
|
/** Replaces the derived policy (other contracts, custom flash receivers). */
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Stellar lending user-operation transaction builders.
|
|
3
3
|
*
|
|
4
4
|
* Each builder takes a typed Stellar argument object plus shared
|
|
5
|
-
* `StellarLendingBuilderOptions` ({ network, caller, sourceSequence,
|
|
5
|
+
* `StellarLendingBuilderOptions` ({ network, caller, sourceAccount?, sourceSequence,
|
|
6
6
|
* controllerAddress, ... }) and returns
|
|
7
7
|
* unsigned transaction XDR that must be prepared before wallet signing.
|
|
8
8
|
*
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
*
|
|
13
13
|
* Builders are RPC-free: they accept a caller-supplied `sourceSequence` so the
|
|
14
14
|
* returned XDR is deterministic and snapshot-testable. The caller fetches the
|
|
15
|
-
* current sequence via `rpc.Server.getAccount(caller)` and runs
|
|
15
|
+
* current sequence via `rpc.Server.getAccount(sourceAccount ?? caller)` and runs
|
|
16
16
|
* `rpc.Server.prepareTransaction` (simulation + Soroban footprint/auth/resource
|
|
17
17
|
* fee) before handing the XDR to the wallet to sign.
|
|
18
18
|
*/
|
|
@@ -29,17 +29,19 @@ import { type StellarSeizeModeInput, type StellarStrategySwapHopInput, type Stel
|
|
|
29
29
|
*/
|
|
30
30
|
export declare function parseStellarTokenAmount(amount: string, decimals: number | null): string;
|
|
31
31
|
/**
|
|
32
|
-
* Stellar `G...` public key of
|
|
32
|
+
* Stellar `G...` public key of a transaction source account.
|
|
33
33
|
*/
|
|
34
34
|
export type StellarAccountAddress = string;
|
|
35
35
|
export interface StellarBuilderOptions {
|
|
36
36
|
/** Must match the API deployment, RPC and signing wallet. */
|
|
37
37
|
network: StellarNetwork;
|
|
38
|
-
/**
|
|
39
|
-
caller:
|
|
38
|
+
/** Authorizing asset owner (G... account or C... smart account). */
|
|
39
|
+
caller: string;
|
|
40
|
+
/** G... transaction source; defaults to caller. Required for a C... caller. */
|
|
41
|
+
sourceAccount?: StellarAccountAddress;
|
|
40
42
|
/**
|
|
41
|
-
* Current sequence number of
|
|
42
|
-
*
|
|
43
|
+
* Current sequence number of sourceAccount ?? caller, as a decimal string.
|
|
44
|
+
* Fetch this from Soroban RPC (`server.getAccount(sourceAccount ?? caller)`) before
|
|
43
45
|
* building. Keeping it as an input (rather than fetching inside the builder)
|
|
44
46
|
* makes builders sync-friendly, RPC-free, and deterministic for snapshot tests.
|
|
45
47
|
*/
|
|
@@ -2,6 +2,19 @@ import { rpc } from '@stellar/stellar-sdk';
|
|
|
2
2
|
import type { FeeBumpTransaction, Transaction } from '@stellar/stellar-sdk';
|
|
3
3
|
import type { BuiltStellarTx } from './lending.js';
|
|
4
4
|
import { type StellarNetwork } from './contracts.js';
|
|
5
|
+
import { type StellarPreparedTxAuthOptions } from './auth-policy.js';
|
|
6
|
+
export interface StellarAuthorizationContext {
|
|
7
|
+
readonly networkPassphrase: string;
|
|
8
|
+
readonly latestLedger: number;
|
|
9
|
+
}
|
|
10
|
+
export interface StellarAuthorizationOptions extends Pick<StellarPreparedTxAuthOptions, 'caller' | 'policy' | 'deployment'> {
|
|
11
|
+
/**
|
|
12
|
+
* Authorize one base64 SorobanAuthorizationEntry using the account's policy.
|
|
13
|
+
* Return base64 with only signature and signatureExpirationLedger changed.
|
|
14
|
+
* XDR strings keep wallet SDK instances outside this SDK's bundle boundary.
|
|
15
|
+
*/
|
|
16
|
+
authorizeEntry(entryXdr: string, context: StellarAuthorizationContext): Promise<string>;
|
|
17
|
+
}
|
|
5
18
|
/** Options for preparing unsigned XDR. The wallet must sign on this network. */
|
|
6
19
|
export interface StellarPrepareOptions {
|
|
7
20
|
/** Selected Stellar network. Omission preserves the legacy unsigned-XDR API. */
|
|
@@ -17,12 +30,14 @@ export interface StellarPrepareOptions {
|
|
|
17
30
|
* or maximum-position flows. The resulting resources must fit network limits.
|
|
18
31
|
*/
|
|
19
32
|
instructionLeeway?: number;
|
|
33
|
+
/** Custom/address authorization. Requires network; validates policy before callbacks. */
|
|
34
|
+
authorization?: StellarAuthorizationOptions;
|
|
20
35
|
}
|
|
21
|
-
/**
|
|
36
|
+
/** Custom adapters must forward authMode when address authorization is requested. */
|
|
22
37
|
export interface StellarTxPreparer {
|
|
23
38
|
simulateTransaction(tx: Transaction | FeeBumpTransaction, resources?: {
|
|
24
39
|
cpuInstructions?: number;
|
|
25
|
-
}): Promise<rpc.Api.SimulateTransactionResponse>;
|
|
40
|
+
}, authMode?: 'record' | 'enforce'): Promise<rpc.Api.SimulateTransactionResponse>;
|
|
26
41
|
}
|
|
27
42
|
export declare function tagStellarInvokedContractError(contractId: string, error: unknown): Error;
|
|
28
43
|
/**
|
|
@@ -32,7 +47,8 @@ export declare function tagStellarInvokedContractError(contractId: string, error
|
|
|
32
47
|
* @param server - Host application's RPC server on the selected network.
|
|
33
48
|
* @param xdr - Unsigned builder output, not a signed transaction.
|
|
34
49
|
* @param opts - Pass `network` explicitly; it must match the RPC and wallet.
|
|
35
|
-
* @returns Prepared, unsigned base64 XDR for
|
|
50
|
+
* @returns Prepared, unsigned base64 XDR for source signing. With authorization,
|
|
51
|
+
* address signatures are attached and checked by a second, enforced simulation.
|
|
36
52
|
* @remarks This helper does not sign, submit, or wait for confirmation. Existing
|
|
37
53
|
* calls without `network` retain a neutral hash domain because XDR contains no
|
|
38
54
|
* passphrase; always select the actual network when the wallet signs.
|
|
@@ -71,15 +71,22 @@ export interface StellarQuoteLp {
|
|
|
71
71
|
* fields the DTO does not yet declare. `alternatives` is still on the DTO but
|
|
72
72
|
* the service no longer returns it.
|
|
73
73
|
*/
|
|
74
|
+
export type StellarQuoteTransactionPayload = StellarQuoteTransactionPayloadDto & {
|
|
75
|
+
/** Address auth entries still need authorization and enforced simulation. */
|
|
76
|
+
authorizationRequired?: boolean;
|
|
77
|
+
};
|
|
74
78
|
export type StellarQuoteResponse = StellarAggregatorQuoteResponseDto & {
|
|
75
79
|
snapshot?: StellarQuoteSnapshot | null;
|
|
76
80
|
degraded?: StellarQuoteDegraded | null;
|
|
77
81
|
lp?: StellarQuoteLp | null;
|
|
78
82
|
/** Ordered LP-conversion steps; mutually exclusive with `transaction`. */
|
|
79
|
-
transactions?:
|
|
83
|
+
transactions?: StellarQuoteTransactionPayload[] | null;
|
|
84
|
+
transaction?: StellarQuoteTransactionPayload | null;
|
|
80
85
|
};
|
|
81
86
|
/** Quote request; adds `simulate`, which the DTO does not yet declare. */
|
|
82
87
|
export type StellarQuoteRequest = StellarAggregatorQuoteRequestDto & {
|
|
88
|
+
/** G... transaction source. Required with a C... sender; otherwise defaults to sender. */
|
|
89
|
+
sourceAccount?: string;
|
|
83
90
|
/** With `sender`: simulate and prepare resource data (service default `true`). */
|
|
84
91
|
simulate?: boolean;
|
|
85
92
|
};
|
|
@@ -102,11 +109,13 @@ export interface StellarQuoteFetchOptions {
|
|
|
102
109
|
* `steps`. Amounts are token base-unit decimal strings. Requote after changes
|
|
103
110
|
* to amount, direction or slippage; preparation is the execution check.
|
|
104
111
|
*
|
|
105
|
-
* Passing `sender` (G...) together with `slippage` returns a direct-swap
|
|
112
|
+
* Passing `sender` (G... or C...) together with `slippage` returns a direct-swap
|
|
106
113
|
* envelope under `transaction.envelopeXdr`. The router comes from the service's
|
|
107
114
|
* `/api/v1/config`; the `router` request field is ignored by the service and is
|
|
108
115
|
* no longer sent. Prepare the envelope before wallet signing unless
|
|
109
|
-
* `transaction.simulated` is true.
|
|
116
|
+
* `transaction.simulated` is true. C... senders require a G... `sourceAccount`.
|
|
117
|
+
* A distinct source selects Soroban execution. If `authorizationRequired` is
|
|
118
|
+
* true, authorize the address entries and enforce simulation before source signing.
|
|
110
119
|
* @param request - Exactly one of amountIn or amountOut; optional referralId is forwarded unchanged.
|
|
111
120
|
* @param opts - Quote-server URL and optional fetch options supplied by the host.
|
|
112
121
|
* @returns Quote estimates and opaque route bytes; does not execute a swap.
|
|
@@ -32,7 +32,7 @@ function _define_property(obj, key, value) {
|
|
|
32
32
|
* | controller `migrate_from_blend` | `blend_pool.submit(caller, controller, controller, [withdraw / withdraw_collateral / repay])`, at most twice | `contracts/controller/src/external/blend.rs:46-97` |
|
|
33
33
|
* | controller `borrow`, `withdraw`, `swap_debt`, `swap_collateral`, `repay_debt_with_collateral`, `flash_loan`, `flash_position`, any other | none | the controller funds these from its own custody (`strategies/legs.rs:50`, `strategies/flash_position.rs:285`) |
|
|
34
34
|
* | governance, any | none | proposer auth only |
|
|
35
|
-
* | router `execute_strategy` | `token_in.transfer(sender, router, <= total_in)` | `contracts/swap-aggregator/src/execute/mod.rs:81` |
|
|
35
|
+
* | router `execute_strategy` | `token_in.transfer(sender, router, <= total_in)`; `token_out.trust(sender)` at most once, when the sender has no trustline for a Stellar Asset Contract output (CAP-73) | `contracts/swap-aggregator/src/execute/mod.rs:81`, `:139` |
|
|
36
36
|
*/ import { Address, scValToBigInt, StrKey, xdr } from "@stellar/stellar-sdk";
|
|
37
37
|
import { getStellarDeployment } from "./contracts.js";
|
|
38
38
|
import { decodeStellarRouteBytes } from "./route-verify.js";
|
|
@@ -222,6 +222,15 @@ const blendSubmitAllowance = (blendPool, caller, controller)=>({
|
|
|
222
222
|
maxAmount: totalIn
|
|
223
223
|
}
|
|
224
224
|
];
|
|
225
|
+
// `ensure_trustline` opens the sender's missing trustline for the output token before the payout.
|
|
226
|
+
policy.calls = [
|
|
227
|
+
{
|
|
228
|
+
contract: route.tokenOut,
|
|
229
|
+
fn: 'trust',
|
|
230
|
+
maxCount: 1,
|
|
231
|
+
reject: (trustArgs)=>trustArgs.length === 1 && addressOf(trustArgs[0]) === caller ? undefined : '`trust` is not for the caller'
|
|
232
|
+
}
|
|
233
|
+
];
|
|
225
234
|
return policy;
|
|
226
235
|
}
|
|
227
236
|
if (contract !== deployment.lendingController) {
|
|
@@ -288,6 +297,7 @@ const singleInvocation = (envelopeXdr)=>{
|
|
|
288
297
|
}
|
|
289
298
|
return {
|
|
290
299
|
source: accountOf(xdrField(operation, 'sourceAccount') ?? xdrField(tx, 'sourceAccount')),
|
|
300
|
+
transactionSource: accountOf(xdrField(tx, 'sourceAccount')),
|
|
291
301
|
op,
|
|
292
302
|
call: xdrField(xdrField(op, 'hostFunction'), 'invokeContract')
|
|
293
303
|
};
|
|
@@ -299,6 +309,12 @@ const singleInvocation = (envelopeXdr)=>{
|
|
|
299
309
|
* @throws StellarAuthPolicyError | StellarRouteVerificationError
|
|
300
310
|
*/ export function assertStellarPreparedTxAuth(preparedXdr, opts) {
|
|
301
311
|
const prepared = singleInvocation(preparedXdr);
|
|
312
|
+
if (opts.builtXdr !== undefined) {
|
|
313
|
+
const built = singleInvocation(opts.builtXdr);
|
|
314
|
+
if (built.source !== prepared.source || built.transactionSource !== prepared.transactionSource) {
|
|
315
|
+
throw new StellarAuthPolicyError('SOURCE_CHANGED', 'the prepared transaction changes the source account');
|
|
316
|
+
}
|
|
317
|
+
}
|
|
302
318
|
if (opts.builtXdr !== undefined && xdrField(singleInvocation(opts.builtXdr).op, 'hostFunction').toXDR('base64') !== xdrField(prepared.op, 'hostFunction').toXDR('base64')) {
|
|
303
319
|
throw new StellarAuthPolicyError('INVOCATION_CHANGED', 'the prepared transaction does not invoke what was built', describe(prepared.call));
|
|
304
320
|
}
|
|
@@ -306,5 +322,16 @@ const singleInvocation = (envelopeXdr)=>{
|
|
|
306
322
|
...getStellarDeployment(opts.network),
|
|
307
323
|
...opts.deployment
|
|
308
324
|
});
|
|
325
|
+
if (policy.caller !== opts.caller) {
|
|
326
|
+
throw new StellarAuthPolicyError('CALLER_MISMATCH', 'the policy caller differs from the authorizing caller');
|
|
327
|
+
}
|
|
328
|
+
if (opts.caller !== prepared.source) {
|
|
329
|
+
for (const entry of xdrField(prepared.op, 'auth')){
|
|
330
|
+
const credentials = xdrField(entry, 'credentials');
|
|
331
|
+
if (xdrType(credentials) === 'sorobanCredentialsSourceAccount' || xdrType(credentials) === 'sorobanCredentialsAddress' && Address.fromScAddress(xdrField(xdrField(credentials, 'address'), 'address')).toString() === prepared.source) {
|
|
332
|
+
throw new StellarAuthPolicyError('UNEXPECTED_SENDER', 'the separate source account must not authorize contract calls');
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
309
336
|
assertStellarAuthEntries(xdrField(prepared.op, 'auth'), policy, prepared.source);
|
|
310
337
|
}
|
|
@@ -11,7 +11,7 @@ const hexToBuffer = (hex)=>{
|
|
|
11
11
|
/**
|
|
12
12
|
* Build an unsigned XDR for the Stellar CCTP forwarder `mint_and_forward`.
|
|
13
13
|
*/ export function buildStellarCctpForwardTx(opts) {
|
|
14
|
-
const source = new Account(opts.caller, opts.sourceSequence);
|
|
14
|
+
const source = new Account(opts.sourceAccount ?? opts.caller, opts.sourceSequence);
|
|
15
15
|
const operation = new Contract(opts.forwarderAddress).call('mint_and_forward', xdr.ScVal.scvBytes(hexToBuffer(opts.message)), xdr.ScVal.scvBytes(hexToBuffer(opts.attestation)));
|
|
16
16
|
const tx = new TransactionBuilder(source, {
|
|
17
17
|
fee: opts.fee ?? '10000000',
|
|
@@ -123,7 +123,7 @@ const encodeRoleArgs = (a)=>scStruct({
|
|
|
123
123
|
throw new Error('Stellar governanceAddress is required');
|
|
124
124
|
}
|
|
125
125
|
const contract = new Contract(opts.governanceAddress);
|
|
126
|
-
const source = new Account(opts.caller, opts.sourceSequence);
|
|
126
|
+
const source = new Account(opts.sourceAccount ?? opts.caller, opts.sourceSequence);
|
|
127
127
|
const tx = new TransactionBuilder(source, {
|
|
128
128
|
fee: opts.fee ?? BASE_FEE,
|
|
129
129
|
networkPassphrase: STELLAR_NETWORK_PASSPHRASE[opts.network]
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Stellar lending user-operation transaction builders.
|
|
3
3
|
*
|
|
4
4
|
* Each builder takes a typed Stellar argument object plus shared
|
|
5
|
-
* `StellarLendingBuilderOptions` ({ network, caller, sourceSequence,
|
|
5
|
+
* `StellarLendingBuilderOptions` ({ network, caller, sourceAccount?, sourceSequence,
|
|
6
6
|
* controllerAddress, ... }) and returns
|
|
7
7
|
* unsigned transaction XDR that must be prepared before wallet signing.
|
|
8
8
|
*
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
*
|
|
13
13
|
* Builders are RPC-free: they accept a caller-supplied `sourceSequence` so the
|
|
14
14
|
* returned XDR is deterministic and snapshot-testable. The caller fetches the
|
|
15
|
-
* current sequence via `rpc.Server.getAccount(caller)` and runs
|
|
15
|
+
* current sequence via `rpc.Server.getAccount(sourceAccount ?? caller)` and runs
|
|
16
16
|
* `rpc.Server.prepareTransaction` (simulation + Soroban footprint/auth/resource
|
|
17
17
|
* fee) before handing the XDR to the wallet to sign.
|
|
18
18
|
*/ import { Account, BASE_FEE, Contract, Soroban, scValToNative, TransactionBuilder, xdr } from "@stellar/stellar-sdk";
|
|
@@ -54,7 +54,7 @@ import { addr, asStellarBytes, asStellarStrategySwapBytes, bool, hubAsset, i128,
|
|
|
54
54
|
throw new Error('Stellar lending controllerAddress is required');
|
|
55
55
|
}
|
|
56
56
|
const contract = new Contract(opts.controllerAddress);
|
|
57
|
-
const source = new Account(opts.caller, opts.sourceSequence);
|
|
57
|
+
const source = new Account(opts.sourceAccount ?? opts.caller, opts.sourceSequence);
|
|
58
58
|
const tx = new TransactionBuilder(source, {
|
|
59
59
|
fee: opts.fee ?? BASE_FEE,
|
|
60
60
|
networkPassphrase: STELLAR_NETWORK_PASSPHRASE[opts.network]
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import { TransactionBuilder, rpc } from "@stellar/stellar-sdk";
|
|
1
|
+
import { Address, Operation, TransactionBuilder, rpc, xdr } from "@stellar/stellar-sdk";
|
|
2
2
|
import { STELLAR_NETWORK_PASSPHRASE } from "./contracts.js";
|
|
3
|
+
import { assertStellarPreparedTxAuth } from "./auth-policy.js";
|
|
4
|
+
import { xdrField, xdrType } from "./xdr-compat.js";
|
|
3
5
|
export function tagStellarInvokedContractError(contractId, error) {
|
|
4
6
|
const message = error instanceof Error ? error.message : String(error);
|
|
5
7
|
return new Error(`[xoxno-invoked:${contractId}] ${message}`);
|
|
@@ -11,7 +13,8 @@ export function tagStellarInvokedContractError(contractId, error) {
|
|
|
11
13
|
* @param server - Host application's RPC server on the selected network.
|
|
12
14
|
* @param xdr - Unsigned builder output, not a signed transaction.
|
|
13
15
|
* @param opts - Pass `network` explicitly; it must match the RPC and wallet.
|
|
14
|
-
* @returns Prepared, unsigned base64 XDR for
|
|
16
|
+
* @returns Prepared, unsigned base64 XDR for source signing. With authorization,
|
|
17
|
+
* address signatures are attached and checked by a second, enforced simulation.
|
|
15
18
|
* @remarks This helper does not sign, submit, or wait for confirmation. Existing
|
|
16
19
|
* calls without `network` retain a neutral hash domain because XDR contains no
|
|
17
20
|
* passphrase; always select the actual network when the wallet signs.
|
|
@@ -23,6 +26,10 @@ export function tagStellarInvokedContractError(contractId, error) {
|
|
|
23
26
|
}
|
|
24
27
|
const tx = TransactionBuilder.fromXDR(xdr, opts?.network ? STELLAR_NETWORK_PASSPHRASE[opts.network] : '');
|
|
25
28
|
try {
|
|
29
|
+
if (opts?.authorization) {
|
|
30
|
+
if (!opts.network) throw new Error('network is required for address authorization');
|
|
31
|
+
return await prepareAuthorizedTx(server, tx, opts.network, instructionLeeway, opts.authorization);
|
|
32
|
+
}
|
|
26
33
|
const simulation = await server.simulateTransaction(tx, {
|
|
27
34
|
cpuInstructions: instructionLeeway
|
|
28
35
|
});
|
|
@@ -35,6 +42,80 @@ export function tagStellarInvokedContractError(contractId, error) {
|
|
|
35
42
|
throw error;
|
|
36
43
|
}
|
|
37
44
|
}
|
|
45
|
+
async function prepareAuthorizedTx(server, tx, network, instructionLeeway, authorization) {
|
|
46
|
+
if ('innerTransaction' in tx || tx.signatures.length !== 0 || tx.operations.length !== 1) {
|
|
47
|
+
throw new Error('address authorization requires an unsigned single-operation transaction');
|
|
48
|
+
}
|
|
49
|
+
const op = tx.operations[0];
|
|
50
|
+
if (op.type !== 'invokeHostFunction') {
|
|
51
|
+
throw new Error('address authorization requires a contract invocation');
|
|
52
|
+
}
|
|
53
|
+
// Always record fresh entries; quote envelopes may contain old placeholder nonces.
|
|
54
|
+
const recording = TransactionBuilder.cloneFrom(tx).clearOperations().addOperation(Operation.invokeHostFunction({
|
|
55
|
+
...op,
|
|
56
|
+
auth: []
|
|
57
|
+
})).build();
|
|
58
|
+
const resources = {
|
|
59
|
+
cpuInstructions: instructionLeeway
|
|
60
|
+
};
|
|
61
|
+
const simulation = await server.simulateTransaction(recording, resources, 'record');
|
|
62
|
+
if (rpc.Api.isSimulationError(simulation)) throw new Error(simulation.error);
|
|
63
|
+
const recorded = rpc.assembleTransaction(recording, simulation).build();
|
|
64
|
+
assertStellarPreparedTxAuth(recorded.toXDR(), {
|
|
65
|
+
...authorization,
|
|
66
|
+
network,
|
|
67
|
+
builtXdr: tx.toXDR()
|
|
68
|
+
});
|
|
69
|
+
const recordedOp = recorded.operations[0];
|
|
70
|
+
if (recordedOp.type !== 'invokeHostFunction') {
|
|
71
|
+
throw new Error('simulation changed the operation type');
|
|
72
|
+
}
|
|
73
|
+
const entries = recordedOp.auth ?? [];
|
|
74
|
+
// Check every signer before invoking any signing callback.
|
|
75
|
+
for (const entry of entries){
|
|
76
|
+
const root = xdrField(xdrField(entry, 'rootInvocation'), 'function');
|
|
77
|
+
if (xdrType(root) !== 'sorobanAuthorizedFunctionTypeContractFn' || xdrField(root, 'contractFn').toXDR('base64') !== xdrField(recordedOp.func, 'invokeContract').toXDR('base64')) {
|
|
78
|
+
throw new Error('recorded authorization root differs from the built invocation');
|
|
79
|
+
}
|
|
80
|
+
const credentials = xdrField(entry, 'credentials');
|
|
81
|
+
const kind = xdrType(credentials);
|
|
82
|
+
if (kind === 'sorobanCredentialsSourceAccount') continue;
|
|
83
|
+
if (kind !== 'sorobanCredentialsAddress' || Address.fromScAddress(xdrField(xdrField(credentials, 'address'), 'address')).toString() !== authorization.caller) {
|
|
84
|
+
throw new Error('unexpected signer in recorded address authorization');
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const signed = [];
|
|
88
|
+
const context = Object.freeze({
|
|
89
|
+
networkPassphrase: STELLAR_NETWORK_PASSPHRASE[network],
|
|
90
|
+
latestLedger: simulation.latestLedger
|
|
91
|
+
});
|
|
92
|
+
for (const entry of entries){
|
|
93
|
+
const credentials = xdrField(entry, 'credentials');
|
|
94
|
+
if (xdrType(credentials) === 'sorobanCredentialsSourceAccount') {
|
|
95
|
+
signed.push(entry);
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
const result = xdr.SorobanAuthorizationEntry.fromXDR(await authorization.authorizeEntry(entry.toXDR('base64'), context), 'base64');
|
|
99
|
+
const resultCredentials = xdrField(result, 'credentials');
|
|
100
|
+
if (xdrType(resultCredentials) !== 'sorobanCredentialsAddress') {
|
|
101
|
+
throw new Error('authorization callback changed the credentials type');
|
|
102
|
+
}
|
|
103
|
+
const before = xdrField(credentials, 'address');
|
|
104
|
+
const after = xdrField(resultCredentials, 'address');
|
|
105
|
+
if (xdrField(before, 'address').toXDR('base64') !== xdrField(after, 'address').toXDR('base64') || xdrField(before, 'nonce').toString() !== xdrField(after, 'nonce').toString() || xdrField(entry, 'rootInvocation').toXDR('base64') !== xdrField(result, 'rootInvocation').toXDR('base64')) {
|
|
106
|
+
throw new Error('authorization callback changed the signer, nonce or invocation');
|
|
107
|
+
}
|
|
108
|
+
signed.push(result);
|
|
109
|
+
}
|
|
110
|
+
const authorized = TransactionBuilder.cloneFrom(recorded).clearOperations().addOperation(Operation.invokeHostFunction({
|
|
111
|
+
...recordedOp,
|
|
112
|
+
auth: signed
|
|
113
|
+
})).build();
|
|
114
|
+
// Enforce, rather than record again: check signatures and include __check_auth resources.
|
|
115
|
+
const enforced = await server.simulateTransaction(authorized, resources, 'enforce');
|
|
116
|
+
if (rpc.Api.isSimulationError(enforced)) throw new Error(enforced.error);
|
|
117
|
+
return rpc.assembleTransaction(authorized, enforced).build().toXDR();
|
|
118
|
+
}
|
|
38
119
|
/**
|
|
39
120
|
* Prepare {@link BuiltStellarTx} before wallet signing.
|
|
40
121
|
* @param server - Host RPC server for the selected deployment.
|
|
@@ -31,11 +31,13 @@
|
|
|
31
31
|
* `steps`. Amounts are token base-unit decimal strings. Requote after changes
|
|
32
32
|
* to amount, direction or slippage; preparation is the execution check.
|
|
33
33
|
*
|
|
34
|
-
* Passing `sender` (G...) together with `slippage` returns a direct-swap
|
|
34
|
+
* Passing `sender` (G... or C...) together with `slippage` returns a direct-swap
|
|
35
35
|
* envelope under `transaction.envelopeXdr`. The router comes from the service's
|
|
36
36
|
* `/api/v1/config`; the `router` request field is ignored by the service and is
|
|
37
37
|
* no longer sent. Prepare the envelope before wallet signing unless
|
|
38
|
-
* `transaction.simulated` is true.
|
|
38
|
+
* `transaction.simulated` is true. C... senders require a G... `sourceAccount`.
|
|
39
|
+
* A distinct source selects Soroban execution. If `authorizationRequired` is
|
|
40
|
+
* true, authorize the address entries and enforce simulation before source signing.
|
|
39
41
|
* @param request - Exactly one of amountIn or amountOut; optional referralId is forwarded unchanged.
|
|
40
42
|
* @param opts - Quote-server URL and optional fetch options supplied by the host.
|
|
41
43
|
* @returns Quote estimates and opaque route bytes; does not execute a swap.
|
|
@@ -57,6 +59,7 @@
|
|
|
57
59
|
slippage: request.slippage,
|
|
58
60
|
includePaths: request.includePaths,
|
|
59
61
|
sender: request.sender,
|
|
62
|
+
sourceAccount: request.sourceAccount,
|
|
60
63
|
simulate: request.simulate,
|
|
61
64
|
referralId: request.referralId,
|
|
62
65
|
platform: request.platform,
|
|
@@ -33,7 +33,7 @@ export const encodeStrategyPayloadToRouteXdr = (payload)=>encodeStrategyPayload(
|
|
|
33
33
|
throw new Error('Stellar aggregator routerAddress is required');
|
|
34
34
|
}
|
|
35
35
|
const contract = new Contract(opts.routerAddress);
|
|
36
|
-
const source = new Account(opts.caller, opts.sourceSequence);
|
|
36
|
+
const source = new Account(opts.sourceAccount ?? opts.caller, opts.sourceSequence);
|
|
37
37
|
const swapBytes = asStellarStrategySwapBytes(withDefaultReferral(swap, opts.referralId));
|
|
38
38
|
const tx = new TransactionBuilder(source, {
|
|
39
39
|
fee: opts.fee ?? BASE_FEE,
|