Skip to content

Commit

Permalink
feat: add WalletClientSigner (#90)
Browse files Browse the repository at this point in the history
* feat: add `JsonRpcAccountSigner`

* fix: plain string message signing in `JsonRpcAccountSigner`

Co-authored-by: Michael Moldoveanu <moldy530@gmail.com>

* fix(core): rename `JsonRpcAccountSigner` to `WalletClientSigner`

Co-authored-by: Michael Moldoveanu <moldy530@gmail.com>

* chore: `json-rpc-account` -> `wallet-client`

* fix: lint error in `wallet-client.ts`

Co-authored-by: Michael Moldoveanu <moldy530@gmail.com>

* fix: lint error in `wallet-client.ts`

---------

Co-authored-by: Michael Moldoveanu <moldy530@gmail.com>
  • Loading branch information
therealharpaljadeja and moldy530 committed Sep 8, 2023
1 parent ed7d220 commit b1ede6c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type { BaseSmartAccountParams } from "./account/base.js";
export { SimpleSmartContractAccount } from "./account/simple.js";
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,
});
};
}

0 comments on commit b1ede6c

Please sign in to comment.