/
local-account.ts
59 lines (52 loc) · 1.49 KB
/
local-account.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import {
type HDAccount,
type HDOptions,
type Hex,
type LocalAccount,
type PrivateKeyAccount,
type SignableMessage,
type TypedData,
type TypedDataDefinition,
} from "viem";
import { mnemonicToAccount, privateKeyToAccount } from "viem/accounts";
import type { SmartAccountSigner } from "./types.js";
export class LocalAccountSigner<
T extends HDAccount | PrivateKeyAccount | LocalAccount
> implements SmartAccountSigner<T>
{
inner: T;
signerType: string;
constructor(inner: T) {
this.inner = inner;
this.signerType = inner.type; // type: "local"
}
readonly signMessage: (message: SignableMessage) => Promise<`0x${string}`> = (
message
) => {
return this.inner.signMessage({ message });
};
readonly signTypedData = async <
const TTypedData extends TypedData | { [key: string]: unknown },
TPrimaryType extends string = string
>(
params: TypedDataDefinition<TTypedData, TPrimaryType>
): Promise<Hex> => {
return this.inner.signTypedData(params);
};
readonly getAddress: () => Promise<`0x${string}`> = async () => {
return this.inner.address;
};
static mnemonicToAccountSigner(
key: string,
opts?: HDOptions
): LocalAccountSigner<HDAccount> {
const signer = mnemonicToAccount(key, opts);
return new LocalAccountSigner(signer);
}
static privateKeyToAccountSigner(
key: Hex
): LocalAccountSigner<PrivateKeyAccount> {
const signer = privateKeyToAccount(key);
return new LocalAccountSigner(signer);
}
}