Skip to content

Commit

Permalink
fix: accept contract addresses as pool address (leather-io#1165)
Browse files Browse the repository at this point in the history
  • Loading branch information
friedger committed Mar 22, 2023
1 parent 83bb80e commit 7c6e228
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
4 changes: 2 additions & 2 deletions app/pages/stacking/delegated-stacking/pooled-stacking.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { useBackButton } from '@hooks/use-back-url';
import { DelegatedStackingModal } from '@modals/delegated-stacking/delegated-stacking-modal';
import { selectNextCycleInfo, selectPoxInfo } from '@store/stacking';
import { calculateUntilBurnHeightBlockFromCycles } from '@utils/calculate-burn-height';
import { stxAddressSchema } from '@utils/validators/stx-address-validator';
import { stxPrincipalSchema } from '@utils/validators/stx-address-validator';
import {
UI_IMPOSED_MAX_STACKING_AMOUNT_USTX,
MIN_DELEGATED_STACKING_AMOUNT_USTX,
Expand Down Expand Up @@ -83,7 +83,7 @@ export const StackingDelegation: FC = () => {
);

const validationSchema = yup.object().shape({
stxAddress: stxAddressSchema().test({
stxAddress: stxPrincipalSchema().test({
name: 'cannot-pool-to-yourself',
message: 'Cannot pool to your own STX address',
test(value: any) {
Expand Down
10 changes: 10 additions & 0 deletions app/utils/get-stx-transfer-direction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,13 @@ export const validateStacksAddress = (stacksAddress: string): boolean => {
return false;
}
};

export const validateStacksPrincipal = (contractId: string): boolean => {
try {
const [stacksAddress] = contractId.split(".")
c32addressDecode(stacksAddress);
return true;
} catch (e) {
return false;
}
};
7 changes: 4 additions & 3 deletions app/utils/validators/stx-address-validator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as yup from 'yup';
import { validateStacksAddress } from '../get-stx-transfer-direction';
import { validateStacksPrincipal } from '../get-stx-transfer-direction';
import { validateAddressChain } from '../../crypto/validate-address-net';
import { NETWORK } from '@constants/index';

export function stxAddressSchema() {
// accepts standard and contract addresses
export function stxPrincipalSchema() {
let timer = 0;
return yup
.string()
Expand All @@ -15,7 +16,7 @@ export function stxAddressSchema() {
clearTimeout(timer);
timer = window.setTimeout(() => {
if (!value) return resolve(false);
const valid = validateStacksAddress(value);
const valid = validateStacksPrincipal(value);

if (!valid) {
return resolve(
Expand Down

0 comments on commit 7c6e228

Please sign in to comment.