Skip to content

Commit

Permalink
fix(deployment): added provider validation for trial
Browse files Browse the repository at this point in the history
  • Loading branch information
baktun14 committed Oct 22, 2024
1 parent 2f30701 commit 48ad3f0
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as v1beta4 from "@akashnetwork/akash-api/v1beta4";
import { EncodeObject } from "@cosmjs/proto-signing";
import { singleton } from "tsyringe";

import { UserWalletOutput } from "@src/billing/repositories";
import { ChainErrorService } from "../chain-error/chain-error.service";

@singleton()
export class AnonymousValidateService {
private readonly authorizedProviders = [
"akash1824w2vqx57n8zr8707dnyh85kjrkfkrrs94pk9",
"akash19ah5c95kq4kz2g6q5rdkdgt80kc3xycsd8plq8",
"akash1g7az2pus6atgeufgttlcnl0wzlzwd0lrsy6d7s",
"akash1tfuvntkwt3cpxnhukdnsvvsumjnrvmvh244l3w",
"akash18mcffkg5jp9eqc36evlv67uqcj04fvk324ap54",
"akash1t0sk5nhc8n3xply5ft60x9det0s7jwplzzycnv",
"akash1cnzkdynwd4u6j7s8z5j0fg76h3g6yhsggmuqta",
"akash1eyrkgz2ufjs27hnpe3jekemp8anrfk8tqzhpsn",
"akash15tl6v6gd0nte0syyxnv57zmmspgju4c3xfmdhk",
"akash1rfqs6aajq2d4azhjj88wav2d6ra6vvuzn58dt5",
"akash16aflfteeg8c2j63sy0rlxj57pty98zzm5mvgfa"
];

constructor(private readonly chainErrorService: ChainErrorService) {}

validateLeaseProviders(decoded: EncodeObject, userWallet: UserWalletOutput) {
if (userWallet.isTrialing && decoded.typeUrl === "/akash.market.v1beta4.MsgCreateLease") {
const value = decoded.value as v1beta4.MsgCreateLease;
if (!this.authorizedProviders.includes(value.bidId.provider)) {
throw new Error(`provider not authorized: ${value.bidId.provider}`);
}
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ export class ChainErrorService {
"insufficient funds": {
code: 400,
message: "Insufficient funds"
},
"provider not authorized": {
code: 400,
message: "Provider not authorized"
}
};

private MESSAGE_ERROR_TITLES: Record<string, string> = {
"/akash.deployment.v1beta3.MsgCreateDeployment": "Failed to create deployment"
"/akash.deployment.v1beta3.MsgCreateDeployment": "Failed to create deployment",
"/akash.market.v1beta4.MsgCreateLease": "Failed to create lease"
};

public toAppError(error: Error, messages: readonly EncodeObject[]) {
Expand Down
10 changes: 9 additions & 1 deletion apps/api/src/billing/services/tx-signer/tx-signer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { InjectTypeRegistry } from "@src/billing/providers/type-registry.provide
import { UserWalletOutput, UserWalletRepository } from "@src/billing/repositories";
import { MasterWalletService } from "@src/billing/services";
import { BalancesService } from "@src/billing/services/balances/balances.service";
import { AnonymousValidateService } from "../anonymous-validate/anonymous-validate";
import { ChainErrorService } from "../chain-error/chain-error.service";

type StringifiedEncodeObject = Omit<EncodeObject, "value"> & { value: string };
Expand All @@ -32,7 +33,8 @@ export class TxSignerService {
private readonly masterWalletService: MasterWalletService,
private readonly balancesService: BalancesService,
private readonly authService: AuthService,
private readonly chainErrorService: ChainErrorService
private readonly chainErrorService: ChainErrorService,
private readonly anonymousValidateService: AnonymousValidateService
) {}

async signAndBroadcast(userId: UserWalletOutput["userId"], messages: StringifiedEncodeObject[]) {
Expand All @@ -41,6 +43,12 @@ export class TxSignerService {

const decodedMessages = this.decodeMessages(messages);

try {
decodedMessages.forEach(message => this.anonymousValidateService.validateLeaseProviders(message, userWallet));
} catch (error) {
throw this.chainErrorService.toAppError(error, decodedMessages);
}

const client = await this.getClientForAddressIndex(userWallet.id);
const tx = await client.signAndBroadcast(decodedMessages);

Expand Down
2 changes: 2 additions & 0 deletions apps/deploy-web/src/components/new-deployment/CreateLease.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ export const CreateLease: React.FunctionComponent<Props> = ({ dseq }) => {
label: "Create lease"
});
await sendManifest();
} catch (error) {
console.error(error);
} finally {
setIsCreatingLeases(false);
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

0 comments on commit 48ad3f0

Please sign in to comment.