-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.ts
36 lines (35 loc) · 897 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
31
32
33
34
35
36
import { utils } from "@project-serum/anchor";
import { Keypair } from "@solana/web3.js";
export const keypairFrom = (s: string | undefined, n?: string): Keypair => {
if (!s) throw `${n ?? "keypair"} is undefined`;
try {
if (s.includes("[")) {
return Keypair.fromSecretKey(
Buffer.from(
s
.replace("[", "")
.replace("]", "")
.split(",")
.map((c) => parseInt(c))
)
);
} else {
return Keypair.fromSecretKey(utils.bytes.bs58.decode(s));
}
} catch (e) {
try {
return Keypair.fromSecretKey(
Buffer.from(
JSON.parse(
require("fs").readFileSync(s, {
encoding: "utf-8",
})
)
)
);
} catch (e) {
process.stdout.write(`${n ?? "keypair"} is not valid keypair`);
process.exit(1);
}
}
};