Merge pull request #104 from cosmos/fix/use-sig-crash
Check if pubkey is multisig
This commit is contained in:
commit
4e98b1bcfb
@ -1,8 +1,10 @@
|
||||
import { StargateClient } from "@cosmjs/stargate";
|
||||
import { assert } from "@cosmjs/utils";
|
||||
import { NextRouter, withRouter } from "next/router";
|
||||
import { useState } from "react";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useAppContext } from "../../context/AppContext";
|
||||
import { exampleAddress } from "../../lib/displayHelpers";
|
||||
import { getMultisigAccount } from "../../lib/multisigHelpers";
|
||||
import Button from "../inputs/Button";
|
||||
import Input from "../inputs/Input";
|
||||
import StackableContainer from "../layout/StackableContainer";
|
||||
@ -14,11 +16,36 @@ interface Props {
|
||||
const FindMultisigForm = (props: Props) => {
|
||||
const { state } = useAppContext();
|
||||
const [address, setAddress] = useState("");
|
||||
const [multisigError, setMultisigError] = useState("");
|
||||
|
||||
const handleSearch = () => {
|
||||
props.router.push(`/multi/${address}`);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
(async function () {
|
||||
if (!address) {
|
||||
setMultisigError("");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
assert(state.chain.nodeAddress, "Node address missing");
|
||||
const client = await StargateClient.connect(state.chain.nodeAddress);
|
||||
assert(state.chain.addressPrefix, "addressPrefix missing");
|
||||
await getMultisigAccount(address, state.chain.addressPrefix, client);
|
||||
setMultisigError("");
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
setMultisigError(error.message);
|
||||
} else {
|
||||
setMultisigError("Multisig error");
|
||||
}
|
||||
console.error("Multisig error:", error);
|
||||
}
|
||||
})();
|
||||
}, [address, state.chain.addressPrefix, state.chain.nodeAddress]);
|
||||
|
||||
assert(state.chain.addressPrefix, "addressPrefix missing");
|
||||
|
||||
return (
|
||||
@ -36,8 +63,14 @@ const FindMultisigForm = (props: Props) => {
|
||||
label="Multisig Address"
|
||||
name="address"
|
||||
placeholder={`E.g. ${exampleAddress(0, state.chain.addressPrefix)}`}
|
||||
error={multisigError}
|
||||
/>
|
||||
<Button
|
||||
label="Use this Multisig"
|
||||
onClick={handleSearch}
|
||||
primary
|
||||
disabled={!address || !!multisigError}
|
||||
/>
|
||||
<Button label="Use this Multisig" onClick={handleSearch} primary />
|
||||
</StackableContainer>
|
||||
<StackableContainer lessPadding>
|
||||
<p className="create-help">Don't have a multisig?</p>
|
||||
|
||||
@ -1,13 +1,13 @@
|
||||
import axios from "axios";
|
||||
import {
|
||||
createMultisigThresholdPubkey,
|
||||
isMultisigThresholdPubkey,
|
||||
MultisigThresholdPubkey,
|
||||
pubkeyToAddress,
|
||||
} from "@cosmjs/amino";
|
||||
import { Account } from "@cosmjs/stargate";
|
||||
import { StargateClient } from "@cosmjs/stargate";
|
||||
import { Account, StargateClient } from "@cosmjs/stargate";
|
||||
import { assert } from "@cosmjs/utils";
|
||||
import axios from "axios";
|
||||
import { checkAddress } from "./displayHelpers";
|
||||
|
||||
/**
|
||||
* Turns array of compressed Secp256k1 pubkeys
|
||||
@ -56,12 +56,18 @@ const createMultisigFromCompressedSecp256k1Pubkeys = async (
|
||||
*/
|
||||
const getMultisigAccount = async (
|
||||
address: string,
|
||||
addressPrefix: string,
|
||||
client: StargateClient,
|
||||
): Promise<[MultisigThresholdPubkey, Account | null]> => {
|
||||
// we need the multisig pubkeys to create transactions, if the multisig
|
||||
// is new, and has never submitted a transaction its pubkeys will not be
|
||||
// available from a node. If the multisig was created with this instance
|
||||
// of this tool its pubkey will be available in the fauna datastore
|
||||
const addressError = checkAddress(address, addressPrefix);
|
||||
if (addressError) {
|
||||
throw new Error(addressError);
|
||||
}
|
||||
|
||||
const accountOnChain = await client.getAccount(address);
|
||||
const chainId = await client.getChainId();
|
||||
|
||||
|
||||
@ -49,16 +49,21 @@ const Multipage = () => {
|
||||
assert(state.chain.denom, "denom missing");
|
||||
const tempHoldings = await client.getAllBalances(address);
|
||||
setHoldings(tempHoldings);
|
||||
const result = await getMultisigAccount(address, client);
|
||||
setPubkey(result[0]);
|
||||
setAccountOnChain(result[1]);
|
||||
assert(state.chain.addressPrefix, "addressPrefix missing");
|
||||
const [newPubkey, newAccountOnChain] = await getMultisigAccount(
|
||||
address,
|
||||
state.chain.addressPrefix,
|
||||
client,
|
||||
);
|
||||
setPubkey(newPubkey);
|
||||
setAccountOnChain(newAccountOnChain);
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
} catch (error: any) {
|
||||
setAccountError(error.message);
|
||||
console.log("Account error:", error);
|
||||
}
|
||||
},
|
||||
[state.chain.denom, state.chain.nodeAddress],
|
||||
[state.chain.addressPrefix, state.chain.denom, state.chain.nodeAddress],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
|
||||
@ -87,7 +87,8 @@ const TransactionPage = ({
|
||||
try {
|
||||
assert(state.chain.nodeAddress, "Node address missing");
|
||||
const client = await StargateClient.connect(state.chain.nodeAddress);
|
||||
const result = await getMultisigAccount(address, client);
|
||||
assert(state.chain.addressPrefix, "addressPrefix missing");
|
||||
const result = await getMultisigAccount(address, state.chain.addressPrefix, client);
|
||||
setPubkey(result[0]);
|
||||
setAccountOnChain(result[1]);
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
@ -96,7 +97,7 @@ const TransactionPage = ({
|
||||
console.log("Account error:", error);
|
||||
}
|
||||
},
|
||||
[state.chain.nodeAddress],
|
||||
[state.chain.addressPrefix, state.chain.nodeAddress],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user