-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathFakeAccount.ts
More file actions
68 lines (58 loc) · 1.9 KB
/
Copy pathFakeAccount.ts
File metadata and controls
68 lines (58 loc) · 1.9 KB
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
60
61
62
63
64
65
66
67
68
import { Keypair } from '@solana/web3.js';
import nacl from 'tweetnacl';
import { type PrivateKeyAccount, privateKeyToAccount } from 'viem/accounts';
import type { Hash, Option } from './types';
export interface FakeAccount {
generate: () => void;
getAddress: () => Option<string, Hash>;
getRawAddress: () => Uint8Array;
signMessage: (
message: Option<string, Uint8Array, Hash>,
) => Promise<Option<string, Uint8Array, Hash>>;
}
export class EthereumFakeAccount implements FakeAccount {
private account!: PrivateKeyAccount;
constructor() {
this.generate();
}
generate = (): void => {
this.account = privateKeyToAccount(
'0x0000000000000000000000000000000000000000000000000000000000000001',
);
};
getAddress = (): Option<string, Hash> => this.account.address;
getRawAddress = (): Uint8Array =>
Buffer.from(this.account.address.slice(2), 'hex');
signMessage = (
message: Option<string, Uint8Array, Hash>,
): Promise<Option<string, Uint8Array, Hash>> => {
return this.account.signMessage({
message: message as string,
});
};
}
export class SolanaFakeAccount implements FakeAccount {
private keypair!: Keypair;
constructor() {
this.generate();
}
generate = (): void => {
this.keypair = Keypair.generate();
};
getAddress = (): Option<string, Hash> => this.keypair.publicKey.toString();
getRawAddress = (): Uint8Array => this.keypair.publicKey.toBytes();
private getSmallTxId = (
message: Option<string, Uint8Array, Hash>,
): Uint8Array => {
const txIdNo0x = message.slice(2);
const idBytes = `${txIdNo0x.slice(0, 16)}${txIdNo0x.slice(-16)}`;
return new TextEncoder().encode(idBytes);
};
signMessage = (
message: Option<string, Uint8Array, Hash>,
): Promise<Option<string, Uint8Array, Hash>> => {
return Promise.resolve(
nacl.sign.detached(this.getSmallTxId(message), this.keypair.secretKey),
);
};
}