@dynamic-labs/stellar 4.59.1 → 4.60.0
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/CHANGELOG.md +23 -0
- package/package.cjs +1 -1
- package/package.js +1 -1
- package/package.json +10 -4
- package/src/StellarLocalStorageCache.cjs +65 -0
- package/src/StellarLocalStorageCache.d.ts +29 -0
- package/src/StellarLocalStorageCache.js +61 -0
- package/src/connectors/FreighterWalletConnector/FreighterProvider.cjs +84 -0
- package/src/connectors/FreighterWalletConnector/FreighterProvider.d.ts +6 -0
- package/src/connectors/FreighterWalletConnector/FreighterProvider.js +80 -0
- package/src/connectors/FreighterWalletConnector/FreighterWalletConnector.cjs +45 -0
- package/src/connectors/FreighterWalletConnector/FreighterWalletConnector.d.ts +16 -0
- package/src/connectors/FreighterWalletConnector/FreighterWalletConnector.js +41 -0
- package/src/connectors/FreighterWalletConnector/index.d.ts +2 -0
- package/src/connectors/LobstrWalletConnector/LobstrWalletConnector.cjs +95 -0
- package/src/connectors/LobstrWalletConnector/LobstrWalletConnector.d.ts +22 -0
- package/src/connectors/LobstrWalletConnector/LobstrWalletConnector.js +91 -0
- package/src/connectors/LobstrWalletConnector/index.d.ts +1 -0
- package/src/connectors/OneKeyWalletConnector/OneKeyProvider.cjs +124 -0
- package/src/connectors/OneKeyWalletConnector/OneKeyProvider.d.ts +6 -0
- package/src/connectors/OneKeyWalletConnector/OneKeyProvider.js +120 -0
- package/src/connectors/OneKeyWalletConnector/OneKeyWalletConnector.cjs +43 -0
- package/src/connectors/OneKeyWalletConnector/OneKeyWalletConnector.d.ts +14 -0
- package/src/connectors/OneKeyWalletConnector/OneKeyWalletConnector.js +39 -0
- package/src/connectors/StellarWalletConnector/StellarWalletConnector.cjs +427 -0
- package/src/connectors/StellarWalletConnector/StellarWalletConnector.d.ts +112 -0
- package/src/connectors/StellarWalletConnector/StellarWalletConnector.js +423 -0
- package/src/connectors/StellarWalletConnector/index.d.ts +1 -0
- package/src/index.cjs +10 -1
- package/src/index.d.ts +5 -2
- package/src/index.js +7 -1
- package/src/injected/fetchInjectedWalletConnectors.cjs +56 -0
- package/src/injected/fetchInjectedWalletConnectors.d.ts +16 -0
- package/src/injected/fetchInjectedWalletConnectors.js +52 -0
- package/src/types/IStellarProvider.d.ts +73 -0
- package/src/types/LobstrProvider.d.ts +33 -0
- package/src/types.d.ts +13 -26
- package/src/utils/buildPaymentTransaction.cjs +37 -0
- package/src/utils/buildPaymentTransaction.d.ts +21 -0
- package/src/utils/buildPaymentTransaction.js +33 -0
- package/src/utils/checkTrustline.cjs +33 -0
- package/src/utils/checkTrustline.d.ts +9 -0
- package/src/utils/checkTrustline.js +29 -0
- package/src/utils/createPaymentAsset.cjs +23 -0
- package/src/utils/createPaymentAsset.d.ts +13 -0
- package/src/utils/createPaymentAsset.js +19 -0
- package/src/utils/createTransactionMemo.d.ts +13 -0
- package/src/utils/getAccountBalance.cjs +33 -0
- package/src/utils/getAccountBalance.d.ts +11 -0
- package/src/utils/getAccountBalance.js +29 -0
- package/src/utils/getNetworkPassphrase.d.ts +7 -0
- package/src/utils/index.d.ts +7 -0
- package/src/utils/normalizeNetworkName.d.ts +14 -0
- package/src/wallet/StellarWallet.cjs +65 -10
- package/src/wallet/StellarWallet.d.ts +17 -21
- package/src/wallet/StellarWallet.js +65 -10
- package/src/wallet/index.d.ts +1 -0
- package/src/wallet/isStellarWallet/index.d.ts +1 -0
- package/src/wallet/isStellarWallet/isStellarWallet.cjs +8 -0
- package/src/wallet/isStellarWallet/isStellarWallet.js +4 -0
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unified Stellar Provider Interface
|
|
3
|
+
*
|
|
4
|
+
* This is the normalized interface that all Stellar wallet provider adapters
|
|
5
|
+
* must implement. It provides a consistent API regardless of the underlying
|
|
6
|
+
* wallet implementation (OneKey, Freighter, Lobstr, etc.)
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Events emitted by Stellar wallet providers
|
|
10
|
+
*/
|
|
11
|
+
export type StellarProviderEvent = 'accountChanged' | 'networkChanged';
|
|
12
|
+
/**
|
|
13
|
+
* Callback for Stellar provider events
|
|
14
|
+
*/
|
|
15
|
+
export type StellarEventCallback = (data: unknown) => void;
|
|
16
|
+
/**
|
|
17
|
+
* Options for signing Stellar transactions
|
|
18
|
+
*/
|
|
19
|
+
export type StellarSignTransactionOptions = {
|
|
20
|
+
/** Account public key to sign with (for multi-account wallets) */
|
|
21
|
+
accountToSign?: string;
|
|
22
|
+
/** Network identifier ('PUBLIC', 'TESTNET', 'FUTURENET') */
|
|
23
|
+
network?: string;
|
|
24
|
+
/** Network passphrase for the transaction */
|
|
25
|
+
networkPassphrase?: string;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Unified interface that all Stellar wallet providers must implement
|
|
29
|
+
* after normalization/adaptation.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* // Connect to wallet
|
|
34
|
+
* const publicKey = await provider.connect();
|
|
35
|
+
*
|
|
36
|
+
* // Get address
|
|
37
|
+
* const address = await provider.getAddress();
|
|
38
|
+
*
|
|
39
|
+
* // Sign a transaction
|
|
40
|
+
* const signedXdr = await provider.sign(transactionXdr, {
|
|
41
|
+
* network: 'TESTNET',
|
|
42
|
+
* networkPassphrase: 'Test SDF Network ; September 2015'
|
|
43
|
+
* });
|
|
44
|
+
*
|
|
45
|
+
* // Sign a message
|
|
46
|
+
* const signature = await provider.signMessage("Hello Stellar");
|
|
47
|
+
*
|
|
48
|
+
* // Subscribe to events
|
|
49
|
+
* provider.on('accountChanged', (newAddress) => {
|
|
50
|
+
* console.log('Account changed:', newAddress);
|
|
51
|
+
* });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export type IStellarProvider = {
|
|
55
|
+
/** Close the wallet connection and cleanup resources */
|
|
56
|
+
close(): void;
|
|
57
|
+
/** Connect to the wallet and return the user's public key */
|
|
58
|
+
connect(): Promise<string>;
|
|
59
|
+
/** Disconnect from the wallet */
|
|
60
|
+
disconnect(): Promise<void>;
|
|
61
|
+
/** Get the user's Stellar public key/address */
|
|
62
|
+
getAddress(): Promise<string>;
|
|
63
|
+
/** Get the current network (PUBLIC, TESTNET, FUTURENET) */
|
|
64
|
+
getNetwork(): Promise<string>;
|
|
65
|
+
/** Check if the wallet is currently unlocked */
|
|
66
|
+
isUnlocked(): Promise<boolean>;
|
|
67
|
+
/** Subscribe to wallet events (account/network changes) */
|
|
68
|
+
on(event: StellarProviderEvent, callback: StellarEventCallback): void;
|
|
69
|
+
/** Sign a Stellar transaction XDR */
|
|
70
|
+
sign(transactionXdr: string, options?: StellarSignTransactionOptions): Promise<string>;
|
|
71
|
+
/** Sign an arbitrary message */
|
|
72
|
+
signMessage(message: string): Promise<string>;
|
|
73
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lobstr Signer Extension Provider Types
|
|
3
|
+
*
|
|
4
|
+
* Types for the Lobstr Signer Extension API
|
|
5
|
+
* From @lobstrco/signer-extension-api package
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Lobstr Signer Extension API
|
|
9
|
+
* These are the raw methods provided by @lobstrco/signer-extension-api package
|
|
10
|
+
*
|
|
11
|
+
* Note: Lobstr uses a functional API (individual exported functions)
|
|
12
|
+
* rather than a provider object at window.lobstr
|
|
13
|
+
*/
|
|
14
|
+
export type LobstrSignerApi = {
|
|
15
|
+
/** Get the user's public key */
|
|
16
|
+
getPublicKey: () => Promise<string>;
|
|
17
|
+
/** Sign a transaction */
|
|
18
|
+
signTransaction: (transactionXdr: string) => Promise<string>;
|
|
19
|
+
/** Check if extension is installed and accessible */
|
|
20
|
+
isConnected: () => Promise<boolean>;
|
|
21
|
+
/** Sign a message */
|
|
22
|
+
signMessage: (message: string) => Promise<{
|
|
23
|
+
signedMessage: string;
|
|
24
|
+
signerAddress: string;
|
|
25
|
+
} | null>;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Lobstr sign message response
|
|
29
|
+
*/
|
|
30
|
+
export type LobstrSignMessageResponse = {
|
|
31
|
+
signedMessage: string;
|
|
32
|
+
signerAddress: string;
|
|
33
|
+
} | null;
|
package/src/types.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import type { Transaction } from '@stellar/stellar-sdk';
|
|
2
1
|
import type { GenericNetwork } from '@dynamic-labs/types';
|
|
3
|
-
import type {
|
|
2
|
+
import type { WalletConnectorConstructor } from '@dynamic-labs/wallet-connector-core';
|
|
4
3
|
/**
|
|
5
4
|
* Interface for Stellar wallet providers.
|
|
6
5
|
*
|
|
@@ -74,20 +73,15 @@ export type StellarConnectionResult = {
|
|
|
74
73
|
export type StellarSendBalanceProps = {
|
|
75
74
|
/** Amount to send (in XLM for native transfers) */
|
|
76
75
|
amount: string;
|
|
77
|
-
/** Optional asset information for non-XLM transfers */
|
|
78
|
-
asset?: {
|
|
79
|
-
/** Asset code */
|
|
80
|
-
code: string;
|
|
81
|
-
/** Asset issuer */
|
|
82
|
-
issuer: string;
|
|
83
|
-
};
|
|
84
|
-
/** Optional memo */
|
|
85
|
-
memo?: {
|
|
86
|
-
type: 'hash' | 'id' | 'return' | 'text';
|
|
87
|
-
value: string;
|
|
88
|
-
};
|
|
89
76
|
/** Recipient address (Stellar public key) */
|
|
90
77
|
toAddress: string;
|
|
78
|
+
/** Optional token information for non-XLM transfers */
|
|
79
|
+
token?: {
|
|
80
|
+
/** Token address in format "CODE:ISSUER" (e.g., "USDC:GXXXXXXX...") */
|
|
81
|
+
address: string;
|
|
82
|
+
/** Token decimals (optional, Stellar uses 7 decimals by default) */
|
|
83
|
+
decimals?: number;
|
|
84
|
+
};
|
|
91
85
|
};
|
|
92
86
|
/**
|
|
93
87
|
* Stellar wallet connector constructor properties
|
|
@@ -100,16 +94,6 @@ export type StellarWalletConnectorProps = {
|
|
|
100
94
|
/** Wallet book schema */
|
|
101
95
|
walletBook: unknown;
|
|
102
96
|
};
|
|
103
|
-
/**
|
|
104
|
-
* Extension locators for identifying Stellar wallet browser extensions.
|
|
105
|
-
* These flags are typically set on the injected provider object to identify
|
|
106
|
-
* which wallet extension is present.
|
|
107
|
-
*/
|
|
108
|
-
export type ExtensionLocator = 'isAlbedo' | 'isFreighter' | 'isLobstr' | 'isRabet' | 'isXBull';
|
|
109
|
-
/**
|
|
110
|
-
* Type for Stellar provider condition used in provider lookup.
|
|
111
|
-
*/
|
|
112
|
-
export type StellarProviderCondition = ProviderCondition<ExtensionLocator>;
|
|
113
97
|
/**
|
|
114
98
|
* Stellar network names
|
|
115
99
|
*/
|
|
@@ -119,6 +103,9 @@ export type StellarNetworkName = 'FUTURENET' | 'PUBLIC' | 'TESTNET';
|
|
|
119
103
|
*/
|
|
120
104
|
export type StellarMethodName = 'close' | 'connect' | 'disconnect' | 'getNetwork' | 'isUnlocked' | 'on' | 'sign' | 'signMessage';
|
|
121
105
|
/**
|
|
122
|
-
* Stellar
|
|
106
|
+
* Stellar wallet connector with isConnected check.
|
|
107
|
+
* Extends WalletConnectorConstructor with an optional isConnected method.
|
|
123
108
|
*/
|
|
124
|
-
export
|
|
109
|
+
export interface StellarWalletConnectorWithIsConnected extends WalletConnectorConstructor {
|
|
110
|
+
isConnected?: () => Promise<boolean>;
|
|
111
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var stellarSdk = require('@stellar/stellar-sdk');
|
|
7
|
+
|
|
8
|
+
const DEFAULT_TIMEOUT = 180;
|
|
9
|
+
/**
|
|
10
|
+
* Builds a Stellar payment transaction.
|
|
11
|
+
*
|
|
12
|
+
* @param params - Transaction parameters
|
|
13
|
+
* @param params.sourceAccount - The source account
|
|
14
|
+
* @param params.networkPassphrase - The network passphrase
|
|
15
|
+
* @param params.toAddress - Recipient address
|
|
16
|
+
* @param params.amount - Amount to send
|
|
17
|
+
* @param params.asset - Asset to send
|
|
18
|
+
* @param params.memo - Optional transaction memo
|
|
19
|
+
* @returns A built Stellar transaction
|
|
20
|
+
*/
|
|
21
|
+
const buildPaymentTransaction = (params) => {
|
|
22
|
+
const { sourceAccount, networkPassphrase, toAddress, amount, asset, memo } = params;
|
|
23
|
+
const transactionBuilder = new stellarSdk.TransactionBuilder(sourceAccount, {
|
|
24
|
+
fee: stellarSdk.BASE_FEE,
|
|
25
|
+
networkPassphrase,
|
|
26
|
+
}).addOperation(stellarSdk.Operation.payment({
|
|
27
|
+
amount,
|
|
28
|
+
asset,
|
|
29
|
+
destination: toAddress,
|
|
30
|
+
}));
|
|
31
|
+
if (memo) {
|
|
32
|
+
transactionBuilder.addMemo(memo);
|
|
33
|
+
}
|
|
34
|
+
return transactionBuilder.setTimeout(DEFAULT_TIMEOUT).build();
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
exports.buildPaymentTransaction = buildPaymentTransaction;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type Asset, type Horizon, type Memo, Transaction } from '@stellar/stellar-sdk';
|
|
2
|
+
/**
|
|
3
|
+
* Builds a Stellar payment transaction.
|
|
4
|
+
*
|
|
5
|
+
* @param params - Transaction parameters
|
|
6
|
+
* @param params.sourceAccount - The source account
|
|
7
|
+
* @param params.networkPassphrase - The network passphrase
|
|
8
|
+
* @param params.toAddress - Recipient address
|
|
9
|
+
* @param params.amount - Amount to send
|
|
10
|
+
* @param params.asset - Asset to send
|
|
11
|
+
* @param params.memo - Optional transaction memo
|
|
12
|
+
* @returns A built Stellar transaction
|
|
13
|
+
*/
|
|
14
|
+
export declare const buildPaymentTransaction: (params: {
|
|
15
|
+
amount: string;
|
|
16
|
+
asset: Asset;
|
|
17
|
+
memo?: Memo;
|
|
18
|
+
networkPassphrase: string;
|
|
19
|
+
sourceAccount: Horizon.AccountResponse;
|
|
20
|
+
toAddress: string;
|
|
21
|
+
}) => Transaction;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { TransactionBuilder, BASE_FEE, Operation } from '@stellar/stellar-sdk';
|
|
3
|
+
|
|
4
|
+
const DEFAULT_TIMEOUT = 180;
|
|
5
|
+
/**
|
|
6
|
+
* Builds a Stellar payment transaction.
|
|
7
|
+
*
|
|
8
|
+
* @param params - Transaction parameters
|
|
9
|
+
* @param params.sourceAccount - The source account
|
|
10
|
+
* @param params.networkPassphrase - The network passphrase
|
|
11
|
+
* @param params.toAddress - Recipient address
|
|
12
|
+
* @param params.amount - Amount to send
|
|
13
|
+
* @param params.asset - Asset to send
|
|
14
|
+
* @param params.memo - Optional transaction memo
|
|
15
|
+
* @returns A built Stellar transaction
|
|
16
|
+
*/
|
|
17
|
+
const buildPaymentTransaction = (params) => {
|
|
18
|
+
const { sourceAccount, networkPassphrase, toAddress, amount, asset, memo } = params;
|
|
19
|
+
const transactionBuilder = new TransactionBuilder(sourceAccount, {
|
|
20
|
+
fee: BASE_FEE,
|
|
21
|
+
networkPassphrase,
|
|
22
|
+
}).addOperation(Operation.payment({
|
|
23
|
+
amount,
|
|
24
|
+
asset,
|
|
25
|
+
destination: toAddress,
|
|
26
|
+
}));
|
|
27
|
+
if (memo) {
|
|
28
|
+
transactionBuilder.addMemo(memo);
|
|
29
|
+
}
|
|
30
|
+
return transactionBuilder.setTimeout(DEFAULT_TIMEOUT).build();
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export { buildPaymentTransaction };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Checks if an account has a trustline for a specific asset.
|
|
8
|
+
*
|
|
9
|
+
* @param account - The Horizon account response
|
|
10
|
+
* @param asset - The asset to check for trustline
|
|
11
|
+
* @returns True if the account has a trustline for the asset, false otherwise
|
|
12
|
+
*/
|
|
13
|
+
const checkTrustline = (account, asset) => {
|
|
14
|
+
// Native asset (XLM) doesn't require a trustline
|
|
15
|
+
if (asset.isNative()) {
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
// Check if the account has a balance for this asset
|
|
19
|
+
const assetCode = asset.getCode();
|
|
20
|
+
const assetIssuer = asset.getIssuer();
|
|
21
|
+
return account.balances.some((balance) => {
|
|
22
|
+
if (balance.asset_type === 'native') {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
if (balance.asset_type === 'credit_alphanum4' ||
|
|
26
|
+
balance.asset_type === 'credit_alphanum12') {
|
|
27
|
+
return (balance.asset_code === assetCode && balance.asset_issuer === assetIssuer);
|
|
28
|
+
}
|
|
29
|
+
return false;
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
exports.checkTrustline = checkTrustline;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Asset, Horizon } from '@stellar/stellar-sdk';
|
|
2
|
+
/**
|
|
3
|
+
* Checks if an account has a trustline for a specific asset.
|
|
4
|
+
*
|
|
5
|
+
* @param account - The Horizon account response
|
|
6
|
+
* @param asset - The asset to check for trustline
|
|
7
|
+
* @returns True if the account has a trustline for the asset, false otherwise
|
|
8
|
+
*/
|
|
9
|
+
export declare const checkTrustline: (account: Horizon.AccountResponse, asset: Asset) => boolean;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
/**
|
|
3
|
+
* Checks if an account has a trustline for a specific asset.
|
|
4
|
+
*
|
|
5
|
+
* @param account - The Horizon account response
|
|
6
|
+
* @param asset - The asset to check for trustline
|
|
7
|
+
* @returns True if the account has a trustline for the asset, false otherwise
|
|
8
|
+
*/
|
|
9
|
+
const checkTrustline = (account, asset) => {
|
|
10
|
+
// Native asset (XLM) doesn't require a trustline
|
|
11
|
+
if (asset.isNative()) {
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
// Check if the account has a balance for this asset
|
|
15
|
+
const assetCode = asset.getCode();
|
|
16
|
+
const assetIssuer = asset.getIssuer();
|
|
17
|
+
return account.balances.some((balance) => {
|
|
18
|
+
if (balance.asset_type === 'native') {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
if (balance.asset_type === 'credit_alphanum4' ||
|
|
22
|
+
balance.asset_type === 'credit_alphanum12') {
|
|
23
|
+
return (balance.asset_code === assetCode && balance.asset_issuer === assetIssuer);
|
|
24
|
+
}
|
|
25
|
+
return false;
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export { checkTrustline };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var stellarSdk = require('@stellar/stellar-sdk');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Creates a Stellar Asset object for payment operations.
|
|
10
|
+
*
|
|
11
|
+
* @param asset - Optional asset information
|
|
12
|
+
* @param asset.code - Asset code
|
|
13
|
+
* @param asset.issuer - Asset issuer public key
|
|
14
|
+
* @returns A Stellar Asset object (native XLM or custom asset)
|
|
15
|
+
*/
|
|
16
|
+
const createPaymentAsset = (asset) => {
|
|
17
|
+
if (asset) {
|
|
18
|
+
return new stellarSdk.Asset(asset.code, asset.issuer);
|
|
19
|
+
}
|
|
20
|
+
return stellarSdk.Asset.native();
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
exports.createPaymentAsset = createPaymentAsset;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Asset } from '@stellar/stellar-sdk';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a Stellar Asset object for payment operations.
|
|
4
|
+
*
|
|
5
|
+
* @param asset - Optional asset information
|
|
6
|
+
* @param asset.code - Asset code
|
|
7
|
+
* @param asset.issuer - Asset issuer public key
|
|
8
|
+
* @returns A Stellar Asset object (native XLM or custom asset)
|
|
9
|
+
*/
|
|
10
|
+
export declare const createPaymentAsset: (asset?: {
|
|
11
|
+
code: string;
|
|
12
|
+
issuer: string;
|
|
13
|
+
}) => Asset;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { Asset } from '@stellar/stellar-sdk';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Creates a Stellar Asset object for payment operations.
|
|
6
|
+
*
|
|
7
|
+
* @param asset - Optional asset information
|
|
8
|
+
* @param asset.code - Asset code
|
|
9
|
+
* @param asset.issuer - Asset issuer public key
|
|
10
|
+
* @returns A Stellar Asset object (native XLM or custom asset)
|
|
11
|
+
*/
|
|
12
|
+
const createPaymentAsset = (asset) => {
|
|
13
|
+
if (asset) {
|
|
14
|
+
return new Asset(asset.code, asset.issuer);
|
|
15
|
+
}
|
|
16
|
+
return Asset.native();
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export { createPaymentAsset };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Memo } from '@stellar/stellar-sdk';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a Stellar transaction memo from memo parameters.
|
|
4
|
+
*
|
|
5
|
+
* @param memo - Optional memo information
|
|
6
|
+
* @param memo.type - Memo type ('text', 'id', 'hash', or 'return')
|
|
7
|
+
* @param memo.value - Memo value
|
|
8
|
+
* @returns A Stellar Memo object or undefined if no memo provided
|
|
9
|
+
*/
|
|
10
|
+
export declare const createTransactionMemo: (memo?: {
|
|
11
|
+
type: 'hash' | 'id' | 'return' | 'text';
|
|
12
|
+
value: string;
|
|
13
|
+
}) => Memo | undefined;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Gets the balance for a specific asset from a Stellar account.
|
|
8
|
+
*
|
|
9
|
+
* @param account - The Horizon account object
|
|
10
|
+
* @param assetCode - Optional asset code. If not provided, returns native XLM balance
|
|
11
|
+
* @param assetIssuer - Optional asset issuer. Required if assetCode is provided
|
|
12
|
+
* @returns The balance as a string
|
|
13
|
+
* @throws Error if assetCode is provided without assetIssuer
|
|
14
|
+
*/
|
|
15
|
+
const getAccountBalance = (account, assetCode, assetIssuer) => {
|
|
16
|
+
if (!assetCode) {
|
|
17
|
+
const nativeBalance = account.balances.find((balance) => balance.asset_type === 'native');
|
|
18
|
+
return nativeBalance && 'balance' in nativeBalance
|
|
19
|
+
? nativeBalance.balance
|
|
20
|
+
: '0';
|
|
21
|
+
}
|
|
22
|
+
if (!assetIssuer) {
|
|
23
|
+
throw new Error('Asset issuer is required when asset code is provided');
|
|
24
|
+
}
|
|
25
|
+
const assetBalance = account.balances.find((balance) => balance.asset_type !== 'native' &&
|
|
26
|
+
'asset_code' in balance &&
|
|
27
|
+
'asset_issuer' in balance &&
|
|
28
|
+
balance.asset_code === assetCode &&
|
|
29
|
+
balance.asset_issuer === assetIssuer);
|
|
30
|
+
return assetBalance && 'balance' in assetBalance ? assetBalance.balance : '0';
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
exports.getAccountBalance = getAccountBalance;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Horizon } from '@stellar/stellar-sdk';
|
|
2
|
+
/**
|
|
3
|
+
* Gets the balance for a specific asset from a Stellar account.
|
|
4
|
+
*
|
|
5
|
+
* @param account - The Horizon account object
|
|
6
|
+
* @param assetCode - Optional asset code. If not provided, returns native XLM balance
|
|
7
|
+
* @param assetIssuer - Optional asset issuer. Required if assetCode is provided
|
|
8
|
+
* @returns The balance as a string
|
|
9
|
+
* @throws Error if assetCode is provided without assetIssuer
|
|
10
|
+
*/
|
|
11
|
+
export declare const getAccountBalance: (account: Horizon.AccountResponse, assetCode?: string, assetIssuer?: string) => string;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
/**
|
|
3
|
+
* Gets the balance for a specific asset from a Stellar account.
|
|
4
|
+
*
|
|
5
|
+
* @param account - The Horizon account object
|
|
6
|
+
* @param assetCode - Optional asset code. If not provided, returns native XLM balance
|
|
7
|
+
* @param assetIssuer - Optional asset issuer. Required if assetCode is provided
|
|
8
|
+
* @returns The balance as a string
|
|
9
|
+
* @throws Error if assetCode is provided without assetIssuer
|
|
10
|
+
*/
|
|
11
|
+
const getAccountBalance = (account, assetCode, assetIssuer) => {
|
|
12
|
+
if (!assetCode) {
|
|
13
|
+
const nativeBalance = account.balances.find((balance) => balance.asset_type === 'native');
|
|
14
|
+
return nativeBalance && 'balance' in nativeBalance
|
|
15
|
+
? nativeBalance.balance
|
|
16
|
+
: '0';
|
|
17
|
+
}
|
|
18
|
+
if (!assetIssuer) {
|
|
19
|
+
throw new Error('Asset issuer is required when asset code is provided');
|
|
20
|
+
}
|
|
21
|
+
const assetBalance = account.balances.find((balance) => balance.asset_type !== 'native' &&
|
|
22
|
+
'asset_code' in balance &&
|
|
23
|
+
'asset_issuer' in balance &&
|
|
24
|
+
balance.asset_code === assetCode &&
|
|
25
|
+
balance.asset_issuer === assetIssuer);
|
|
26
|
+
return assetBalance && 'balance' in assetBalance ? assetBalance.balance : '0';
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export { getAccountBalance };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gets the network passphrase for a given network name.
|
|
3
|
+
*
|
|
4
|
+
* @param network - The network name ('PUBLIC', 'TESTNET', or 'FUTURENET')
|
|
5
|
+
* @returns The network passphrase
|
|
6
|
+
*/
|
|
7
|
+
export declare const getNetworkPassphrase: (network: string | undefined) => string;
|
package/src/utils/index.d.ts
CHANGED
|
@@ -1 +1,8 @@
|
|
|
1
|
+
export { buildPaymentTransaction } from './buildPaymentTransaction';
|
|
2
|
+
export { checkTrustline } from './checkTrustline';
|
|
3
|
+
export { createPaymentAsset } from './createPaymentAsset';
|
|
4
|
+
export { createTransactionMemo } from './createTransactionMemo';
|
|
5
|
+
export { getAccountBalance } from './getAccountBalance';
|
|
1
6
|
export { getNetworkFromAddress } from './getNetworkFromAddress';
|
|
7
|
+
export { getNetworkPassphrase } from './getNetworkPassphrase';
|
|
8
|
+
export { normalizeNetworkName } from './normalizeNetworkName';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Normalize network name to standard format
|
|
3
|
+
*
|
|
4
|
+
* @param network - Raw network name from wallet
|
|
5
|
+
* @returns Normalized network name (PUBLIC, TESTNET, or FUTURENET)
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* normalizeNetworkName('mainnet') // 'PUBLIC'
|
|
10
|
+
* normalizeNetworkName('testnet') // 'TESTNET'
|
|
11
|
+
* normalizeNetworkName('Test SDF Network ; September 2015') // 'TESTNET'
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export declare const normalizeNetworkName: (network: string) => string;
|
|
@@ -4,7 +4,12 @@
|
|
|
4
4
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
5
|
|
|
6
6
|
var _tslib = require('../../_virtual/_tslib.cjs');
|
|
7
|
+
var stellarSdk = require('@stellar/stellar-sdk');
|
|
7
8
|
var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
|
|
9
|
+
var buildPaymentTransaction = require('../utils/buildPaymentTransaction.cjs');
|
|
10
|
+
var checkTrustline = require('../utils/checkTrustline.cjs');
|
|
11
|
+
var createPaymentAsset = require('../utils/createPaymentAsset.cjs');
|
|
12
|
+
var getAccountBalance = require('../utils/getAccountBalance.cjs');
|
|
8
13
|
|
|
9
14
|
/**
|
|
10
15
|
* Stellar wallet implementation that provides chain-specific functionality
|
|
@@ -16,26 +21,76 @@ var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
|
|
|
16
21
|
class StellarWallet extends walletConnectorCore.Wallet {
|
|
17
22
|
/**
|
|
18
23
|
* Get balance of the wallet.
|
|
19
|
-
* @
|
|
24
|
+
* @param assetCode - Optional asset code. If not provided, returns native XLM balance
|
|
25
|
+
* @param assetIssuer - Optional asset issuer. Required if assetCode is provided
|
|
26
|
+
* @returns Balance of the wallet
|
|
20
27
|
*/
|
|
21
|
-
getBalance() {
|
|
28
|
+
getBalance(assetCode, assetIssuer) {
|
|
22
29
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
23
30
|
yield this._connector.connect();
|
|
24
|
-
|
|
25
|
-
|
|
31
|
+
const horizonServer = yield this.getHorizonServer();
|
|
32
|
+
const account = yield horizonServer.loadAccount(this.address);
|
|
33
|
+
return getAccountBalance.getAccountBalance(account, assetCode, assetIssuer);
|
|
26
34
|
});
|
|
27
35
|
}
|
|
28
36
|
/**
|
|
29
37
|
* Sends balance to another address.
|
|
30
|
-
*
|
|
31
|
-
*
|
|
38
|
+
* Supports both native XLM and custom Stellar assets.
|
|
39
|
+
*
|
|
40
|
+
* @param params - Send balance parameters
|
|
41
|
+
* @param params.amount - Amount to send
|
|
42
|
+
* @param params.toAddress - Recipient address (Stellar public key)
|
|
43
|
+
* @param params.token - Optional token information for non-XLM transfers
|
|
44
|
+
* @param params.token.address - Token address in format "CODE:ISSUER"
|
|
45
|
+
* @param params.token.decimals - Token decimals (optional, Stellar uses 7 decimals by default)
|
|
32
46
|
* @returns Transaction hash
|
|
33
47
|
*/
|
|
34
|
-
sendBalance(
|
|
35
|
-
return _tslib.__awaiter(this,
|
|
48
|
+
sendBalance(params) {
|
|
49
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
50
|
+
const { amount, toAddress, token } = params;
|
|
36
51
|
yield this._connector.connect();
|
|
37
|
-
|
|
38
|
-
|
|
52
|
+
const horizonServer = yield this.getHorizonServer();
|
|
53
|
+
const sourceAccount = yield horizonServer.loadAccount(this.address);
|
|
54
|
+
const networkPassphrase = yield this._connector.getNetworkPassphrase();
|
|
55
|
+
let asset;
|
|
56
|
+
if (token === null || token === void 0 ? void 0 : token.address) {
|
|
57
|
+
const [code, issuer] = token.address.split(':');
|
|
58
|
+
if (!code || !issuer) {
|
|
59
|
+
throw new Error('Invalid token address format. Expected "CODE:ISSUER" (e.g., "USDC:GXXXXXXX...")');
|
|
60
|
+
}
|
|
61
|
+
asset = { code, issuer };
|
|
62
|
+
}
|
|
63
|
+
const paymentAsset = createPaymentAsset.createPaymentAsset(asset);
|
|
64
|
+
let destinationAccount;
|
|
65
|
+
try {
|
|
66
|
+
destinationAccount = yield horizonServer.loadAccount(toAddress);
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
throw new Error(`Failed to load destination account ${toAddress}. The account may not exist or be funded.`);
|
|
70
|
+
}
|
|
71
|
+
// Check if destination account has trustline for non-native assets
|
|
72
|
+
if (!paymentAsset.isNative()) {
|
|
73
|
+
const hasTrustline = checkTrustline.checkTrustline(destinationAccount, paymentAsset);
|
|
74
|
+
if (!hasTrustline) {
|
|
75
|
+
const assetCode = paymentAsset.getCode();
|
|
76
|
+
const assetIssuer = paymentAsset.getIssuer();
|
|
77
|
+
throw new Error(`Destination account ${toAddress} does not have a trustline for asset ${assetCode}:${assetIssuer}. ` +
|
|
78
|
+
'The recipient must establish a trustline before receiving this asset.');
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const transaction = buildPaymentTransaction.buildPaymentTransaction({
|
|
82
|
+
amount,
|
|
83
|
+
asset: paymentAsset,
|
|
84
|
+
memo: undefined,
|
|
85
|
+
networkPassphrase,
|
|
86
|
+
sourceAccount,
|
|
87
|
+
toAddress,
|
|
88
|
+
});
|
|
89
|
+
const transactionXdr = transaction.toXDR();
|
|
90
|
+
const signedXdr = yield this.signTransaction(transactionXdr);
|
|
91
|
+
const transactionToSubmit = stellarSdk.TransactionBuilder.fromXDR(signedXdr, networkPassphrase);
|
|
92
|
+
const response = yield horizonServer.submitTransaction(transactionToSubmit);
|
|
93
|
+
return response.hash;
|
|
39
94
|
});
|
|
40
95
|
}
|
|
41
96
|
/**
|