@layerzerolabs/lz-corekit-aptos 3.0.15 → 3.0.16

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 CHANGED
@@ -1,5 +1,14 @@
1
1
  # @layerzerolabs/lz-corekit-aptos
2
2
 
3
+ ## 3.0.16
4
+
5
+ ### Patch Changes
6
+
7
+ - 87a4bc9: islander mainnet
8
+ - Updated dependencies [87a4bc9]
9
+ - @layerzerolabs/lz-core@3.0.16
10
+ - @layerzerolabs/lz-utilities@3.0.16
11
+
3
12
  ## 3.0.15
4
13
 
5
14
  ### Patch Changes
package/README.md CHANGED
@@ -1 +1,110 @@
1
- # @layerzerolabs/lz-corekit
1
+ # @layerzerolabs/lz-corekit-aptos
2
+
3
+ The Aptos CoreKit is a comprehensive SDK designed to interact with the Aptos blockchain. It provides a set of utilities and modules to facilitate the development and integration of applications with the Aptos blockchain.
4
+
5
+ ## Features
6
+
7
+ - **Retrieve Account Information**: Gets the balance of the specified address.
8
+ - **Retrieve Block Information**: Gets the block height, timestamp and related transactions of the specified block.
9
+ - **Transaction Management**: Get, build, sign, send and confirm transactions.
10
+
11
+ ## Installation
12
+
13
+ To install the Aptos CoreKit, you can use npm or yarn:
14
+
15
+ ```sh
16
+ npm install @layerzerolabs/lz-corekit-aptos
17
+ ```
18
+
19
+ or
20
+
21
+ ```sh
22
+ yarn add @layerzerolabs/lz-corekit-aptos
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ### Initialization
28
+
29
+ ```typescript
30
+ import { AptosProvider } from "@layerzerolabs/lz-corekit-aptos";
31
+
32
+ // url is the Aptos chain full node url
33
+ const url = "http://127.0.0.1:8080/v1";
34
+ const provider = AptosProvider.from(url);
35
+ ```
36
+
37
+ If you need to fund your accounts and scan module events by Aptos Indexer, you may need to specify the url of faucet and indexer.
38
+
39
+ ```typescript
40
+ import { AptosProvider } from "@layerzerolabs/lz-corekit-aptos";
41
+
42
+ // url is the Aptos chain full node url
43
+ const url = "http://127.0.0.1:8080/v1";
44
+ const faucetUrl = "http://127.0.0.1:8081";
45
+ const indexerUrl = "http://127.0.0.1:8090/v1/graphql";
46
+ const provider = AptosProvider.from(url, faucetUrl, indexerUrl);
47
+ ```
48
+
49
+ ### Retrieve Account Information
50
+
51
+ #### Get Account Balance
52
+
53
+ ```typescript
54
+ import { AptosProvider } from "@layerzerolabs/lz-corekit-aptos";
55
+
56
+ // url is the Aptos chain full node url
57
+ const url = "http://127.0.0.1:8080/v1";
58
+ const provider = AptosProvider.from(url);
59
+
60
+ const address = "0x1";
61
+ const balance = await provider.getBalance(address);
62
+ ```
63
+
64
+ ### Retrieve Block Information
65
+
66
+ #### Get Latest Block Height
67
+
68
+ ```typescript
69
+ import { AptosProvider } from "@layerzerolabs/lz-corekit-aptos";
70
+
71
+ // url is the Aptos chain full node url
72
+ const url = "http://127.0.0.1:8080/v1";
73
+ const provider = AptosProvider.from(url);
74
+
75
+ const number = await provider.getBlockNumber();
76
+ ```
77
+
78
+ ### Transaction Management
79
+
80
+ #### Get Transaction by hash
81
+
82
+ ```typescript
83
+ import { AptosProvider } from "@layerzerolabs/lz-corekit-aptos";
84
+
85
+ // url is the Aptos chain full node url
86
+ const url = "http://127.0.0.1:8080/v1";
87
+ const provider = AptosProvider.from(url);
88
+
89
+ const hash = "0x1";
90
+ const tx = await provider.getTransaction(hash);
91
+ ```
92
+
93
+ #### Sign, Send and Confirm Transaction
94
+
95
+ ```typescript
96
+ import { AptosProvider, AptosSigner } from '@layerzerolabs/lz-corekit-aptos'
97
+ import { SignedTransaction, TransactionReceipt, TransactionRequest } from '@layerzerolabs/lz-core'
98
+
99
+ // url is the Aptos chain full node url
100
+ const url = 'http://127.0.0.1:8080/v1'
101
+ const provider = AptosProvider.from(url)
102
+
103
+ const privateKey = '0x1234'
104
+ const signer = AptosSigner.from(privateKey)
105
+ signer.connect(provider)
106
+
107
+ const tx: TransactionRequest = ...
108
+ const stx: SignedTransaction = await signer.signTransaction(tx)
109
+ const receipt: TransactionReceipt = await signer.sendAndConfirm(stx)
110
+ ```
package/dist/index.cjs CHANGED
@@ -26,12 +26,28 @@ var aptos2__namespace = /*#__PURE__*/_interopNamespace(aptos2);
26
26
 
27
27
  // src/providers/aptos.ts
28
28
  var AptosProvider = class _AptosProvider {
29
+ /**
30
+ * Creates an instance of AptosProvider.
31
+ *
32
+ * @param {string} url - The URL of the Aptos node.
33
+ * @param {string} [faucet] - The URL of the faucet (optional).
34
+ * @param {string} [indexer] - The URL of the indexer (optional).
35
+ */
29
36
  constructor(url, faucet, indexer) {
30
37
  this.url = url;
31
38
  this.faucet = faucet;
32
39
  this.indexer = indexer;
33
40
  this.nativeProvider = new aptos2__namespace.AptosClient(url);
34
41
  }
42
+ /**
43
+ * Creates an instance of AptosProvider from the given parameters.
44
+ *
45
+ * @param {string} source - The URL of the Aptos node.
46
+ * @param {string} [faucet] - The URL of the faucet (optional).
47
+ * @param {string} [indexer] - The URL of the indexer (optional).
48
+ * @returns {AptosProvider} The created AptosProvider instance.
49
+ * @throws {Error} If the source parameter is not a string.
50
+ */
35
51
  static from(source, faucet, indexer) {
36
52
  if (typeof source === "string") {
37
53
  return new _AptosProvider(source, faucet, indexer);
@@ -39,11 +55,23 @@ var AptosProvider = class _AptosProvider {
39
55
  throw new Error("Invalid parameters");
40
56
  }
41
57
  }
58
+ /**
59
+ * Gets the native Aptos client instance.
60
+ *
61
+ * @returns {aptos.AptosClient} The native Aptos client instance.
62
+ */
42
63
  get native() {
43
64
  return this.nativeProvider;
44
65
  }
66
+ /**
67
+ * Gets the balance of the specified address.
68
+ *
69
+ * @param {string} address - The address to get the balance of.
70
+ * @returns {Promise<string>} A promise that resolves to the balance of the address.
71
+ * @throws {Error} If the address is not a valid Aptos address.
72
+ */
45
73
  async getBalance(address) {
46
- if (!lzUtilities.isHexString(address)) {
74
+ if (!lzUtilities.isHex(address)) {
47
75
  throw new Error("Invalid Aptos address");
48
76
  }
49
77
  const aptosCoin = "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>";
@@ -55,6 +83,13 @@ var AptosProvider = class _AptosProvider {
55
83
  const { coin } = accountResource.data;
56
84
  return BigInt(coin.value).toString();
57
85
  }
86
+ /**
87
+ * Gets the block specified by blockTag.
88
+ *
89
+ * @param {BlockTag} blockTag - The block tag to specify the block.
90
+ * @returns {Promise<Block>} A promise that resolves to the block.
91
+ * @throws {Error} If the blockTag is invalid.
92
+ */
58
93
  async getBlock(blockTag) {
59
94
  let blockNumber = 0;
60
95
  if (typeof blockTag === "number") {
@@ -67,6 +102,13 @@ var AptosProvider = class _AptosProvider {
67
102
  const response = await this.nativeProvider.getBlockByHeight(blockNumber);
68
103
  return lzCore.Block.from(response);
69
104
  }
105
+ /**
106
+ * Gets the block with transactions specified by blockTag.
107
+ *
108
+ * @param {BlockTag} blockTag - The block tag to specify the block.
109
+ * @returns {Promise<BlockWithTransactions>} A promise that resolves to the block with transactions.
110
+ * @throws {Error} If the blockTag is invalid.
111
+ */
70
112
  async getBlockWithTransactions(blockTag) {
71
113
  let blockNumber = 0;
72
114
  if (typeof blockTag === "number") {
@@ -79,13 +121,32 @@ var AptosProvider = class _AptosProvider {
79
121
  const response = await this.nativeProvider.getBlockByHeight(blockNumber, true);
80
122
  return lzCore.BlockWithTransactions.from(response);
81
123
  }
124
+ /**
125
+ * Gets the current block number.
126
+ *
127
+ * @returns {Promise<number>} A promise that resolves to the current block number.
128
+ */
82
129
  async getBlockNumber() {
83
130
  return this.nativeProvider.getLedgerInfo().then((ledgerInfo) => Number(ledgerInfo.block_height));
84
131
  }
132
+ /**
133
+ * Gets the current slot number for commitment, not suitable for Aptos.
134
+ *
135
+ * @param {Finality} [commitment] - The commitment level (optional).
136
+ * @returns {Promise<number>} A promise that resolves to the current slot number.
137
+ * @throws {Error} Method not implemented.
138
+ */
85
139
  async getSlot(_finality) {
86
140
  await Promise.resolve();
87
141
  throw new Error("Method not implemented.");
88
142
  }
143
+ /**
144
+ * Gets the timestamp (A string containing a 64-bit unsigned integer) for the block identified by blockTag.
145
+ *
146
+ * @param {BlockTag} blockTag - The block tag to specify the block.
147
+ * @returns {Promise<number>} A promise that resolves to the timestamp of the block.
148
+ * @throws {Error} If the blockTag is invalid.
149
+ */
89
150
  async getBlockTimestamp(blockTag) {
90
151
  if (typeof blockTag === "number") {
91
152
  return this.nativeProvider.getBlockByHeight(blockTag).then((block) => Number(block.block_timestamp));
@@ -95,33 +156,70 @@ var AptosProvider = class _AptosProvider {
95
156
  throw new Error("Invalid blockTag");
96
157
  }
97
158
  }
159
+ /**
160
+ * Gets information about a transaction.
161
+ *
162
+ * @param {string} txHash - The hash of the transaction.
163
+ * @returns {Promise<TransactionResponse>} A promise that resolves to the transaction response.
164
+ * @throws {Error} If the transaction hash is invalid.
165
+ */
98
166
  async getTransaction(txHash) {
99
- if (!lzUtilities.isHexString(txHash)) {
167
+ if (!lzUtilities.isHex(txHash)) {
100
168
  throw new Error("Invalid Aptos transaction hash");
101
169
  }
102
170
  const response = await this.nativeProvider.getTransactionByHash(txHash);
103
171
  return lzCore.TransactionResponse.from(response);
104
172
  }
173
+ /**
174
+ * Gets the receipt of a transaction.
175
+ *
176
+ * @param {string} txHash - The hash of the transaction.
177
+ * @returns {Promise<TransactionReceipt>} A promise that resolves to the transaction receipt.
178
+ * @throws {Error} If the transaction hash is invalid.
179
+ */
105
180
  async getTransactionReceipt(txHash) {
106
- if (!lzUtilities.isHexString(txHash)) {
181
+ if (!lzUtilities.isHex(txHash)) {
107
182
  throw new Error("Invalid Aptos transaction hash");
108
183
  }
109
184
  const response = await this.nativeProvider.getTransactionByHash(txHash);
110
185
  return lzCore.TransactionReceipt.from(response);
111
186
  }
187
+ /**
188
+ * Gets the number of transactions sent from the specified address.
189
+ *
190
+ * @param {string | Promise<string>} addressOrName - The address to get the number of transactions from.
191
+ * @param {BlockTag | Promise<BlockTag>} [blockTag] - The block tag to get the number of transactions from (optional).
192
+ * @returns {Promise<number>} A promise that resolves to the number of transactions sent from the address.
193
+ * @throws {Error} If the address is invalid.
194
+ */
112
195
  async getTransactionCount(addressOrName, _blockTag) {
113
196
  const _addressOrName = await Promise.resolve(addressOrName);
114
- if (!lzUtilities.isHexString(_addressOrName)) {
197
+ if (!lzUtilities.isHex(_addressOrName)) {
115
198
  throw new Error("Invalid Aptos transaction hash");
116
199
  }
117
200
  const response = await this.nativeProvider.getAccount(_addressOrName);
118
201
  return parseInt(response.sequence_number);
119
202
  }
203
+ /**
204
+ * Sends a signed transaction to the blockchain.
205
+ *
206
+ * @param {SignedTransaction} transaction - The signed transaction to send.
207
+ * @param {object} [sendOptions] - Optional parameters for sending the transaction.
208
+ * @returns {Promise<TransactionPending>} A promise that resolves to the pending transaction.
209
+ */
120
210
  async sendTransaction(transaction, _sendOptions) {
121
211
  const stx = transaction.signed;
122
212
  const response = await this.nativeProvider.submitSignedBCSTransaction(stx);
123
213
  return lzCore.TransactionPending.from(response);
124
214
  }
215
+ /**
216
+ * Confirms a pending transaction.
217
+ *
218
+ * @param {TransactionPending} pending - The pending transaction to confirm.
219
+ * @param {object} [opts] - Optional parameters for the confirmation.
220
+ * @returns {Promise<TransactionReceipt>} A promise that resolves to the transaction receipt.
221
+ * @throws {Error} If the transaction fails.
222
+ */
125
223
  async confirmTransaction(pending, opts) {
126
224
  const nativePending = pending.pending;
127
225
  const response = await this.nativeProvider.waitForTransactionWithResult(nativePending.hash);
@@ -134,19 +232,34 @@ var AptosProvider = class _AptosProvider {
134
232
  }
135
233
  return lzCore.TransactionReceipt.from(response);
136
234
  }
235
+ /**
236
+ * Sends a signed transaction and waits for confirmation.
237
+ *
238
+ * @param {SignedTransaction} transaction - The signed transaction to send.
239
+ * @param {object} [opts] - Optional parameters for sending and confirming the transaction.
240
+ * @returns {Promise<TransactionReceipt>} A promise that resolves to the transaction receipt.
241
+ */
137
242
  async sendAndConfirm(transaction, opts) {
138
243
  const pending = await this.sendTransaction(transaction, opts);
139
244
  return this.confirmTransaction(pending, opts);
140
245
  }
141
246
  };
142
247
  var AptosSigner = class {
248
+ /**
249
+ * Creates an instance of AptosSigner.
250
+ *
251
+ * @param {aptos.AptosAccount} signer - The Aptos account to use as the signer.
252
+ */
143
253
  constructor(signer) {
144
254
  this.nativeSigner = signer;
145
255
  }
146
256
  /**
257
+ * Creates an instance of AptosSigner from the given parameters.
147
258
  *
148
- * @param source
149
- * @param path e.g. m/44'/637'/0'/0'/0'
259
+ * @param {string | aptos.AptosAccount} source - The source to create the signer from, could be a private key, a mnemonics or an Aptos account.
260
+ * @param {string} [path] - The derivation path (optional). e.g. m/44'/637'/0'/0'/0' If provided, the source is treated as a mnemonics.
261
+ * @returns {AptosSigner} The created AptosSigner instance.
262
+ * @throws {Error} If the parameters are invalid.
150
263
  */
151
264
  static from(source, path) {
152
265
  if (source instanceof aptos2__namespace.AptosAccount) {
@@ -161,9 +274,21 @@ var AptosSigner = class {
161
274
  }
162
275
  throw new Error("Invalid parameters");
163
276
  }
277
+ /**
278
+ * Gets the native Aptos signer instance.
279
+ *
280
+ * @returns {aptos.AptosAccount} The native Aptos signer instance.
281
+ */
164
282
  get native() {
165
283
  return this.nativeSigner;
166
284
  }
285
+ /**
286
+ * Connects the signer to a provider.
287
+ *
288
+ * @param {Provider} provider - The provider to connect to.
289
+ * @returns {this} The connected signer.
290
+ * @throws {Error} If the provider is not an instance of AptosProvider.
291
+ */
167
292
  connect(provider) {
168
293
  if (!(provider instanceof AptosProvider)) {
169
294
  throw new Error("Only aptos.AptosClient is supported.");
@@ -171,24 +296,57 @@ var AptosSigner = class {
171
296
  this.provider = provider;
172
297
  return this;
173
298
  }
299
+ /**
300
+ * Gets the address of the signer.
301
+ *
302
+ * @returns {Promise<string>} A promise that resolves to the address of the signer.
303
+ */
174
304
  async getAddress() {
175
305
  return Promise.resolve(this.nativeSigner.address().toString());
176
306
  }
307
+ /**
308
+ * Gets the address of the signer.
309
+ *
310
+ * @returns {string} The address of the signer.
311
+ */
177
312
  get address() {
178
313
  return this.nativeSigner.address().toString();
179
314
  }
315
+ /**
316
+ * Sends a signed transaction and waits for confirmation.
317
+ *
318
+ * @param {SignedTransaction} transaction - The signed transaction to send.
319
+ * @param {object} [opts] - Optional parameters for sending and confirming the transaction.
320
+ * @returns {Promise<TransactionReceipt>} A promise that resolves to the transaction receipt.
321
+ * @throws {Error} If the provider is not connected.
322
+ */
180
323
  async sendAndConfirm(transaction, opts) {
181
324
  if (this.provider === void 0) {
182
325
  throw new Error("provider is required");
183
326
  }
184
327
  return this.provider.sendAndConfirm(transaction, opts);
185
328
  }
329
+ /**
330
+ * Sends a signed transaction.
331
+ *
332
+ * @param {SignedTransaction} transaction - The signed transaction to send.
333
+ * @param {object} [sendOptions] - Optional parameters for sending the transaction.
334
+ * @returns {Promise<TransactionPending>} A promise that resolves to the pending transaction.
335
+ * @throws {Error} If the provider is not connected.
336
+ */
186
337
  async sendTransaction(transaction, sendOptions) {
187
338
  if (this.provider === void 0) {
188
339
  throw new Error("provider is required");
189
340
  }
190
341
  return this.provider.sendTransaction(transaction, sendOptions);
191
342
  }
343
+ /**
344
+ * Signs a transaction.
345
+ *
346
+ * @param {TransactionRequest} transaction - The transaction request to sign.
347
+ * @returns {Promise<SignedTransaction>} A promise that resolves to the signed transaction.
348
+ * @throws {Error} If the native provider is not connected.
349
+ */
192
350
  async signTransaction(transaction) {
193
351
  const nativeProvider = this.provider?.native;
194
352
  if (nativeProvider === void 0) {
@@ -198,10 +356,23 @@ var AptosSigner = class {
198
356
  const response = await nativeProvider.signTransaction(this.nativeSigner, tx);
199
357
  return lzCore.SignedTransaction.from(response);
200
358
  }
359
+ /**
360
+ * Signs a buffer (e.g., a message hash) using the native Aptos signer.
361
+ *
362
+ * @param {Uint8Array} buffer - The buffer to sign.
363
+ * @returns {Promise<Uint8Array>} A promise that resolves to the signed buffer as a Uint8Array.
364
+ */
201
365
  async signBuffer(buffer) {
202
366
  const rv = this.nativeSigner.signBuffer(Buffer.from(buffer)).toUint8Array();
203
367
  return Promise.resolve(rv);
204
368
  }
369
+ /**
370
+ * Builds a transaction.
371
+ *
372
+ * @param {unknown} buildTxRequest - The transaction request to build.
373
+ * @returns {Promise<TransactionRequest>} A promise that resolves to the built transaction request.
374
+ * @throws {Error} Method not implemented.
375
+ */
205
376
  async buildTransaction(buildTxRequest) {
206
377
  return Promise.reject(new Error("Method not implemented."));
207
378
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/providers/aptos.ts","../src/signers/aptos.ts"],"names":["aptos","SignedTransaction"],"mappings":";AAAA,YAAY,WAAW;AAEvB;AAAA,EACI;AAAA,EAEA;AAAA,EAIA;AAAA,EACA;AAAA,EACA;AAAA,OACG;AACP,SAAS,mBAAmB;AAErB,IAAM,gBAAN,MAAM,eAAkC;AAAA,EAGnC,YACY,KACA,QACA,SAClB;AAHkB;AACA;AACA;AAEhB,SAAK,iBAAiB,IAAU,kBAAY,GAAG;AAAA,EACnD;AAAA,EAEA,OAAO,KAAK,QAAgB,QAAiB,SAAiC;AAC1E,QAAI,OAAO,WAAW,UAAU;AAC5B,aAAO,IAAI,eAAc,QAAQ,QAAQ,OAAO;AAAA,IACpD,OAAO;AACH,YAAM,IAAI,MAAM,oBAAoB;AAAA,IACxC;AAAA,EACJ;AAAA,EAEA,IAAI,SAA4B;AAC5B,WAAO,KAAK;AAAA,EAChB;AAAA,EAEA,MAAM,WAAW,SAAkC;AAC/C,QAAI,CAAC,YAAY,OAAO,GAAG;AACvB,YAAM,IAAI,MAAM,uBAAuB;AAAA,IAC3C;AAEA,UAAM,YAAY;AAClB,UAAM,YAAY,MAAM,KAAK,eAAe,oBAAoB,OAAO;AACvE,UAAM,kBAAkB,UAAU,KAAK,CAAC,MAAM,EAAE,SAAS,SAAS;AAClE,QAAI,oBAAoB,QAAW;AAC/B,YAAM,IAAI,MAAM,4BAA4B;AAAA,IAChD;AAEA,UAAM,EAAE,KAAK,IAAI,gBAAgB;AACjC,WAAO,OAAO,KAAK,KAAK,EAAE,SAAS;AAAA,EACvC;AAAA,EAEA,MAAM,S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sB;AAAA,IAC1C;AAEA,WAAO,KAAK,SAAS,eAAe,aAAa,IAAI;AAAA,EACzD;AAAA,EAEA,MAAM,gBAAgB,aAAgC,aAAmD;AACrG,QAAI,KAAK,aAAa,QAAW;AAC7B,YAAM,IAAI,MAAM,sBAAsB;AAAA,IAC1C;AAEA,WAAO,KAAK,SAAS,gBAAgB,aAAa,WAAW;AAAA,EACjE;AAAA,EAEA,MAAM,gBAAgB,aAA6D;AAC/E,UAAM,iBAAiB,KAAK,UAAU;AACtC,QAAI,mBAAmB,QAAW;AAC9B,YAAM,IAAI,MAAM,8DAA8D;AAAA,IAClF;AAGA,UAAM,KAAK,YAAY;AAGvB,UAAM,WAAW,MAAM,eAAe,gBAAgB,KAAK,cAAc,EAAE;AAC3E,WAAOA,mBAAkB,KAAK,QAAQ;AAAA,EAC1C;AAAA,EAEA,MAAM,WAAW,QAAyC;AACtD,UAAM,KAAK,KAAK,aAAa,WAAW,OAAO,KAAK,MAAM,CAAC,EAAE,aAAa;AAC1E,WAAO,QAAQ,QAAQ,EAAE;AAAA,EAC7B;AAAA,EAEA,MAAM,iBAAiB,gBAAsD;AACzE,WAAO,QAAQ,OAAO,IAAI,MAAM,yBAAyB,CAAC;AAAA,EAC9D;AACJ","sourcesContent":["import * as aptos from 'aptos'\n\nimport {\n Block,\n BlockTag,\n BlockWithTransactions,\n Finality,\n Provider,\n SignedTransaction,\n TransactionPending,\n TransactionReceipt,\n TransactionResponse,\n} from '@layerzerolabs/lz-core'\nimport { isHexString } from '@layerzerolabs/lz-utilities'\n\nexport class AptosProvider implements Provider {\n readonly nativeProvider: aptos.AptosClient\n\n private constructor(\n public readonly url: string,\n public readonly faucet?: string,\n public readonly indexer?: string\n ) {\n this.nativeProvider = new aptos.AptosClient(url)\n }\n\n static from(source: string, faucet?: string, indexer?: string): AptosProvider {\n if (typeof source === 'string') {\n return new AptosProvider(source, faucet, indexer)\n } else {\n throw new Error('Invalid parameters')\n }\n }\n\n get native(): aptos.AptosClient {\n return this.nativeProvider\n }\n\n async getBalance(address: string): Promise<string> {\n if (!isHexString(address)) {\n throw new Error('Invalid Aptos address')\n }\n\n const aptosCoin = '0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>'\n const resources = await this.nativeProvider.getAccountResources(address)\n const accountResource = resources.find((r) => r.type === aptosCoin)\n if (accountResource === undefined) {\n throw new Error('Account resource not found')\n }\n\n const { coin } = accountResource.data as { coin: { value: string } }\n return BigInt(coin.value).toString()\n }\n\n async getBlock(blockTag: string | number): Promise<Block> {\n let blockNumber = 0\n if (typeof blockTag === 'number') {\n blockNumber = blockTag\n } else if (blockTag === 'latest') {\n blockNumber = await this.nativeProvider\n .getLedgerInfo()\n .then((ledgerInfo) => Number(ledgerInfo.block_height))\n } else {\n throw new Error('Invalid blockTag')\n }\n\n const response = await this.nativeProvider.getBlockByHeight(blockNumber)\n return Block.from(response)\n }\n\n async getBlockWithTransactions(blockTag: string | number): Promise<BlockWithTransactions> {\n let blockNumber = 0\n if (typeof blockTag === 'number') {\n blockNumber = blockTag\n } else if (blockTag === 'latest') {\n blockNumber = await this.getBlockNumber()\n } else {\n throw new Error('Invalid blockTag')\n }\n\n const response = await this.nativeProvider.getBlockByHeight(blockNumber, true)\n return BlockWithTransactions.from(response)\n }\n\n async getBlockNumber(): Promise<number> {\n return this.nativeProvider.getLedgerInfo().then((ledgerInfo) => Number(ledgerInfo.block_height))\n }\n\n async getSlot(_finality?: Finality): Promise<number> {\n await Promise.resolve()\n throw new Error('Method not implemented.')\n }\n\n async getBlockTimestamp(blockTag: string | number): Promise<number> {\n if (typeof blockTag === 'number') {\n return this.nativeProvider.getBlockByHeight(blockTag).then((block) => Number(block.block_timestamp))\n } else if (blockTag === 'latest') {\n return this.nativeProvider.getLedgerInfo().then((ledgerInfo) => Number(ledgerInfo.ledger_timestamp))\n } else {\n throw new Error('Invalid blockTag')\n }\n }\n\n async getTransaction(txHash: string): Promise<TransactionResponse> {\n if (!isHexString(txHash)) {\n throw new Error('Invalid Aptos transaction hash')\n }\n\n const response = await this.nativeProvider.getTransactionByHash(txHash)\n return TransactionResponse.from(response)\n }\n\n async getTransactionReceipt(txHash: string): Promise<TransactionReceipt> {\n if (!isHexString(txHash)) {\n throw new Error('Invalid Aptos transaction hash')\n }\n\n const response = await this.nativeProvider.getTransactionByHash(txHash)\n return TransactionReceipt.from(response)\n }\n\n async getTransactionCount(\n addressOrName: string | Promise<string>,\n _blockTag?: BlockTag | Promise<BlockTag>\n ): Promise<number> {\n const _addressOrName = await Promise.resolve(addressOrName)\n if (!isHexString(_addressOrName)) {\n throw new Error('Invalid Aptos transaction hash')\n }\n\n const response = await this.nativeProvider.getAccount(_addressOrName)\n return parseInt(response.sequence_number)\n }\n\n async sendTransaction(transaction: SignedTransaction, _sendOptions?: object): Promise<TransactionPending> {\n type NativeSignedTransaction = Parameters<typeof this.nativeProvider.submitSignedBCSTransaction>[0] // Uint8Array\n const stx = transaction.signed as NativeSignedTransaction\n\n const response = await this.nativeProvider.submitSignedBCSTransaction(stx)\n return TransactionPending.from(response)\n }\n\n async confirmTransaction(pending: TransactionPending, opts?: object): Promise<TransactionReceipt> {\n const nativePending = pending.pending as aptos.Types.PendingTransaction\n const response = await this.nativeProvider.waitForTransactionWithResult(nativePending.hash)\n if (response.type !== 'user_transaction') {\n throw new Error(`Invalid response type: ${response.type}`)\n }\n const userTransaction = response as aptos.Types.UserTransaction\n if (!userTransaction.success) {\n throw new Error(`aptos transaction failed ,vm_status: ${userTransaction.vm_status}`)\n }\n return TransactionReceipt.from(response)\n }\n\n async sendAndConfirm(transaction: SignedTransaction, opts?: object): Promise<TransactionReceipt> {\n const pending = await this.sendTransaction(transaction, opts)\n return this.confirmTransaction(pending, opts)\n }\n}\n","import * as aptos from 'aptos'\n\nimport {\n Provider,\n SignedTransaction,\n Signer,\n TransactionPending,\n TransactionReceipt,\n TransactionRequest,\n} from '@layerzerolabs/lz-core'\nimport { trim0x } from '@layerzerolabs/lz-utilities'\n\nimport { AptosProvider } from '../providers'\n\nexport class AptosSigner implements Signer {\n public nativeSigner: aptos.AptosAccount\n public provider: AptosProvider | undefined\n\n private constructor(signer: aptos.AptosAccount) {\n this.nativeSigner = signer\n }\n\n /**\n *\n * @param source\n * @param path e.g. m/44'/637'/0'/0'/0'\n */\n static from(source: string | aptos.AptosAccount, path?: string): AptosSigner {\n if (source instanceof aptos.AptosAccount) {\n return new this(source)\n }\n if (path === undefined) {\n const signer = new aptos.AptosAccount(Buffer.from(trim0x(source)))\n return new this(signer)\n } else if (path.startsWith('m/')) {\n const signer = aptos.AptosAccount.fromDerivePath(path, source)\n return new this(signer)\n }\n throw new Error('Invalid parameters')\n }\n\n get native(): aptos.AptosAccount {\n return this.nativeSigner\n }\n\n connect(provider: Provider): this {\n if (!(provider instanceof AptosProvider)) {\n throw new Error('Only aptos.AptosClient is supported.')\n }\n\n this.provider = provider\n return this\n }\n\n async getAddress(): Promise<string> {\n return Promise.resolve(this.nativeSigner.address().toString())\n }\n\n get address(): string {\n return this.nativeSigner.address().toString()\n }\n\n async sendAndConfirm(transaction: SignedTransaction, opts?: object): Promise<TransactionReceipt> {\n if (this.provider === undefined) {\n throw new Error('provider is required')\n }\n\n return this.provider.sendAndConfirm(transaction, opts)\n }\n\n async sendTransaction(transaction: SignedTransaction, sendOptions?: object): Promise<TransactionPending> {\n if (this.provider === undefined) {\n throw new Error('provider is required')\n }\n\n return this.provider.sendTransaction(transaction, sendOptions)\n }\n\n async signTransaction(transaction: TransactionRequest): Promise<SignedTransaction> {\n const nativeProvider = this.provider?.native\n if (nativeProvider === undefined) {\n throw new Error('Connect the native provider first with AptosSigner.connect()')\n }\n\n type NativeTransactionRequest = Parameters<aptos.AptosClient['signTransaction']>[1]\n const tx = transaction.request as NativeTransactionRequest\n\n // signTransaction is an offline operation\n const response = await nativeProvider.signTransaction(this.nativeSigner, tx)\n return SignedTransaction.from(response)\n }\n\n async signBuffer(buffer: Uint8Array): Promise<Uint8Array> {\n const rv = this.nativeSigner.signBuffer(Buffer.from(buffer)).toUint8Array()\n return Promise.resolve(rv)\n }\n\n async buildTransaction(buildTxRequest: unknown): Promise<TransactionRequest> {\n return Promise.reject(new Error('Method not implemented.'))\n }\n}\n"]}
1
+ {"version":3,"sources":["../src/providers/aptos.ts","../src/signers/aptos.ts"],"names":["aptos","SignedTransaction"],"mappings":";AAAA,YAAY,WAAW;AAEvB;AAAA,EACI;AAAA,EAEA;AAAA,EAIA;AAAA,EACA;AAAA,EACA;AAAA,OACG;AACP,SAAS,aAAa;AAMf,IAAM,gBAAN,MAAM,eAAkC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUnC,YACY,KACA,QACA,SAClB;AAHkB;AACA;AACA;AAEhB,SAAK,iBAAiB,IAAU,kBAAY,GAAG;AAAA,EACnD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWA,OAAO,KAAK,QAAgB,QAAiB,SAAiC;AAC1E,QAAI,OAAO,WAAW,UAAU;AAC5B,aAAO,IAAI,eAAc,QAAQ,QAAQ,OAAO;AAAA,IACpD,OAAO;AACH,YAAM,IAAI,MAAM,oBAAoB;AAAA,IACxC;AAAA,EACJ;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,IAAI,SAA4B;AAC5B,WAAO,KAAK;AAAA,EAChB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,MAAM,WAAW,SAAkC;AAC/C,QAAI,CAAC,MAAM,OAAO,GAAG;AACjB,YAAM,IAAI,MAAM,uBAAuB;AAAA,IAC3C;AACA,UAAM,YAAY;AAClB,UAAM,YAAY,MAAM,KAAK,eAAe,oBAAoB,OAAO;AACvE,UAAM,kBAAkB,UAAU,KAAK,CAAC,MAAM,EAAE,SAAS,SAAS;AAClE,QAAI,oBAAoB,QAAW;AAC/B,YAAM,IAAI,MAAM,4BAA4B;AAAA,IAChD;AAEA,UAAM,EAAE,KAAK,IAAI,gBAAgB;AACjC,W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KAAK,YAAY;AAGvB,UAAM,WAAW,MAAM,eAAe,gBAAgB,KAAK,cAAc,EAAE;AAC3E,WAAOA,mBAAkB,KAAK,QAAQ;AAAA,EAC1C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAM,WAAW,QAAyC;AACtD,UAAM,KAAK,KAAK,aAAa,WAAW,OAAO,KAAK,MAAM,CAAC,EAAE,aAAa;AAC1E,WAAO,QAAQ,QAAQ,EAAE;AAAA,EAC7B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,MAAM,iBAAiB,gBAAsD;AACzE,WAAO,QAAQ,OAAO,IAAI,MAAM,yBAAyB,CAAC;AAAA,EAC9D;AACJ","sourcesContent":["import * as aptos from 'aptos'\n\nimport {\n Block,\n BlockTag,\n BlockWithTransactions,\n Finality,\n Provider,\n SignedTransaction,\n TransactionPending,\n TransactionReceipt,\n TransactionResponse,\n} from '@layerzerolabs/lz-core'\nimport { isHex } from '@layerzerolabs/lz-utilities'\n\n/**\n * Represents an Aptos blockchain provider.\n * Implements the Provider interface for interacting with the Aptos blockchain.\n */\nexport class AptosProvider implements Provider {\n readonly nativeProvider: aptos.AptosClient\n\n /**\n * Creates an instance of AptosProvider.\n *\n * @param {string} url - The URL of the Aptos node.\n * @param {string} [faucet] - The URL of the faucet (optional).\n * @param {string} [indexer] - The URL of the indexer (optional).\n */\n private constructor(\n public readonly url: string,\n public readonly faucet?: string,\n public readonly indexer?: string\n ) {\n this.nativeProvider = new aptos.AptosClient(url)\n }\n\n /**\n * Creates an instance of AptosProvider from the given parameters.\n *\n * @param {string} source - The URL of the Aptos node.\n * @param {string} [faucet] - The URL of the faucet (optional).\n * @param {string} [indexer] - The URL of the indexer (optional).\n * @returns {AptosProvider} The created AptosProvider instance.\n * @throws {Error} If the source parameter is not a string.\n */\n static from(source: string, faucet?: string, indexer?: string): AptosProvider {\n if (typeof source === 'string') {\n return new AptosProvider(source, faucet, indexer)\n } else {\n throw new Error('Invalid parameters')\n }\n }\n\n /**\n * Gets the native Aptos client instance.\n *\n * @returns {aptos.AptosClient} The native Aptos client instance.\n */\n get native(): aptos.AptosClient {\n return this.nativeProvider\n }\n\n /**\n * Gets the balance of the specified address.\n *\n * @param {string} address - The address to get the balance of.\n * @returns {Promise<string>} A promise that resolves to the balance of the address.\n * @throws {Error} If the address is not a valid Aptos address.\n */\n async getBalance(address: string): Promise<string> {\n if (!isHex(address)) {\n throw new Error('Invalid Aptos address')\n }\n const aptosCoin = '0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>'\n const resources = await this.nativeProvider.getAccountResources(address)\n const accountResource = resources.find((r) => r.type === aptosCoin)\n if (accountResource === undefined) {\n throw new Error('Account resource not found')\n }\n\n const { coin } = accountResource.data as { coin: { value: string } }\n return BigInt(coin.value).toString()\n }\n\n /**\n * Gets the block specified by blockTag.\n *\n * @param {BlockTag} blockTag - The block tag to specify the block.\n * @returns {Promise<Block>} A promise that resolves to the block.\n * @throws {Error} If the blockTag is invalid.\n */\n async getBlock(blockTag: string | number): Promise<Block> {\n let blockNumber = 0\n if (typeof blockTag === 'number') {\n blockNumber = blockTag\n } else if (blockTag === 'latest') {\n blockNumber = await this.nativeProvider\n .getLedgerInfo()\n .then((ledgerInfo) => Number(ledgerInfo.block_height))\n } else {\n throw new Error('Invalid blockTag')\n }\n\n const response = await this.nativeProvider.getBlockByHeight(blockNumber)\n return Block.from(response)\n }\n\n /**\n * Gets the block with transactions specified by blockTag.\n *\n * @param {BlockTag} blockTag - The block tag to specify the block.\n * @returns {Promise<BlockWithTransactions>} A promise that resolves to the block with transactions.\n * @throws {Error} If the blockTag is invalid.\n */\n async getBlockWithTransactions(blockTag: string | number): Promise<BlockWithTransactions> {\n let blockNumber = 0\n if (typeof blockTag === 'number') {\n blockNumber = blockTag\n } else if (blockTag === 'latest') {\n blockNumber = await this.getBlockNumber()\n } else {\n throw new Error('Invalid blockTag')\n }\n\n const response = await this.nativeProvider.getBlockByHeight(blockNumber, true)\n return BlockWithTransactions.from(response)\n }\n\n /**\n * Gets the current block number.\n *\n * @returns {Promise<number>} A promise that resolves to the current block number.\n */\n async getBlockNumber(): Promise<number> {\n return this.nativeProvider.getLedgerInfo().then((ledgerInfo) => Number(ledgerInfo.block_height))\n }\n\n /**\n * Gets the current slot number for commitment, not suitable for Aptos.\n *\n * @param {Finality} [commitment] - The commitment level (optional).\n * @returns {Promise<number>} A promise that resolves to the current slot number.\n * @throws {Error} Method not implemented.\n */\n async getSlot(_finality?: Finality): Promise<number> {\n await Promise.resolve()\n throw new Error('Method not implemented.')\n }\n\n /**\n * Gets the timestamp (A string containing a 64-bit unsigned integer) for the block identified by blockTag.\n *\n * @param {BlockTag} blockTag - The block tag to specify the block.\n * @returns {Promise<number>} A promise that resolves to the timestamp of the block.\n * @throws {Error} If the blockTag is invalid.\n */\n async getBlockTimestamp(blockTag: string | number): Promise<number> {\n if (typeof blockTag === 'number') {\n return this.nativeProvider.getBlockByHeight(blockTag).then((block) => Number(block.block_timestamp))\n } else if (blockTag === 'latest') {\n return this.nativeProvider.getLedgerInfo().then((ledgerInfo) => Number(ledgerInfo.ledger_timestamp))\n } else {\n throw new Error('Invalid blockTag')\n }\n }\n\n /**\n * Gets information about a transaction.\n *\n * @param {string} txHash - The hash of the transaction.\n * @returns {Promise<TransactionResponse>} A promise that resolves to the transaction response.\n * @throws {Error} If the transaction hash is invalid.\n */\n async getTransaction(txHash: string): Promise<TransactionResponse> {\n if (!isHex(txHash)) {\n throw new Error('Invalid Aptos transaction hash')\n }\n\n const response = await this.nativeProvider.getTransactionByHash(txHash)\n return TransactionResponse.from(response)\n }\n\n /**\n * Gets the receipt of a transaction.\n *\n * @param {string} txHash - The hash of the transaction.\n * @returns {Promise<TransactionReceipt>} A promise that resolves to the transaction receipt.\n * @throws {Error} If the transaction hash is invalid.\n */\n async getTransactionReceipt(txHash: string): Promise<TransactionReceipt> {\n if (!isHex(txHash)) {\n throw new Error('Invalid Aptos transaction hash')\n }\n\n const response = await this.nativeProvider.getTransactionByHash(txHash)\n return TransactionReceipt.from(response)\n }\n\n /**\n * Gets the number of transactions sent from the specified address.\n *\n * @param {string | Promise<string>} addressOrName - The address to get the number of transactions from.\n * @param {BlockTag | Promise<BlockTag>} [blockTag] - The block tag to get the number of transactions from (optional).\n * @returns {Promise<number>} A promise that resolves to the number of transactions sent from the address.\n * @throws {Error} If the address is invalid.\n */\n async getTransactionCount(\n addressOrName: string | Promise<string>,\n _blockTag?: BlockTag | Promise<BlockTag>\n ): Promise<number> {\n const _addressOrName = await Promise.resolve(addressOrName)\n if (!isHex(_addressOrName)) {\n throw new Error('Invalid Aptos transaction hash')\n }\n\n const response = await this.nativeProvider.getAccount(_addressOrName)\n return parseInt(response.sequence_number)\n }\n\n /**\n * Sends a signed transaction to the blockchain.\n *\n * @param {SignedTransaction} transaction - The signed transaction to send.\n * @param {object} [sendOptions] - Optional parameters for sending the transaction.\n * @returns {Promise<TransactionPending>} A promise that resolves to the pending transaction.\n */\n async sendTransaction(transaction: SignedTransaction, _sendOptions?: object): Promise<TransactionPending> {\n type NativeSignedTransaction = Parameters<typeof this.nativeProvider.submitSignedBCSTransaction>[0] // Uint8Array\n const stx = transaction.signed as NativeSignedTransaction\n\n const response = await this.nativeProvider.submitSignedBCSTransaction(stx)\n return TransactionPending.from(response)\n }\n\n /**\n * Confirms a pending transaction.\n *\n * @param {TransactionPending} pending - The pending transaction to confirm.\n * @param {object} [opts] - Optional parameters for the confirmation.\n * @returns {Promise<TransactionReceipt>} A promise that resolves to the transaction receipt.\n * @throws {Error} If the transaction fails.\n */\n async confirmTransaction(pending: TransactionPending, opts?: object): Promise<TransactionReceipt> {\n const nativePending = pending.pending as aptos.Types.PendingTransaction\n const response = await this.nativeProvider.waitForTransactionWithResult(nativePending.hash)\n if (response.type !== 'user_transaction') {\n throw new Error(`Invalid response type: ${response.type}`)\n }\n const userTransaction = response as aptos.Types.UserTransaction\n if (!userTransaction.success) {\n throw new Error(`aptos transaction failed ,vm_status: ${userTransaction.vm_status}`)\n }\n return TransactionReceipt.from(response)\n }\n\n /**\n * Sends a signed transaction and waits for confirmation.\n *\n * @param {SignedTransaction} transaction - The signed transaction to send.\n * @param {object} [opts] - Optional parameters for sending and confirming the transaction.\n * @returns {Promise<TransactionReceipt>} A promise that resolves to the transaction receipt.\n */\n async sendAndConfirm(transaction: SignedTransaction, opts?: object): Promise<TransactionReceipt> {\n const pending = await this.sendTransaction(transaction, opts)\n return this.confirmTransaction(pending, opts)\n }\n}\n","import * as aptos from 'aptos'\n\nimport {\n Provider,\n SignedTransaction,\n Signer,\n TransactionPending,\n TransactionReceipt,\n TransactionRequest,\n} from '@layerzerolabs/lz-core'\nimport { trim0x } from '@layerzerolabs/lz-utilities'\n\nimport { AptosProvider } from '../providers'\n\n/**\n * Represents an Aptos blockchain signer.\n * Implements the Signer interface for interacting with the Aptos blockchain.\n */\nexport class AptosSigner implements Signer {\n public nativeSigner: aptos.AptosAccount\n public provider: AptosProvider | undefined\n\n /**\n * Creates an instance of AptosSigner.\n *\n * @param {aptos.AptosAccount} signer - The Aptos account to use as the signer.\n */\n private constructor(signer: aptos.AptosAccount) {\n this.nativeSigner = signer\n }\n\n /**\n * Creates an instance of AptosSigner from the given parameters.\n *\n * @param {string | aptos.AptosAccount} source - The source to create the signer from, could be a private key, a mnemonics or an Aptos account.\n * @param {string} [path] - The derivation path (optional). e.g. m/44'/637'/0'/0'/0' If provided, the source is treated as a mnemonics.\n * @returns {AptosSigner} The created AptosSigner instance.\n * @throws {Error} If the parameters are invalid.\n */\n static from(source: string | aptos.AptosAccount, path?: string): AptosSigner {\n if (source instanceof aptos.AptosAccount) {\n return new this(source)\n }\n if (path === undefined) {\n const signer = new aptos.AptosAccount(Buffer.from(trim0x(source)))\n return new this(signer)\n } else if (path.startsWith('m/')) {\n const signer = aptos.AptosAccount.fromDerivePath(path, source)\n return new this(signer)\n }\n throw new Error('Invalid parameters')\n }\n\n /**\n * Gets the native Aptos signer instance.\n *\n * @returns {aptos.AptosAccount} The native Aptos signer instance.\n */\n get native(): aptos.AptosAccount {\n return this.nativeSigner\n }\n\n /**\n * Connects the signer to a provider.\n *\n * @param {Provider} provider - The provider to connect to.\n * @returns {this} The connected signer.\n * @throws {Error} If the provider is not an instance of AptosProvider.\n */\n connect(provider: Provider): this {\n if (!(provider instanceof AptosProvider)) {\n throw new Error('Only aptos.AptosClient is supported.')\n }\n\n this.provider = provider\n return this\n }\n\n /**\n * Gets the address of the signer.\n *\n * @returns {Promise<string>} A promise that resolves to the address of the signer.\n */\n async getAddress(): Promise<string> {\n return Promise.resolve(this.nativeSigner.address().toString())\n }\n\n /**\n * Gets the address of the signer.\n *\n * @returns {string} The address of the signer.\n */\n get address(): string {\n return this.nativeSigner.address().toString()\n }\n\n /**\n * Sends a signed transaction and waits for confirmation.\n *\n * @param {SignedTransaction} transaction - The signed transaction to send.\n * @param {object} [opts] - Optional parameters for sending and confirming the transaction.\n * @returns {Promise<TransactionReceipt>} A promise that resolves to the transaction receipt.\n * @throws {Error} If the provider is not connected.\n */\n async sendAndConfirm(transaction: SignedTransaction, opts?: object): Promise<TransactionReceipt> {\n if (this.provider === undefined) {\n throw new Error('provider is required')\n }\n\n return this.provider.sendAndConfirm(transaction, opts)\n }\n\n /**\n * Sends a signed transaction.\n *\n * @param {SignedTransaction} transaction - The signed transaction to send.\n * @param {object} [sendOptions] - Optional parameters for sending the transaction.\n * @returns {Promise<TransactionPending>} A promise that resolves to the pending transaction.\n * @throws {Error} If the provider is not connected.\n */\n async sendTransaction(transaction: SignedTransaction, sendOptions?: object): Promise<TransactionPending> {\n if (this.provider === undefined) {\n throw new Error('provider is required')\n }\n\n return this.provider.sendTransaction(transaction, sendOptions)\n }\n\n /**\n * Signs a transaction.\n *\n * @param {TransactionRequest} transaction - The transaction request to sign.\n * @returns {Promise<SignedTransaction>} A promise that resolves to the signed transaction.\n * @throws {Error} If the native provider is not connected.\n */\n async signTransaction(transaction: TransactionRequest): Promise<SignedTransaction> {\n const nativeProvider = this.provider?.native\n if (nativeProvider === undefined) {\n throw new Error('Connect the native provider first with AptosSigner.connect()')\n }\n\n type NativeTransactionRequest = Parameters<aptos.AptosClient['signTransaction']>[1]\n const tx = transaction.request as NativeTransactionRequest\n\n // signTransaction is an offline operation\n const response = await nativeProvider.signTransaction(this.nativeSigner, tx)\n return SignedTransaction.from(response)\n }\n\n /**\n * Signs a buffer (e.g., a message hash) using the native Aptos signer.\n *\n * @param {Uint8Array} buffer - The buffer to sign.\n * @returns {Promise<Uint8Array>} A promise that resolves to the signed buffer as a Uint8Array.\n */\n async signBuffer(buffer: Uint8Array): Promise<Uint8Array> {\n const rv = this.nativeSigner.signBuffer(Buffer.from(buffer)).toUint8Array()\n return Promise.resolve(rv)\n }\n\n /**\n * Builds a transaction.\n *\n * @param {unknown} buildTxRequest - The transaction request to build.\n * @returns {Promise<TransactionRequest>} A promise that resolves to the built transaction request.\n * @throws {Error} Method not implemented.\n */\n async buildTransaction(buildTxRequest: unknown): Promise<TransactionRequest> {\n return Promise.reject(new Error('Method not implemented.'))\n }\n}\n"]}