Skip to content

Commit

Permalink
better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
d4mr committed Feb 4, 2025
1 parent f102ee3 commit 3459d8f
Showing 1 changed file with 72 additions and 23 deletions.
95 changes: 72 additions & 23 deletions src/server/utils/wallets/circle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,17 @@ export async function provisionCircleWallet({
});

if (!walletSetId) {
const walletSet = await circleDeveloperSdk.createWalletSet({
name: `Engine WalletSet ${new Date().toISOString()}`,
});
const walletSet = await circleDeveloperSdk
.createWalletSet({
name: `Engine WalletSet ${new Date().toISOString()}`,
})
.catch((e) => {
throw new CircleWalletError(
`[Circle] Could not create walletset:\n${JSON.stringify(
e?.response?.data,
)}`,
);
});

walletSetId = walletSet.data?.walletSet.id;
}
Expand All @@ -63,12 +71,20 @@ export async function provisionCircleWallet({
"Did not receive walletSetId, and failed to create one automatically",
);

const provisionWalletResponse = await circleDeveloperSdk.createWallets({
accountType: "EOA",
blockchains: ["EVM"],
count: 1,
walletSetId: walletSetId,
});
const provisionWalletResponse = await circleDeveloperSdk
.createWallets({
accountType: "EOA",
blockchains: ["EVM"],
count: 1,
walletSetId: walletSetId,
})
.catch((e) => {
throw new CircleWalletError(
`[Circle] Could not provision wallet:\n${JSON.stringify(
e?.response?.data,
)}`,
);
});

const provisionedWallet = provisionWalletResponse.data?.wallets?.[0];

Expand Down Expand Up @@ -119,7 +135,16 @@ export async function getCircleAccount({
entitySecret,
});

const walletResponse = await circleDeveloperSdk.getWallet({ id: walletId });
const walletResponse = await circleDeveloperSdk
.getWallet({ id: walletId })
.catch((e) => {
throw new CircleWalletError(
`[Circle] Could not get wallet with id:${walletId}:\n${JSON.stringify(
e?.response?.data,
)}`,
);
});

if (!walletResponse) {
throw new CircleWalletError(
`Unable to get circle wallet with id:${walletId}`,
Expand All @@ -138,10 +163,18 @@ export async function getCircleAccount({
}

async function signTransaction(tx: SerializableTransaction) {
const signature = await circleDeveloperSdk.signTransaction({
walletId,
transaction: stringify(tx),
});
const signature = await circleDeveloperSdk
.signTransaction({
walletId,
transaction: stringify(tx),
})
.catch((e) => {
throw new CircleWalletError(
`[Circle] Could not get transaction signature:\n${JSON.stringify(
e?.response?.data,
)}`,
);
});

if (!signature.data?.signature) {
throw new CircleWalletError("Unable to sign transaction");
Expand Down Expand Up @@ -177,10 +210,18 @@ export async function getCircleAccount({
const typedData extends TypedData | Record<string, unknown>,
primaryType extends keyof typedData | "EIP712Domain" = keyof typedData,
>(_typedData: TypedDataDefinition<typedData, primaryType>): Promise<Hex> {
const signatureResponse = await circleDeveloperSdk.signTypedData({
data: stringify(_typedData),
walletId,
});
const signatureResponse = await circleDeveloperSdk
.signTypedData({
data: stringify(_typedData),
walletId,
})
.catch((e) => {
throw new CircleWalletError(
`[Circle] Could not get signature:\n${JSON.stringify(
e?.response?.data,
)}`,
);
});

if (!signatureResponse.data?.signature) {
throw new CircleWalletError("Could not sign typed data");
Expand All @@ -201,11 +242,19 @@ export async function getCircleAccount({
messageToSign = toHex(messageToSign);
}

const signatureResponse = await circleDeveloperSdk.signMessage({
walletId,
message: messageToSign,
encodedByHex: isRawMessage,
});
const signatureResponse = await circleDeveloperSdk
.signMessage({
walletId,
message: messageToSign,
encodedByHex: isRawMessage,
})
.catch((e) => {
throw new CircleWalletError(
`[Circle] Could not get signature:\n${JSON.stringify(
e?.response?.data,
)}`,
);
});

if (!signatureResponse.data?.signature)
throw new CircleWalletError("Could not get signature");
Expand Down

0 comments on commit 3459d8f

Please sign in to comment.