@dynamic-labs/aptos 4.38.0 → 4.40.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 +21 -0
- package/_virtual/_tslib.cjs +36 -0
- package/_virtual/_tslib.js +32 -0
- package/package.cjs +1 -1
- package/package.js +1 -1
- package/package.json +7 -3
- package/src/connectors/AptosWalletConnector/AptosWalletConnector.cjs +208 -0
- package/src/connectors/AptosWalletConnector/AptosWalletConnector.d.ts +71 -0
- package/src/connectors/AptosWalletConnector/AptosWalletConnector.js +204 -0
- package/src/connectors/AptosWalletConnector/index.d.ts +1 -0
- package/src/consts/index.cjs +19 -0
- package/src/consts/index.js +15 -0
- package/src/index.cjs +27 -6
- package/src/index.d.ts +10 -1
- package/src/index.js +19 -6
- package/src/injected/AptosProviderHelper.cjs +317 -0
- package/src/injected/AptosProviderHelper.d.ts +106 -0
- package/src/injected/AptosProviderHelper.js +313 -0
- package/src/injected/InjectedWalletBase.cjs +87 -0
- package/src/injected/InjectedWalletBase.d.ts +28 -0
- package/src/injected/InjectedWalletBase.js +83 -0
- package/src/injected/fetchInjectedWalletConnectors.cjs +171 -0
- package/src/injected/fetchInjectedWalletConnectors.d.ts +48 -0
- package/src/injected/fetchInjectedWalletConnectors.js +167 -0
- package/src/injected/index.d.ts +3 -0
- package/src/types.d.ts +33 -113
- package/src/utils/assertProvider/assertProvider.cjs +36 -0
- package/src/utils/assertProvider/assertProvider.d.ts +6 -5
- package/src/utils/assertProvider/assertProvider.js +32 -0
- package/src/utils/getWalletStandardWallets/getWalletStandardWallets.cjs +68 -0
- package/src/utils/getWalletStandardWallets/getWalletStandardWallets.js +64 -0
- package/src/utils/invokeWalletMethod/invokeWalletMethod.cjs +61 -0
- package/src/utils/invokeWalletMethod/invokeWalletMethod.d.ts +6 -7
- package/src/utils/invokeWalletMethod/invokeWalletMethod.js +57 -0
- package/src/utils/isWalletWithRequiredFeatureSet/isWalletWithRequiredFeatureSet.cjs +10 -0
- package/src/utils/isWalletWithRequiredFeatureSet/isWalletWithRequiredFeatureSet.js +6 -0
- package/src/utils/parseConnectionResult/parseConnectionResult.cjs +40 -0
- package/src/utils/parseConnectionResult/parseConnectionResult.d.ts +7 -10
- package/src/utils/parseConnectionResult/parseConnectionResult.js +36 -0
- package/src/utils/parseTransactionResponse/parseTransactionResponse.cjs +53 -0
- package/src/utils/parseTransactionResponse/parseTransactionResponse.d.ts +3 -3
- package/src/utils/parseTransactionResponse/parseTransactionResponse.js +49 -0
- package/src/wallet/AptosWallet.cjs +138 -0
- package/src/wallet/AptosWallet.d.ts +76 -0
- package/src/wallet/AptosWallet.js +134 -0
- package/src/wallet/index.d.ts +1 -0
- package/src/walletStandard/createAptosSignerFromWalletStandard.cjs +244 -0
- package/src/walletStandard/createAptosSignerFromWalletStandard.d.ts +9 -0
- package/src/walletStandard/createAptosSignerFromWalletStandard.js +240 -0
- package/src/walletStandard/getConnectorConstructorForWalletStandardWallet.cjs +31 -0
- package/src/walletStandard/getConnectorConstructorForWalletStandardWallet.d.ts +3 -0
- package/src/walletStandard/getConnectorConstructorForWalletStandardWallet.js +27 -0
- package/src/connectors/index.d.ts +0 -0
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { __awaiter } from '../../_virtual/_tslib.js';
|
|
3
|
+
import { Wallet } from '@dynamic-labs/wallet-connector-core';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Aptos wallet implementation that provides chain-specific functionality
|
|
7
|
+
* for interacting with the Aptos blockchain.
|
|
8
|
+
*
|
|
9
|
+
* This class extends the base Wallet class and provides Aptos-specific
|
|
10
|
+
* methods for transactions, signing, and network operations.
|
|
11
|
+
*/
|
|
12
|
+
class AptosWallet extends Wallet {
|
|
13
|
+
/**
|
|
14
|
+
* Sends balance to another address.
|
|
15
|
+
* @param amount - Amount to send (in APT for native transfers)
|
|
16
|
+
* @param toAddress - Recipient address
|
|
17
|
+
* @param token - Optional token information for non-APT transfers
|
|
18
|
+
* @returns Transaction hash
|
|
19
|
+
*/
|
|
20
|
+
sendBalance(_a) {
|
|
21
|
+
return __awaiter(this, arguments, void 0, function* ({ amount, toAddress, token, }) {
|
|
22
|
+
yield this._connector.connect();
|
|
23
|
+
// Get the Aptos client to create the transaction
|
|
24
|
+
const aptosClient = yield this.getAptosClient();
|
|
25
|
+
if (!aptosClient) {
|
|
26
|
+
throw new Error('Aptos client not available');
|
|
27
|
+
}
|
|
28
|
+
// Get current account info
|
|
29
|
+
const accountInfo = yield this.getAccountInfo();
|
|
30
|
+
if (!accountInfo) {
|
|
31
|
+
throw new Error('No account connected');
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
// Calculate amount with proper decimals (APT has 8 decimals by default)
|
|
35
|
+
const decimals = (token === null || token === void 0 ? void 0 : token.decimals) || 8;
|
|
36
|
+
const transferAmount = parseFloat(amount) * Math.pow(10, decimals);
|
|
37
|
+
// Create the transfer transaction
|
|
38
|
+
const transaction = yield aptosClient.transferCoinTransaction({
|
|
39
|
+
amount: transferAmount,
|
|
40
|
+
coinType: token === null || token === void 0 ? void 0 : token.address,
|
|
41
|
+
recipient: toAddress,
|
|
42
|
+
sender: accountInfo.address, // If undefined, defaults to APT (0x1::aptos_coin::AptosCoin)
|
|
43
|
+
});
|
|
44
|
+
// Sign and submit the transaction using the wallet provider
|
|
45
|
+
const txHash = yield this.signAndSubmitTransaction(transaction);
|
|
46
|
+
return txHash;
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
throw new Error(`Failed to send balance: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Returns the Aptos client configured for the wallet's current network.
|
|
55
|
+
*
|
|
56
|
+
* @returns The Aptos client instance or undefined if not available
|
|
57
|
+
*/
|
|
58
|
+
getAptosClient() {
|
|
59
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
+
return this._connector.getAptosClient();
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Returns the wallet's current account information.
|
|
65
|
+
*
|
|
66
|
+
* @returns The current account info or undefined if not connected
|
|
67
|
+
*/
|
|
68
|
+
getAccountInfo() {
|
|
69
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
70
|
+
return this._connector.getAccountInfo();
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Returns the wallet's current network information.
|
|
75
|
+
*
|
|
76
|
+
* @returns The current network info or undefined if not available
|
|
77
|
+
*/
|
|
78
|
+
getNetworkInfo() {
|
|
79
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
80
|
+
return this._connector.getNetworkInfo();
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Signs a transaction for the Aptos blockchain.
|
|
85
|
+
*
|
|
86
|
+
* @param transaction - The transaction to sign
|
|
87
|
+
* @param asFeePayer - Whether to sign as fee payer (optional)
|
|
88
|
+
* @returns The signed transaction or user response
|
|
89
|
+
*/
|
|
90
|
+
signTransaction(transaction, asFeePayer) {
|
|
91
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
92
|
+
yield this._connector.connect();
|
|
93
|
+
return this._connector.signTransaction(transaction, asFeePayer);
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Signs a message for authentication purposes.
|
|
98
|
+
*
|
|
99
|
+
* @param input - The message signing input parameters
|
|
100
|
+
* @returns The signature result or user response
|
|
101
|
+
*/
|
|
102
|
+
signAptosMessage(input) {
|
|
103
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
104
|
+
yield this._connector.connect();
|
|
105
|
+
return this._connector.signAptosMessage(input);
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Signs and submits a transaction to the Aptos network.
|
|
110
|
+
*
|
|
111
|
+
* @param transaction - The transaction to sign and submit
|
|
112
|
+
* @returns The transaction hash
|
|
113
|
+
*/
|
|
114
|
+
signAndSubmitTransaction(transaction) {
|
|
115
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
116
|
+
yield this._connector.connect();
|
|
117
|
+
return this._connector.signAndSubmitTransaction(transaction);
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Submits a pre-signed transaction to the network.
|
|
122
|
+
*
|
|
123
|
+
* @param signedTransaction - The signed transaction to submit
|
|
124
|
+
* @returns The transaction hash
|
|
125
|
+
*/
|
|
126
|
+
submitTransaction(signedTransaction) {
|
|
127
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
128
|
+
yield this._connector.connect();
|
|
129
|
+
return this._connector.submitTransaction(signedTransaction);
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export { AptosWallet };
|
package/src/wallet/index.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { AptosWallet } from './AptosWallet';
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var _tslib = require('../../_virtual/_tslib.cjs');
|
|
7
|
+
var walletStandard = require('@aptos-labs/wallet-standard');
|
|
8
|
+
var logger$1 = require('@dynamic-labs/logger');
|
|
9
|
+
|
|
10
|
+
const logger = new logger$1.Logger('AptosWalletStandardConnector');
|
|
11
|
+
const createAptosSignerFromWalletStandard = ({ wallet, walletConnector, }) => {
|
|
12
|
+
const features = wallet.features;
|
|
13
|
+
const hasAutoConnectedAccounts = () => {
|
|
14
|
+
var _a, _b, _c;
|
|
15
|
+
return Boolean(((_a = wallet.accounts) === null || _a === void 0 ? void 0 : _a.length) > 0 &&
|
|
16
|
+
((_b = wallet.accounts[0]) === null || _b === void 0 ? void 0 : _b.publicKey) &&
|
|
17
|
+
((_c = wallet.accounts[0]) === null || _c === void 0 ? void 0 : _c.address));
|
|
18
|
+
};
|
|
19
|
+
const getCurrentAccount = () => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
20
|
+
var _a, _b;
|
|
21
|
+
const accountMethod = (_a = features['aptos:account']) === null || _a === void 0 ? void 0 : _a.account;
|
|
22
|
+
if (accountMethod) {
|
|
23
|
+
return accountMethod();
|
|
24
|
+
}
|
|
25
|
+
if (((_b = wallet.accounts) === null || _b === void 0 ? void 0 : _b.length) > 0) {
|
|
26
|
+
const [account] = wallet.accounts;
|
|
27
|
+
// Validate that the account has valid address and publicKey
|
|
28
|
+
if (account.address && account.publicKey) {
|
|
29
|
+
const addressStr = account.address.toString();
|
|
30
|
+
if (addressStr && addressStr !== '0x' && addressStr.length > 2) {
|
|
31
|
+
return new walletStandard.AccountInfo({
|
|
32
|
+
address: account.address,
|
|
33
|
+
// using this typing since account.publicKey is a ReadonlyUint8Array
|
|
34
|
+
publicKey: account.publicKey,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
throw new Error('Account not found');
|
|
40
|
+
});
|
|
41
|
+
const connect = () => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
42
|
+
var _c;
|
|
43
|
+
const autoConnectedAccounts = wallet.accounts || [];
|
|
44
|
+
if (hasAutoConnectedAccounts()) {
|
|
45
|
+
const [account] = autoConnectedAccounts;
|
|
46
|
+
return {
|
|
47
|
+
args: new walletStandard.AccountInfo({
|
|
48
|
+
address: account.address,
|
|
49
|
+
publicKey: account.publicKey,
|
|
50
|
+
}),
|
|
51
|
+
status: walletStandard.UserResponseStatus.APPROVED,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
const connectMethod = (_c = features['aptos:connect']) === null || _c === void 0 ? void 0 : _c.connect;
|
|
55
|
+
if (!connectMethod) {
|
|
56
|
+
throw new Error('Connect method not implemented by wallet');
|
|
57
|
+
}
|
|
58
|
+
const result = yield connectMethod();
|
|
59
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
60
|
+
const resultArgs = result.args;
|
|
61
|
+
if (!(resultArgs === null || resultArgs === void 0 ? void 0 : resultArgs.address)) {
|
|
62
|
+
throw new Error('No account connected');
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
args: new walletStandard.AccountInfo({
|
|
66
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
67
|
+
address: resultArgs.address,
|
|
68
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
69
|
+
publicKey: resultArgs.publicKey,
|
|
70
|
+
}),
|
|
71
|
+
status: walletStandard.UserResponseStatus.APPROVED,
|
|
72
|
+
};
|
|
73
|
+
});
|
|
74
|
+
const disconnect = () => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
75
|
+
var _d;
|
|
76
|
+
const disconnectMethod = (_d = features['aptos:disconnect']) === null || _d === void 0 ? void 0 : _d.disconnect;
|
|
77
|
+
if (!disconnectMethod) {
|
|
78
|
+
logger.debug('[AptosWalletStandardConnector] Disconnect method not implemented');
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
yield disconnectMethod();
|
|
82
|
+
});
|
|
83
|
+
const account = () => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
84
|
+
const currentAccount = yield getCurrentAccount();
|
|
85
|
+
const address = typeof currentAccount === 'string'
|
|
86
|
+
? currentAccount
|
|
87
|
+
: currentAccount.address;
|
|
88
|
+
return new walletStandard.AccountInfo({
|
|
89
|
+
address: address,
|
|
90
|
+
publicKey: currentAccount.publicKey,
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
const network = () => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
94
|
+
try {
|
|
95
|
+
// Try to get network info from the wallet connector
|
|
96
|
+
const networkInfo = yield walletConnector.getNetworkInfo();
|
|
97
|
+
if (networkInfo) {
|
|
98
|
+
return networkInfo;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
catch (err) {
|
|
102
|
+
logger.debug('[AptosWalletStandardConnector] Failed to get network info from connector:', err);
|
|
103
|
+
}
|
|
104
|
+
// Return a default mainnet if not available
|
|
105
|
+
return {
|
|
106
|
+
chainId: 1,
|
|
107
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
108
|
+
name: 'Mainnet',
|
|
109
|
+
url: 'https://fullnode.mainnet.aptoslabs.com/v1',
|
|
110
|
+
};
|
|
111
|
+
});
|
|
112
|
+
const signTransaction = (transaction, asFeePayer) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
113
|
+
const signTransactionMethod = features['aptos:signTransaction'];
|
|
114
|
+
if (!(signTransactionMethod === null || signTransactionMethod === void 0 ? void 0 : signTransactionMethod.signTransaction)) {
|
|
115
|
+
logger.error('[AptosWalletStandardConnector] Sign transaction not implemented by wallet');
|
|
116
|
+
throw new Error('Sign transaction not implemented by wallet');
|
|
117
|
+
}
|
|
118
|
+
const result = yield signTransactionMethod.signTransaction(transaction, asFeePayer);
|
|
119
|
+
return result;
|
|
120
|
+
});
|
|
121
|
+
const signMessage = (input) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
122
|
+
const signMessageMethod = features['aptos:signMessage'];
|
|
123
|
+
if (!(signMessageMethod === null || signMessageMethod === void 0 ? void 0 : signMessageMethod.signMessage)) {
|
|
124
|
+
logger.error('[AptosWalletStandardConnector] Sign message not implemented by wallet');
|
|
125
|
+
throw new Error('Sign message not implemented by wallet');
|
|
126
|
+
}
|
|
127
|
+
const result = yield signMessageMethod.signMessage(input);
|
|
128
|
+
return result;
|
|
129
|
+
});
|
|
130
|
+
const signAndSubmitTransaction = (transaction) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
131
|
+
const signAndSubmitMethod = features['aptos:signAndSubmitTransaction'];
|
|
132
|
+
if (!(signAndSubmitMethod === null || signAndSubmitMethod === void 0 ? void 0 : signAndSubmitMethod.signAndSubmitTransaction)) {
|
|
133
|
+
logger.error('[AptosWalletStandardConnector] Sign and submit transaction not implemented by wallet');
|
|
134
|
+
throw new Error('Sign and submit transaction not implemented by wallet');
|
|
135
|
+
}
|
|
136
|
+
const result = yield signAndSubmitMethod.signAndSubmitTransaction({
|
|
137
|
+
transaction,
|
|
138
|
+
});
|
|
139
|
+
return result;
|
|
140
|
+
});
|
|
141
|
+
const onAccountChange = (callback) => {
|
|
142
|
+
var _a;
|
|
143
|
+
const onMethod = (_a = features['standard:events']) === null || _a === void 0 ? void 0 : _a.on;
|
|
144
|
+
if (!onMethod) {
|
|
145
|
+
logger.debug('[AptosWalletStandardConnector] Events not implemented by wallet');
|
|
146
|
+
return () => { };
|
|
147
|
+
}
|
|
148
|
+
logger.debug('[AptosWalletStandardConnector] Setting up account change listener');
|
|
149
|
+
const wrappedCallback = (prop) => {
|
|
150
|
+
var _a;
|
|
151
|
+
const account = (_a = prop.accounts) === null || _a === void 0 ? void 0 : _a[0];
|
|
152
|
+
if (account) {
|
|
153
|
+
callback(new walletStandard.AccountInfo({
|
|
154
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
155
|
+
address: account.address,
|
|
156
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
157
|
+
publicKey: account.publicKey,
|
|
158
|
+
}));
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
callback(null);
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
// 'change' is the only event that is supported by the wallet standard
|
|
165
|
+
return onMethod('change', wrappedCallback);
|
|
166
|
+
};
|
|
167
|
+
const onNetworkChange = (callback) => {
|
|
168
|
+
var _a;
|
|
169
|
+
const onMethod = (_a = features['standard:events']) === null || _a === void 0 ? void 0 : _a.on;
|
|
170
|
+
if (!onMethod) {
|
|
171
|
+
logger.debug('[AptosWalletStandardConnector] Events not implemented by wallet');
|
|
172
|
+
return () => { };
|
|
173
|
+
}
|
|
174
|
+
logger.debug('[AptosWalletStandardConnector] Setting up network change listener');
|
|
175
|
+
const wrappedCallback = (prop) => {
|
|
176
|
+
// Network changes might be indicated through feature changes
|
|
177
|
+
// This is wallet-specific and may need adjustment
|
|
178
|
+
if (prop.features) {
|
|
179
|
+
// Trigger network check
|
|
180
|
+
network()
|
|
181
|
+
.then((networkInfo) => {
|
|
182
|
+
if (networkInfo) {
|
|
183
|
+
callback(networkInfo);
|
|
184
|
+
}
|
|
185
|
+
})
|
|
186
|
+
.catch((err) => {
|
|
187
|
+
logger.debug('[AptosWalletStandardConnector] Failed to get network info:', err);
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
// 'change' is the only event that is supported by the wallet standard
|
|
192
|
+
return onMethod('change', wrappedCallback);
|
|
193
|
+
};
|
|
194
|
+
return {
|
|
195
|
+
account,
|
|
196
|
+
connect,
|
|
197
|
+
disconnect,
|
|
198
|
+
features: {
|
|
199
|
+
'aptos:account': {
|
|
200
|
+
account,
|
|
201
|
+
version: '1.0.0',
|
|
202
|
+
},
|
|
203
|
+
'aptos:connect': {
|
|
204
|
+
connect,
|
|
205
|
+
version: '1.0.0',
|
|
206
|
+
},
|
|
207
|
+
'aptos:disconnect': {
|
|
208
|
+
disconnect,
|
|
209
|
+
version: '1.0.0',
|
|
210
|
+
},
|
|
211
|
+
'aptos:network': {
|
|
212
|
+
network,
|
|
213
|
+
version: '1.0.0',
|
|
214
|
+
},
|
|
215
|
+
'aptos:onAccountChange': {
|
|
216
|
+
onAccountChange,
|
|
217
|
+
version: '1.0.0',
|
|
218
|
+
},
|
|
219
|
+
'aptos:onNetworkChange': {
|
|
220
|
+
onNetworkChange,
|
|
221
|
+
version: '1.0.0',
|
|
222
|
+
},
|
|
223
|
+
'aptos:signAndSubmitTransaction': {
|
|
224
|
+
signAndSubmitTransaction,
|
|
225
|
+
version: '1.0.0',
|
|
226
|
+
},
|
|
227
|
+
'aptos:signMessage': {
|
|
228
|
+
signMessage,
|
|
229
|
+
version: '1.0.0',
|
|
230
|
+
},
|
|
231
|
+
'aptos:signTransaction': {
|
|
232
|
+
signTransaction,
|
|
233
|
+
version: '1.0.0',
|
|
234
|
+
},
|
|
235
|
+
},
|
|
236
|
+
onAccountChange,
|
|
237
|
+
onNetworkChange,
|
|
238
|
+
signAndSubmitTransaction,
|
|
239
|
+
signMessage,
|
|
240
|
+
signTransaction,
|
|
241
|
+
};
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
exports.createAptosSignerFromWalletStandard = createAptosSignerFromWalletStandard;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Wallet } from '@aptos-labs/wallet-standard';
|
|
2
|
+
import type { IAptosProvider } from '../types';
|
|
3
|
+
import { AptosWalletConnector } from '../connectors/AptosWalletConnector';
|
|
4
|
+
type CreateAptosSignerFromWalletStandardProps = {
|
|
5
|
+
wallet: Wallet;
|
|
6
|
+
walletConnector: AptosWalletConnector;
|
|
7
|
+
};
|
|
8
|
+
export declare const createAptosSignerFromWalletStandard: ({ wallet, walletConnector, }: CreateAptosSignerFromWalletStandardProps) => IAptosProvider;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { __awaiter } from '../../_virtual/_tslib.js';
|
|
3
|
+
import { AccountInfo, UserResponseStatus } from '@aptos-labs/wallet-standard';
|
|
4
|
+
import { Logger } from '@dynamic-labs/logger';
|
|
5
|
+
|
|
6
|
+
const logger = new Logger('AptosWalletStandardConnector');
|
|
7
|
+
const createAptosSignerFromWalletStandard = ({ wallet, walletConnector, }) => {
|
|
8
|
+
const features = wallet.features;
|
|
9
|
+
const hasAutoConnectedAccounts = () => {
|
|
10
|
+
var _a, _b, _c;
|
|
11
|
+
return Boolean(((_a = wallet.accounts) === null || _a === void 0 ? void 0 : _a.length) > 0 &&
|
|
12
|
+
((_b = wallet.accounts[0]) === null || _b === void 0 ? void 0 : _b.publicKey) &&
|
|
13
|
+
((_c = wallet.accounts[0]) === null || _c === void 0 ? void 0 : _c.address));
|
|
14
|
+
};
|
|
15
|
+
const getCurrentAccount = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
16
|
+
var _a, _b;
|
|
17
|
+
const accountMethod = (_a = features['aptos:account']) === null || _a === void 0 ? void 0 : _a.account;
|
|
18
|
+
if (accountMethod) {
|
|
19
|
+
return accountMethod();
|
|
20
|
+
}
|
|
21
|
+
if (((_b = wallet.accounts) === null || _b === void 0 ? void 0 : _b.length) > 0) {
|
|
22
|
+
const [account] = wallet.accounts;
|
|
23
|
+
// Validate that the account has valid address and publicKey
|
|
24
|
+
if (account.address && account.publicKey) {
|
|
25
|
+
const addressStr = account.address.toString();
|
|
26
|
+
if (addressStr && addressStr !== '0x' && addressStr.length > 2) {
|
|
27
|
+
return new AccountInfo({
|
|
28
|
+
address: account.address,
|
|
29
|
+
// using this typing since account.publicKey is a ReadonlyUint8Array
|
|
30
|
+
publicKey: account.publicKey,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
throw new Error('Account not found');
|
|
36
|
+
});
|
|
37
|
+
const connect = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
38
|
+
var _c;
|
|
39
|
+
const autoConnectedAccounts = wallet.accounts || [];
|
|
40
|
+
if (hasAutoConnectedAccounts()) {
|
|
41
|
+
const [account] = autoConnectedAccounts;
|
|
42
|
+
return {
|
|
43
|
+
args: new AccountInfo({
|
|
44
|
+
address: account.address,
|
|
45
|
+
publicKey: account.publicKey,
|
|
46
|
+
}),
|
|
47
|
+
status: UserResponseStatus.APPROVED,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
const connectMethod = (_c = features['aptos:connect']) === null || _c === void 0 ? void 0 : _c.connect;
|
|
51
|
+
if (!connectMethod) {
|
|
52
|
+
throw new Error('Connect method not implemented by wallet');
|
|
53
|
+
}
|
|
54
|
+
const result = yield connectMethod();
|
|
55
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
56
|
+
const resultArgs = result.args;
|
|
57
|
+
if (!(resultArgs === null || resultArgs === void 0 ? void 0 : resultArgs.address)) {
|
|
58
|
+
throw new Error('No account connected');
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
args: new AccountInfo({
|
|
62
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
63
|
+
address: resultArgs.address,
|
|
64
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
65
|
+
publicKey: resultArgs.publicKey,
|
|
66
|
+
}),
|
|
67
|
+
status: UserResponseStatus.APPROVED,
|
|
68
|
+
};
|
|
69
|
+
});
|
|
70
|
+
const disconnect = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
71
|
+
var _d;
|
|
72
|
+
const disconnectMethod = (_d = features['aptos:disconnect']) === null || _d === void 0 ? void 0 : _d.disconnect;
|
|
73
|
+
if (!disconnectMethod) {
|
|
74
|
+
logger.debug('[AptosWalletStandardConnector] Disconnect method not implemented');
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
yield disconnectMethod();
|
|
78
|
+
});
|
|
79
|
+
const account = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
80
|
+
const currentAccount = yield getCurrentAccount();
|
|
81
|
+
const address = typeof currentAccount === 'string'
|
|
82
|
+
? currentAccount
|
|
83
|
+
: currentAccount.address;
|
|
84
|
+
return new AccountInfo({
|
|
85
|
+
address: address,
|
|
86
|
+
publicKey: currentAccount.publicKey,
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
const network = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
90
|
+
try {
|
|
91
|
+
// Try to get network info from the wallet connector
|
|
92
|
+
const networkInfo = yield walletConnector.getNetworkInfo();
|
|
93
|
+
if (networkInfo) {
|
|
94
|
+
return networkInfo;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
catch (err) {
|
|
98
|
+
logger.debug('[AptosWalletStandardConnector] Failed to get network info from connector:', err);
|
|
99
|
+
}
|
|
100
|
+
// Return a default mainnet if not available
|
|
101
|
+
return {
|
|
102
|
+
chainId: 1,
|
|
103
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
104
|
+
name: 'Mainnet',
|
|
105
|
+
url: 'https://fullnode.mainnet.aptoslabs.com/v1',
|
|
106
|
+
};
|
|
107
|
+
});
|
|
108
|
+
const signTransaction = (transaction, asFeePayer) => __awaiter(void 0, void 0, void 0, function* () {
|
|
109
|
+
const signTransactionMethod = features['aptos:signTransaction'];
|
|
110
|
+
if (!(signTransactionMethod === null || signTransactionMethod === void 0 ? void 0 : signTransactionMethod.signTransaction)) {
|
|
111
|
+
logger.error('[AptosWalletStandardConnector] Sign transaction not implemented by wallet');
|
|
112
|
+
throw new Error('Sign transaction not implemented by wallet');
|
|
113
|
+
}
|
|
114
|
+
const result = yield signTransactionMethod.signTransaction(transaction, asFeePayer);
|
|
115
|
+
return result;
|
|
116
|
+
});
|
|
117
|
+
const signMessage = (input) => __awaiter(void 0, void 0, void 0, function* () {
|
|
118
|
+
const signMessageMethod = features['aptos:signMessage'];
|
|
119
|
+
if (!(signMessageMethod === null || signMessageMethod === void 0 ? void 0 : signMessageMethod.signMessage)) {
|
|
120
|
+
logger.error('[AptosWalletStandardConnector] Sign message not implemented by wallet');
|
|
121
|
+
throw new Error('Sign message not implemented by wallet');
|
|
122
|
+
}
|
|
123
|
+
const result = yield signMessageMethod.signMessage(input);
|
|
124
|
+
return result;
|
|
125
|
+
});
|
|
126
|
+
const signAndSubmitTransaction = (transaction) => __awaiter(void 0, void 0, void 0, function* () {
|
|
127
|
+
const signAndSubmitMethod = features['aptos:signAndSubmitTransaction'];
|
|
128
|
+
if (!(signAndSubmitMethod === null || signAndSubmitMethod === void 0 ? void 0 : signAndSubmitMethod.signAndSubmitTransaction)) {
|
|
129
|
+
logger.error('[AptosWalletStandardConnector] Sign and submit transaction not implemented by wallet');
|
|
130
|
+
throw new Error('Sign and submit transaction not implemented by wallet');
|
|
131
|
+
}
|
|
132
|
+
const result = yield signAndSubmitMethod.signAndSubmitTransaction({
|
|
133
|
+
transaction,
|
|
134
|
+
});
|
|
135
|
+
return result;
|
|
136
|
+
});
|
|
137
|
+
const onAccountChange = (callback) => {
|
|
138
|
+
var _a;
|
|
139
|
+
const onMethod = (_a = features['standard:events']) === null || _a === void 0 ? void 0 : _a.on;
|
|
140
|
+
if (!onMethod) {
|
|
141
|
+
logger.debug('[AptosWalletStandardConnector] Events not implemented by wallet');
|
|
142
|
+
return () => { };
|
|
143
|
+
}
|
|
144
|
+
logger.debug('[AptosWalletStandardConnector] Setting up account change listener');
|
|
145
|
+
const wrappedCallback = (prop) => {
|
|
146
|
+
var _a;
|
|
147
|
+
const account = (_a = prop.accounts) === null || _a === void 0 ? void 0 : _a[0];
|
|
148
|
+
if (account) {
|
|
149
|
+
callback(new AccountInfo({
|
|
150
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
151
|
+
address: account.address,
|
|
152
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
153
|
+
publicKey: account.publicKey,
|
|
154
|
+
}));
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
callback(null);
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
// 'change' is the only event that is supported by the wallet standard
|
|
161
|
+
return onMethod('change', wrappedCallback);
|
|
162
|
+
};
|
|
163
|
+
const onNetworkChange = (callback) => {
|
|
164
|
+
var _a;
|
|
165
|
+
const onMethod = (_a = features['standard:events']) === null || _a === void 0 ? void 0 : _a.on;
|
|
166
|
+
if (!onMethod) {
|
|
167
|
+
logger.debug('[AptosWalletStandardConnector] Events not implemented by wallet');
|
|
168
|
+
return () => { };
|
|
169
|
+
}
|
|
170
|
+
logger.debug('[AptosWalletStandardConnector] Setting up network change listener');
|
|
171
|
+
const wrappedCallback = (prop) => {
|
|
172
|
+
// Network changes might be indicated through feature changes
|
|
173
|
+
// This is wallet-specific and may need adjustment
|
|
174
|
+
if (prop.features) {
|
|
175
|
+
// Trigger network check
|
|
176
|
+
network()
|
|
177
|
+
.then((networkInfo) => {
|
|
178
|
+
if (networkInfo) {
|
|
179
|
+
callback(networkInfo);
|
|
180
|
+
}
|
|
181
|
+
})
|
|
182
|
+
.catch((err) => {
|
|
183
|
+
logger.debug('[AptosWalletStandardConnector] Failed to get network info:', err);
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
// 'change' is the only event that is supported by the wallet standard
|
|
188
|
+
return onMethod('change', wrappedCallback);
|
|
189
|
+
};
|
|
190
|
+
return {
|
|
191
|
+
account,
|
|
192
|
+
connect,
|
|
193
|
+
disconnect,
|
|
194
|
+
features: {
|
|
195
|
+
'aptos:account': {
|
|
196
|
+
account,
|
|
197
|
+
version: '1.0.0',
|
|
198
|
+
},
|
|
199
|
+
'aptos:connect': {
|
|
200
|
+
connect,
|
|
201
|
+
version: '1.0.0',
|
|
202
|
+
},
|
|
203
|
+
'aptos:disconnect': {
|
|
204
|
+
disconnect,
|
|
205
|
+
version: '1.0.0',
|
|
206
|
+
},
|
|
207
|
+
'aptos:network': {
|
|
208
|
+
network,
|
|
209
|
+
version: '1.0.0',
|
|
210
|
+
},
|
|
211
|
+
'aptos:onAccountChange': {
|
|
212
|
+
onAccountChange,
|
|
213
|
+
version: '1.0.0',
|
|
214
|
+
},
|
|
215
|
+
'aptos:onNetworkChange': {
|
|
216
|
+
onNetworkChange,
|
|
217
|
+
version: '1.0.0',
|
|
218
|
+
},
|
|
219
|
+
'aptos:signAndSubmitTransaction': {
|
|
220
|
+
signAndSubmitTransaction,
|
|
221
|
+
version: '1.0.0',
|
|
222
|
+
},
|
|
223
|
+
'aptos:signMessage': {
|
|
224
|
+
signMessage,
|
|
225
|
+
version: '1.0.0',
|
|
226
|
+
},
|
|
227
|
+
'aptos:signTransaction': {
|
|
228
|
+
signTransaction,
|
|
229
|
+
version: '1.0.0',
|
|
230
|
+
},
|
|
231
|
+
},
|
|
232
|
+
onAccountChange,
|
|
233
|
+
onNetworkChange,
|
|
234
|
+
signAndSubmitTransaction,
|
|
235
|
+
signMessage,
|
|
236
|
+
signTransaction,
|
|
237
|
+
};
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
export { createAptosSignerFromWalletStandard };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var utils = require('@dynamic-labs/utils');
|
|
7
|
+
var InjectedWalletBase = require('../injected/InjectedWalletBase.cjs');
|
|
8
|
+
var createAptosSignerFromWalletStandard = require('./createAptosSignerFromWalletStandard.cjs');
|
|
9
|
+
|
|
10
|
+
const getConnectorConstructorForWalletStandardWallet = (wallet, walletBookMetadata = {}) => {
|
|
11
|
+
const sanitizedName = utils.sanitizeName(wallet.name);
|
|
12
|
+
return class extends InjectedWalletBase.InjectedWalletBase {
|
|
13
|
+
constructor(props) {
|
|
14
|
+
super(wallet.name, Object.assign(Object.assign({}, props), { metadata: Object.assign(Object.assign({}, walletBookMetadata), { groupKey: sanitizedName, icon: wallet.icon, id: sanitizedName, name: wallet.name }) }));
|
|
15
|
+
this.name = wallet.name;
|
|
16
|
+
this.overrideKey = `${sanitizedName}aptos`;
|
|
17
|
+
this._provider = createAptosSignerFromWalletStandard.createAptosSignerFromWalletStandard({
|
|
18
|
+
wallet,
|
|
19
|
+
walletConnector: this,
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
findProvider() {
|
|
23
|
+
return this._provider;
|
|
24
|
+
}
|
|
25
|
+
getProvider() {
|
|
26
|
+
return this._provider;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
exports.getConnectorConstructorForWalletStandardWallet = getConnectorConstructorForWalletStandardWallet;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { Wallet } from '@aptos-labs/wallet-standard';
|
|
2
|
+
import { WalletConnectorConstructor, WalletMetadata } from '@dynamic-labs/wallet-connector-core';
|
|
3
|
+
export declare const getConnectorConstructorForWalletStandardWallet: (wallet: Wallet, walletBookMetadata?: Partial<WalletMetadata>) => WalletConnectorConstructor;
|