-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.js
116 lines (100 loc) · 4.34 KB
/
helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import {Tendermint34Client, Tendermint37Client} from "@cosmjs/tendermint-rpc";
import {createProtobufRpcClient, QueryClient, SigningStargateClient} from "@cosmjs/stargate";
import {decodePubkey, DirectSecp256k1HdWallet, Registry} from "@cosmjs/proto-signing";
import {defaultRegistryTypes as defaultStargateTypes} from "@cosmjs/stargate/build/signingstargateclient.js";
import {registry as liquidstakeRegistry} from "persistenceonejs/pstake/liquidstake/v1beta1/tx.registry.js";
import {registry as liquidstakeibcRegistry} from "persistenceonejs/pstake/liquidstakeibc/v1beta1/msgs.registry.js";
import {registry as lscosmosRegistry} from "persistenceonejs/pstake/lscosmos/v1beta1/msgs.registry.js";
import {registry as govv1Registry} from "persistenceonejs/cosmos/gov/v1/tx.registry.js";
import {COMETBFT_VERSIONS, MNEMONIC} from "./constants.js";
import {Long} from "cosmjs-types/helpers";
import {fromBase64, fromBech32, toBech32} from "@cosmjs/encoding";
import {sha256} from "@cosmjs/crypto";
import {buildQuery} from "@cosmjs/tendermint-rpc/build/tendermint34/requests.js";
export const CustomRegistry = new Registry([...defaultStargateTypes, ...liquidstakeRegistry,
...liquidstakeibcRegistry, ...lscosmosRegistry, ...govv1Registry]);
export async function RpcClient(chainInfo) {
let tendermintClient = {}
switch (chainInfo.tmVersion) {
case COMETBFT_VERSIONS.comet34:
tendermintClient = await Tendermint34Client.connect(chainInfo.rpc);
case COMETBFT_VERSIONS.comet37:
tendermintClient = await Tendermint37Client.connect(chainInfo.rpc);
case COMETBFT_VERSIONS.comet38:
tendermintClient = await Tendermint37Client.connect(chainInfo.rpc);
}
const queryClient = new QueryClient(tendermintClient);
return [tendermintClient, createProtobufRpcClient(queryClient)];
}
export async function CreateWallet(address) {
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(MNEMONIC, {
prefix: address.prefix,
hdPaths: [address.hdPath]
});
const [firstAccount] = await wallet.getAccounts();
if (firstAccount.address !== address.address) {
throw new Error("Incorrect address generated, expected: " + address.address + ",got: " + firstAccount.address);
}
return [wallet, firstAccount]
}
export async function CreateSigningClient(chainInfo, wallet) {
return await SigningStargateClient.connectWithSigner(chainInfo.rpc, wallet, {
prefix: chainInfo.prefix,
registry: CustomRegistry,
gasPrice: chainInfo.gasPrice,
})
}
export async function CreateSigningClientFromAddress(address) {
const [wallet, _] = await CreateWallet(address)
return CreateSigningClient(address.chainInfo, wallet)
}
export async function AllPaginatedQuery(queryFunc, queryArgs, aggregationKey) {
let key = new Uint8Array();
let totalElements = [];
do {
queryArgs.pagination = {
key: key,
offset: Long.fromNumber(0, true),
limit: Long.fromNumber(0, true),
countTotal: true
}
const response = await queryFunc(queryArgs)
key = response.pagination.nextKey;
totalElements.push(...response[aggregationKey]);
} while (key.length !== 0);
return totalElements
}
export function ChangeAddressPrefix(address, prefix) {
let decoded = fromBech32(address)
return toBech32(prefix, decoded.data)
}
export function ValidatorPubkeyToBech32(validatorPubKey, prefix) {
let valConsPubKey = decodePubkey(validatorPubKey)
const ed25519PubkeyRaw = fromBase64(valConsPubKey.value);
const addressData = sha256(ed25519PubkeyRaw).slice(0, 20);
return toBech32(prefix, addressData)
}
export function txSearchParams(tags, pageNumber, perPage) {
// tags = [{key: "message.sender", value: "persistence1xxx"}]
return {
query: buildQuery({
tags: tags,
}),
page: pageNumber,
per_page: perPage,
order_by: "desc" //latest first
}
}
export function stringifyJson(data) {
return JSON.stringify(data, (key, value) =>
typeof value === "bigint" ? value.toString() + "b" : value
);
}
export function parseJson(json) {
return JSON.parse(json, (key, value) => {
if (typeof value === "string" && /^\d+b$/.test(value)) {
return BigInt(value.substr(0, value.length - 1));
}
return value;
})
}