@swapkit/toolboxes 1.0.0-beta.0 → 1.0.0-beta.10
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/dist/chunk-0f0249b1.js +3 -0
- package/dist/chunk-0f0249b1.js.map +10 -0
- package/dist/chunk-0h4xdrwz.js +4 -0
- package/dist/chunk-0h4xdrwz.js.map +10 -0
- package/dist/chunk-4yap1fvd.js +3 -0
- package/dist/chunk-4yap1fvd.js.map +10 -0
- package/dist/chunk-fjfxga2v.js +3 -0
- package/dist/chunk-fjfxga2v.js.map +10 -0
- package/dist/{chunk-tvrdndbw.js → chunk-p1kdg37m.js} +2 -2
- package/dist/{chunk-tvrdndbw.js.map → chunk-p1kdg37m.js.map} +1 -1
- package/dist/cosmos/index.cjs +2 -2
- package/dist/cosmos/index.cjs.map +10 -13
- package/dist/cosmos/index.js +2 -2
- package/dist/cosmos/index.js.map +10 -13
- package/dist/evm/index.cjs +2 -2
- package/dist/evm/index.cjs.map +10 -16
- package/dist/evm/index.js +2 -2
- package/dist/evm/index.js.map +10 -16
- package/dist/index.cjs +2 -2
- package/dist/index.cjs.map +4 -3
- package/dist/index.js +2 -2
- package/dist/index.js.map +4 -3
- package/dist/radix/index.cjs +2 -2
- package/dist/radix/index.cjs.map +3 -3
- package/dist/radix/index.js +2 -2
- package/dist/radix/index.js.map +3 -3
- package/dist/ripple/index.cjs +3 -0
- package/dist/ripple/index.cjs.map +10 -0
- package/dist/ripple/index.js +3 -0
- package/dist/ripple/index.js.map +10 -0
- package/dist/solana/index.cjs +2 -2
- package/dist/solana/index.cjs.map +3 -3
- package/dist/solana/index.js +2 -2
- package/dist/solana/index.js.map +3 -3
- package/dist/substrate/index.cjs +2 -2
- package/dist/substrate/index.cjs.map +5 -6
- package/dist/substrate/index.js +2 -2
- package/dist/substrate/index.js.map +5 -6
- package/dist/utxo/index.cjs +2 -2
- package/dist/utxo/index.cjs.map +9 -11
- package/dist/utxo/index.js +2 -2
- package/dist/utxo/index.js.map +9 -11
- package/package.json +27 -23
- package/src/cosmos/index.ts +2 -9
- package/src/cosmos/thorchainUtils/addressFormat.ts +1 -2
- package/src/cosmos/thorchainUtils/index.ts +1 -1
- package/src/cosmos/thorchainUtils/messages.ts +74 -56
- package/src/cosmos/thorchainUtils/registry.ts +19 -26
- package/src/cosmos/thorchainUtils/types/{proto/MsgCompiled.ts → MsgCompiled.ts} +1 -3
- package/src/cosmos/thorchainUtils/types/client-types.ts +16 -23
- package/src/cosmos/toolbox/cosmos.ts +334 -0
- package/src/cosmos/toolbox/index.ts +33 -0
- package/src/cosmos/toolbox/thorchain.ts +125 -137
- package/src/cosmos/types.ts +37 -18
- package/src/cosmos/util.ts +24 -74
- package/src/evm/__tests__/ethereum.test.ts +110 -116
- package/src/evm/api.ts +11 -147
- package/src/evm/helpers.ts +111 -83
- package/src/evm/index.ts +1 -17
- package/src/evm/toolbox/baseEVMToolbox.ts +744 -0
- package/src/evm/toolbox/evm.ts +69 -0
- package/src/evm/toolbox/index.ts +36 -0
- package/src/evm/toolbox/op.ts +97 -143
- package/src/evm/types.ts +50 -28
- package/src/index.ts +235 -0
- package/src/radix/index.ts +18 -19
- package/src/ripple/index.ts +199 -0
- package/src/solana/index.ts +11 -5
- package/src/solana/toolbox.ts +227 -137
- package/src/substrate/index.ts +2 -3
- package/src/substrate/{toolbox/baseSubstrateToolbox.ts → substrate.ts} +104 -72
- package/src/substrate/types.ts +120 -0
- package/src/utils.ts +27 -0
- package/src/utxo/helpers/api.ts +27 -23
- package/src/utxo/helpers/bchaddrjs.ts +21 -21
- package/src/utxo/helpers/index.ts +0 -1
- package/src/utxo/helpers/txSize.ts +3 -4
- package/src/utxo/index.ts +3 -7
- package/src/utxo/toolbox/bitcoinCash.ts +165 -155
- package/src/utxo/toolbox/index.ts +63 -24
- package/src/utxo/toolbox/utxo.ts +376 -229
- package/src/utxo/types.ts +26 -39
- package/src/cosmos/thorchainUtils/types/proto/MsgCompiled.js +0 -2806
- package/src/cosmos/thorchainUtils/util.ts +0 -46
- package/src/cosmos/toolbox/BaseCosmosToolbox.ts +0 -254
- package/src/cosmos/toolbox/gaia.ts +0 -39
- package/src/cosmos/toolbox/getToolboxByChain.ts +0 -29
- package/src/cosmos/toolbox/kujira.ts +0 -61
- package/src/evm/provider.ts +0 -6
- package/src/evm/toolbox/EVMToolbox.ts +0 -662
- package/src/evm/toolbox/arb.ts +0 -61
- package/src/evm/toolbox/avax.ts +0 -36
- package/src/evm/toolbox/base.ts +0 -42
- package/src/evm/toolbox/bsc.ts +0 -34
- package/src/evm/toolbox/eth.ts +0 -44
- package/src/evm/toolbox/getToolboxByChain.ts +0 -42
- package/src/evm/toolbox/matic.ts +0 -42
- package/src/radix/toolbox.ts +0 -693
- package/src/substrate/toolbox/index.ts +0 -40
- package/src/substrate/types/index.ts +0 -2
- package/src/substrate/types/network.ts +0 -42
- package/src/substrate/types/wallet.ts +0 -78
- package/src/utxo/helpers/utils.ts +0 -45
|
@@ -1,49 +1,50 @@
|
|
|
1
1
|
import type { Pubkey, Secp256k1HdWallet } from "@cosmjs/amino";
|
|
2
|
-
import type { Account } from "@cosmjs/stargate";
|
|
3
2
|
import { base64 } from "@scure/base";
|
|
4
3
|
import {
|
|
5
4
|
BaseDecimal,
|
|
6
5
|
Chain,
|
|
7
|
-
type ChainId,
|
|
8
6
|
CosmosChainPrefixes,
|
|
9
|
-
DerivationPath,
|
|
10
7
|
FeeOption,
|
|
8
|
+
type GenericTransferParams,
|
|
9
|
+
NetworkDerivationPath,
|
|
11
10
|
RequestClient,
|
|
12
11
|
SKConfig,
|
|
13
|
-
SwapKitError,
|
|
14
12
|
SwapKitNumber,
|
|
13
|
+
derivationPathToString,
|
|
14
|
+
updateDerivationPath,
|
|
15
15
|
} from "@swapkit/helpers";
|
|
16
16
|
|
|
17
|
+
import { P, match } from "ts-pattern";
|
|
17
18
|
import {
|
|
18
19
|
buildAminoMsg,
|
|
19
|
-
buildDepositTx,
|
|
20
20
|
buildEncodedTxBody,
|
|
21
|
-
buildTransferTx,
|
|
22
21
|
convertToSignable,
|
|
23
22
|
createDefaultAminoTypes,
|
|
24
23
|
createDefaultRegistry,
|
|
24
|
+
getCreateTransaction,
|
|
25
25
|
parseAminoMessageForDirectSigning,
|
|
26
|
-
} from "../thorchainUtils
|
|
27
|
-
import type {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
} from "../thorchainUtils/types/client-types";
|
|
31
|
-
import type { Signer, TransferParams } from "../types";
|
|
26
|
+
} from "../thorchainUtils";
|
|
27
|
+
import type { ThorchainConstantsResponse } from "../thorchainUtils/types/client-types";
|
|
28
|
+
import type { MultisigTx } from "../types";
|
|
29
|
+
import type { CosmosToolboxParams, MultiSigSigner } from "../types";
|
|
32
30
|
import {
|
|
33
31
|
createOfflineStargateClient,
|
|
34
32
|
createSigningStargateClient,
|
|
35
33
|
createStargateClient,
|
|
36
34
|
getDefaultChainFee,
|
|
37
35
|
} from "../util";
|
|
38
|
-
import {
|
|
36
|
+
import { createCosmosToolbox } from "./cosmos";
|
|
39
37
|
|
|
40
38
|
function secp256k1HdWalletFromMnemonic({
|
|
41
39
|
prefix,
|
|
42
40
|
derivationPath,
|
|
43
|
-
}: {
|
|
41
|
+
}: {
|
|
42
|
+
prefix: string;
|
|
43
|
+
derivationPath?: string;
|
|
44
|
+
}) {
|
|
44
45
|
return async function secp256k1HdWalletFromMnemonic(mnemonic: string, index = 0) {
|
|
45
|
-
const { Secp256k1HdWallet } = await import("@cosmjs/amino");
|
|
46
|
-
const { stringToPath } = await import("@cosmjs/crypto");
|
|
46
|
+
const { Secp256k1HdWallet } = (await import("@cosmjs/amino")).default;
|
|
47
|
+
const { stringToPath } = (await import("@cosmjs/crypto")).default;
|
|
47
48
|
|
|
48
49
|
return Secp256k1HdWallet.fromMnemonic(mnemonic, {
|
|
49
50
|
hdPaths: [stringToPath(`${derivationPath}/${index}`)],
|
|
@@ -56,65 +57,65 @@ function exportSignature(signature: Uint8Array) {
|
|
|
56
57
|
return base64.encode(signature);
|
|
57
58
|
}
|
|
58
59
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
) {
|
|
64
|
-
const {
|
|
65
|
-
msgs,
|
|
66
|
-
accountNumber,
|
|
67
|
-
sequence,
|
|
68
|
-
chainId,
|
|
69
|
-
fee,
|
|
70
|
-
memo,
|
|
60
|
+
function signMultisigTx(chain: Chain.THORChain | Chain.Maya) {
|
|
61
|
+
return async function signMultisigTx({
|
|
62
|
+
wallet,
|
|
63
|
+
tx,
|
|
71
64
|
}: {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
chainId
|
|
76
|
-
|
|
77
|
-
memo: string;
|
|
78
|
-
} = JSON.parse(tx);
|
|
79
|
-
|
|
80
|
-
const address = (await wallet.getAccounts())?.[0]?.address || "";
|
|
81
|
-
const aminoTypes = await createDefaultAminoTypes(chain);
|
|
82
|
-
const registry = await createDefaultRegistry();
|
|
83
|
-
const signingClient = await createOfflineStargateClient(wallet, { registry, aminoTypes });
|
|
84
|
-
const msgForSigning = [];
|
|
85
|
-
|
|
86
|
-
for (const msg of msgs) {
|
|
87
|
-
const signMsg = await convertToSignable(msg, chain);
|
|
88
|
-
msgForSigning.push(signMsg);
|
|
89
|
-
}
|
|
65
|
+
wallet: Secp256k1HdWallet;
|
|
66
|
+
tx: string | MultisigTx;
|
|
67
|
+
}) {
|
|
68
|
+
const { msgs, accountNumber, sequence, chainId, fee, memo } =
|
|
69
|
+
typeof tx === "string" ? JSON.parse(tx) : tx;
|
|
90
70
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
71
|
+
const address = (await wallet.getAccounts())?.[0]?.address || "";
|
|
72
|
+
const aminoTypes = await createDefaultAminoTypes(chain);
|
|
73
|
+
const registry = await createDefaultRegistry();
|
|
74
|
+
const signingClient = await createOfflineStargateClient(wallet, {
|
|
75
|
+
registry,
|
|
76
|
+
aminoTypes,
|
|
77
|
+
});
|
|
78
|
+
const msgForSigning = [];
|
|
98
79
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
80
|
+
for (const msg of msgs) {
|
|
81
|
+
const signMsg = await convertToSignable(msg, chain);
|
|
82
|
+
msgForSigning.push(signMsg);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const {
|
|
86
|
+
signatures: [signature],
|
|
87
|
+
} = await signingClient.sign(address, msgForSigning, fee, memo, {
|
|
88
|
+
accountNumber,
|
|
89
|
+
sequence,
|
|
90
|
+
chainId,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const bodyBytes = await buildEncodedTxBody({
|
|
94
|
+
chain,
|
|
95
|
+
memo,
|
|
96
|
+
msgs: msgs.map(parseAminoMessageForDirectSigning),
|
|
97
|
+
});
|
|
104
98
|
|
|
105
|
-
|
|
99
|
+
return { signature: exportSignature(signature as Uint8Array), bodyBytes };
|
|
100
|
+
};
|
|
106
101
|
}
|
|
107
102
|
|
|
108
|
-
function broadcastMultisigTx({
|
|
103
|
+
function broadcastMultisigTx({
|
|
104
|
+
prefix,
|
|
105
|
+
rpcUrl,
|
|
106
|
+
}: {
|
|
107
|
+
prefix: string;
|
|
108
|
+
rpcUrl: string;
|
|
109
|
+
}) {
|
|
109
110
|
return async function broadcastMultisigTx(
|
|
110
111
|
tx: string,
|
|
111
|
-
signers:
|
|
112
|
+
signers: MultiSigSigner[],
|
|
112
113
|
membersPubKeys: string[],
|
|
113
114
|
threshold: number,
|
|
114
115
|
bodyBytes: Uint8Array,
|
|
115
116
|
) {
|
|
116
|
-
const { encodeSecp256k1Pubkey, pubkeyToAddress } = await import("@cosmjs/amino");
|
|
117
|
-
const { makeMultisignedTxBytes } = await import("@cosmjs/stargate");
|
|
117
|
+
const { encodeSecp256k1Pubkey, pubkeyToAddress } = (await import("@cosmjs/amino")).default;
|
|
118
|
+
const { makeMultisignedTxBytes } = (await import("@cosmjs/stargate")).default;
|
|
118
119
|
|
|
119
120
|
const { sequence, fee } = JSON.parse(tx);
|
|
120
121
|
const multisigPubkey = await createMultisig(membersPubKeys, threshold);
|
|
@@ -141,7 +142,8 @@ function broadcastMultisigTx({ prefix, rpcUrl }: { prefix: string; rpcUrl: strin
|
|
|
141
142
|
}
|
|
142
143
|
|
|
143
144
|
async function createMultisig(pubKeys: string[], threshold: number, noSortPubKeys = true) {
|
|
144
|
-
const { createMultisigThresholdPubkey, encodeSecp256k1Pubkey } = await import("@cosmjs/amino")
|
|
145
|
+
const { createMultisigThresholdPubkey, encodeSecp256k1Pubkey } = (await import("@cosmjs/amino"))
|
|
146
|
+
.default;
|
|
145
147
|
return createMultisigThresholdPubkey(
|
|
146
148
|
pubKeys.map((pubKey) => encodeSecp256k1Pubkey(base64.decode(pubKey))),
|
|
147
149
|
threshold,
|
|
@@ -156,51 +158,46 @@ function importSignature(signature: string) {
|
|
|
156
158
|
async function signWithPrivateKey({
|
|
157
159
|
privateKey,
|
|
158
160
|
message,
|
|
159
|
-
}: {
|
|
160
|
-
|
|
161
|
+
}: {
|
|
162
|
+
privateKey: Uint8Array;
|
|
163
|
+
message: string;
|
|
164
|
+
}) {
|
|
165
|
+
const { Secp256k1 } = (await import("@cosmjs/crypto")).default;
|
|
161
166
|
|
|
162
167
|
const signature = await Secp256k1.createSignature(base64.decode(message), privateKey);
|
|
163
168
|
return base64.encode(Buffer.concat([signature.r(32), signature.s(32)]));
|
|
164
169
|
}
|
|
165
170
|
|
|
166
|
-
function
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
address,
|
|
171
|
-
}: {
|
|
172
|
-
signature: string;
|
|
173
|
-
message: string;
|
|
174
|
-
address: string;
|
|
175
|
-
}) {
|
|
176
|
-
const account = await getAccount(address);
|
|
177
|
-
if (!account?.pubkey) throw new SwapKitError("toolbox_cosmos_verify_signature_no_pubkey");
|
|
178
|
-
const { Secp256k1Signature, Secp256k1 } = await import("@cosmjs/crypto");
|
|
179
|
-
|
|
180
|
-
const secpSignature = Secp256k1Signature.fromFixedLength(base64.decode(signature));
|
|
181
|
-
return Secp256k1.verifySignature(secpSignature, base64.decode(message), account.pubkey.value);
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
export function BaseThorchainToolbox(chain: Chain.THORChain | Chain.Maya) {
|
|
171
|
+
export async function createThorchainToolbox({
|
|
172
|
+
chain,
|
|
173
|
+
...toolboxParams
|
|
174
|
+
}: CosmosToolboxParams<Chain.THORChain | Chain.Maya>) {
|
|
186
175
|
const nodeUrl = SKConfig.get("nodeUrls")[chain];
|
|
187
176
|
const rpcUrl = SKConfig.get("rpcUrls")[chain];
|
|
188
177
|
const { isStagenet } = SKConfig.get("envs");
|
|
189
|
-
|
|
190
178
|
const isThorchain = chain === Chain.THORChain;
|
|
191
179
|
const chainPrefix = `${isStagenet ? "s" : ""}${CosmosChainPrefixes[chain]}`;
|
|
192
|
-
const derivationPath = DerivationPath[chain];
|
|
193
180
|
|
|
194
|
-
const
|
|
195
|
-
const defaultFee = getDefaultChainFee(chain);
|
|
181
|
+
const index = "index" in toolboxParams ? toolboxParams.index || 0 : 0;
|
|
196
182
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
183
|
+
const derivationPath =
|
|
184
|
+
"derivationPath" in toolboxParams && toolboxParams.derivationPath
|
|
185
|
+
? toolboxParams.derivationPath
|
|
186
|
+
: updateDerivationPath(NetworkDerivationPath[chain], { index });
|
|
187
|
+
|
|
188
|
+
const cosmosToolbox = await createCosmosToolbox({
|
|
189
|
+
chain,
|
|
190
|
+
...toolboxParams,
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
const signer = await match(toolboxParams)
|
|
194
|
+
.with({ phrase: P.string }, ({ phrase }) =>
|
|
195
|
+
cosmosToolbox.getSignerFromPhrase({ phrase, derivationPath }),
|
|
196
|
+
)
|
|
197
|
+
.with({ signer: P.any }, ({ signer }) => signer)
|
|
198
|
+
.otherwise(() => undefined);
|
|
199
|
+
|
|
200
|
+
const defaultFee = getDefaultChainFee(chain);
|
|
204
201
|
|
|
205
202
|
async function getFees() {
|
|
206
203
|
let fee: SwapKitNumber;
|
|
@@ -218,21 +215,23 @@ export function BaseThorchainToolbox(chain: Chain.THORChain | Chain.Maya) {
|
|
|
218
215
|
|
|
219
216
|
fee = new SwapKitNumber(nativeFee);
|
|
220
217
|
} catch {
|
|
221
|
-
fee = new SwapKitNumber({
|
|
218
|
+
fee = new SwapKitNumber({
|
|
219
|
+
value: isThorchain ? 0.02 : 1,
|
|
220
|
+
decimal: BaseDecimal[chain],
|
|
221
|
+
});
|
|
222
222
|
}
|
|
223
223
|
|
|
224
224
|
return { [FeeOption.Average]: fee, [FeeOption.Fast]: fee, [FeeOption.Fastest]: fee };
|
|
225
225
|
}
|
|
226
226
|
|
|
227
227
|
async function transfer({
|
|
228
|
-
from,
|
|
229
|
-
recipient,
|
|
230
228
|
assetValue,
|
|
231
229
|
memo = "",
|
|
232
|
-
|
|
233
|
-
}: Omit<
|
|
230
|
+
recipient,
|
|
231
|
+
}: Omit<GenericTransferParams, "recipient"> & { recipient?: string }) {
|
|
234
232
|
const { TxRaw } = await import("cosmjs-types/cosmos/tx/v1beta1/tx");
|
|
235
|
-
|
|
233
|
+
const sender = (await signer?.getAccounts())?.[0]?.address;
|
|
234
|
+
if (!(sender && signer)) throw new Error("Signer not defined");
|
|
236
235
|
|
|
237
236
|
const isAminoSigner = "signAmino" in signer;
|
|
238
237
|
const registry = await createDefaultRegistry();
|
|
@@ -242,13 +241,18 @@ export function BaseThorchainToolbox(chain: Chain.THORChain | Chain.Maya) {
|
|
|
242
241
|
aminoTypes,
|
|
243
242
|
});
|
|
244
243
|
|
|
245
|
-
const aminoMessage = buildAminoMsg({
|
|
244
|
+
const aminoMessage = buildAminoMsg({
|
|
245
|
+
assetValue,
|
|
246
|
+
sender,
|
|
247
|
+
recipient,
|
|
248
|
+
memo,
|
|
249
|
+
});
|
|
246
250
|
|
|
247
251
|
if (isAminoSigner) {
|
|
248
252
|
const msgSign = await convertToSignable(aminoMessage, chain);
|
|
249
253
|
|
|
250
254
|
const { signatures, authInfoBytes } = await signingClient.sign(
|
|
251
|
-
|
|
255
|
+
sender,
|
|
252
256
|
[msgSign],
|
|
253
257
|
defaultFee,
|
|
254
258
|
memo,
|
|
@@ -271,51 +275,35 @@ export function BaseThorchainToolbox(chain: Chain.THORChain | Chain.Maya) {
|
|
|
271
275
|
|
|
272
276
|
const preparedMessage = parseAminoMessageForDirectSigning(aminoMessage);
|
|
273
277
|
const msgSign = await convertToSignable(preparedMessage, chain);
|
|
274
|
-
const txResponse = await signingClient.signAndBroadcast(
|
|
278
|
+
const txResponse = await signingClient.signAndBroadcast(sender, [msgSign], defaultFee, memo);
|
|
275
279
|
|
|
276
280
|
return txResponse.transactionHash;
|
|
277
281
|
}
|
|
278
282
|
|
|
279
283
|
return {
|
|
280
284
|
...cosmosToolbox,
|
|
281
|
-
|
|
282
|
-
pubkeyToAddress: async (pubkey: Pubkey) => {
|
|
283
|
-
const { pubkeyToAddress } = await import("@cosmjs/amino");
|
|
284
|
-
return pubkeyToAddress(pubkey, chainPrefix);
|
|
285
|
-
},
|
|
286
|
-
transfer,
|
|
287
|
-
getFees,
|
|
285
|
+
broadcastMultisigTx: broadcastMultisigTx({ prefix: chainPrefix, rpcUrl }),
|
|
288
286
|
buildAminoMsg,
|
|
289
|
-
convertToSignable,
|
|
290
|
-
buildDepositTx: buildDepositTx(rpcUrl),
|
|
291
|
-
buildTransferTx: buildTransferTx(rpcUrl),
|
|
292
287
|
buildEncodedTxBody,
|
|
293
|
-
|
|
288
|
+
convertToSignable,
|
|
294
289
|
createDefaultAminoTypes: () => createDefaultAminoTypes(chain),
|
|
295
290
|
createDefaultRegistry,
|
|
291
|
+
createMultisig,
|
|
292
|
+
createTransaction: getCreateTransaction(rpcUrl),
|
|
293
|
+
deposit: transfer,
|
|
294
|
+
getFees,
|
|
295
|
+
importSignature,
|
|
296
|
+
parseAminoMessageForDirectSigning,
|
|
296
297
|
secp256k1HdWalletFromMnemonic: secp256k1HdWalletFromMnemonic({
|
|
297
|
-
derivationPath,
|
|
298
|
+
derivationPath: derivationPathToString(derivationPath),
|
|
298
299
|
prefix: chainPrefix,
|
|
299
300
|
}),
|
|
300
|
-
signMultisigTx:
|
|
301
|
-
broadcastMultisigTx: broadcastMultisigTx({ prefix: chainPrefix, rpcUrl }),
|
|
302
|
-
createMultisig,
|
|
303
|
-
importSignature,
|
|
304
|
-
loadAddressBalances,
|
|
301
|
+
signMultisigTx: signMultisigTx(chain),
|
|
305
302
|
signWithPrivateKey,
|
|
306
|
-
|
|
303
|
+
transfer,
|
|
304
|
+
pubkeyToAddress: async (pubkey: Pubkey) => {
|
|
305
|
+
const { pubkeyToAddress } = (await import("@cosmjs/amino")).default;
|
|
306
|
+
return pubkeyToAddress(pubkey, chainPrefix);
|
|
307
|
+
},
|
|
307
308
|
};
|
|
308
309
|
}
|
|
309
|
-
|
|
310
|
-
export function ThorchainToolbox() {
|
|
311
|
-
return BaseThorchainToolbox(Chain.THORChain);
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
export function MayaToolbox() {
|
|
315
|
-
return BaseThorchainToolbox(Chain.Maya);
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
export type ThorchainWallet = Omit<ReturnType<typeof BaseThorchainToolbox>, "signMessage">;
|
|
319
|
-
export type ThorchainWallets = {
|
|
320
|
-
[chain in Chain.THORChain | Chain.Maya]: ThorchainWallet;
|
|
321
|
-
};
|
package/src/cosmos/types.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
import type { OfflineAminoSigner
|
|
2
|
-
import type { OfflineDirectSigner } from "@cosmjs/proto-signing";
|
|
3
|
-
import type {
|
|
1
|
+
import type { OfflineAminoSigner } from "@cosmjs/amino";
|
|
2
|
+
import type { DirectSecp256k1HdWallet, OfflineDirectSigner } from "@cosmjs/proto-signing";
|
|
3
|
+
import type { Chain, ChainId, CosmosChain, DerivationPathArray } from "@swapkit/helpers";
|
|
4
|
+
import type { buildAminoMsg } from "./thorchainUtils";
|
|
5
|
+
import type { createCosmosToolbox } from "./toolbox/cosmos";
|
|
6
|
+
import type { createThorchainToolbox } from "./toolbox/thorchain";
|
|
7
|
+
import type { getDefaultChainFee } from "./util";
|
|
4
8
|
|
|
5
9
|
export type CosmosSDKClientParams = {
|
|
6
10
|
server: string;
|
|
@@ -9,23 +13,38 @@ export type CosmosSDKClientParams = {
|
|
|
9
13
|
stagenet?: boolean;
|
|
10
14
|
};
|
|
11
15
|
|
|
12
|
-
export type
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
feeOptionKey?: FeeOption;
|
|
16
|
-
from: string;
|
|
17
|
-
memo?: string;
|
|
18
|
-
privkey?: Uint8Array;
|
|
19
|
-
recipient: string;
|
|
20
|
-
signer?: OfflineDirectSigner | OfflineAminoSigner;
|
|
16
|
+
export type MultiSigSigner = {
|
|
17
|
+
pubKey: string;
|
|
18
|
+
signature: string;
|
|
21
19
|
};
|
|
22
20
|
|
|
23
|
-
export type
|
|
24
|
-
|
|
25
|
-
|
|
21
|
+
export type MultisigTx = {
|
|
22
|
+
msgs: ReturnType<typeof buildAminoMsg>[];
|
|
23
|
+
accountNumber: number;
|
|
24
|
+
sequence: number;
|
|
25
|
+
chainId: ChainId;
|
|
26
|
+
fee: ReturnType<typeof getDefaultChainFee>;
|
|
27
|
+
memo: string;
|
|
26
28
|
};
|
|
27
29
|
|
|
28
|
-
export type
|
|
29
|
-
|
|
30
|
-
|
|
30
|
+
export type CosmosSigner = DirectSecp256k1HdWallet | OfflineDirectSigner | OfflineAminoSigner;
|
|
31
|
+
|
|
32
|
+
export type CosmosToolboxParams<T = CosmosChain> = {
|
|
33
|
+
chain: T;
|
|
34
|
+
} & (
|
|
35
|
+
| { signer?: CosmosSigner }
|
|
36
|
+
| { phrase?: string; derivationPath?: DerivationPathArray; index?: number }
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
export type BaseCosmosToolboxType = ReturnType<typeof createCosmosToolbox>;
|
|
40
|
+
export type BaseCosmosWallet = Awaited<ReturnType<typeof createCosmosToolbox>>;
|
|
41
|
+
export type CosmosWallets = {
|
|
42
|
+
[chain in Chain.Cosmos | Chain.Kujira]: BaseCosmosWallet;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export type ThorchainWallet = Awaited<
|
|
46
|
+
Omit<ReturnType<typeof createThorchainToolbox>, "signMessage">
|
|
47
|
+
>;
|
|
48
|
+
export type ThorchainWallets = {
|
|
49
|
+
[chain in Chain.THORChain | Chain.Maya]: ThorchainWallet;
|
|
31
50
|
};
|
package/src/cosmos/util.ts
CHANGED
|
@@ -1,16 +1,8 @@
|
|
|
1
1
|
import type { OfflineSigner } from "@cosmjs/proto-signing";
|
|
2
2
|
import type { SigningStargateClientOptions } from "@cosmjs/stargate";
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
ChainId,
|
|
7
|
-
type CosmosChain,
|
|
8
|
-
SKConfig,
|
|
9
|
-
defaultRequestHeaders,
|
|
10
|
-
getGasAsset,
|
|
11
|
-
} from "@swapkit/helpers";
|
|
12
|
-
|
|
13
|
-
import type { CosmosNativeTransferTxParams } from "./thorchainUtils";
|
|
3
|
+
import { AssetValue, Chain, ChainId, type CosmosChain, SKConfig } from "@swapkit/helpers";
|
|
4
|
+
|
|
5
|
+
import type { CosmosCreateTransactionParams } from "./thorchainUtils";
|
|
14
6
|
|
|
15
7
|
export const USK_KUJIRA_FACTORY_DENOM =
|
|
16
8
|
"FACTORY/KUJIRA1QK00H5ATUTPSV900X202PXX42NPJR9THG58DNQPA72F2P7M2LUASE444A7/UUSK";
|
|
@@ -76,55 +68,10 @@ export const getDenomWithChain = ({ symbol, chain }: AssetValue) => {
|
|
|
76
68
|
return getMsgSendDenom(symbol, false);
|
|
77
69
|
};
|
|
78
70
|
|
|
79
|
-
// TODO: figure out some better way to initialize from base value
|
|
80
|
-
export const getAssetFromDenom = (denom: string, amount: string) => {
|
|
81
|
-
switch (denom) {
|
|
82
|
-
case "rune":
|
|
83
|
-
return AssetValue.from({
|
|
84
|
-
chain: Chain.THORChain,
|
|
85
|
-
value: Number.parseInt(amount) / 1e8,
|
|
86
|
-
});
|
|
87
|
-
case "uatom":
|
|
88
|
-
case "atom":
|
|
89
|
-
return AssetValue.from({
|
|
90
|
-
chain: Chain.Cosmos,
|
|
91
|
-
value: Number.parseInt(amount) / 1e6,
|
|
92
|
-
});
|
|
93
|
-
case "cacao":
|
|
94
|
-
return AssetValue.from({
|
|
95
|
-
chain: Chain.Maya,
|
|
96
|
-
value: Number.parseInt(amount) / 1e10,
|
|
97
|
-
});
|
|
98
|
-
case "maya":
|
|
99
|
-
return AssetValue.from({
|
|
100
|
-
asset: `${Chain.Maya}.${Chain.Maya}`,
|
|
101
|
-
value: Number.parseInt(amount) / 1e4,
|
|
102
|
-
});
|
|
103
|
-
case "ukuji":
|
|
104
|
-
case "kuji":
|
|
105
|
-
return AssetValue.from({
|
|
106
|
-
chain: Chain.Kujira,
|
|
107
|
-
value: Number.parseInt(amount) / 1e6,
|
|
108
|
-
});
|
|
109
|
-
case USK_KUJIRA_FACTORY_DENOM:
|
|
110
|
-
// USK on Kujira
|
|
111
|
-
return AssetValue.from({
|
|
112
|
-
asset: `${Chain.Kujira}.USK`,
|
|
113
|
-
value: Number.parseInt(amount) / 1e6,
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
default:
|
|
117
|
-
return AssetValue.from({
|
|
118
|
-
asset: denom,
|
|
119
|
-
value: Number.parseInt(amount) / 1e8,
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
|
|
124
71
|
export async function createStargateClient(url: string) {
|
|
125
|
-
const { StargateClient } = await import("@cosmjs/stargate");
|
|
72
|
+
const { StargateClient } = (await import("@cosmjs/stargate")).default;
|
|
126
73
|
|
|
127
|
-
return StargateClient.connect(
|
|
74
|
+
return StargateClient.connect(url);
|
|
128
75
|
}
|
|
129
76
|
|
|
130
77
|
export async function createSigningStargateClient(
|
|
@@ -132,11 +79,11 @@ export async function createSigningStargateClient(
|
|
|
132
79
|
signer: any,
|
|
133
80
|
optionsOrBaseGas: string | SigningStargateClientOptions = {},
|
|
134
81
|
) {
|
|
135
|
-
const { SigningStargateClient, GasPrice } = await import("@cosmjs/stargate");
|
|
82
|
+
const { SigningStargateClient, GasPrice } = (await import("@cosmjs/stargate")).default;
|
|
136
83
|
const gasPrice = typeof optionsOrBaseGas === "string" ? optionsOrBaseGas : "0.0003uatom";
|
|
137
84
|
const options = typeof optionsOrBaseGas === "string" ? {} : optionsOrBaseGas;
|
|
138
85
|
|
|
139
|
-
return SigningStargateClient.connectWithSigner(
|
|
86
|
+
return SigningStargateClient.connectWithSigner(url, signer, {
|
|
140
87
|
gasPrice: GasPrice.fromString(gasPrice),
|
|
141
88
|
...options,
|
|
142
89
|
});
|
|
@@ -146,7 +93,7 @@ export async function createOfflineStargateClient(
|
|
|
146
93
|
wallet: OfflineSigner,
|
|
147
94
|
registry?: SigningStargateClientOptions,
|
|
148
95
|
) {
|
|
149
|
-
const { SigningStargateClient } = await import("@cosmjs/stargate");
|
|
96
|
+
const { SigningStargateClient } = (await import("@cosmjs/stargate")).default;
|
|
150
97
|
|
|
151
98
|
return SigningStargateClient.offline(wallet, registry);
|
|
152
99
|
}
|
|
@@ -186,45 +133,48 @@ const getTransferMsgTypeByChain = (chain: CosmosChain) => {
|
|
|
186
133
|
/**
|
|
187
134
|
* Used to build tx for Cosmos and Kujira
|
|
188
135
|
*/
|
|
189
|
-
export const
|
|
190
|
-
|
|
191
|
-
|
|
136
|
+
export const cosmosCreateTransaction = async ({
|
|
137
|
+
sender,
|
|
138
|
+
recipient,
|
|
192
139
|
assetValue,
|
|
193
140
|
memo = "",
|
|
194
|
-
|
|
195
|
-
|
|
141
|
+
feeRate,
|
|
142
|
+
sequence,
|
|
143
|
+
accountNumber,
|
|
144
|
+
}: CosmosCreateTransactionParams) => {
|
|
196
145
|
const { chain, chainId } = assetValue;
|
|
197
146
|
|
|
198
147
|
const url = getRPC(chainId);
|
|
199
148
|
const client = await createStargateClient(url);
|
|
200
|
-
const accountOnChain = await client.getAccount(
|
|
149
|
+
const accountOnChain = await client.getAccount(sender);
|
|
201
150
|
|
|
202
151
|
if (!accountOnChain) {
|
|
203
152
|
throw new Error("Account does not exist");
|
|
204
153
|
}
|
|
205
154
|
|
|
206
|
-
const
|
|
155
|
+
const gasAsset = AssetValue.from({ chain });
|
|
156
|
+
const feeAsset = getMsgSendDenom(gasAsset.symbol);
|
|
207
157
|
const defaultFee = getDefaultChainFee(chain as CosmosChain);
|
|
208
158
|
|
|
209
159
|
const txFee =
|
|
210
|
-
feeAsset &&
|
|
211
|
-
? { amount: [{ denom: feeAsset, amount:
|
|
160
|
+
feeAsset && feeRate
|
|
161
|
+
? { amount: [{ denom: feeAsset, amount: feeRate.toString() }], gas: defaultFee.gas }
|
|
212
162
|
: defaultFee;
|
|
213
163
|
|
|
214
164
|
const msgSend = {
|
|
215
|
-
fromAddress,
|
|
216
|
-
toAddress,
|
|
165
|
+
fromAddress: sender,
|
|
166
|
+
toAddress: recipient,
|
|
217
167
|
amount: [
|
|
218
168
|
{ amount: assetValue.getBaseValue("string"), denom: getMsgSendDenom(assetValue.symbol) },
|
|
219
169
|
],
|
|
220
170
|
};
|
|
221
171
|
|
|
222
172
|
return {
|
|
223
|
-
accountNumber: accountOnChain.accountNumber,
|
|
173
|
+
accountNumber: accountNumber ?? accountOnChain.accountNumber,
|
|
224
174
|
chainId,
|
|
225
175
|
fee: txFee,
|
|
226
176
|
memo,
|
|
227
|
-
sequence: accountOnChain.sequence,
|
|
177
|
+
sequence: sequence ?? accountOnChain.sequence,
|
|
228
178
|
msgs: [{ typeUrl: getTransferMsgTypeByChain(chain as CosmosChain), value: msgSend }],
|
|
229
179
|
};
|
|
230
180
|
};
|