-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.ts
30 lines (27 loc) · 818 Bytes
/
utils.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
import { readFile, writeFile } from 'fs/promises';
import { decode } from 'bs58';
import { Keypair } from '@solana/web3.js';
export async function loadSecretKey(filename: string): Promise<Keypair | undefined> {
try {
const data = (await readFile('secrets/' + filename)).toString();
let secretKey;
try {
const keyArray = JSON.parse(data);
secretKey = new Uint8Array(keyArray);
} catch (e) {
secretKey = decode(data);
}
return Keypair.fromSecretKey(secretKey);
}
catch (e) {
console.log(e);
console.log(`Please setup ${filename} in secrets folder`);
}
}
export async function saveSecretKey(keypair: Keypair, filename: string) {
try {
await writeFile('secrets/' + filename, `[${keypair.secretKey.toString()}]`);
} catch (e) {
console.log(e);
}
}