@swapkit/toolboxes 4.0.0-beta.50 → 4.0.0-beta.52
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/src/cosmos/index.cjs +2 -2
- package/dist/src/cosmos/index.cjs.map +10 -6
- package/dist/src/cosmos/index.js +2 -2
- package/dist/src/cosmos/index.js.map +10 -6
- package/dist/src/evm/index.cjs +2 -2
- package/dist/src/evm/index.cjs.map +10 -8
- package/dist/src/evm/index.js +2 -2
- package/dist/src/evm/index.js.map +10 -8
- package/dist/src/index.cjs +4 -2
- package/dist/src/index.cjs.map +54 -4
- package/dist/src/index.js +4 -2
- package/dist/src/index.js.map +54 -4
- package/dist/src/near/index.cjs +2 -2
- package/dist/src/near/index.cjs.map +7 -5
- package/dist/src/near/index.js +2 -2
- package/dist/src/near/index.js.map +7 -5
- package/dist/src/radix/index.cjs +2 -2
- package/dist/src/radix/index.cjs.map +2 -2
- package/dist/src/radix/index.js +2 -2
- package/dist/src/radix/index.js.map +2 -2
- package/dist/src/ripple/index.cjs +2 -2
- package/dist/src/ripple/index.cjs.map +2 -2
- package/dist/src/ripple/index.js +2 -2
- package/dist/src/ripple/index.js.map +2 -2
- package/dist/src/solana/index.cjs +2 -2
- package/dist/src/solana/index.cjs.map +5 -4
- package/dist/src/solana/index.js +2 -2
- package/dist/src/solana/index.js.map +5 -4
- package/dist/src/substrate/index.cjs +2 -2
- package/dist/src/substrate/index.cjs.map +6 -5
- package/dist/src/substrate/index.js +2 -2
- package/dist/src/substrate/index.js.map +6 -5
- package/dist/src/tron/index.cjs +2 -2
- package/dist/src/tron/index.cjs.map +6 -5
- package/dist/src/tron/index.js +2 -2
- package/dist/src/tron/index.js.map +6 -5
- package/dist/src/utxo/index.cjs +4 -4
- package/dist/src/utxo/index.cjs.map +10 -7
- package/dist/src/utxo/index.js +4 -4
- package/dist/src/utxo/index.js.map +10 -7
- package/{src/cosmos/index.ts → dist/types/cosmos/index.d.ts} +1 -0
- package/dist/types/cosmos/index.d.ts.map +1 -0
- package/dist/types/cosmos/thorchainUtils/addressFormat.d.ts +5 -0
- package/dist/types/cosmos/thorchainUtils/addressFormat.d.ts.map +1 -0
- package/{src/cosmos/thorchainUtils/index.ts → dist/types/cosmos/thorchainUtils/index.d.ts} +1 -0
- package/dist/types/cosmos/thorchainUtils/index.d.ts.map +1 -0
- package/dist/types/cosmos/thorchainUtils/messages.d.ts +208 -0
- package/dist/types/cosmos/thorchainUtils/messages.d.ts.map +1 -0
- package/dist/types/cosmos/thorchainUtils/registry.d.ts +4 -0
- package/dist/types/cosmos/thorchainUtils/registry.d.ts.map +1 -0
- package/dist/types/cosmos/thorchainUtils/types/MsgCompiled.d.ts +2 -0
- package/dist/types/cosmos/thorchainUtils/types/MsgCompiled.d.ts.map +1 -0
- package/dist/types/cosmos/thorchainUtils/types/client-types.d.ts +66 -0
- package/dist/types/cosmos/thorchainUtils/types/client-types.d.ts.map +1 -0
- package/dist/types/cosmos/thorchainUtils/types/index.d.ts +2 -0
- package/dist/types/cosmos/thorchainUtils/types/index.d.ts.map +1 -0
- package/dist/types/cosmos/toolbox/cosmos.d.ts +93 -0
- package/dist/types/cosmos/toolbox/cosmos.d.ts.map +1 -0
- package/dist/types/cosmos/toolbox/index.d.ts +14 -0
- package/dist/types/cosmos/toolbox/index.d.ts.map +1 -0
- package/dist/types/cosmos/toolbox/thorchain.d.ts +158 -0
- package/dist/types/cosmos/toolbox/thorchain.d.ts.map +1 -0
- package/{src/cosmos/types.ts → dist/types/cosmos/types.d.ts} +24 -29
- package/dist/types/cosmos/types.d.ts.map +1 -0
- package/dist/types/cosmos/util.d.ts +68 -0
- package/dist/types/cosmos/util.d.ts.map +1 -0
- package/dist/types/evm/api.d.ts +8 -0
- package/dist/types/evm/api.d.ts.map +1 -0
- package/dist/types/evm/contracts/eth/multicall.d.ts +36 -0
- package/dist/types/evm/contracts/eth/multicall.d.ts.map +1 -0
- package/dist/types/evm/contracts/op/gasOracle.d.ts +40 -0
- package/dist/types/evm/contracts/op/gasOracle.d.ts.map +1 -0
- package/dist/types/evm/helpers.d.ts +20 -0
- package/dist/types/evm/helpers.d.ts.map +1 -0
- package/{src/evm/index.ts → dist/types/evm/index.d.ts} +1 -0
- package/dist/types/evm/index.d.ts.map +1 -0
- package/dist/types/evm/toolbox/baseEVMToolbox.d.ts +82 -0
- package/dist/types/evm/toolbox/baseEVMToolbox.d.ts.map +1 -0
- package/dist/types/evm/toolbox/evm.d.ts +367 -0
- package/dist/types/evm/toolbox/evm.d.ts.map +1 -0
- package/dist/types/evm/toolbox/index.d.ts +89 -0
- package/dist/types/evm/toolbox/index.d.ts.map +1 -0
- package/dist/types/evm/toolbox/op.d.ts +62 -0
- package/dist/types/evm/toolbox/op.d.ts.map +1 -0
- package/dist/types/evm/types.d.ts +98 -0
- package/dist/types/evm/types.d.ts.map +1 -0
- package/dist/types/index.d.ts +63 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/near/helpers/core.d.ts +15 -0
- package/dist/types/near/helpers/core.d.ts.map +1 -0
- package/dist/types/near/helpers/gasEstimation.d.ts +41 -0
- package/dist/types/near/helpers/gasEstimation.d.ts.map +1 -0
- package/dist/types/near/helpers/nep141.d.ts +72 -0
- package/dist/types/near/helpers/nep141.d.ts.map +1 -0
- package/dist/types/near/index.d.ts +10 -0
- package/dist/types/near/index.d.ts.map +1 -0
- package/dist/types/near/toolbox.d.ts +4 -0
- package/dist/types/near/toolbox.d.ts.map +1 -0
- package/dist/types/near/types/contract.d.ts +38 -0
- package/dist/types/near/types/contract.d.ts.map +1 -0
- package/dist/types/near/types/nep141.d.ts +56 -0
- package/dist/types/near/types/nep141.d.ts.map +1 -0
- package/dist/types/near/types/toolbox.d.ts +57 -0
- package/dist/types/near/types/toolbox.d.ts.map +1 -0
- package/dist/types/near/types.d.ts +44 -0
- package/dist/types/near/types.d.ts.map +1 -0
- package/dist/types/radix/index.d.ts +14 -0
- package/dist/types/radix/index.d.ts.map +1 -0
- package/dist/types/ripple/index.d.ts +43 -0
- package/dist/types/ripple/index.d.ts.map +1 -0
- package/dist/types/solana/index.d.ts +37 -0
- package/dist/types/solana/index.d.ts.map +1 -0
- package/dist/types/solana/toolbox.d.ts +41 -0
- package/dist/types/solana/toolbox.d.ts.map +1 -0
- package/dist/types/substrate/balance.d.ts +17 -0
- package/dist/types/substrate/balance.d.ts.map +1 -0
- package/{src/substrate/index.ts → dist/types/substrate/index.d.ts} +1 -0
- package/dist/types/substrate/index.d.ts.map +1 -0
- package/dist/types/substrate/substrate.d.ts +148 -0
- package/dist/types/substrate/substrate.d.ts.map +1 -0
- package/dist/types/substrate/types.d.ts +100 -0
- package/dist/types/substrate/types.d.ts.map +1 -0
- package/dist/types/tron/helpers/trc20.abi.d.ts +156 -0
- package/dist/types/tron/helpers/trc20.abi.d.ts.map +1 -0
- package/dist/types/tron/helpers/trongrid.d.ts +8 -0
- package/dist/types/tron/helpers/trongrid.d.ts.map +1 -0
- package/dist/types/tron/index.d.ts +6 -0
- package/dist/types/tron/index.d.ts.map +1 -0
- package/dist/types/tron/toolbox.d.ts +26 -0
- package/dist/types/tron/toolbox.d.ts.map +1 -0
- package/dist/types/tron/types.d.ts +101 -0
- package/dist/types/tron/types.d.ts.map +1 -0
- package/dist/types/types.d.ts +18 -0
- package/dist/types/types.d.ts.map +1 -0
- package/dist/types/utils.d.ts +4 -0
- package/dist/types/utils.d.ts.map +1 -0
- package/dist/types/utxo/helpers/api.d.ts +133 -0
- package/dist/types/utxo/helpers/api.d.ts.map +1 -0
- package/dist/types/utxo/helpers/bchaddrjs.d.ts +10 -0
- package/dist/types/utxo/helpers/bchaddrjs.d.ts.map +1 -0
- package/dist/types/utxo/helpers/coinselect.d.ts +16 -0
- package/dist/types/utxo/helpers/coinselect.d.ts.map +1 -0
- package/{src/utxo/helpers/index.ts → dist/types/utxo/helpers/index.d.ts} +1 -0
- package/dist/types/utxo/helpers/index.d.ts.map +1 -0
- package/dist/types/utxo/helpers/txSize.d.ts +21 -0
- package/dist/types/utxo/helpers/txSize.d.ts.map +1 -0
- package/{src/utxo/index.ts → dist/types/utxo/index.d.ts} +1 -0
- package/dist/types/utxo/index.d.ts.map +1 -0
- package/dist/types/utxo/toolbox/bitcoinCash.d.ts +104 -0
- package/dist/types/utxo/toolbox/bitcoinCash.d.ts.map +1 -0
- package/dist/types/utxo/toolbox/index.d.ts +50 -0
- package/dist/types/utxo/toolbox/index.d.ts.map +1 -0
- package/dist/types/utxo/toolbox/utxo.d.ts +102 -0
- package/dist/types/utxo/toolbox/utxo.d.ts.map +1 -0
- package/dist/types/utxo/toolbox/zcash.d.ts +83 -0
- package/dist/types/utxo/toolbox/zcash.d.ts.map +1 -0
- package/dist/types/utxo/types.d.ts +46 -0
- package/dist/types/utxo/types.d.ts.map +1 -0
- package/package.json +15 -18
- package/dist/chunk-0h4xdrwz.js +0 -5
- package/dist/chunk-0h4xdrwz.js.map +0 -10
- package/dist/chunk-4yap1fvd.js +0 -4
- package/dist/chunk-4yap1fvd.js.map +0 -10
- package/dist/chunk-9bqegm61.js +0 -4
- package/dist/chunk-9bqegm61.js.map +0 -10
- package/dist/chunk-fazw0jvt.js +0 -4
- package/dist/chunk-fazw0jvt.js.map +0 -9
- package/dist/chunk-fjfxga2v.js +0 -4
- package/dist/chunk-fjfxga2v.js.map +0 -10
- package/dist/chunk-s47y8512.js +0 -5
- package/dist/chunk-s47y8512.js.map +0 -9
- package/dist/chunk-vtd17cje.js +0 -4
- package/dist/chunk-vtd17cje.js.map +0 -10
- package/dist/chunk-zcdeg6h9.js +0 -5
- package/dist/chunk-zcdeg6h9.js.map +0 -10
- package/src/cosmos/thorchainUtils/addressFormat.ts +0 -26
- package/src/cosmos/thorchainUtils/messages.ts +0 -262
- package/src/cosmos/thorchainUtils/registry.ts +0 -44
- package/src/cosmos/thorchainUtils/types/MsgCompiled.ts +0 -2800
- package/src/cosmos/thorchainUtils/types/client-types.ts +0 -73
- package/src/cosmos/thorchainUtils/types/index.ts +0 -1
- package/src/cosmos/toolbox/cosmos.ts +0 -375
- package/src/cosmos/toolbox/index.ts +0 -33
- package/src/cosmos/toolbox/thorchain.ts +0 -313
- package/src/cosmos/util.ts +0 -266
- package/src/evm/__tests__/address-validation.test.ts +0 -86
- package/src/evm/__tests__/ethereum.test.ts +0 -141
- package/src/evm/api.ts +0 -21
- package/src/evm/contracts/eth/multicall.ts +0 -165
- package/src/evm/contracts/op/gasOracle.ts +0 -151
- package/src/evm/helpers.ts +0 -194
- package/src/evm/toolbox/baseEVMToolbox.ts +0 -762
- package/src/evm/toolbox/evm.ts +0 -66
- package/src/evm/toolbox/index.ts +0 -52
- package/src/evm/toolbox/op.ts +0 -131
- package/src/evm/types.ts +0 -146
- package/src/index.ts +0 -263
- package/src/near/__tests__/core.test.ts +0 -80
- package/src/near/helpers/contractFactory.ts +0 -22
- package/src/near/helpers/core.ts +0 -91
- package/src/near/helpers/gasEstimation.ts +0 -110
- package/src/near/helpers/index.ts +0 -5
- package/src/near/helpers/nep141.ts +0 -110
- package/src/near/index.ts +0 -24
- package/src/near/toolbox.ts +0 -509
- package/src/near/types/contract.ts +0 -48
- package/src/near/types/nep141.ts +0 -66
- package/src/near/types.ts +0 -57
- package/src/radix/index.ts +0 -156
- package/src/ripple/index.ts +0 -192
- package/src/solana/index.ts +0 -55
- package/src/solana/toolbox.ts +0 -433
- package/src/substrate/balance.ts +0 -92
- package/src/substrate/substrate.ts +0 -320
- package/src/substrate/types.ts +0 -120
- package/src/tron/__tests__/toolbox.test.ts +0 -147
- package/src/tron/helpers/trc20.abi.ts +0 -107
- package/src/tron/helpers/trongrid.ts +0 -54
- package/src/tron/index.ts +0 -17
- package/src/tron/toolbox.ts +0 -650
- package/src/tron/types.ts +0 -120
- package/src/utils.ts +0 -27
- package/src/utxo/__tests__/zcash-integration.test.ts +0 -114
- package/src/utxo/helpers/api.ts +0 -560
- package/src/utxo/helpers/bchaddrjs.ts +0 -183
- package/src/utxo/helpers/coinselect.ts +0 -98
- package/src/utxo/helpers/txSize.ts +0 -104
- package/src/utxo/toolbox/bitcoinCash.ts +0 -320
- package/src/utxo/toolbox/index.ts +0 -90
- package/src/utxo/toolbox/utxo.ts +0 -525
- package/src/utxo/toolbox/zcash.ts +0 -208
- package/src/utxo/types.ts +0 -57
|
@@ -1,313 +0,0 @@
|
|
|
1
|
-
import type { Pubkey, Secp256k1HdWallet } from "@cosmjs/amino";
|
|
2
|
-
import { base64 } from "@scure/base";
|
|
3
|
-
import {
|
|
4
|
-
BaseDecimal,
|
|
5
|
-
Chain,
|
|
6
|
-
CosmosChainPrefixes,
|
|
7
|
-
FeeOption,
|
|
8
|
-
type GenericTransferParams,
|
|
9
|
-
NetworkDerivationPath,
|
|
10
|
-
RequestClient,
|
|
11
|
-
SKConfig,
|
|
12
|
-
SwapKitError,
|
|
13
|
-
SwapKitNumber,
|
|
14
|
-
derivationPathToString,
|
|
15
|
-
updateDerivationPath,
|
|
16
|
-
} from "@swapkit/helpers";
|
|
17
|
-
|
|
18
|
-
import { P, match } from "ts-pattern";
|
|
19
|
-
import {
|
|
20
|
-
buildAminoMsg,
|
|
21
|
-
buildEncodedTxBody,
|
|
22
|
-
convertToSignable,
|
|
23
|
-
createDefaultAminoTypes,
|
|
24
|
-
createDefaultRegistry,
|
|
25
|
-
getCreateTransaction,
|
|
26
|
-
parseAminoMessageForDirectSigning,
|
|
27
|
-
} from "../thorchainUtils";
|
|
28
|
-
import type { ThorchainConstantsResponse } from "../thorchainUtils/types/client-types";
|
|
29
|
-
import type { MultisigTx } from "../types";
|
|
30
|
-
import type { CosmosToolboxParams, MultiSigSigner } from "../types";
|
|
31
|
-
import {
|
|
32
|
-
createOfflineStargateClient,
|
|
33
|
-
createSigningStargateClient,
|
|
34
|
-
createStargateClient,
|
|
35
|
-
getDefaultChainFee,
|
|
36
|
-
} from "../util";
|
|
37
|
-
import { createCosmosToolbox } from "./cosmos";
|
|
38
|
-
|
|
39
|
-
function secp256k1HdWalletFromMnemonic({
|
|
40
|
-
prefix,
|
|
41
|
-
derivationPath,
|
|
42
|
-
}: {
|
|
43
|
-
prefix: string;
|
|
44
|
-
derivationPath?: string;
|
|
45
|
-
}) {
|
|
46
|
-
return async function secp256k1HdWalletFromMnemonic(mnemonic: string, index = 0) {
|
|
47
|
-
const importedAmino = await import("@cosmjs/amino");
|
|
48
|
-
const Secp256k1HdWallet =
|
|
49
|
-
importedAmino.Secp256k1HdWallet ?? importedAmino.default?.Secp256k1HdWallet;
|
|
50
|
-
const importedCrypto = await import("@cosmjs/crypto");
|
|
51
|
-
const stringToPath = importedCrypto.stringToPath ?? importedCrypto.default?.stringToPath;
|
|
52
|
-
|
|
53
|
-
return Secp256k1HdWallet.fromMnemonic(mnemonic, {
|
|
54
|
-
hdPaths: [stringToPath(`${derivationPath}/${index}`)],
|
|
55
|
-
prefix,
|
|
56
|
-
});
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
function exportSignature(signature: Uint8Array) {
|
|
61
|
-
return base64.encode(signature);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function signMultisigTx(chain: Chain.THORChain | Chain.Maya) {
|
|
65
|
-
return async function signMultisigTx({
|
|
66
|
-
wallet,
|
|
67
|
-
tx,
|
|
68
|
-
}: {
|
|
69
|
-
wallet: Secp256k1HdWallet;
|
|
70
|
-
tx: string | MultisigTx;
|
|
71
|
-
}) {
|
|
72
|
-
const { msgs, accountNumber, sequence, chainId, fee, memo } =
|
|
73
|
-
typeof tx === "string" ? JSON.parse(tx) : tx;
|
|
74
|
-
|
|
75
|
-
const address = (await wallet.getAccounts())?.[0]?.address || "";
|
|
76
|
-
const aminoTypes = await createDefaultAminoTypes(chain);
|
|
77
|
-
const registry = await createDefaultRegistry();
|
|
78
|
-
const signingClient = await createOfflineStargateClient(wallet, {
|
|
79
|
-
registry,
|
|
80
|
-
aminoTypes,
|
|
81
|
-
});
|
|
82
|
-
const msgForSigning = [];
|
|
83
|
-
|
|
84
|
-
for (const msg of msgs) {
|
|
85
|
-
const signMsg = await convertToSignable(msg, chain);
|
|
86
|
-
msgForSigning.push(signMsg);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const {
|
|
90
|
-
signatures: [signature],
|
|
91
|
-
} = await signingClient.sign(address, msgForSigning, fee, memo, {
|
|
92
|
-
accountNumber,
|
|
93
|
-
sequence,
|
|
94
|
-
chainId,
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
const bodyBytes = await buildEncodedTxBody({
|
|
98
|
-
chain,
|
|
99
|
-
memo,
|
|
100
|
-
msgs: msgs.map(parseAminoMessageForDirectSigning),
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
return { signature: exportSignature(signature as Uint8Array), bodyBytes };
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function broadcastMultisigTx({
|
|
108
|
-
prefix,
|
|
109
|
-
rpcUrl,
|
|
110
|
-
}: {
|
|
111
|
-
prefix: string;
|
|
112
|
-
rpcUrl: string;
|
|
113
|
-
}) {
|
|
114
|
-
return async function broadcastMultisigTx(
|
|
115
|
-
tx: string,
|
|
116
|
-
signers: MultiSigSigner[],
|
|
117
|
-
membersPubKeys: string[],
|
|
118
|
-
threshold: number,
|
|
119
|
-
bodyBytes: Uint8Array,
|
|
120
|
-
) {
|
|
121
|
-
const { encodeSecp256k1Pubkey, pubkeyToAddress } = (await import("@cosmjs/amino")).default;
|
|
122
|
-
const { makeMultisignedTxBytes } = (await import("@cosmjs/stargate")).default;
|
|
123
|
-
|
|
124
|
-
const { sequence, fee } = JSON.parse(tx);
|
|
125
|
-
const multisigPubkey = await createMultisig(membersPubKeys, threshold);
|
|
126
|
-
|
|
127
|
-
const addressesAndSignatures: [string, Uint8Array][] = signers.map((signer) => [
|
|
128
|
-
pubkeyToAddress(encodeSecp256k1Pubkey(base64.decode(signer.pubKey)), prefix),
|
|
129
|
-
base64.decode(signer.signature),
|
|
130
|
-
]);
|
|
131
|
-
|
|
132
|
-
const broadcaster = await createStargateClient(rpcUrl);
|
|
133
|
-
|
|
134
|
-
const { transactionHash } = await broadcaster.broadcastTx(
|
|
135
|
-
makeMultisignedTxBytes(
|
|
136
|
-
multisigPubkey,
|
|
137
|
-
sequence,
|
|
138
|
-
fee,
|
|
139
|
-
bodyBytes,
|
|
140
|
-
new Map<string, Uint8Array>(addressesAndSignatures),
|
|
141
|
-
),
|
|
142
|
-
);
|
|
143
|
-
|
|
144
|
-
return transactionHash;
|
|
145
|
-
};
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
async function createMultisig(pubKeys: string[], threshold: number, noSortPubKeys = true) {
|
|
149
|
-
const { createMultisigThresholdPubkey, encodeSecp256k1Pubkey } = (await import("@cosmjs/amino"))
|
|
150
|
-
.default;
|
|
151
|
-
return createMultisigThresholdPubkey(
|
|
152
|
-
pubKeys.map((pubKey) => encodeSecp256k1Pubkey(base64.decode(pubKey))),
|
|
153
|
-
threshold,
|
|
154
|
-
noSortPubKeys,
|
|
155
|
-
);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
function importSignature(signature: string) {
|
|
159
|
-
return base64.decode(signature);
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
async function signWithPrivateKey({
|
|
163
|
-
privateKey,
|
|
164
|
-
message,
|
|
165
|
-
}: {
|
|
166
|
-
privateKey: Uint8Array;
|
|
167
|
-
message: string;
|
|
168
|
-
}) {
|
|
169
|
-
const { Secp256k1 } = (await import("@cosmjs/crypto")).default;
|
|
170
|
-
|
|
171
|
-
const signature = await Secp256k1.createSignature(base64.decode(message), privateKey);
|
|
172
|
-
return base64.encode(Buffer.concat([signature.r(32), signature.s(32)]));
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
export async function createThorchainToolbox({
|
|
176
|
-
chain,
|
|
177
|
-
...toolboxParams
|
|
178
|
-
}: CosmosToolboxParams<Chain.THORChain | Chain.Maya>) {
|
|
179
|
-
const nodeUrl = SKConfig.get("nodeUrls")[chain];
|
|
180
|
-
const rpcUrl = SKConfig.get("rpcUrls")[chain];
|
|
181
|
-
const { isStagenet } = SKConfig.get("envs");
|
|
182
|
-
const isThorchain = chain === Chain.THORChain;
|
|
183
|
-
const chainPrefix = `${isStagenet ? "s" : ""}${CosmosChainPrefixes[chain]}`;
|
|
184
|
-
|
|
185
|
-
const index = "index" in toolboxParams ? toolboxParams.index || 0 : 0;
|
|
186
|
-
|
|
187
|
-
const derivationPath =
|
|
188
|
-
"derivationPath" in toolboxParams && toolboxParams.derivationPath
|
|
189
|
-
? toolboxParams.derivationPath
|
|
190
|
-
: updateDerivationPath(NetworkDerivationPath[chain], { index });
|
|
191
|
-
|
|
192
|
-
const cosmosToolbox = await createCosmosToolbox({
|
|
193
|
-
chain,
|
|
194
|
-
...toolboxParams,
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
const signer = await match(toolboxParams)
|
|
198
|
-
.with({ phrase: P.string }, ({ phrase }) =>
|
|
199
|
-
cosmosToolbox.getSignerFromPhrase({ phrase, derivationPath }),
|
|
200
|
-
)
|
|
201
|
-
.with({ signer: P.any }, ({ signer }) => signer)
|
|
202
|
-
.otherwise(() => undefined);
|
|
203
|
-
|
|
204
|
-
const defaultFee = getDefaultChainFee(chain);
|
|
205
|
-
|
|
206
|
-
async function getFees() {
|
|
207
|
-
let fee: SwapKitNumber;
|
|
208
|
-
|
|
209
|
-
const constantsUrl = `${nodeUrl}/${isThorchain ? "thorchain" : "mayachain"}/constants`;
|
|
210
|
-
|
|
211
|
-
try {
|
|
212
|
-
const {
|
|
213
|
-
int_64_values: { NativeTransactionFee: nativeFee },
|
|
214
|
-
} = await RequestClient.get<ThorchainConstantsResponse>(constantsUrl);
|
|
215
|
-
|
|
216
|
-
if (!nativeFee || Number.isNaN(nativeFee) || nativeFee < 0) {
|
|
217
|
-
throw new SwapKitError("toolbox_cosmos_invalid_fee", { nativeFee: nativeFee.toString() });
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
fee = new SwapKitNumber(nativeFee);
|
|
221
|
-
} catch {
|
|
222
|
-
fee = new SwapKitNumber({
|
|
223
|
-
value: isThorchain ? 0.02 : 1,
|
|
224
|
-
decimal: BaseDecimal[chain],
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
return { [FeeOption.Average]: fee, [FeeOption.Fast]: fee, [FeeOption.Fastest]: fee };
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
async function transfer({
|
|
232
|
-
assetValue,
|
|
233
|
-
memo = "",
|
|
234
|
-
recipient,
|
|
235
|
-
}: Omit<GenericTransferParams, "recipient"> & { recipient?: string }) {
|
|
236
|
-
const { TxRaw } = await import("cosmjs-types/cosmos/tx/v1beta1/tx");
|
|
237
|
-
const sender = (await signer?.getAccounts())?.[0]?.address;
|
|
238
|
-
if (!(sender && signer)) throw new SwapKitError("toolbox_cosmos_no_signer");
|
|
239
|
-
|
|
240
|
-
const isAminoSigner = "signAmino" in signer;
|
|
241
|
-
const registry = await createDefaultRegistry();
|
|
242
|
-
const aminoTypes = await createDefaultAminoTypes(chain);
|
|
243
|
-
const signingClient = await createSigningStargateClient(rpcUrl, signer, {
|
|
244
|
-
registry,
|
|
245
|
-
aminoTypes,
|
|
246
|
-
});
|
|
247
|
-
|
|
248
|
-
const aminoMessage = buildAminoMsg({
|
|
249
|
-
assetValue,
|
|
250
|
-
sender,
|
|
251
|
-
recipient,
|
|
252
|
-
memo,
|
|
253
|
-
});
|
|
254
|
-
|
|
255
|
-
if (isAminoSigner) {
|
|
256
|
-
const msgSign = await convertToSignable(aminoMessage, chain);
|
|
257
|
-
|
|
258
|
-
const { signatures, authInfoBytes } = await signingClient.sign(
|
|
259
|
-
sender,
|
|
260
|
-
[msgSign],
|
|
261
|
-
defaultFee,
|
|
262
|
-
memo,
|
|
263
|
-
);
|
|
264
|
-
|
|
265
|
-
const tx = TxRaw.encode({
|
|
266
|
-
signatures,
|
|
267
|
-
authInfoBytes,
|
|
268
|
-
bodyBytes: await buildEncodedTxBody({
|
|
269
|
-
chain,
|
|
270
|
-
msgs: [aminoMessage].map(parseAminoMessageForDirectSigning),
|
|
271
|
-
memo,
|
|
272
|
-
}),
|
|
273
|
-
}).finish();
|
|
274
|
-
|
|
275
|
-
const txResponse = await signingClient.broadcastTx(tx);
|
|
276
|
-
|
|
277
|
-
return txResponse.transactionHash;
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
const preparedMessage = parseAminoMessageForDirectSigning(aminoMessage);
|
|
281
|
-
const msgSign = await convertToSignable(preparedMessage, chain);
|
|
282
|
-
const txResponse = await signingClient.signAndBroadcast(sender, [msgSign], defaultFee, memo);
|
|
283
|
-
|
|
284
|
-
return txResponse.transactionHash;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
return {
|
|
288
|
-
...cosmosToolbox,
|
|
289
|
-
broadcastMultisigTx: broadcastMultisigTx({ prefix: chainPrefix, rpcUrl }),
|
|
290
|
-
buildAminoMsg,
|
|
291
|
-
buildEncodedTxBody,
|
|
292
|
-
convertToSignable,
|
|
293
|
-
createDefaultAminoTypes: () => createDefaultAminoTypes(chain),
|
|
294
|
-
createDefaultRegistry,
|
|
295
|
-
createMultisig,
|
|
296
|
-
createTransaction: getCreateTransaction(rpcUrl),
|
|
297
|
-
deposit: transfer,
|
|
298
|
-
getFees,
|
|
299
|
-
importSignature,
|
|
300
|
-
parseAminoMessageForDirectSigning,
|
|
301
|
-
secp256k1HdWalletFromMnemonic: secp256k1HdWalletFromMnemonic({
|
|
302
|
-
derivationPath: derivationPathToString(derivationPath),
|
|
303
|
-
prefix: chainPrefix,
|
|
304
|
-
}),
|
|
305
|
-
signMultisigTx: signMultisigTx(chain),
|
|
306
|
-
signWithPrivateKey,
|
|
307
|
-
transfer,
|
|
308
|
-
pubkeyToAddress: async (pubkey: Pubkey) => {
|
|
309
|
-
const { pubkeyToAddress } = (await import("@cosmjs/amino")).default;
|
|
310
|
-
return pubkeyToAddress(pubkey, chainPrefix);
|
|
311
|
-
},
|
|
312
|
-
};
|
|
313
|
-
}
|
package/src/cosmos/util.ts
DELETED
|
@@ -1,266 +0,0 @@
|
|
|
1
|
-
import type { OfflineSigner } from "@cosmjs/proto-signing";
|
|
2
|
-
import type { SigningStargateClientOptions } from "@cosmjs/stargate";
|
|
3
|
-
import {
|
|
4
|
-
AssetValue,
|
|
5
|
-
BaseDecimal,
|
|
6
|
-
Chain,
|
|
7
|
-
ChainId,
|
|
8
|
-
type CosmosChain,
|
|
9
|
-
SKConfig,
|
|
10
|
-
SwapKitError,
|
|
11
|
-
} from "@swapkit/helpers";
|
|
12
|
-
|
|
13
|
-
import type { CosmosCreateTransactionParams } from "./thorchainUtils";
|
|
14
|
-
|
|
15
|
-
export const USK_KUJIRA_FACTORY_DENOM =
|
|
16
|
-
"FACTORY/KUJIRA1QK00H5ATUTPSV900X202PXX42NPJR9THG58DNQPA72F2P7M2LUASE444A7/UUSK";
|
|
17
|
-
|
|
18
|
-
export const YUM_KUJIRA_FACTORY_DENOM =
|
|
19
|
-
"FACTORY/KUJIRA1YGFXN0ER40KLCNCK8THLTUPRDXLCK6WVNPKF2K/UYUM";
|
|
20
|
-
|
|
21
|
-
export const DEFAULT_COSMOS_FEE_MAINNET = {
|
|
22
|
-
amount: [{ denom: "uatom", amount: "500" }],
|
|
23
|
-
gas: "200000",
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
export const DEFAULT_KUJI_FEE_MAINNET = {
|
|
27
|
-
amount: [{ denom: "ukuji", amount: "1000" }],
|
|
28
|
-
gas: "200000",
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
export function getDefaultChainFee(chain: CosmosChain) {
|
|
32
|
-
switch (chain) {
|
|
33
|
-
case Chain.Maya:
|
|
34
|
-
return { amount: [], gas: "10000000000" };
|
|
35
|
-
case Chain.THORChain:
|
|
36
|
-
return { amount: [], gas: "500000000" };
|
|
37
|
-
case Chain.Kujira:
|
|
38
|
-
return DEFAULT_KUJI_FEE_MAINNET;
|
|
39
|
-
default:
|
|
40
|
-
return DEFAULT_COSMOS_FEE_MAINNET;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export const getMsgSendDenom = (symbol: string, isThorchain = false) => {
|
|
45
|
-
if (isThorchain) {
|
|
46
|
-
return symbol.toLowerCase();
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
switch (symbol) {
|
|
50
|
-
case "uUSK":
|
|
51
|
-
case "USK":
|
|
52
|
-
return USK_KUJIRA_FACTORY_DENOM;
|
|
53
|
-
case "uYUM":
|
|
54
|
-
case "YUM":
|
|
55
|
-
return YUM_KUJIRA_FACTORY_DENOM;
|
|
56
|
-
case "uKUJI":
|
|
57
|
-
case "KUJI":
|
|
58
|
-
return "ukuji";
|
|
59
|
-
case "ATOM":
|
|
60
|
-
case "uATOM":
|
|
61
|
-
return "uatom";
|
|
62
|
-
default:
|
|
63
|
-
return symbol;
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
export const getDenomWithChain = ({ symbol, chain }: AssetValue) => {
|
|
68
|
-
if (chain === Chain.Maya) {
|
|
69
|
-
return (symbol.toUpperCase() !== "CACAO" ? symbol : `${Chain.Maya}.${symbol}`).toUpperCase();
|
|
70
|
-
}
|
|
71
|
-
if (chain === Chain.THORChain) {
|
|
72
|
-
return (
|
|
73
|
-
["RUNE", "TCY", "RUJI"].includes(symbol.toUpperCase())
|
|
74
|
-
? `${Chain.THORChain}.${symbol}`
|
|
75
|
-
: symbol
|
|
76
|
-
).toUpperCase();
|
|
77
|
-
}
|
|
78
|
-
return getMsgSendDenom(symbol, false);
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
export async function createStargateClient(url: string) {
|
|
82
|
-
const imported = await import("@cosmjs/stargate");
|
|
83
|
-
|
|
84
|
-
const StargateClient = imported.StargateClient ?? imported.default?.StargateClient;
|
|
85
|
-
|
|
86
|
-
const defaultRequestHeaders =
|
|
87
|
-
typeof window !== "undefined"
|
|
88
|
-
? ({} as Record<string, string>)
|
|
89
|
-
: { referrer: "https://sdk.swapkit.dev", referer: "https://sdk.swapkit.dev" };
|
|
90
|
-
|
|
91
|
-
return StargateClient.connect({
|
|
92
|
-
url,
|
|
93
|
-
headers: defaultRequestHeaders,
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export async function createSigningStargateClient(
|
|
98
|
-
url: string,
|
|
99
|
-
signer: OfflineSigner,
|
|
100
|
-
optionsOrBaseGas: string | SigningStargateClientOptions = {},
|
|
101
|
-
) {
|
|
102
|
-
const imported = await import("@cosmjs/stargate");
|
|
103
|
-
const SigningStargateClient =
|
|
104
|
-
imported.SigningStargateClient ?? imported.default?.SigningStargateClient;
|
|
105
|
-
const GasPrice = imported.GasPrice ?? imported.default?.GasPrice;
|
|
106
|
-
const gasPrice = typeof optionsOrBaseGas === "string" ? optionsOrBaseGas : "0.0003uatom";
|
|
107
|
-
const options = typeof optionsOrBaseGas === "string" ? {} : optionsOrBaseGas;
|
|
108
|
-
|
|
109
|
-
return SigningStargateClient.connectWithSigner(url, signer, {
|
|
110
|
-
gasPrice: GasPrice.fromString(gasPrice),
|
|
111
|
-
...options,
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
export async function createOfflineStargateClient(
|
|
116
|
-
wallet: OfflineSigner,
|
|
117
|
-
registry?: SigningStargateClientOptions,
|
|
118
|
-
) {
|
|
119
|
-
const imported = await import("@cosmjs/stargate");
|
|
120
|
-
const SigningStargateClient =
|
|
121
|
-
imported.SigningStargateClient ?? imported.default?.SigningStargateClient;
|
|
122
|
-
return SigningStargateClient.offline(wallet, registry);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
export const getRPC = (chainId: ChainId) => {
|
|
126
|
-
const { isStagenet } = SKConfig.get("envs");
|
|
127
|
-
const rpcUrls = SKConfig.get("rpcUrls");
|
|
128
|
-
|
|
129
|
-
switch (chainId) {
|
|
130
|
-
case ChainId.Kujira:
|
|
131
|
-
return rpcUrls.KUJI;
|
|
132
|
-
|
|
133
|
-
case ChainId.THORChain:
|
|
134
|
-
case "thorchain-mainnet-v1" as ChainId:
|
|
135
|
-
return isStagenet ? rpcUrls.THOR_STAGENET : rpcUrls.THOR;
|
|
136
|
-
case ChainId.Maya:
|
|
137
|
-
return isStagenet ? rpcUrls.MAYA_STAGENET : rpcUrls.MAYA;
|
|
138
|
-
|
|
139
|
-
default:
|
|
140
|
-
return rpcUrls.GAIA;
|
|
141
|
-
}
|
|
142
|
-
};
|
|
143
|
-
|
|
144
|
-
const getTransferMsgTypeByChain = (chain: CosmosChain) => {
|
|
145
|
-
switch (chain) {
|
|
146
|
-
case Chain.Maya:
|
|
147
|
-
case Chain.THORChain:
|
|
148
|
-
return "/types.MsgSend";
|
|
149
|
-
case Chain.Cosmos:
|
|
150
|
-
case Chain.Kujira:
|
|
151
|
-
return "/cosmos.bank.v1beta1.MsgSend";
|
|
152
|
-
default:
|
|
153
|
-
throw new SwapKitError("toolbox_cosmos_not_supported", { chain });
|
|
154
|
-
}
|
|
155
|
-
};
|
|
156
|
-
|
|
157
|
-
/**
|
|
158
|
-
* Used to build tx for Cosmos and Kujira
|
|
159
|
-
*/
|
|
160
|
-
export const cosmosCreateTransaction = async ({
|
|
161
|
-
sender,
|
|
162
|
-
recipient,
|
|
163
|
-
assetValue,
|
|
164
|
-
memo = "",
|
|
165
|
-
feeRate,
|
|
166
|
-
sequence,
|
|
167
|
-
accountNumber,
|
|
168
|
-
}: CosmosCreateTransactionParams) => {
|
|
169
|
-
const { chain, chainId } = assetValue;
|
|
170
|
-
|
|
171
|
-
const url = getRPC(chainId);
|
|
172
|
-
const client = await createStargateClient(url);
|
|
173
|
-
const accountOnChain = await client.getAccount(sender);
|
|
174
|
-
|
|
175
|
-
if (!accountOnChain) {
|
|
176
|
-
throw new SwapKitError("toolbox_cosmos_account_not_found", { sender });
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
const gasAsset = AssetValue.from({ chain });
|
|
180
|
-
const feeAsset = getMsgSendDenom(gasAsset.symbol);
|
|
181
|
-
const defaultFee = getDefaultChainFee(chain as CosmosChain);
|
|
182
|
-
|
|
183
|
-
const txFee =
|
|
184
|
-
feeAsset && feeRate
|
|
185
|
-
? { amount: [{ denom: feeAsset, amount: feeRate.toString() }], gas: defaultFee.gas }
|
|
186
|
-
: defaultFee;
|
|
187
|
-
|
|
188
|
-
const msgSend = {
|
|
189
|
-
fromAddress: sender,
|
|
190
|
-
toAddress: recipient,
|
|
191
|
-
amount: [
|
|
192
|
-
{ amount: assetValue.getBaseValue("string"), denom: getMsgSendDenom(assetValue.symbol) },
|
|
193
|
-
],
|
|
194
|
-
};
|
|
195
|
-
|
|
196
|
-
return {
|
|
197
|
-
accountNumber: accountNumber ?? accountOnChain.accountNumber,
|
|
198
|
-
chainId,
|
|
199
|
-
fee: txFee,
|
|
200
|
-
memo,
|
|
201
|
-
sequence: sequence ?? accountOnChain.sequence,
|
|
202
|
-
msgs: [{ typeUrl: getTransferMsgTypeByChain(chain as CosmosChain), value: msgSend }],
|
|
203
|
-
};
|
|
204
|
-
};
|
|
205
|
-
|
|
206
|
-
// Map of known denoms to their asset configurations
|
|
207
|
-
const DENOM_MAP = {
|
|
208
|
-
// THORChain denoms
|
|
209
|
-
rune: { chain: Chain.THORChain, decimals: BaseDecimal[Chain.THORChain] },
|
|
210
|
-
tcy: { asset: "THOR.TCY", decimals: BaseDecimal[Chain.THORChain] },
|
|
211
|
-
"x/kuji": { asset: "THOR.KUJI", decimals: BaseDecimal[Chain.THORChain] },
|
|
212
|
-
|
|
213
|
-
// Cosmos denoms
|
|
214
|
-
uatom: { chain: Chain.Cosmos, decimals: BaseDecimal[Chain.Cosmos] },
|
|
215
|
-
atom: { chain: Chain.Cosmos, decimals: BaseDecimal[Chain.Cosmos] },
|
|
216
|
-
|
|
217
|
-
// Maya denoms
|
|
218
|
-
cacao: { chain: Chain.Maya, decimals: 10 }, // Maya uses 10 decimals for CACAO
|
|
219
|
-
maya: { asset: `${Chain.Maya}.${Chain.Maya}`, decimals: 4 }, // MAYA token uses 4 decimals
|
|
220
|
-
|
|
221
|
-
// Kujira denoms
|
|
222
|
-
ukuji: { chain: Chain.Kujira, decimals: BaseDecimal[Chain.Kujira] },
|
|
223
|
-
kuji: { chain: Chain.Kujira, decimals: BaseDecimal[Chain.Kujira] },
|
|
224
|
-
|
|
225
|
-
// USK on Kujira (lowercase version of the factory denom)
|
|
226
|
-
[USK_KUJIRA_FACTORY_DENOM.toLowerCase()]: {
|
|
227
|
-
asset: `${Chain.Kujira}.USK`,
|
|
228
|
-
decimals: BaseDecimal[Chain.Kujira],
|
|
229
|
-
},
|
|
230
|
-
};
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
* Converts a Cosmos denom and amount to an AssetValue with proper decimal handling
|
|
234
|
-
* @param denom - The denomination string
|
|
235
|
-
* @param amount - The amount in base units as a string
|
|
236
|
-
* @returns AssetValue with the correct decimal conversion
|
|
237
|
-
*/
|
|
238
|
-
export const getAssetFromDenom = (denom: string, amount: string) => {
|
|
239
|
-
const config = DENOM_MAP[denom.toLowerCase()];
|
|
240
|
-
|
|
241
|
-
if (!config) {
|
|
242
|
-
// For unknown denoms, default to 8 decimals (common for many Cosmos chains)
|
|
243
|
-
// This preserves the original behavior while using fromBaseDecimal
|
|
244
|
-
return AssetValue.from({
|
|
245
|
-
asset: denom,
|
|
246
|
-
value: amount,
|
|
247
|
-
fromBaseDecimal: 8,
|
|
248
|
-
});
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
const { chain, asset, decimals } = config;
|
|
252
|
-
|
|
253
|
-
const assetOrChain = (
|
|
254
|
-
chain
|
|
255
|
-
? {
|
|
256
|
-
chain,
|
|
257
|
-
}
|
|
258
|
-
: { asset }
|
|
259
|
-
) as { asset: string } | { chain: CosmosChain };
|
|
260
|
-
|
|
261
|
-
return AssetValue.from({
|
|
262
|
-
...assetOrChain,
|
|
263
|
-
value: amount,
|
|
264
|
-
fromBaseDecimal: decimals,
|
|
265
|
-
});
|
|
266
|
-
};
|
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
import { beforeAll, describe, expect, test } from "bun:test";
|
|
2
|
-
import { Chain } from "@swapkit/helpers";
|
|
3
|
-
import { getEvmToolbox } from "../toolbox";
|
|
4
|
-
|
|
5
|
-
const context: {
|
|
6
|
-
validateAddress: (address: string) => boolean;
|
|
7
|
-
} = {} as any;
|
|
8
|
-
|
|
9
|
-
beforeAll(async () => {
|
|
10
|
-
// Get EVM toolbox for address validation
|
|
11
|
-
const toolbox = await getEvmToolbox(Chain.Ethereum);
|
|
12
|
-
context.validateAddress = toolbox.validateAddress;
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
describe("EVM Address Validation", () => {
|
|
16
|
-
test("should validate valid EVM addresses", () => {
|
|
17
|
-
const validAddresses = [
|
|
18
|
-
"0xa052Ddf1c1739419B90FB7bf722843AD3e63114B", // User provided address
|
|
19
|
-
"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC contract
|
|
20
|
-
"0x6d6e022eE439C8aB8B7a7dBb0576f8090319CDc6", // Test address
|
|
21
|
-
"0xE29E61479420Dd1029A9946710Ac31A0d140e77F", // Another valid address
|
|
22
|
-
"0x0000000000000000000000000000000000000000", // Zero address
|
|
23
|
-
"0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF", // Max address
|
|
24
|
-
"0x1234567890123456789012345678901234567890", // Mixed case
|
|
25
|
-
];
|
|
26
|
-
|
|
27
|
-
for (const address of validAddresses) {
|
|
28
|
-
expect(context.validateAddress(address)).toBe(true);
|
|
29
|
-
}
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
test("should reject invalid EVM addresses", () => {
|
|
33
|
-
const invalidAddresses = [
|
|
34
|
-
"", // Empty string
|
|
35
|
-
"invalid", // Random string
|
|
36
|
-
"TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t", // TRON address
|
|
37
|
-
"1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa", // Bitcoin address
|
|
38
|
-
"cosmos1abc123", // Cosmos address
|
|
39
|
-
"0xa052Ddf1c1739419B90FB7bf722843AD3e63114", // Too short (missing 1 char)
|
|
40
|
-
"0xa052Ddf1c1739419B90FB7bf722843AD3e63114BB", // Too long (extra char)
|
|
41
|
-
"0XA052DDF1C1739419B90FB7BF722843AD3E63114B", // Uppercase 0X prefix
|
|
42
|
-
"0xG052Ddf1c1739419B90FB7bf722843AD3e63114B", // Invalid hex character
|
|
43
|
-
"0x", // Only prefix
|
|
44
|
-
];
|
|
45
|
-
|
|
46
|
-
for (const address of invalidAddresses) {
|
|
47
|
-
expect(context.validateAddress(address)).toBe(false);
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
test("should handle case normalization properly", () => {
|
|
52
|
-
const address = "0xa052Ddf1c1739419B90FB7bf722843AD3e63114B"; // Proper checksum
|
|
53
|
-
const lowerCase = address.toLowerCase();
|
|
54
|
-
|
|
55
|
-
// Valid case variations should be accepted
|
|
56
|
-
expect(context.validateAddress(address)).toBe(true);
|
|
57
|
-
expect(context.validateAddress(lowerCase)).toBe(true);
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
test("should reject invalid checksum addresses", () => {
|
|
61
|
-
const invalidChecksumAddress = "0xA052dDF1C1739419b90fb7BF722843ad3E63114b"; // Invalid checksum
|
|
62
|
-
|
|
63
|
-
// Should reject mixed case with invalid checksum
|
|
64
|
-
expect(context.validateAddress(invalidChecksumAddress)).toBe(false);
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
test("should handle edge cases", () => {
|
|
68
|
-
const edgeCases = [null, undefined, 123, {}, [], true, false];
|
|
69
|
-
|
|
70
|
-
for (const testCase of edgeCases) {
|
|
71
|
-
// Should not throw but return false for invalid inputs
|
|
72
|
-
expect(context.validateAddress(testCase as any)).toBe(false);
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
test("should validate checksummed addresses", () => {
|
|
77
|
-
const checksummedAddresses = [
|
|
78
|
-
"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC with proper checksum
|
|
79
|
-
"0x6d6e022eE439C8aB8B7a7dBb0576f8090319CDc6", // Another checksummed address
|
|
80
|
-
];
|
|
81
|
-
|
|
82
|
-
for (const address of checksummedAddresses) {
|
|
83
|
-
expect(context.validateAddress(address)).toBe(true);
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
});
|