-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
ed7d220
commit b1ede6c
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
}; | ||
} |