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

feat: adds 7702 support to alchemy signer #1269

Merged
merged 3 commits into from
Jan 9, 2025
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
65 changes: 60 additions & 5 deletions account-kit/signer/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
} from "./types.js";
import { assertNever } from "./utils/typeAssertions.js";
import type { SessionManagerEvents } from "./session/types";
import { hashAuthorization, type Authorization } from "viem/experimental";

export interface BaseAlchemySignerParams<TClient extends BaseSignerClient> {
client: TClient;
Expand All @@ -51,6 +52,12 @@ type AlchemySignerStore = {
isNewUser?: boolean;
};

type UnpackedSignature = {
r: `0x${string}`;
s: `0x${string}`;
v: bigint;
};

type InternalStore = Mutate<
StoreApi<AlchemySignerStore>,
[["zustand/subscribeWithSelector", never]]
Expand Down Expand Up @@ -524,16 +531,64 @@ export abstract class BaseAlchemySigner<TClient extends BaseSignerClient>
keccak256(serializedTx)
);

const signature = {
r: takeBytes(signatureHex, { count: 32 }),
s: takeBytes(signatureHex, { count: 32, offset: 32 }),
v: BigInt(takeBytes(signatureHex, { count: 1, offset: 64 })),
};
const signature = this.unpackSignRawMessageBytes(signatureHex);

return serializeFn(tx, signature);
}
);

/**
* Signs an EIP-7702 Authorization and then returns the authorization with the signature.
*
* @example
* ```ts
* import { AlchemyWebSigner } from "@account-kit/signer";
*
* const signer = new AlchemyWebSigner({
* client: {
* connection: {
* rpcUrl: "/api/rpc",
* },
* iframeConfig: {
* iframeContainerId: "alchemy-signer-iframe-container",
* },
* },
* });
*
* const tx = await signer.signAuthorization({
* contractAddress: "0x1234",
* chainId: 1,
* nonce: 0,
* });
* ```
*
* @param {Authorization<number, false>} unsignedAuthorization the authorization to be signed
* @returns {Promise<Authorization<number, true>> | undefined} a promise that resolves to the authorization with the signature
*/
signAuthorization: (
unsignedAuthorization: Authorization<number, false>
) => Promise<Authorization<number, true>> | undefined = SignerLogger.profiled(
"BaseAlchemySigner.signAuthorization",
async (unsignedAuthorization) => {
const hashedAuthorization = hashAuthorization(unsignedAuthorization);
const signedAuthorizationHex = await this.inner.signRawMessage(
hashedAuthorization
);
const signature = this.unpackSignRawMessageBytes(signedAuthorizationHex);
return { ...unsignedAuthorization, ...signature };
}
);

private unpackSignRawMessageBytes = (
hex: `0x${string}`
): UnpackedSignature => {
return {
r: takeBytes(hex, { count: 32 }),
s: takeBytes(hex, { count: 32, offset: 32 }),
v: BigInt(takeBytes(hex, { count: 1, offset: 64 })),
};
};

/**
* Unauthenticated call to look up a user's organizationId by email
*
Expand Down

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

Loading