-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.ts
More file actions
79 lines (67 loc) · 2.85 KB
/
helper.ts
File metadata and controls
79 lines (67 loc) · 2.85 KB
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
import {pubkeyToAddress, Tendermint34Client} from "@cosmjs/tendermint-rpc";
import {
createProtobufRpcClient,
QueryClient,
SigningStargateClient
} from "@cosmjs/stargate";
import {anyToSinglePubkey, decodePubkey, DirectSecp256k1HdWallet, Registry} from "@cosmjs/proto-signing";
import {defaultRegistryTypes as defaultStargateTypes} from "@cosmjs/stargate/build/signingstargateclient.js";
import {registry as liquidstakeibcRegistry} from "persistenceonejs/pstake/liquidstakeibc/v1beta1/msgs.registry.js";
import {MNEMONIC} from "./constants.js";
import {Long} from "cosmjs-types/helpers";
import {fromBase64, fromBech32, toBech32} from "@cosmjs/encoding";
import {sha256} from "@cosmjs/crypto";
export const CustomRegistry = new Registry([...defaultStargateTypes, ...liquidstakeibcRegistry]);
export async function RpcClient(rpc) {
const tendermintClient = await Tendermint34Client.connect(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)
}