Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(multisig): allow to import existing multisig by address #1397

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 81 additions & 2 deletions packages/screens/Multisig/MultisigCreateScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { createMultisigThresholdPubkey } from "@cosmjs/amino";
import {
Pubkey,
createMultisigThresholdPubkey,
pubkeyToAddress,
} from "@cosmjs/amino";
import React, { useEffect, useMemo, useState } from "react";
import { useForm } from "react-hook-form";
import { Pressable, ScrollView, View } from "react-native";
Expand All @@ -22,7 +26,12 @@ import { useMultisigAuthToken } from "@/hooks/multisig/useMultisigAuthToken";
import { useMultisigClient } from "@/hooks/multisig/useMultisigClient";
import { useAppNavigation } from "@/hooks/navigation/useAppNavigation";
import { useSelectedNetworkInfo } from "@/hooks/useSelectedNetwork";
import { getUserId, NetworkKind, parseUserId } from "@/networks";
import {
getNonSigningStargateClient,
getUserId,
NetworkKind,
parseUserId,
} from "@/networks";
import { getCosmosAccount } from "@/utils/cosmos";
import {
patternOnlyNumbers,
Expand Down Expand Up @@ -210,6 +219,76 @@ export const MultisigCreateScreen = () => {
/>
<SpacerColumn size={3} />

<TextInputCustom
name="existing-address"
variant="labelOutside"
noBrokenCorners
label="Optional existing multisig address"
onChangeText={async (addr) => {
try {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's more lisible to use a function and remove the logic from the JSX imo

console.log("addr", addr);
if (selectedNetwork?.kind !== NetworkKind.Cosmos) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove console.log usages

console.error("not a cosmos netwokr");
return;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.error("not a cosmos netwokr");
console.error("not a cosmos network");

}
const networkId = selectedNetwork?.id;
if (!networkId) {
console.error("invalid network id", networkId);
return;
}
const client = await getNonSigningStargateClient(networkId);
const account = await client.getAccount(addr);
console.log("got account", account);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should validateAddress before using the addr to getAccount to avoid unnecessary queries.
Wdyt?

const multisig = account?.pubkey;
if (!multisig) {
console.error("multisig not found on chain");
return;
}
if (multisig.type !== "tendermint/PubKeyMultisigThreshold") {
console.error("invalid multisig type", multisig.type);
return;
}
console.log(multisig.value);
const threshold: string = multisig.value.threshold;
const participants: Pubkey[] = multisig.value.pubkeys;
console.log("threshold", threshold);
console.log("participants", participants);
for (const p of participants) {
if (typeof p.value !== "string") {
console.error(
"invalid participant pubkey value type",
participants,
);
return;
}
}
const formPubkeys = participants.map((p) => {
const address = pubkeyToAddress(
p,
selectedNetwork.addressPrefix,
);
return {
address,
compressedPubkey: p.value,
};
});
setValue("maxSignature", formPubkeys.length.toString());
setValue("signatureRequired", threshold);
setValue(
"addresses",
formPubkeys.map((fpk) => ({ address: fpk.address })),
);
setAddressIndexes(formPubkeys);
} catch (err) {
console.error(err);
}
}}
placeHolder="Type the address of an existing multisig to import it"
iconSVG={walletInputSVG}
/>

<SpacerColumn size={3} />

{addressIndexes.map((_, index) => (
<>
<View key={index.toString()}>
Expand Down
Loading