@tetherto/wdk-wallet 1.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,127 @@
1
+ /** @interface */
2
+ export interface ISwapProtocol {
3
+ /**
4
+ * Swaps a pair of tokens.
5
+ *
6
+ * @param {SwapOptions} options - The swap's options.
7
+ * @returns {Promise<SwapResult>} The swap's result.
8
+ */
9
+ swap(options: SwapOptions): Promise<SwapResult>;
10
+ /**
11
+ * Quotes the costs of a swap operation.
12
+ *
13
+ * @param {SwapOptions} options - The swap's options.
14
+ * @returns {Promise<Omit<SwapResult, 'hash'>>} The swap's quotes.
15
+ */
16
+ quoteSwap(options: SwapOptions): Promise<Omit<SwapResult, "hash">>;
17
+ }
18
+ /** @abstract */
19
+ export default abstract class SwapProtocol implements ISwapProtocol {
20
+ /**
21
+ * Creates a new read-only swap protocol.
22
+ *
23
+ * @overload
24
+ * @param {IWalletAccountReadOnly} account - The wallet account to use to interact with the protocol.
25
+ * @param {SwapProtocolConfig} [config] - The swap protocol configuration.
26
+ */
27
+ constructor(account: IWalletAccountReadOnly, config?: SwapProtocolConfig);
28
+ /**
29
+ * Creates a new swap protocol.
30
+ *
31
+ * @overload
32
+ * @param {IWalletAccount} account - The wallet account to use to interact with the protocol.
33
+ * @param {SwapProtocolConfig} [config] - The swap protocol configuration.
34
+ */
35
+ constructor(account: IWalletAccount, config?: SwapProtocolConfig);
36
+ /**
37
+ * The wallet account to use to interact with the protocol.
38
+ *
39
+ * @protected
40
+ * @type {IWalletAccountReadOnly | IWalletAccount}
41
+ */
42
+ protected _account: IWalletAccountReadOnly | IWalletAccount;
43
+ /**
44
+ * The swap protocol configuration.
45
+ *
46
+ * @protected
47
+ * @type {SwapProtocolConfig}
48
+ */
49
+ protected _config: SwapProtocolConfig;
50
+ /**
51
+ * Swaps a pair of tokens.
52
+ *
53
+ * @abstract
54
+ * @param {SwapOptions} options - The swap's options.
55
+ * @returns {Promise<SwapResult>} The swap's result.
56
+ */
57
+ abstract swap(options: SwapOptions): Promise<SwapResult>;
58
+ /**
59
+ * Quotes the costs of a swap operation.
60
+ *
61
+ * @abstract
62
+ * @param {SwapOptions} options - The swap's options.
63
+ * @returns {Promise<Omit<SwapResult, 'hash'>>} The swap's quotes.
64
+ */
65
+ abstract quoteSwap(options: SwapOptions): Promise<Omit<SwapResult, "hash">>;
66
+ }
67
+ export type IWalletAccountReadOnly = import("../wallet-account-read-only.js").IWalletAccountReadOnly;
68
+ export type IWalletAccount = import("../wallet-account.js").IWalletAccount;
69
+ export type SwapProtocolConfig = {
70
+ /**
71
+ * - The maximum fee amount for swap operations.
72
+ */
73
+ swapMaxFee?: number | bigint;
74
+ };
75
+ export type SwapOptions = SwapCommonOptions & (SwapBuyOptions | SwapSellOptions);
76
+ type SwapCommonOptions = {
77
+ /**
78
+ * - The address of the token to sell.
79
+ */
80
+ tokenIn: string;
81
+ /**
82
+ * - The address of the token to buy.
83
+ */
84
+ tokenOut: string;
85
+ /**
86
+ * - The address that will receive the output tokens. If not set, the account itself will receive the funds.
87
+ */
88
+ to?: string;
89
+ };
90
+ type SwapBuyOptions = {
91
+ /**
92
+ * - The amount of input tokens to sell (in base unit).
93
+ */
94
+ tokenInAmount?: never;
95
+ /**
96
+ * - The amount of output tokens to buy (in base unit).
97
+ */
98
+ tokenOutAmount: number | bigint;
99
+ };
100
+ type SwapSellOptions = {
101
+ /**
102
+ * - The amount of input tokens to sell (in base unit).
103
+ */
104
+ tokenInAmount: number | bigint;
105
+ /**
106
+ * - The amount of output tokens to buy (in base unit).
107
+ */
108
+ tokenOutAmount?: never;
109
+ };
110
+ export type SwapResult = {
111
+ /**
112
+ * - The hash of the swap operation.
113
+ */
114
+ hash: string;
115
+ /**
116
+ * - The gas cost.
117
+ */
118
+ fee: bigint;
119
+ /**
120
+ * - The amount of input tokens sold.
121
+ */
122
+ tokenInAmount: bigint;
123
+ /**
124
+ * - The amount of output tokens bought.
125
+ */
126
+ tokenOutAmount: bigint;
127
+ };
@@ -0,0 +1,153 @@
1
+ /** @interface */
2
+ export interface IWalletAccountReadOnly {
3
+ /**
4
+ * Returns the account's address.
5
+ *
6
+ * @returns {Promise<string>} The account's address.
7
+ */
8
+ getAddress(): Promise<string>;
9
+ /**
10
+ * Returns the account's native token balance.
11
+ *
12
+ * @returns {Promise<bigint>} The native token balance.
13
+ */
14
+ getBalance(): Promise<bigint>;
15
+ /**
16
+ * Returns the account balance for a specific token.
17
+ *
18
+ * @param {string} tokenAddress - The smart contract address of the token.
19
+ * @returns {Promise<bigint>} The token balance.
20
+ */
21
+ getTokenBalance(tokenAddress: string): Promise<bigint>;
22
+ /**
23
+ * Quotes the costs of a send transaction operation.
24
+ *
25
+ * @param {Transaction} tx - The transaction.
26
+ * @returns {Promise<Omit<TransactionResult, 'hash'>>} The transaction's quotes.
27
+ */
28
+ quoteSendTransaction(tx: Transaction): Promise<Omit<TransactionResult, "hash">>;
29
+ /**
30
+ * Quotes the costs of a transfer operation.
31
+ *
32
+ * @param {TransferOptions} options - The transfer's options.
33
+ * @returns {Promise<Omit<TransferResult, 'hash'>>} The transfer's quotes.
34
+ */
35
+ quoteTransfer(options: TransferOptions): Promise<Omit<TransferResult, "hash">>;
36
+ /**
37
+ * Returns a transaction's receipt.
38
+ *
39
+ * @param {string} hash - The transaction's hash.
40
+ * @returns {Promise<unknown | null>} The receipt, or null if the transaction has not been included in a block yet.
41
+ */
42
+ getTransactionReceipt(hash: string): Promise<unknown | null>;
43
+ }
44
+ /**
45
+ * @abstract
46
+ * @implements {IWalletAccountReadOnly}
47
+ */
48
+ export default abstract class WalletAccountReadOnly implements IWalletAccountReadOnly {
49
+ /**
50
+ * Creates a new read-only wallet account.
51
+ *
52
+ * @param {string} [address] - The account's address.
53
+ */
54
+ constructor(address?: string);
55
+ /** @private */
56
+ private __address;
57
+ /**
58
+ * The account's address.
59
+ *
60
+ * @protected
61
+ * @type {string | undefined}
62
+ */
63
+ protected get _address(): string | undefined;
64
+ /**
65
+ * Returns the account's address.
66
+ *
67
+ * @returns {Promise<string>} The account's address.
68
+ */
69
+ getAddress(): Promise<string>;
70
+ /**
71
+ * Returns the account's native token balance.
72
+ *
73
+ * @abstract
74
+ * @returns {Promise<bigint>} The native token balance.
75
+ */
76
+ abstract getBalance(): Promise<bigint>;
77
+ /**
78
+ * Returns the account balance for a specific token.
79
+ *
80
+ * @abstract
81
+ * @param {string} tokenAddress - The smart contract address of the token.
82
+ * @returns {Promise<bigint>} The token balance.
83
+ */
84
+ abstract getTokenBalance(tokenAddress: string): Promise<bigint>;
85
+ /**
86
+ * Quotes the costs of a send transaction operation.
87
+ *
88
+ * @abstract
89
+ * @param {Transaction} tx - The transaction.
90
+ * @returns {Promise<Omit<TransactionResult, 'hash'>>} The transaction's quotes.
91
+ */
92
+ abstract quoteSendTransaction(tx: Transaction): Promise<Omit<TransactionResult, "hash">>;
93
+ /**
94
+ * Quotes the costs of a transfer operation.
95
+ *
96
+ * @abstract
97
+ * @param {TransferOptions} options - The transfer's options.
98
+ * @returns {Promise<Omit<TransferResult, 'hash'>>} The transfer's quotes.
99
+ */
100
+ abstract quoteTransfer(options: TransferOptions): Promise<Omit<TransferResult, "hash">>;
101
+ /**
102
+ * Returns a transaction's receipt.
103
+ *
104
+ * @abstract
105
+ * @param {string} hash - The transaction's hash.
106
+ * @returns {Promise<unknown | null>} The receipt, or null if the transaction has not been included in a block yet.
107
+ */
108
+ abstract getTransactionReceipt(hash: string): Promise<unknown | null>;
109
+ }
110
+ export type Transaction = {
111
+ /**
112
+ * - The transaction's recipient.
113
+ */
114
+ to: string;
115
+ /**
116
+ * - The amount of native tokens to send to the recipient (in base unit).
117
+ */
118
+ value: number | bigint;
119
+ };
120
+ export type TransactionResult = {
121
+ /**
122
+ * - The transaction's hash.
123
+ */
124
+ hash: string;
125
+ /**
126
+ * - The gas cost.
127
+ */
128
+ fee: bigint;
129
+ };
130
+ export type TransferOptions = {
131
+ /**
132
+ * - The address of the token to transfer.
133
+ */
134
+ token: string;
135
+ /**
136
+ * - The address of the recipient.
137
+ */
138
+ recipient: string;
139
+ /**
140
+ * - The amount of tokens to transfer to the recipient (in base units).
141
+ */
142
+ amount: number | bigint;
143
+ };
144
+ export type TransferResult = {
145
+ /**
146
+ * - The hash of the transfer operation.
147
+ */
148
+ hash: string;
149
+ /**
150
+ * - The gas cost.
151
+ */
152
+ fee: bigint;
153
+ };
@@ -0,0 +1,75 @@
1
+ /** @interface */
2
+ export interface IWalletAccount extends IWalletAccountReadOnly {
3
+ /**
4
+ * The derivation path's index of this account.
5
+ *
6
+ * @type {number}
7
+ */
8
+ get index(): number;
9
+ /**
10
+ * The derivation path of this account (see [BIP-44](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki)).
11
+ *
12
+ * @type {string}
13
+ */
14
+ get path(): string;
15
+ /**
16
+ * The account's key pair.
17
+ *
18
+ * @type {KeyPair}
19
+ */
20
+ get keyPair(): KeyPair;
21
+ /**
22
+ * Signs a message.
23
+ *
24
+ * @param {string} message - The message to sign.
25
+ * @returns {Promise<string>} The message's signature.
26
+ */
27
+ sign(message: string): Promise<string>;
28
+ /**
29
+ * Verifies a message's signature.
30
+ *
31
+ * @param {string} message - The original message.
32
+ * @param {string} signature - The signature to verify.
33
+ * @returns {Promise<boolean>} True if the signature is valid.
34
+ */
35
+ verify(message: string, signature: string): Promise<boolean>;
36
+ /**
37
+ * Sends a transaction.
38
+ *
39
+ * @param {Transaction} tx - The transaction.
40
+ * @returns {Promise<TransactionResult>} The transaction's result.
41
+ */
42
+ sendTransaction(tx: Transaction): Promise<TransactionResult>;
43
+ /**
44
+ * Transfers a token to another address.
45
+ *
46
+ * @param {TransferOptions} options - The transfer's options.
47
+ * @returns {Promise<TransferResult>} The transfer's result.
48
+ */
49
+ transfer(options: TransferOptions): Promise<TransferResult>;
50
+ /**
51
+ * Returns a read-only copy of the account.
52
+ *
53
+ * @returns {Promise<IWalletAccountReadOnly>} The read-only account.
54
+ */
55
+ toReadOnlyAccount(): Promise<IWalletAccountReadOnly>;
56
+ /**
57
+ * Disposes the wallet account, erasing the private key from the memory.
58
+ */
59
+ dispose(): void;
60
+ }
61
+ export type Transaction = import("./wallet-account-read-only.js").Transaction;
62
+ export type TransactionResult = import("./wallet-account-read-only.js").TransactionResult;
63
+ export type TransferOptions = import("./wallet-account-read-only.js").TransferOptions;
64
+ export type TransferResult = import("./wallet-account-read-only.js").TransferResult;
65
+ export type KeyPair = {
66
+ /**
67
+ * - The public key.
68
+ */
69
+ publicKey: Uint8Array;
70
+ /**
71
+ * - The private key (null if the account has been disposed).
72
+ */
73
+ privateKey: Uint8Array | null;
74
+ };
75
+ import { IWalletAccountReadOnly } from './wallet-account-read-only.js';
@@ -0,0 +1,91 @@
1
+ /** @abstract */
2
+ export default abstract class WalletManager {
3
+ /**
4
+ * Returns a random [BIP-39](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki) seed phrase.
5
+ *
6
+ * @returns {string} The seed phrase.
7
+ */
8
+ static getRandomSeedPhrase(): string;
9
+ /**
10
+ * Checks if a seed phrase is valid.
11
+ *
12
+ * @param {string} seedPhrase - The seed phrase.
13
+ * @returns {boolean} True if the seed phrase is valid.
14
+ */
15
+ static isValidSeedPhrase(seedPhrase: string): boolean;
16
+ /**
17
+ * Creates a new wallet manager.
18
+ *
19
+ * @param {string | Uint8Array} seed - The wallet's [BIP-39](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki) seed phrase.
20
+ * @param {WalletConfig} [config] - The wallet configuration.
21
+ */
22
+ constructor(seed: string | Uint8Array, config?: WalletConfig);
23
+ /** @private */
24
+ private _seed;
25
+ /**
26
+ * The wallet configuration.
27
+ *
28
+ * @protected
29
+ * @type {WalletConfig}
30
+ */
31
+ protected _config: WalletConfig;
32
+ /**
33
+ * A map between derivation paths and wallet accounts. The {@link dispose} method will automatically dispose
34
+ * all the accounts in this map, so developers are encouraged to map all accounts accessed through the
35
+ * {@link getAccount} and {@link getAccountByPath} methods.
36
+ *
37
+ * @protected
38
+ * @type {{ [path: string]: IWalletAccount }}
39
+ */
40
+ protected _accounts: { [path: string]: IWalletAccount };
41
+ /**
42
+ * The seed phrase of the wallet.
43
+ *
44
+ * @type {Uint8Array}
45
+ */
46
+ get seed(): Uint8Array;
47
+ /**
48
+ * Returns the wallet account at a specific index (see [BIP-44](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki)).
49
+ *
50
+ * @abstract
51
+ * @param {number} [index] - The index of the account to get (default: 0).
52
+ * @returns {Promise<IWalletAccount>} The account.
53
+ */
54
+ abstract getAccount(index?: number): Promise<IWalletAccount>;
55
+ /**
56
+ * Returns the wallet account at a specific [BIP-44](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki) derivation path.
57
+ *
58
+ * @abstract
59
+ * @param {string} path - The derivation path (e.g. "0'/0/0").
60
+ * @returns {Promise<IWalletAccount>} The account.
61
+ */
62
+ abstract getAccountByPath(path: string): Promise<IWalletAccount>;
63
+ /**
64
+ * Returns the current fee rates.
65
+ *
66
+ * @abstract
67
+ * @returns {Promise<FeeRates>} The fee rates (in base unit).
68
+ */
69
+ abstract getFeeRates(): Promise<FeeRates>;
70
+ /**
71
+ * Disposes all the wallet accounts, erasing their private keys from the memory.
72
+ */
73
+ dispose(): void;
74
+ }
75
+ export type IWalletAccount = import("./wallet-account.js").IWalletAccount;
76
+ export type WalletConfig = {
77
+ /**
78
+ * - The maximum fee amount for transfer operations.
79
+ */
80
+ transferMaxFee?: number | bigint;
81
+ };
82
+ export type FeeRates = {
83
+ /**
84
+ * - The fee rate for transaction sent with normal priority.
85
+ */
86
+ normal: bigint;
87
+ /**
88
+ * - The fee rate for transaction sent with fast priority.
89
+ */
90
+ fast: bigint;
91
+ };