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: add JsonRpcAccountSigner #90

Merged
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type {
SimpleSmartAccountParams,
} from "./account/simple.js";
export type * from "./account/types.js";
export { WalletClientSigner } from "./signer/wallet-client.js";
export { HdAccountSigner } from "./signer/hd-account.js";
export { LocalAccountSigner } from "./signer/local-account.js";
export { PrivateKeySigner } from "./signer/private-key.js";
Expand Down
46 changes: 46 additions & 0 deletions packages/core/src/signer/wallet-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {
getAddress,
type ByteArray,
type Hex,
type WalletClient,
isHex,
} from "viem";
import type { SmartAccountSigner } from "./types";
import type { SignTypedDataParams } from "../account/types";

export class WalletClientSigner implements SmartAccountSigner {
private client: WalletClient;

constructor(client: WalletClient) {
this.client = client;
}

getAddress: () => Promise<`0x${string}`> = async () => {
let addresses = await this.client.getAddresses();
return getAddress(addresses[0]);
};

readonly signMessage: (
message: string | Hex | ByteArray
) => Promise<`0x${string}`> = async (message) => {
if (typeof message === "string" && !isHex(message)) {
return this.client.signMessage({
account: await this.getAddress(),
message,
});
} else {
return this.client.signMessage({
account: await this.getAddress(),
message: { raw: message },
});
}
};

signTypedData: (params: SignTypedDataParams) => Promise<`0x${string}`> =
async (params) => {
return this.client.signTypedData({
account: await this.getAddress(),
...params,
});
};
}
Loading