@layerzerolabs/lz-corekit-initia 3.0.15 → 3.0.17
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 +22 -0
- package/README.md +98 -1
- package/dist/index.cjs +168 -89
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +191 -25
- package/dist/index.d.ts +191 -25
- package/dist/index.mjs +172 -90
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
# @layerzerolabs/lz-corekit-initia
|
|
2
2
|
|
|
3
|
+
## 3.0.17
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 40f2269: islander mainnet
|
|
8
|
+
- 40f2269: testnets
|
|
9
|
+
- Updated dependencies [40f2269]
|
|
10
|
+
- Updated dependencies [40f2269]
|
|
11
|
+
- @layerzerolabs/lz-core@3.0.17
|
|
12
|
+
- @layerzerolabs/lz-utilities@3.0.17
|
|
13
|
+
- @layerzerolabs/move-definitions@3.0.17
|
|
14
|
+
|
|
15
|
+
## 3.0.16
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- 87a4bc9: islander mainnet
|
|
20
|
+
- Updated dependencies [87a4bc9]
|
|
21
|
+
- @layerzerolabs/lz-core@3.0.16
|
|
22
|
+
- @layerzerolabs/lz-utilities@3.0.16
|
|
23
|
+
- @layerzerolabs/move-definitions@3.0.16
|
|
24
|
+
|
|
3
25
|
## 3.0.15
|
|
4
26
|
|
|
5
27
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -1 +1,98 @@
|
|
|
1
|
-
# @layerzerolabs/lz-corekit
|
|
1
|
+
# @layerzerolabs/lz-corekit-initia
|
|
2
|
+
|
|
3
|
+
The Initia CoreKit is a comprehensive SDK designed to interact with the Initia blockchain. It provides a set of utilities and modules to facilitate the development and integration of applications with the Initia 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 Initia CoreKit, you can use npm or yarn:
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
npm install @layerzerolabs/lz-corekit-initia
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
or
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
yarn add @layerzerolabs/lz-corekit-initia
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
### Initialization
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
import { InitiaProvider } from "@layerzerolabs/lz-corekit-initia";
|
|
31
|
+
|
|
32
|
+
// url is the Initia chain full node url
|
|
33
|
+
const url = "http://127.0.0.1:8516";
|
|
34
|
+
const provider = InitiaProvider.from(url);
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Retrieve Account Information
|
|
38
|
+
|
|
39
|
+
#### Get Account Balance
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
import { InitiaProvider } from "@layerzerolabs/lz-corekit-initia";
|
|
43
|
+
|
|
44
|
+
// url is the Initia chain full node url
|
|
45
|
+
const url = "http://127.0.0.1:8516";
|
|
46
|
+
const provider = InitiaProvider.from(url);
|
|
47
|
+
|
|
48
|
+
const address = "0x1";
|
|
49
|
+
const balance = await provider.getBalance(address);
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Retrieve Block Information
|
|
53
|
+
|
|
54
|
+
#### Get Latest Block Height
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
import { InitiaProvider } from "@layerzerolabs/lz-corekit-initia";
|
|
58
|
+
|
|
59
|
+
// url is the Initia chain full node url
|
|
60
|
+
const url = "http://127.0.0.1:8516";
|
|
61
|
+
const provider = InitiaProvider.from(url);
|
|
62
|
+
|
|
63
|
+
const number = await provider.getBlockNumber();
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Transaction Management
|
|
67
|
+
|
|
68
|
+
#### Get Transaction by hash
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
import { InitiaProvider } from "@layerzerolabs/lz-corekit-initia";
|
|
72
|
+
|
|
73
|
+
// url is the Initia chain full node url
|
|
74
|
+
const url = "http://127.0.0.1:8516";
|
|
75
|
+
const provider = InitiaProvider.from(url);
|
|
76
|
+
|
|
77
|
+
const hash = "0x1";
|
|
78
|
+
const tx = await provider.getTransaction(hash);
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
#### Sign, Send and Confirm Transaction
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
import { InitiaProvider, InitiaSigner } from '@layerzerolabs/lz-corekit-initia'
|
|
85
|
+
import { SignedTransaction, TransactionReceipt, TransactionRequest } from '@layerzerolabs/lz-core'
|
|
86
|
+
|
|
87
|
+
// url is the Initia chain full node url
|
|
88
|
+
const url = 'http://127.0.0.1:8516'
|
|
89
|
+
const provider = InitiaProvider.from(url)
|
|
90
|
+
|
|
91
|
+
const privateKey = '0x1234'
|
|
92
|
+
const signer = InitiaSigner.from(privateKey)
|
|
93
|
+
signer.connect(provider)
|
|
94
|
+
|
|
95
|
+
const tx: TransactionRequest = ...
|
|
96
|
+
const stx: SignedTransaction = await signer.signTransaction(tx)
|
|
97
|
+
const receipt: TransactionReceipt = await signer.sendAndConfirm(stx)
|
|
98
|
+
```
|
package/dist/index.cjs
CHANGED
|
@@ -6,6 +6,11 @@ var lzUtilities = require('@layerzerolabs/lz-utilities');
|
|
|
6
6
|
|
|
7
7
|
// src/providers/initia.ts
|
|
8
8
|
var InitiaProvider = class _InitiaProvider {
|
|
9
|
+
/**
|
|
10
|
+
* Creates an instance of InitiaProvider.
|
|
11
|
+
*
|
|
12
|
+
* @param {string} url - The URL of the Initia node.
|
|
13
|
+
*/
|
|
9
14
|
constructor(url) {
|
|
10
15
|
this.url = url;
|
|
11
16
|
this.nativeProvider = new initia_js.LCDClient(url);
|
|
@@ -17,22 +22,41 @@ var InitiaProvider = class _InitiaProvider {
|
|
|
17
22
|
throw new Error("Invalid parameters");
|
|
18
23
|
}
|
|
19
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* Gets the native Initia provider instance.
|
|
27
|
+
*
|
|
28
|
+
* @returns {LCDClient} The native Initia provider instance.
|
|
29
|
+
*/
|
|
20
30
|
get native() {
|
|
21
31
|
return this.nativeProvider;
|
|
22
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* Gets the balance of the specified address.
|
|
35
|
+
*
|
|
36
|
+
* @param {string} address - The address to get the balance of.
|
|
37
|
+
* @returns {Promise<string>} A promise that resolves to the balance of the address.
|
|
38
|
+
* @throws {Error} If the address is not a valid Initia address.
|
|
39
|
+
*/
|
|
23
40
|
async getBalance(address) {
|
|
24
41
|
let accAddress;
|
|
25
|
-
if (!lzUtilities.
|
|
42
|
+
if (!lzUtilities.isHex(address)) {
|
|
26
43
|
accAddress = initia_js.AccAddress.fromHex(address);
|
|
27
44
|
} else if (address.startsWith("init")) {
|
|
28
45
|
accAddress = address;
|
|
29
46
|
} else {
|
|
30
|
-
throw new Error("Invalid
|
|
47
|
+
throw new Error("Invalid Initia address");
|
|
31
48
|
}
|
|
32
49
|
const resources = await this.nativeProvider.bank.balance(accAddress);
|
|
33
50
|
const coins = resources[0];
|
|
34
51
|
return coins.toString();
|
|
35
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* Gets the block specified by blockTag.
|
|
55
|
+
*
|
|
56
|
+
* @param {BlockTag} blockTag - The block tag to specify the block.
|
|
57
|
+
* @returns {Promise<Block>} A promise that resolves to the block.
|
|
58
|
+
* @throws {Error} If the blockTag is invalid.
|
|
59
|
+
*/
|
|
36
60
|
async getBlock(blockTag) {
|
|
37
61
|
let blockNumber = 0;
|
|
38
62
|
if (typeof blockTag === "number") {
|
|
@@ -45,16 +69,43 @@ var InitiaProvider = class _InitiaProvider {
|
|
|
45
69
|
const response = await this.nativeProvider.tendermint.blockInfo(blockNumber);
|
|
46
70
|
return lzCore.Block.from(response);
|
|
47
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* Gets the block with transactions specified by blockTag.
|
|
74
|
+
* Later: could invoke txInfosByHeight method in TxAPI
|
|
75
|
+
*
|
|
76
|
+
* @param {BlockTag} blockTag - The block tag to specify the block.
|
|
77
|
+
* @returns {Promise<BlockWithTransactions>} A promise that resolves to the block with transactions.
|
|
78
|
+
* @throws {Error} Method not implemented.
|
|
79
|
+
*/
|
|
48
80
|
async getBlockWithTransactions(blockTag) {
|
|
49
81
|
return Promise.reject(new Error("Method not implemented."));
|
|
50
82
|
}
|
|
83
|
+
/**
|
|
84
|
+
* Gets the current block number.
|
|
85
|
+
*
|
|
86
|
+
* @returns {Promise<number>} A promise that resolves to the current block number.
|
|
87
|
+
*/
|
|
51
88
|
async getBlockNumber() {
|
|
52
89
|
return this.nativeProvider.tendermint.blockInfo().then((ledgerInfo) => Number(ledgerInfo.block.header.height));
|
|
53
90
|
}
|
|
91
|
+
/**
|
|
92
|
+
* Gets the current slot number for commitment, not suitable for Initia.
|
|
93
|
+
*
|
|
94
|
+
* @param {Finality} [finality] - The commitment level (optional).
|
|
95
|
+
* @returns {Promise<number>} A promise that resolves to the current slot number.
|
|
96
|
+
* @throws {Error} Method not implemented.
|
|
97
|
+
*/
|
|
54
98
|
async getSlot(_finality) {
|
|
55
99
|
await Promise.resolve();
|
|
56
100
|
throw new Error("Method not implemented.");
|
|
57
101
|
}
|
|
102
|
+
/**
|
|
103
|
+
* Gets the UNIX timestamp for the block identified by blockTag.
|
|
104
|
+
*
|
|
105
|
+
* @param {BlockTag} blockTag - The block tag to specify the block.
|
|
106
|
+
* @returns {Promise<number>} A promise that resolves to the UNIX timestamp of the block.
|
|
107
|
+
* @throws {Error} If the blockTag is invalid.
|
|
108
|
+
*/
|
|
58
109
|
async getBlockTimestamp(blockTag) {
|
|
59
110
|
if (typeof blockTag === "number") {
|
|
60
111
|
return this.nativeProvider.tendermint.blockInfo(blockTag).then((block) => Number(block.block.header.time));
|
|
@@ -64,38 +115,75 @@ var InitiaProvider = class _InitiaProvider {
|
|
|
64
115
|
throw new Error("Invalid blockTag");
|
|
65
116
|
}
|
|
66
117
|
}
|
|
118
|
+
/**
|
|
119
|
+
* Gets information about a transaction.
|
|
120
|
+
*
|
|
121
|
+
* @param {string} txHash - The hash of the transaction.
|
|
122
|
+
* @returns {Promise<TransactionResponse>} A promise that resolves to the transaction response.
|
|
123
|
+
* @throws {Error} If the transaction hash is invalid.
|
|
124
|
+
*/
|
|
67
125
|
async getTransaction(txHash) {
|
|
68
|
-
if (!lzUtilities.
|
|
126
|
+
if (!lzUtilities.isHex(txHash)) {
|
|
69
127
|
throw new Error("Invalid Initia transaction hash");
|
|
70
128
|
}
|
|
71
129
|
const response = await this.nativeProvider.tx.txInfo(txHash);
|
|
72
130
|
return lzCore.TransactionResponse.from(response);
|
|
73
131
|
}
|
|
132
|
+
/**
|
|
133
|
+
* Gets the receipt of a transaction.
|
|
134
|
+
*
|
|
135
|
+
* @param {string} txHash - The hash of the transaction.
|
|
136
|
+
* @returns {Promise<TransactionReceipt>} A promise that resolves to the transaction receipt.
|
|
137
|
+
* @throws {Error} If the transaction hash is invalid.
|
|
138
|
+
*/
|
|
74
139
|
async getTransactionReceipt(txHash) {
|
|
75
|
-
if (!lzUtilities.
|
|
140
|
+
if (!lzUtilities.isHex(txHash)) {
|
|
76
141
|
throw new Error("Invalid Initia transaction hash");
|
|
77
142
|
}
|
|
78
143
|
const response = await this.nativeProvider.tx.txInfo(txHash);
|
|
79
144
|
return lzCore.TransactionReceipt.from(response);
|
|
80
145
|
}
|
|
146
|
+
/**
|
|
147
|
+
* Gets the number of transactions sent from the specified address.
|
|
148
|
+
*
|
|
149
|
+
* @param {string | Promise<string>} addressOrName - The address to get the number of transactions from.
|
|
150
|
+
* @param {BlockTag | Promise<BlockTag>} [blockTag] - The block tag to get the number of transactions from (optional).
|
|
151
|
+
* @returns {Promise<number>} A promise that resolves to the number of transactions sent from the address.
|
|
152
|
+
* @throws {Error} If the address is invalid.
|
|
153
|
+
*/
|
|
81
154
|
async getTransactionCount(addressOrName, _blockTag) {
|
|
82
155
|
const _addressOrName = await Promise.resolve(addressOrName);
|
|
83
156
|
let accAddress;
|
|
84
|
-
if (!lzUtilities.
|
|
157
|
+
if (!lzUtilities.isHex(_addressOrName)) {
|
|
85
158
|
accAddress = initia_js.AccAddress.fromHex(_addressOrName);
|
|
86
159
|
} else if (_addressOrName.startsWith("init")) {
|
|
87
160
|
accAddress = _addressOrName;
|
|
88
161
|
} else {
|
|
89
|
-
throw new Error("Invalid
|
|
162
|
+
throw new Error("Invalid Initia address");
|
|
90
163
|
}
|
|
91
164
|
const response = await this.nativeProvider.auth.accountInfo(accAddress);
|
|
92
165
|
return response.getSequenceNumber();
|
|
93
166
|
}
|
|
167
|
+
/**
|
|
168
|
+
* Sends a signed transaction to the blockchain.
|
|
169
|
+
*
|
|
170
|
+
* @param {SignedTransaction} transaction - The signed transaction to send.
|
|
171
|
+
* @param {object} [sendOptions] - Optional parameters for sending the transaction.
|
|
172
|
+
* @returns {Promise<TransactionPending>} A promise that resolves to the pending transaction.
|
|
173
|
+
*/
|
|
94
174
|
async sendTransaction(transaction, _sendOptions) {
|
|
95
175
|
const stx = transaction.signed;
|
|
96
176
|
const response = await this.nativeProvider.tx.broadcast(stx);
|
|
97
177
|
return lzCore.TransactionPending.from(response);
|
|
98
178
|
}
|
|
179
|
+
/**
|
|
180
|
+
* Confirms a pending transaction.
|
|
181
|
+
*
|
|
182
|
+
* @param {TransactionPending} pending - The pending transaction to confirm.
|
|
183
|
+
* @param {object} [opts] - Optional parameters for the confirmation.
|
|
184
|
+
* @returns {Promise<TransactionReceipt>} A promise that resolves to the transaction receipt.
|
|
185
|
+
* @throws {Error} If the transaction fails.
|
|
186
|
+
*/
|
|
99
187
|
async confirmTransaction(pending, _opts) {
|
|
100
188
|
const nativePending = pending.pending;
|
|
101
189
|
if (nativePending.code !== 0) {
|
|
@@ -103,73 +191,35 @@ var InitiaProvider = class _InitiaProvider {
|
|
|
103
191
|
}
|
|
104
192
|
return Promise.resolve(lzCore.TransactionReceipt.from(nativePending));
|
|
105
193
|
}
|
|
194
|
+
/**
|
|
195
|
+
* Sends a signed transaction and waits for confirmation.
|
|
196
|
+
*
|
|
197
|
+
* @param {SignedTransaction} transaction - The signed transaction to send.
|
|
198
|
+
* @param {object} [opts] - Optional parameters for sending and confirming the transaction.
|
|
199
|
+
* @returns {Promise<TransactionReceipt>} A promise that resolves to the transaction receipt.
|
|
200
|
+
*/
|
|
106
201
|
async sendAndConfirm(transaction, opts) {
|
|
107
202
|
const pending = await this.sendTransaction(transaction, opts);
|
|
108
203
|
return this.confirmTransaction(pending, opts);
|
|
109
204
|
}
|
|
110
205
|
};
|
|
111
|
-
function convertPayloadToMsgExecute(sender, payload) {
|
|
112
|
-
const functionStructs = payload.function.split("::");
|
|
113
|
-
const moduleAddress = functionStructs[0];
|
|
114
|
-
const moduleName = functionStructs[1];
|
|
115
|
-
const functionName = functionStructs[2];
|
|
116
|
-
const args = covertPayloadArgs(payload.functionArgumentTypes ?? [], payload.functionArguments);
|
|
117
|
-
const msg = new initia_js.MsgExecute(
|
|
118
|
-
sender,
|
|
119
|
-
// sender address
|
|
120
|
-
moduleAddress,
|
|
121
|
-
// module owner address
|
|
122
|
-
moduleName,
|
|
123
|
-
// module name
|
|
124
|
-
functionName,
|
|
125
|
-
// function name
|
|
126
|
-
payload.typeArguments ?? [],
|
|
127
|
-
// type args
|
|
128
|
-
args
|
|
129
|
-
);
|
|
130
|
-
return msg;
|
|
131
|
-
}
|
|
132
|
-
function covertPayloadArgs(functionArgumentTypes, functionArguments) {
|
|
133
|
-
const args = [];
|
|
134
|
-
for (let i = 0; i < functionArguments.length; i++) {
|
|
135
|
-
args.push(convertArg(functionArgumentTypes[i], functionArguments[i]));
|
|
136
|
-
}
|
|
137
|
-
return args;
|
|
138
|
-
}
|
|
139
|
-
function convertArg(argsType, arg) {
|
|
140
|
-
switch (argsType) {
|
|
141
|
-
case "bool":
|
|
142
|
-
return initia_js.bcs.bool().serialize(arg).toBase64();
|
|
143
|
-
case "u256":
|
|
144
|
-
return initia_js.bcs.u256().serialize(arg).toBase64();
|
|
145
|
-
case "u128":
|
|
146
|
-
return initia_js.bcs.u128().serialize(arg).toBase64();
|
|
147
|
-
case "u64":
|
|
148
|
-
return initia_js.bcs.u64().serialize(arg).toBase64();
|
|
149
|
-
case "u32":
|
|
150
|
-
return initia_js.bcs.u32().serialize(arg).toBase64();
|
|
151
|
-
case "u16":
|
|
152
|
-
return initia_js.bcs.u16().serialize(arg).toBase64();
|
|
153
|
-
case "u8":
|
|
154
|
-
return initia_js.bcs.u8().serialize(arg).toBase64();
|
|
155
|
-
case "address":
|
|
156
|
-
return initia_js.bcs.address().serialize(arg).toBase64();
|
|
157
|
-
case "vector<u8>":
|
|
158
|
-
return initia_js.bcs.vector(initia_js.bcs.u8()).serialize(arg).toBase64();
|
|
159
|
-
case "vector<address>":
|
|
160
|
-
return initia_js.bcs.vector(initia_js.bcs.address()).serialize(arg).toBase64();
|
|
161
|
-
case "vector<vector<u8>>":
|
|
162
|
-
return initia_js.bcs.vector(initia_js.bcs.vector(initia_js.bcs.u8())).serialize(arg).toBase64();
|
|
163
|
-
default:
|
|
164
|
-
throw new Error(`Invalid type argsType: ${argsType.toString()}`);
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
// src/signers/initia.ts
|
|
169
206
|
var InitiaSigner = class {
|
|
207
|
+
/**
|
|
208
|
+
* Creates an instance of InitiaSigner.
|
|
209
|
+
*
|
|
210
|
+
* @param {MnemonicKey | RawKey} signer - The Initia key to use as the signer.
|
|
211
|
+
*/
|
|
170
212
|
constructor(signer) {
|
|
171
213
|
this.nativeKey = signer;
|
|
172
214
|
}
|
|
215
|
+
/**
|
|
216
|
+
* Creates an instance of InitiaSigner from the given source.
|
|
217
|
+
*
|
|
218
|
+
* @param {string | MnemonicKey} source - The source to create the signer from.
|
|
219
|
+
* @param {string} [path] - The derivation path (optional).
|
|
220
|
+
* @returns {Signer} The created InitiaSigner instance.
|
|
221
|
+
* @throws {Error} If the parameters are invalid.
|
|
222
|
+
*/
|
|
173
223
|
static from(source, path) {
|
|
174
224
|
if (source instanceof initia_js.MnemonicKey) {
|
|
175
225
|
return new this(source);
|
|
@@ -191,12 +241,26 @@ var InitiaSigner = class {
|
|
|
191
241
|
}
|
|
192
242
|
throw new Error("Invalid parameters");
|
|
193
243
|
}
|
|
244
|
+
/**
|
|
245
|
+
* Gets the native Initia wallet instance.
|
|
246
|
+
* Invoke connect method first to make sure the native provider is not undefined.
|
|
247
|
+
*
|
|
248
|
+
* @returns {Wallet} The native Initia wallet instance.
|
|
249
|
+
* @throws {Error} If the native provider is not connected.
|
|
250
|
+
*/
|
|
194
251
|
get native() {
|
|
195
252
|
if (!this.nativeSigner) {
|
|
196
253
|
throw new Error("Connect the native provider first with InitiaSigner.connect()");
|
|
197
254
|
}
|
|
198
255
|
return this.nativeSigner;
|
|
199
256
|
}
|
|
257
|
+
/**
|
|
258
|
+
* Connects the signer to a provider.
|
|
259
|
+
*
|
|
260
|
+
* @param {Provider} provider - The provider to connect to.
|
|
261
|
+
* @returns {Signer} The connected signer.
|
|
262
|
+
* @throws {Error} If the provider is not an instance of InitiaProvider.
|
|
263
|
+
*/
|
|
200
264
|
connect(provider) {
|
|
201
265
|
if (!(provider instanceof InitiaProvider)) {
|
|
202
266
|
throw new Error("Only InitiaProvider is supported.");
|
|
@@ -205,24 +269,57 @@ var InitiaSigner = class {
|
|
|
205
269
|
this.nativeSigner = new initia_js.Wallet(this.provider.native, this.nativeKey);
|
|
206
270
|
return this;
|
|
207
271
|
}
|
|
272
|
+
/**
|
|
273
|
+
* Gets the address of the signer.
|
|
274
|
+
*
|
|
275
|
+
* @returns {Promise<string>} A promise that resolves to the address of the signer.
|
|
276
|
+
*/
|
|
208
277
|
async getAddress() {
|
|
209
278
|
return Promise.resolve(initia_js.AccAddress.toHex(this.nativeKey.accAddress));
|
|
210
279
|
}
|
|
280
|
+
/**
|
|
281
|
+
* Gets the address of the signer.
|
|
282
|
+
*
|
|
283
|
+
* @returns {string} The address of the signer.
|
|
284
|
+
*/
|
|
211
285
|
get address() {
|
|
212
286
|
return initia_js.AccAddress.toHex(this.nativeKey.accAddress);
|
|
213
287
|
}
|
|
288
|
+
/**
|
|
289
|
+
* Sends a signed transaction and waits for confirmation.
|
|
290
|
+
*
|
|
291
|
+
* @param {SignedTransaction} transaction - The signed transaction to send.
|
|
292
|
+
* @param {object} [opts] - Optional parameters for sending and confirming the transaction.
|
|
293
|
+
* @returns {Promise<TransactionReceipt>} A promise that resolves to the transaction receipt.
|
|
294
|
+
* @throws {Error} If the provider is not connected.
|
|
295
|
+
*/
|
|
214
296
|
async sendAndConfirm(transaction, opts) {
|
|
215
297
|
if (this.provider === void 0) {
|
|
216
298
|
throw new Error("provider is required");
|
|
217
299
|
}
|
|
218
300
|
return this.provider.sendAndConfirm(transaction, opts);
|
|
219
301
|
}
|
|
302
|
+
/**
|
|
303
|
+
* Sends a signed transaction.
|
|
304
|
+
*
|
|
305
|
+
* @param {SignedTransaction} transaction - The signed transaction to send.
|
|
306
|
+
* @param {object} [sendOptions] - Optional parameters for sending the transaction.
|
|
307
|
+
* @returns {Promise<TransactionPending>} A promise that resolves to the pending transaction.
|
|
308
|
+
* @throws {Error} If the provider is not connected.
|
|
309
|
+
*/
|
|
220
310
|
async sendTransaction(transaction, sendOptions) {
|
|
221
311
|
if (this.provider === void 0) {
|
|
222
312
|
throw new Error("provider is required");
|
|
223
313
|
}
|
|
224
314
|
return this.provider.sendTransaction(transaction, sendOptions);
|
|
225
315
|
}
|
|
316
|
+
/**
|
|
317
|
+
* Signs a transaction.
|
|
318
|
+
*
|
|
319
|
+
* @param {TransactionRequest} transaction - The transaction request to sign.
|
|
320
|
+
* @returns {Promise<SignedTransaction>} A promise that resolves to the signed transaction.
|
|
321
|
+
* @throws {Error} If the native provider is not connected.
|
|
322
|
+
*/
|
|
226
323
|
async signTransaction(transaction) {
|
|
227
324
|
if (!this.nativeSigner) {
|
|
228
325
|
throw new Error("Connect the native provider first with InitiaSigner.connect()");
|
|
@@ -237,36 +334,18 @@ var InitiaSigner = class {
|
|
|
237
334
|
});
|
|
238
335
|
return lzCore.SignedTransaction.from(response);
|
|
239
336
|
}
|
|
337
|
+
/**
|
|
338
|
+
* Signs a buffer (e.g., a message hash) using the native Initia signer.
|
|
339
|
+
*
|
|
340
|
+
* @param {Uint8Array} buffer - The buffer to sign.
|
|
341
|
+
* @returns {Promise<Uint8Array>} A promise that resolves to the signed buffer as a Uint8Array.
|
|
342
|
+
*/
|
|
240
343
|
async signBuffer(buffer) {
|
|
241
344
|
return this.nativeKey.sign(Buffer.from(buffer));
|
|
242
345
|
}
|
|
243
|
-
async buildTransaction(buildTxRequest) {
|
|
244
|
-
const sender = this.nativeSigner.key.accAddress.toString();
|
|
245
|
-
const msg = convertPayloadToMsgExecute(sender, buildTxRequest.payload);
|
|
246
|
-
const createOptions = {
|
|
247
|
-
msgs: [msg]
|
|
248
|
-
};
|
|
249
|
-
if (buildTxRequest.options?.gas !== void 0) {
|
|
250
|
-
createOptions.gas = buildTxRequest.options.gas;
|
|
251
|
-
}
|
|
252
|
-
if (buildTxRequest.options?.gasPrice !== void 0) {
|
|
253
|
-
if (isNaN(Number(buildTxRequest.options.gasPrice)) && buildTxRequest.options.gasPrice.endsWith("uinit")) {
|
|
254
|
-
createOptions.gasPrices = buildTxRequest.options.gasPrice;
|
|
255
|
-
} else if (!isNaN(Number(buildTxRequest.options.gasPrice))) {
|
|
256
|
-
createOptions.gasPrices = `${buildTxRequest.options.gasPrice}uinit`;
|
|
257
|
-
} else {
|
|
258
|
-
throw Error(`Invalid gas price for initia :${buildTxRequest.options.gasPrice}`);
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
const tx = await this.nativeSigner.createTx(createOptions);
|
|
262
|
-
return lzCore.TransactionRequest.from(tx);
|
|
263
|
-
}
|
|
264
346
|
};
|
|
265
347
|
|
|
266
348
|
exports.InitiaProvider = InitiaProvider;
|
|
267
349
|
exports.InitiaSigner = InitiaSigner;
|
|
268
|
-
|
|
269
|
-
exports.convertPayloadToMsgExecute = convertPayloadToMsgExecute;
|
|
270
|
-
exports.covertPayloadArgs = covertPayloadArgs;
|
|
271
|
-
//# sourceMappingURL=out.js.map
|
|
350
|
+
//# sourceMappingURL=index.cjs.map
|
|
272
351
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/providers/initia.ts","../src/signers/initia.ts","../src/signers/utils.ts"],"names":["AccAddress","SignedTransaction"],"mappings":";AAAA,SAAS,YAAY,iBAAwC;AAE7D;AAAA,EACI;AAAA,EAMA;AAAA,EACA;AAAA,EACA;AAAA,OACG;AACP,SAAS,mBAAmB;AAErB,IAAM,iBAAN,MAAM,gBAAmC;AAAA,EAGpC,YAAmB,KAAa;AAAb;AACvB,SAAK,iBAAiB,IAAI,UAAU,GAAG;AAAA,EAC3C;AAAA,EAGA,OAAO,KAAK,QAAgC;AACxC,QAAI,OAAO,WAAW,UAAU;AAC5B,aAAO,IAAI,gBAAe,MAAM;AAAA,IACpC,OAAO;AACH,YAAM,IAAI,MAAM,oBAAoB;AAAA,IACxC;AAAA,EACJ;AAAA,EAEA,IAAI,SAAoB;AACpB,WAAO,KAAK;AAAA,EAChB;AAAA,EAEA,MAAM,WAAW,SAAkC;AAC/C,QAAI;AACJ,QAAI,CAAC,YAAY,OAAO,GAAG;AACvB,mBAAa,WAAW,QAAQ,OAAO;AAAA,IAC3C,WAAW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ACrC;AACJ","sourcesContent":["import { AccAddress, LCDClient, WaitTxBroadcastResult } from '@initia/initia.js'\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 InitiaProvider implements Provider {\n readonly nativeProvider: LCDClient\n\n private constructor(public url: string) {\n this.nativeProvider = new LCDClient(url)\n }\n\n static from(url: string): InitiaProvider\n static from(source: string): InitiaProvider {\n if (typeof source === 'string') {\n return new InitiaProvider(source)\n } else {\n throw new Error('Invalid parameters')\n }\n }\n\n get native(): LCDClient {\n return this.nativeProvider\n }\n\n async getBalance(address: string): Promise<string> {\n let accAddress: AccAddress\n if (!isHexString(address)) {\n accAddress = AccAddress.fromHex(address)\n } else if (address.startsWith('init')) {\n accAddress = address\n } else {\n throw new Error('Invalid Aptos address')\n }\n\n //todo convert all address to aclAddress\n const resources = await this.nativeProvider.bank.balance(accAddress)\n const coins = resources[0]\n return coins.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.tendermint\n .blockInfo()\n .then((ledgerInfo) => Number(ledgerInfo.block.header.height))\n } else {\n throw new Error('Invalid blockTag')\n }\n\n const response = await this.nativeProvider.tendermint.blockInfo(blockNumber)\n return Block.from(response)\n }\n\n async getBlockWithTransactions(blockTag: string | number): Promise<BlockWithTransactions> {\n return Promise.reject(new Error('Method not implemented.'))\n }\n\n async getBlockNumber(): Promise<number> {\n return this.nativeProvider.tendermint.blockInfo().then((ledgerInfo) => Number(ledgerInfo.block.header.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.tendermint.blockInfo(blockTag).then((block) => Number(block.block.header.time))\n } else if (blockTag === 'latest') {\n return this.nativeProvider.tendermint.blockInfo().then((block) => Number(block.block.header.time))\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 Initia transaction hash')\n }\n\n const response = await this.nativeProvider.tx.txInfo(txHash)\n return TransactionResponse.from(response)\n }\n\n async getTransactionReceipt(txHash: string): Promise<TransactionReceipt> {\n if (!isHexString(txHash)) {\n throw new Error('Invalid Initia transaction hash')\n }\n\n const response = await this.nativeProvider.tx.txInfo(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\n let accAddress: AccAddress\n if (!isHexString(_addressOrName)) {\n accAddress = AccAddress.fromHex(_addressOrName)\n } else if (_addressOrName.startsWith('init')) {\n accAddress = _addressOrName\n } else {\n throw new Error('Invalid Aptos address')\n }\n const response = await this.nativeProvider.auth.accountInfo(accAddress)\n return response.getSequenceNumber()\n }\n\n async sendTransaction(transaction: SignedTransaction, _sendOptions?: object): Promise<TransactionPending> {\n type NativeSignedTransaction = Parameters<typeof this.nativeProvider.tx.broadcast>[0]\n const stx = transaction.signed as NativeSignedTransaction\n\n const response = await this.nativeProvider.tx.broadcast(stx) //TODO need change to broadcastAsync ??\n return TransactionPending.from(response)\n }\n\n async confirmTransaction(pending: TransactionPending, _opts?: object): Promise<TransactionReceipt> {\n const nativePending = pending.pending as WaitTxBroadcastResult\n // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\n if (((nativePending as any).code as number) !== 0) {\n throw new Error(nativePending.raw_log)\n }\n return Promise.resolve(TransactionReceipt.from(nativePending))\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 { AccAddress, CreateTxOptions, Key, MnemonicKey, ModeInfo, RawKey, Wallet } from '@initia/initia.js'\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 { InitiaProvider } from '../providers'\n\nimport { InitiaBuildTxRequest } from './types'\nimport { convertPayloadToMsgExecute } from './utils'\n\nexport class InitiaSigner implements Signer<InitiaBuildTxRequest> {\n public nativeKey: MnemonicKey | RawKey\n public nativeSigner: Wallet | undefined\n public provider: InitiaProvider | undefined\n\n private constructor(signer: MnemonicKey | RawKey) {\n this.nativeKey = signer\n }\n\n static from(signer: MnemonicKey): Signer<InitiaBuildTxRequest>\n static from(privKey: string): Signer<InitiaBuildTxRequest>\n /**\n *\n * @param mnemonic\n * @param path e.g. m/44'/637'/0'/0'/0'\n */\n static from(mnemonic: string, path: string): Signer<InitiaBuildTxRequest>\n\n static from(source: string | MnemonicKey, path?: string): Signer<InitiaBuildTxRequest> {\n if (source instanceof MnemonicKey) {\n return new this(source)\n } else if (typeof source === 'string' && path === undefined) {\n const signer = new RawKey(Buffer.from(trim0x(source))) // todo double check\n return new this(signer)\n } else if (typeof source === 'string' && path !== undefined) {\n if (path.startsWith('m/')) {\n const [_, coinType, account, __, index] = path.match(/\\d+/g)?.map(Number) ?? [44, 118, 0, 0, 0]\n const normalizeMnemonics = source\n .trim()\n .split(/\\s+/)\n .map((part) => part.toLowerCase())\n .join(' ')\n const key = new MnemonicKey({\n mnemonic: normalizeMnemonics,\n coinType,\n account,\n index,\n })\n return new this(key)\n }\n }\n throw new Error('Invalid parameters')\n }\n\n get native(): Wallet {\n if (!this.nativeSigner) {\n throw new Error('Connect the native provider first with InitiaSigner.connect()')\n }\n return this.nativeSigner\n }\n\n connect(provider: Provider): Signer<InitiaBuildTxRequest> {\n if (!(provider instanceof InitiaProvider)) {\n throw new Error('Only InitiaProvider is supported.')\n }\n\n this.provider = provider\n this.nativeSigner = new Wallet(this.provider.native, this.nativeKey)\n return this\n }\n\n async getAddress(): Promise<string> {\n return Promise.resolve(AccAddress.toHex(this.nativeKey.accAddress))\n }\n\n get address(): string {\n return AccAddress.toHex(this.nativeKey.accAddress)\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 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 if (!this.nativeSigner) {\n throw new Error('Connect the native provider first with InitiaSigner.connect()')\n }\n\n type NativeTransactionRequest = Parameters<Key['signTx']>[0]\n const tx = transaction.request as NativeTransactionRequest\n\n const { account_number, sequence } = await this.nativeSigner.accountNumberAndSequence()\n\n const response = await this.nativeSigner.key.signTx(tx, {\n accountNumber: account_number,\n sequence,\n chainId: await this.nativeSigner.lcd.tendermint.chainId(),\n signMode: ModeInfo.SignMode.SIGN_MODE_DIRECT,\n })\n return SignedTransaction.from(response)\n }\n\n async signBuffer(buffer: Uint8Array): Promise<Uint8Array> {\n return this.nativeKey.sign(Buffer.from(buffer))\n }\n\n async buildTransaction(buildTxRequest: InitiaBuildTxRequest): Promise<TransactionRequest> {\n const sender = this.nativeSigner!.key.accAddress.toString()\n const msg = convertPayloadToMsgExecute(sender, buildTxRequest.payload)\n const createOptions: CreateTxOptions & {\n sequence?: number\n } = {\n msgs: [msg],\n }\n if (buildTxRequest.options?.gas !== undefined) {\n createOptions.gas = buildTxRequest.options.gas\n }\n if (buildTxRequest.options?.gasPrice !== undefined) {\n if (isNaN(Number(buildTxRequest.options.gasPrice)) && buildTxRequest.options.gasPrice.endsWith('uinit')) {\n createOptions.gasPrices = buildTxRequest.options.gasPrice\n } else if (!isNaN(Number(buildTxRequest.options.gasPrice))) {\n createOptions.gasPrices = `${buildTxRequest.options.gasPrice}uinit`\n } else {\n throw Error(`Invalid gas price for initia :${buildTxRequest.options.gasPrice}`)\n }\n }\n // createOptions.sequence = initial + offset\n const tx = await this.nativeSigner!.createTx(createOptions)\n return TransactionRequest.from(tx)\n }\n}\n","import { Msg, MsgExecute, bcs } from '@initia/initia.js'\n\nimport { EntryFunctionArgumentTypes, InputEntryFunctionData } from '@layerzerolabs/move-definitions'\n\nexport function convertPayloadToMsgExecute(sender: string, payload: InputEntryFunctionData): Msg {\n const functionStructs = payload.function.split('::')\n const moduleAddress = functionStructs[0]\n const moduleName = functionStructs[1]\n const functionName = functionStructs[2]\n const args = covertPayloadArgs(payload.functionArgumentTypes ?? [], payload.functionArguments)\n const msg = new MsgExecute(\n sender, // sender address\n moduleAddress, // module owner address\n moduleName, // module name\n functionName, // function name\n payload.typeArguments ?? [], // type args\n args\n )\n return msg\n}\nexport function covertPayloadArgs(\n functionArgumentTypes: string[],\n functionArguments: EntryFunctionArgumentTypes[]\n): string[] {\n const args: string[] = []\n for (let i = 0; i < functionArguments.length; i++) {\n args.push(convertArg(functionArgumentTypes[i], functionArguments[i]))\n }\n return args\n}\n\nexport function convertArg(argsType: string, arg: EntryFunctionArgumentTypes): string {\n switch (argsType) {\n case 'bool':\n return bcs\n .bool()\n .serialize(arg as boolean)\n .toBase64()\n case 'u256':\n return bcs\n .u256()\n .serialize(arg as bigint)\n .toBase64()\n case 'u128':\n return bcs\n .u128()\n .serialize(arg as bigint)\n .toBase64()\n case 'u64':\n return bcs\n .u64()\n .serialize(arg as bigint)\n .toBase64()\n case 'u32':\n return bcs\n .u32()\n .serialize(arg as number)\n .toBase64()\n case 'u16':\n return bcs\n .u16()\n .serialize(arg as number)\n .toBase64()\n case 'u8':\n return bcs\n .u8()\n .serialize(arg as number)\n .toBase64()\n case 'address':\n return bcs\n .address()\n .serialize(arg as string)\n .toBase64()\n case 'vector<u8>':\n return bcs\n .vector(bcs.u8())\n .serialize(arg as unknown as Uint8Array)\n .toBase64()\n case 'vector<address>':\n return bcs\n .vector(bcs.address())\n .serialize(arg as unknown as string[])\n .toBase64()\n case 'vector<vector<u8>>':\n return bcs\n .vector(bcs.vector(bcs.u8()))\n .serialize(arg as unknown as Uint8Array[])\n .toBase64()\n default:\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n throw new Error(`Invalid type argsType: ${argsType.toString()}`)\n }\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/providers/initia.ts","../src/signers/initia.ts"],"names":["LCDClient","isHex","AccAddress","Block","TransactionResponse","TransactionReceipt","TransactionPending","MnemonicKey","RawKey","trim0x","Wallet","ModeInfo","SignedTransaction"],"mappings":";;;;;;;AAmBa,IAAA,cAAA,GAAN,MAAM,eAAmC,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQpC,YAAmB,GAAa,EAAA;AAAb,IAAA,IAAA,CAAA,GAAA,GAAA,GAAA;AACvB,IAAK,IAAA,CAAA,cAAA,GAAiB,IAAIA,mBAAA,CAAU,GAAG,CAAA;AAAA;AAC3C,EAUA,OAAO,KAAK,MAAgC,EAAA;AACxC,IAAI,IAAA,OAAO,WAAW,QAAU,EAAA;AAC5B,MAAO,OAAA,IAAI,gBAAe,MAAM,CAAA;AAAA,KAC7B,MAAA;AACH,MAAM,MAAA,IAAI,MAAM,oBAAoB,CAAA;AAAA;AACxC;AACJ;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,IAAI,MAAoB,GAAA;AACpB,IAAA,OAAO,IAAK,CAAA,cAAA;AAAA;AAChB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,MAAM,WAAW,OAAkC,EAAA;AAC/C,IAAI,IAAA,UAAA;AACJ,IAAI,IAAA,CAACC,iBAAM,CAAA,OAAO,CAAG,EAAA;AACjB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AAE,cAAgB,EAAA,QAAA,KAAa,MAAM,IAAA,CAAK,aAAa,wBAAyB,EAAA;AACtF,IAAA,MAAM,WAAW,MAAM,IAAA,CAAK,YAAa,CAAA,GAAA,CAAI,OAAO,EAAI,EAAA;AAAA,MACpD,aAAe,EAAA,cAAA;AAAA,MACf,QAAA;AAAA,MACA,SAAS,MAAM,IAAA,CAAK,YAAa,CAAA,GAAA,CAAI,WAAW,OAAQ,EAAA;AAAA,MACxD,QAAA,EAAUS,mBAAS,QAAS,CAAA;AAAA,KAC/B,CAAA;AACD,IAAOC,OAAAA,wBAAAA,CAAkB,KAAK,QAAQ,CAAA;AAAA;AAC1C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAM,WAAW,MAAyC,EAAA;AACtD,IAAA,OAAO,KAAK,SAAU,CAAA,IAAA,CAAK,MAAO,CAAA,IAAA,CAAK,MAAM,CAAC,CAAA;AAAA;AAEtD","file":"index.cjs","sourcesContent":["import { AccAddress, LCDClient, WaitTxBroadcastResult } from '@initia/initia.js'\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 Initia blockchain provider.\n * Implements the Provider interface for interacting with Initia-compatible blockchains.\n */\nexport class InitiaProvider implements Provider {\n readonly nativeProvider: LCDClient\n\n /**\n * Creates an instance of InitiaProvider.\n *\n * @param {string} url - The URL of the Initia node.\n */\n private constructor(public url: string) {\n this.nativeProvider = new LCDClient(url)\n }\n\n /**\n * Creates an instance of InitiaProvider from the given URL.\n *\n * @param {string} url - The URL of the Initia node.\n * @returns {InitiaProvider} The created InitiaProvider instance.\n * @throws {Error} If the URL parameter is invalid.\n */\n static from(url: string): InitiaProvider\n static from(source: string): InitiaProvider {\n if (typeof source === 'string') {\n return new InitiaProvider(source)\n } else {\n throw new Error('Invalid parameters')\n }\n }\n\n /**\n * Gets the native Initia provider instance.\n *\n * @returns {LCDClient} The native Initia provider instance.\n */\n get native(): LCDClient {\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 Initia address.\n */\n async getBalance(address: string): Promise<string> {\n let accAddress: AccAddress\n if (!isHex(address)) {\n accAddress = AccAddress.fromHex(address)\n } else if (address.startsWith('init')) {\n accAddress = address\n } else {\n throw new Error('Invalid Initia address')\n }\n\n //todo convert all address to aclAddress\n const resources = await this.nativeProvider.bank.balance(accAddress)\n const coins = resources[0]\n return coins.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.tendermint\n .blockInfo()\n .then((ledgerInfo) => Number(ledgerInfo.block.header.height))\n } else {\n throw new Error('Invalid blockTag')\n }\n\n const response = await this.nativeProvider.tendermint.blockInfo(blockNumber)\n return Block.from(response)\n }\n\n /**\n * Gets the block with transactions specified by blockTag.\n * Later: could invoke txInfosByHeight method in TxAPI\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} Method not implemented.\n */\n async getBlockWithTransactions(blockTag: string | number): Promise<BlockWithTransactions> {\n return Promise.reject(new Error('Method not implemented.'))\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.tendermint.blockInfo().then((ledgerInfo) => Number(ledgerInfo.block.header.height))\n }\n\n /**\n * Gets the current slot number for commitment, not suitable for Initia.\n *\n * @param {Finality} [finality] - 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 UNIX timestamp 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 UNIX 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.tendermint.blockInfo(blockTag).then((block) => Number(block.block.header.time))\n } else if (blockTag === 'latest') {\n return this.nativeProvider.tendermint.blockInfo().then((block) => Number(block.block.header.time))\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 Initia transaction hash')\n }\n\n const response = await this.nativeProvider.tx.txInfo(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 Initia transaction hash')\n }\n\n const response = await this.nativeProvider.tx.txInfo(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\n let accAddress: AccAddress\n if (!isHex(_addressOrName)) {\n accAddress = AccAddress.fromHex(_addressOrName)\n } else if (_addressOrName.startsWith('init')) {\n accAddress = _addressOrName\n } else {\n throw new Error('Invalid Initia address')\n }\n\n const response = await this.nativeProvider.auth.accountInfo(accAddress)\n return response.getSequenceNumber()\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.tx.broadcast>[0]\n const stx = transaction.signed as NativeSignedTransaction\n\n const response = await this.nativeProvider.tx.broadcast(stx) //TODO need change to broadcastAsync ??\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 WaitTxBroadcastResult\n // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\n if (((nativePending as any).code as number) !== 0) {\n throw new Error(nativePending.raw_log)\n }\n return Promise.resolve(TransactionReceipt.from(nativePending))\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 { AccAddress, Key, MnemonicKey, ModeInfo, RawKey, Wallet } from '@initia/initia.js'\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 { InitiaProvider } from '../providers'\n\n/**\n * Represents an Initia blockchain signer.\n * Implements the Signer interface for interacting with Initia-compatible blockchains.\n */\nexport class InitiaSigner implements Signer {\n public nativeKey: MnemonicKey | RawKey\n public nativeSigner: Wallet | undefined\n public provider: InitiaProvider | undefined\n\n /**\n * Creates an instance of InitiaSigner.\n *\n * @param {MnemonicKey | RawKey} signer - The Initia key to use as the signer.\n */\n private constructor(signer: MnemonicKey | RawKey) {\n this.nativeKey = signer\n }\n\n /**\n * Creates an instance of InitiaSigner from a MnemonicKey.\n *\n * @param {MnemonicKey} signer - The MnemonicKey to create the signer from.\n * @returns {Signer} The created InitiaSigner instance.\n */\n static from(signer: MnemonicKey): Signer\n\n /**\n * Creates an instance of InitiaSigner from a private key.\n *\n * @param {string} privKey - The private key to create the signer from.\n * @returns {Signer} The created InitiaSigner instance.\n */\n static from(privKey: string): Signer\n\n /**\n * Creates an instance of InitiaSigner from a mnemonic and derivation path.\n *\n * @param {string} mnemonic - The mnemonic to create the signer from.\n * @param {string} path - The derivation path (e.g., m/44'/637'/0'/0'/0').\n * @returns {Signer} The created InitiaSigner instance.\n */\n static from(mnemonic: string, path: string): Signer\n\n /**\n * Creates an instance of InitiaSigner from the given source.\n *\n * @param {string | MnemonicKey} source - The source to create the signer from.\n * @param {string} [path] - The derivation path (optional).\n * @returns {Signer} The created InitiaSigner instance.\n * @throws {Error} If the parameters are invalid.\n */\n static from(source: string | MnemonicKey, path?: string): Signer {\n if (source instanceof MnemonicKey) {\n return new this(source)\n } else if (typeof source === 'string' && path === undefined) {\n const signer = new RawKey(Buffer.from(trim0x(source))) // todo double check\n return new this(signer)\n } else if (typeof source === 'string' && path !== undefined) {\n if (path.startsWith('m/')) {\n const [_, coinType, account, __, index] = path.match(/\\d+/g)?.map(Number) ?? [44, 118, 0, 0, 0]\n const normalizeMnemonics = source\n .trim()\n .split(/\\s+/)\n .map((part) => part.toLowerCase())\n .join(' ')\n const key = new MnemonicKey({\n mnemonic: normalizeMnemonics,\n coinType,\n account,\n index,\n })\n return new this(key)\n }\n }\n throw new Error('Invalid parameters')\n }\n\n /**\n * Gets the native Initia wallet instance.\n * Invoke connect method first to make sure the native provider is not undefined.\n *\n * @returns {Wallet} The native Initia wallet instance.\n * @throws {Error} If the native provider is not connected.\n */\n get native(): Wallet {\n if (!this.nativeSigner) {\n throw new Error('Connect the native provider first with InitiaSigner.connect()')\n }\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 {Signer} The connected signer.\n * @throws {Error} If the provider is not an instance of InitiaProvider.\n */\n connect(provider: Provider): Signer {\n if (!(provider instanceof InitiaProvider)) {\n throw new Error('Only InitiaProvider is supported.')\n }\n\n this.provider = provider\n this.nativeSigner = new Wallet(this.provider.native, this.nativeKey)\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(AccAddress.toHex(this.nativeKey.accAddress))\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 AccAddress.toHex(this.nativeKey.accAddress)\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 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 if (!this.nativeSigner) {\n throw new Error('Connect the native provider first with InitiaSigner.connect()')\n }\n\n type NativeTransactionRequest = Parameters<Key['signTx']>[0]\n const tx = transaction.request as NativeTransactionRequest\n const { account_number, sequence } = await this.nativeSigner.accountNumberAndSequence()\n const response = await this.nativeSigner.key.signTx(tx, {\n accountNumber: account_number,\n sequence,\n chainId: await this.nativeSigner.lcd.tendermint.chainId(),\n signMode: ModeInfo.SignMode.SIGN_MODE_DIRECT,\n })\n return SignedTransaction.from(response)\n }\n\n /**\n * Signs a buffer (e.g., a message hash) using the native Initia 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 return this.nativeKey.sign(Buffer.from(buffer))\n }\n}\n"]}
|