Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions core/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ export const walletAuthSchema = Type.Object({
"x-wallet-address": Type.String({
description: "Wallet address",
}),
"x-account-address": Type.Optional(
Type.String({
description: "Smart account address",
}),
),
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"@sinclair/typebox": "^0.28",
"@t3-oss/env-core": "^0.6.0",
"@thirdweb-dev/chains": "^0.1.46",
"@thirdweb-dev/sdk": "^3.10.60",
"@thirdweb-dev/sdk": "0.0.0-dev-b1d4b3b-20230922050105",
"@thirdweb-dev/service-utils": "^0.4.2",
"@thirdweb-dev/wallets": "^1.1.23",
"body-parser": "^1.20.2",
Expand Down
21 changes: 21 additions & 0 deletions server/api/contract/extensions/account/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { FastifyInstance } from "fastify";
import { getAllAdmins } from "./read/getAllAdmins";
import { getAllSessions } from "./read/getAllSessions";
import { grantAdmin } from "./write/grantAdmin";
import { grantSession } from "./write/grantSession";
import { revokeAdmin } from "./write/revokeAdmin";
import { revokeSession } from "./write/revokeSession";
import { updateSession } from "./write/updateSession";

export const accountRoutes = async (fastify: FastifyInstance) => {
// GET
await fastify.register(getAllAdmins);
await fastify.register(getAllSessions);

// POST
await fastify.register(grantAdmin);
await fastify.register(revokeAdmin);
await fastify.register(grantSession);
await fastify.register(revokeSession);
await fastify.register(updateSession);
};
49 changes: 49 additions & 0 deletions server/api/contract/extensions/account/read/getAllAdmins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Static, Type } from "@sinclair/typebox";
import { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import {
contractParamSchema,
standardResponseSchema,
} from "../../../../../helpers";
import { getChainIdFromChain } from "../../../../../utilities/chain";
import { getContract } from "../../../../../utils/cache/getContract";

const ReplySchema = Type.Object({
result: Type.Array(Type.String(), {
description: "The address of the admins on this account",
}),
});

export const getAllAdmins = async (fastify: FastifyInstance) => {
fastify.route<{
Params: Static<typeof contractParamSchema>;
Reply: Static<typeof ReplySchema>;
}>({
method: "GET",
url: "/contract/:chain/:contract_address/account/admins/get-all",
schema: {
description: "Get all admins on an account",
tags: ["Account"],
operationId: "account:get-all-admins",
params: contractParamSchema,
response: {
...standardResponseSchema,
[StatusCodes.OK]: ReplySchema,
},
},
handler: async (req, rep) => {
const { chain, contract_address } = req.params;
const chainId = getChainIdFromChain(chain);

const contract = await getContract({
chainId,
contractAddress: contract_address,
});
const accountAddresses = await contract.account.getAllAdmins();

rep.status(StatusCodes.OK).send({
result: accountAddresses,
});
},
});
};
55 changes: 55 additions & 0 deletions server/api/contract/extensions/account/read/getAllSessions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Static, Type } from "@sinclair/typebox";
import { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import {
contractParamSchema,
standardResponseSchema,
} from "../../../../../helpers";
import { SessionSchema } from "../../../../../schemas/account";
import { getChainIdFromChain } from "../../../../../utilities/chain";
import { getContract } from "../../../../../utils/cache/getContract";

const ReplySchema = Type.Object({
result: Type.Array(SessionSchema),
});

export const getAllSessions = async (fastify: FastifyInstance) => {
fastify.route<{
Params: Static<typeof contractParamSchema>;
Reply: Static<typeof ReplySchema>;
}>({
method: "GET",
url: "/contract/:chain/:contract_address/account/sessions/get-all",
schema: {
description: "Get all sessions on an account",
tags: ["Account"],
operationId: "account:get-all-sessions",
params: contractParamSchema,
response: {
...standardResponseSchema,
[StatusCodes.OK]: ReplySchema,
},
},
handler: async (req, rep) => {
const { chain, contract_address } = req.params;
const chainId = getChainIdFromChain(chain);

const contract = await getContract({
chainId,
contractAddress: contract_address,
});
const sessions = await contract.account.getAllSigners();

rep.status(StatusCodes.OK).send({
result: sessions.map((session) => ({
signerAddress: session.signer,
startDate: session.permissions.startDate.toISOString(),
expirationDate: session.permissions.expirationDate.toISOString(),
nativeTokenLimitPerTransaction:
session.permissions.nativeTokenLimitPerTransaction.toString(),
approvedCallTargets: session.permissions.approvedCallTargets,
})),
});
},
});
};
64 changes: 64 additions & 0 deletions server/api/contract/extensions/account/write/grantAdmin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Static, Type } from "@sinclair/typebox";
import { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { walletAuthSchema } from "../../../../../../core/schema";
import { queueTx } from "../../../../../../src/db/transactions/queueTx";
import {
contractParamSchema,
standardResponseSchema,
transactionWritesResponseSchema,
} from "../../../../../helpers";
import { getChainIdFromChain } from "../../../../../utilities/chain";
import { getContract } from "../../../../../utils/cache/getContract";

const BodySchema = Type.Object({
signer_address: Type.String({
description: "Address to grant admin permissions to",
}),
});

export const grantAdmin = async (fastify: FastifyInstance) => {
fastify.route<{
Params: Static<typeof contractParamSchema>;
Reply: Static<typeof transactionWritesResponseSchema>;
Body: Static<typeof BodySchema>;
}>({
method: "POST",
url: "/contract/:chain/:contract_address/account/admins/grant",
schema: {
description: "Grant a wallet admin permissions",
tags: ["Account"],
operationId: "account:grant-admin",
headers: walletAuthSchema,
params: contractParamSchema,
body: BodySchema,
response: {
...standardResponseSchema,
[StatusCodes.OK]: transactionWritesResponseSchema,
},
},
handler: async (req, rep) => {
const { chain, contract_address } = req.params;
const { signer_address } = req.body;
const walletAddress = req.headers["x-wallet-address"] as string;
const accountAddress = req.headers["x-account-address"] as string;
const chainId = getChainIdFromChain(chain);

const contract = await getContract({
chainId,
contractAddress: contract_address,
walletAddress,
accountAddress,
});

const tx = await contract.account.grantAdminPermissions.prepare(
signer_address,
);
const queueId = await queueTx({ tx, chainId, extension: "account" });

rep.status(StatusCodes.OK).send({
result: queueId,
});
},
});
};
68 changes: 68 additions & 0 deletions server/api/contract/extensions/account/write/grantSession.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { Static } from "@sinclair/typebox";
import { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { walletAuthSchema } from "../../../../../../core/schema";
import { queueTx } from "../../../../../../src/db/transactions/queueTx";
import {
contractParamSchema,
standardResponseSchema,
transactionWritesResponseSchema,
} from "../../../../../helpers";
import { SessionSchema } from "../../../../../schemas/account";
import { getChainIdFromChain } from "../../../../../utilities/chain";
import { getContract } from "../../../../../utils/cache/getContract";

const BodySchema = SessionSchema;

export const grantSession = async (fastify: FastifyInstance) => {
fastify.route<{
Params: Static<typeof contractParamSchema>;
Reply: Static<typeof transactionWritesResponseSchema>;
Body: Static<typeof BodySchema>;
}>({
method: "POST",
url: "/contract/:chain/:contract_address/account/sessions/create",
schema: {
description: "Create a new session",
tags: ["Account"],
operationId: "account:grant-session",
params: contractParamSchema,
headers: walletAuthSchema,
body: BodySchema,
response: {
...standardResponseSchema,
[StatusCodes.OK]: transactionWritesResponseSchema,
},
},
handler: async (req, rep) => {
const { chain, contract_address } = req.params;
const { signerAddress, ...permissions } = req.body;
const walletAddress = req.headers["x-wallet-address"] as string;
const accountAddress = req.headers["x-account-address"] as string;
const chainId = getChainIdFromChain(chain);

const contract = await getContract({
chainId,
contractAddress: contract_address,
walletAddress,
accountAddress,
});

const tx = await contract.account.grantPermissions.prepare(
signerAddress,
{
startDate: new Date(permissions.startDate),
expirationDate: new Date(permissions.expirationDate),
approvedCallTargets: permissions.approvedCallTargets,
nativeTokenLimitPerTransaction:
permissions.nativeTokenLimitPerTransaction,
},
);
const queueId = await queueTx({ tx, chainId, extension: "account" });

rep.status(StatusCodes.OK).send({
result: queueId,
});
},
});
};
64 changes: 64 additions & 0 deletions server/api/contract/extensions/account/write/revokeAdmin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Static, Type } from "@sinclair/typebox";
import { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { walletAuthSchema } from "../../../../../../core/schema";
import { queueTx } from "../../../../../../src/db/transactions/queueTx";
import {
contractParamSchema,
standardResponseSchema,
transactionWritesResponseSchema,
} from "../../../../../helpers";
import { getChainIdFromChain } from "../../../../../utilities/chain";
import { getContract } from "../../../../../utils/cache/getContract";

const BodySchema = Type.Object({
wallet_address: Type.String({
description: "Address to revoke admin permissions from",
}),
});

export const revokeAdmin = async (fastify: FastifyInstance) => {
fastify.route<{
Params: Static<typeof contractParamSchema>;
Reply: Static<typeof transactionWritesResponseSchema>;
Body: Static<typeof BodySchema>;
}>({
method: "POST",
url: "/contract/:chain/:contract_address/account/admins/revoke",
schema: {
description: "Revoke a wallet's admin permissions",
tags: ["Account"],
operationId: "account:revoke-admin",
headers: walletAuthSchema,
params: contractParamSchema,
body: BodySchema,
response: {
...standardResponseSchema,
[StatusCodes.OK]: transactionWritesResponseSchema,
},
},
handler: async (req, rep) => {
const { chain, contract_address } = req.params;
const { wallet_address } = req.body;
const walletAddress = req.headers["x-wallet-address"] as string;
const accountAddress = req.headers["x-account-address"] as string;
const chainId = getChainIdFromChain(chain);

const contract = await getContract({
chainId,
contractAddress: contract_address,
walletAddress,
accountAddress,
});

const tx = await contract.account.revokeAdminPermissions.prepare(
wallet_address,
);
const queueId = await queueTx({ tx, chainId, extension: "account" });

rep.status(StatusCodes.OK).send({
result: queueId,
});
},
});
};
Loading