Skip to content

Commit

Permalink
feat: add jwt support for Alchemy providers (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
avasisht23 committed Aug 17, 2023
1 parent 362a155 commit af85aa4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
21 changes: 20 additions & 1 deletion packages/alchemy/src/__tests__/provider.test.ts
@@ -1,3 +1,4 @@
import * as AACoreModule from "@alchemy/aa-core";
import {
SimpleSmartContractAccount,
type BatchUserOperationCallData,
Expand All @@ -21,7 +22,8 @@ describe("Alchemy Provider Tests", () => {
};
const chain = polygonMumbai;
const signer = new AlchemyProvider({
apiKey: "test",
rpcUrl: "https://eth-mainnet.g.alchemy.com/v2",
jwt: "test",
chain,
entryPointAddress: "0xENTRYPOINT_ADDRESS",
}).connect((provider) => {
Expand All @@ -40,6 +42,23 @@ describe("Alchemy Provider Tests", () => {
return account;
});

it("should have a JWT propety", async () => {
const spy = vi.spyOn(AACoreModule, "createPublicErc4337Client");
new AlchemyProvider({
rpcUrl: "https://eth-mainnet.g.alchemy.com/v2",
jwt: "test",
chain,
entryPointAddress: "0xENTRYPOINT_ADDRESS",
});
expect(spy.mock.calls[0][0].fetchOptions).toMatchInlineSnapshot(`
{
"headers": {
"Authorization": "Bearer test",
},
}
`);
});

it("should correctly sign the message", async () => {
expect(
// TODO: expose sign message on the provider too
Expand Down
2 changes: 1 addition & 1 deletion packages/alchemy/src/index.ts
Expand Up @@ -7,4 +7,4 @@ export {

export { SupportedChains } from "./chains.js";
export { AlchemyProvider } from "./provider.js";
export type { AlchemyProviderConfig } from "./provider.js";
export type { AlchemyProviderConfig, ConnectionConfig } from "./provider.js";
33 changes: 22 additions & 11 deletions packages/alchemy/src/provider.ts
@@ -1,33 +1,32 @@
import {
BaseSmartContractAccount,
SmartAccountProvider,
createPublicErc4337Client,
deepHexlify,
resolveProperties,
type AccountMiddlewareFn,
type SmartAccountProviderOpts,
} from "@alchemy/aa-core";
import type { Address, Chain, HttpTransport } from "viem";
import { type Address, type Chain, type HttpTransport } from "viem";
import {
arbitrum,
arbitrumGoerli,
optimism,
optimismGoerli,
} from "viem/chains";
import { SupportedChains } from "./chains.js";
import type { ClientWithAlchemyMethods } from "./middleware/client.js";
import { withAlchemyGasFeeEstimator } from "./middleware/gas-fees.js";
import {
alchemyPaymasterAndDataMiddleware,
withAlchemyGasManager,
type AlchemyGasManagerConfig,
alchemyPaymasterAndDataMiddleware,
} from "./middleware/gas-manager.js";
import { withAlchemyGasFeeEstimator } from "./middleware/gas-fees.js";
import type { ClientWithAlchemyMethods } from "./middleware/client.js";

type ConnectionConfig =
| {
apiKey: string;
rpcUrl?: undefined;
}
| { rpcUrl: string; apiKey?: undefined };
export type ConnectionConfig =
| { rpcUrl?: never; apiKey: string; jwt?: never }
| { rpcUrl: string; apiKey?: never; jwt?: never }
| { rpcUrl: string; apiKey?: never; jwt: string };

export type AlchemyProviderConfig = {
chain: Chain | number;
Expand Down Expand Up @@ -82,7 +81,19 @@ export class AlchemyProvider extends SmartAccountProvider<HttpTransport> {
? `${_chain.rpcUrls.alchemy.http[0]}/${connectionConfig.apiKey}`
: connectionConfig.rpcUrl;

super(rpcUrl, entryPointAddress, _chain, account, opts);
const client = createPublicErc4337Client({
chain: _chain,
rpcUrl,
...(connectionConfig.jwt != null && {
fetchOptions: {
headers: {
Authorization: `Bearer ${connectionConfig.jwt}`,
},
},
}),
});

super(client, entryPointAddress, _chain, account, opts);

this.alchemyClient = this.rpcClient as ClientWithAlchemyMethods;
withAlchemyGasFeeEstimator(
Expand Down

0 comments on commit af85aa4

Please sign in to comment.