Skip to content

Commit

Permalink
feat: Try to get wallet mobile working for JWT
Browse files Browse the repository at this point in the history
  • Loading branch information
jarrydallison committed Oct 17, 2024
1 parent 25aeac0 commit 3cbc55b
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 24 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"sideEffects": false,
"dependencies": {
"@chain-registry/types": "0.47.0",
"@cosmos-kit/leap-mobile": "2.13.1",
"@cosmos-kit/react": "2.20.0",
"@esbuild-plugins/node-globals-polyfill": "0.2.3",
"@interchain-ui/react": "1.25.3",
Expand Down
71 changes: 64 additions & 7 deletions src/Components/UserAccount/UserAccount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import useOnEscape from 'react-tiny-hooks/use-on-escape';
// @ts-ignore
import useToggle from 'react-tiny-hooks/use-toggle';
import { useChain } from '@cosmos-kit/react';
import { cosmos } from '@provlabs/provenancejs';
import { signJWT } from '../../utils/jwt';
import { CHAIN_NAME } from '../../config';
import { ICON_NAMES } from '../../consts';
Expand Down Expand Up @@ -39,7 +40,8 @@ const UserAccount = ({ isMobile }: { isMobile: boolean }) => {
const containerRef = useOnClickOutside(deactivateShowPopup);
useOnEscape(deactivateShowPopup);

const { status, connect, address, signArbitrary } = useChain(CHAIN_NAME);
const { status, connect, address, signArbitrary, getSigningStargateClient, getAccount, wallet } =
useChain(CHAIN_NAME);
const { setAuthToken, authToken } = useApp();
const provJWT = localStorage.getItem('provenanceJWT');
const jwtInfo = provJWT ? JSON.parse(provJWT) : '';
Expand Down Expand Up @@ -79,13 +81,58 @@ const UserAccount = ({ isMobile }: { isMobile: boolean }) => {
// This is the effect that signs the local JWT to access the explorer service
useEffect(() => {
const initialSigningEvent = async () => {
if (!authToken && status === 'Connected' && address && !localStorage.getItem('provenanceJWT')) {
const response = await signArbitrary(address, 'Approve the connection to Provenance');
if (response) {
if (
!authToken &&
status === 'Connected' &&
address &&
wallet &&
!localStorage.getItem('provenanceJWT')
) {
let publicKey = '';
let signature = '';
if (wallet.name === 'leap-extension') {
const response = await signArbitrary(address, 'Approve the connection to Provenance');
signature = response.signature;
publicKey = response.pub_key.value;
} else {
const client = await getSigningStargateClient();
const account = await getAccount();
const signed = await client.sign(
address,
[
{
typeUrl: '/cosmos.bank.v1beta1.MsgSend',
value: {
fromAddress: '',
toAddress: '',
amount: [
{
amount: '0',
denom: 'nhash',
},
],
},
},
],
{
amount: [
{
amount: '0',
denom: 'nhash',
},
],
gas: '0',
},
'Approve the connection to Provenance. Because the signArbitrary method is not supported by Leap, signing into your account will cost a small amount of hash. We are working to resolve this issue.'
);
publicKey = cosmos.crypto.ed25519.PubKey.toAmino({ key: account.pubkey }).key as string;
signature = (cosmos.tx.v1beta1.TxRaw.toAmino(signed).signatures ? [0] : '') as string;
}
if (publicKey && signature) {
const jwtResponse = await signJWT({
address,
signature: response.signature,
publicKey: response.pub_key.value,
publicKey,
signature,
});
if (jwtResponse) {
localStorage.setItem(
Expand All @@ -101,7 +148,17 @@ const UserAccount = ({ isMobile }: { isMobile: boolean }) => {
}
};
initialSigningEvent();
}, [address, authToken, setAuthToken, signArbitrary, signedJWT, status]);
}, [
address,
authToken,
getAccount,
getSigningStargateClient,
setAuthToken,
signArbitrary,
signedJWT,
status,
wallet,
]);

const handleLoginClick = () => {
connect();
Expand Down
52 changes: 39 additions & 13 deletions src/Pages/Validators/Components/ManageStakingModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ interface StakingModalProps {
modalOpen: boolean;
onClose: () => void;
validator: CurrentValidator;
validatorPower?: number;
}

interface DelegationFormProps {
Expand All @@ -146,11 +147,17 @@ export const ManageStakingModal = ({
modalOpen,
onClose,
validator,
validatorPower,
}: StakingModalProps) => {
// Hooks
const { tx } = useTx(CHAIN_NAME);
const { allValidators, getValidatorSpotlight, validatorSpotlight, validatorSpotlightLoading, getAllValidators } =
useValidators();
const {
allValidators,
getValidatorSpotlight,
validatorSpotlight,
validatorSpotlightLoading,
getAllValidators,
} = useValidators();
const theme = useTheme();
const { accountAssets } = useAccounts();
const { delegateAction, redelegateAction, undelegateAction, withdrawRewardsAction } =
Expand Down Expand Up @@ -220,14 +227,12 @@ export const ManageStakingModal = ({
const typeUrl = data.json.messages[0]['@type'];
delete data.json.messages[0]['@type'];
const value = data.json.messages[0];
const response = await tx(
[
{
typeUrl,
value,
},
],
);
const response = await tx([
{
typeUrl,
value,
},
]);
if (response.isSuccess) {
// Wait a few seconds until refreshing the validators list
setTimeout(() => {
Expand All @@ -236,7 +241,7 @@ export const ManageStakingModal = ({
count: 100,
status: 'all',
});
}, 3000)
}, 3000);
}
};
// Close Modal
Expand Down Expand Up @@ -278,7 +283,7 @@ export const ManageStakingModal = ({
);

return (
<Modal isOpen={isOpen} onClose={handleModalClose}>
<Modal isOpen={isOpen} onClose={handleModalClose} largeModal>
{validatorSpotlightLoading || validator.addressId !== validatorSpotlight.operatorAddress ? (
<Loading />
) : (
Expand Down Expand Up @@ -396,7 +401,6 @@ export const ManageStakingModal = ({
</ButtonGroup>
</>
)}

{/* Delegating to a Validator */}
{stakingType === STAKING_TYPES.DELEGATE && (
<Info>
Expand All @@ -412,6 +416,28 @@ export const ManageStakingModal = ({
</DisclaimerText>
</div>
</Disclaimer>
{validator.votingPower &&
numberFormat(
(validator.votingPower.count / (validator.votingPower.total || 1)) * 100,
0
) === Math.floor(Number(32)).toString() && (
<Disclaimer>
<DisclaimerIcon>
<Sprite icon="WARNING" size="2rem" color={theme.FONT_WARNING} />
</DisclaimerIcon>
<div>
<DisclaimerTitle>
{validator.moniker} is close to voting capacity
</DisclaimerTitle>
<DisclaimerText>
This validator is near the maximum staking concentration limits.
Depending on the size of your delegation it may not be possible to
delegate. Please consider choosing one of the lower ranked validators
for your delegation to improve the decentralization of the network.
</DisclaimerText>
</div>
</Disclaimer>
)}
{/* Pops a warning if you'll only have 5 hash left, otherwise you might get stuck */}
{new Big(formik.values.amount || 0).gt(
new Big(hashAmount || 0).minus(MIN_HASH_AFTER_STAKING).toNumber()
Expand Down
6 changes: 6 additions & 0 deletions src/Pages/Validators/Components/functions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Coin } from '@cosmjs/stargate';
import { cosmos, provenance } from '@provlabs/provenancejs';
import { MsgDelegate } from '@provlabs/provenancejs/cosmos/staking/v1beta1/tx';
import { SignatureDescriptor_Data, SignatureDescriptors } from '@provlabs/provenancejs/cosmos/tx/signing/v1beta1/signing';
import { Payment } from '@provlabs/provenancejs/provenance/exchange/v1/payments';

export function CreatePaymentMessage(
Expand Down Expand Up @@ -48,6 +49,11 @@ export function CreateDelegateMessage(msg: MsgDelegate) {
return { typeUrl, value: cosmos.staking.v1beta1.MsgDelegate.toAmino(msg) };
}

export function CreateSignatureMessage(msg: SignatureDescriptor_Data) {
const typeUrl = cosmos.tx.signing.v1beta1.SignatureDescriptor_Data.typeUrl;
return { typeUrl, value: cosmos.tx.signing.v1beta1.SignatureDescriptor_Data.toAmino(msg)}
}

export function CreateRejectPaymentMessage(source: string, target: string, externalId: string) {
const { rejectPayment } = provenance.exchange.v1.MessageComposer.withTypeUrl;
const msg = rejectPayment({
Expand Down
19 changes: 15 additions & 4 deletions src/config/testnetprovenance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,25 @@ export const TestnetChainConfig: Chain = {
},
],
},
// fees: {
// fee_tokens: [
// {
// denom: 'nhash',
// fixed_min_gas_price: 1905000,
// low_gas_price: 1905000,
// average_gas_price: 2100000,
// high_gas_price: 2500000,
// },
// ],
// },
fees: {
fee_tokens: [
{
denom: 'nhash',
fixed_min_gas_price: 1905000,
low_gas_price: 1905000,
average_gas_price: 2100000,
high_gas_price: 2500000,
fixed_min_gas_price: 1905,
low_gas_price: 1905,
average_gas_price: 2100,
high_gas_price: 2500,
},
],
},
Expand Down

0 comments on commit 3cbc55b

Please sign in to comment.