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

[FEATURE] adds blockaid scan tx service method and route #121

Merged
merged 4 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions src/helper/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ export const ERROR = {
},
INVALID_RUN_MODE: "invalid run mode",
UNABLE_TO_SCAN_SITE: "unable to scan site using blockaid",
UNABLE_TO_SCAN_TX: "unable to scan tx using blockaid",
};
36 changes: 36 additions & 0 deletions src/route/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,42 @@ export async function initApiServer(
},
});

instance.route({
method: "GET",
url: "/scan-tx",
schema: {
querystring: {
["tx_xdr"]: {
type: "string",
},
["network"]: {
type: "string",
validator: (qStr: string) => isNetwork(qStr),
},
},
},
handler: async (
request: FastifyRequest<{
Querystring: {
["tx_xdr"]: string;
["network"]: NetworkNames;
};
}>,
reply
) => {
const { tx_xdr, network } = request.query;
try {
const { data, error } = await blockAidService.scanTx(
tx_xdr,
network
);
return reply.code(error ? 400 : 200).send({ data, error });
} catch (error) {
return reply.code(500).send(ERROR.SERVER_ERROR);
}
},
});

instance.route({
method: "POST",
url: "/subscription/token",
Expand Down
37 changes: 37 additions & 0 deletions src/service/blockaid/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import Blockaid from "@blockaid/client";
import Prometheus from "prom-client";
import { Logger } from "pino";
import { Networks, TransactionBuilder } from "stellar-sdk";

import { ERROR } from "../../helper/error";
import { NetworkNames } from "../../helper/validate";

const NetworkNameBlockaid: { [index: string]: "pubnet" | "futurenet" } = {
PUBLIC: "pubnet",
FUTURENET: "futurenet",
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this include testnet? From their docs, it looked like they support testnet now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

from their docs it does look that way but their type here is "pubnet" | "futurenet" so I think maybe that's an open question to bring up with them.


export class BlockAidService {
blockAidClient: Blockaid;
Expand Down Expand Up @@ -38,4 +45,34 @@ export class BlockAidService {
return { data: null, error: ERROR.UNABLE_TO_SCAN_SITE };
}
};

scanTx = async (txXdr: string, network: NetworkNames) => {
try {
const networkPassphrase = Networks[network];
const tx = TransactionBuilder.fromXDR(txXdr, networkPassphrase);
let source = "";
if ("innerTransaction" in tx) {
source = tx.innerTransaction.source;
} else {
source = tx.source;
}
const body = {
chain: NetworkNameBlockaid[network],
options: ["validation", "simulation"] as Array<
"validation" | "simulation"
>,
metadata: {
type: "wallet" as "wallet",
url: "", // TODO: can this be optional, we dont always have it
},
transactions: [txXdr],
account_address: source,
};
const data = await this.blockAidClient.stellar.transaction.scan(body);
return { data, error: null };
} catch (error) {
this.logger.error(error);
return { data: null, error: ERROR.UNABLE_TO_SCAN_TX };
}
};
}