forked from ubiquity/ubiquibot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprivate.ts
More file actions
138 lines (119 loc) · 4.44 KB
/
Copy pathprivate.ts
File metadata and controls
138 lines (119 loc) · 4.44 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import _sodium from "libsodium-wrappers";
import YAML from "yaml";
import { Payload } from "../types";
import { Context } from "probot";
import { getAnalyticsMode, getAutoPayMode, getBaseMultiplier, getBountyHunterMax, getChainId, getPriorityLabels, getTimeLabels } from "./helpers";
const CONFIG_REPO = "ubiquibot-config";
const KEY_PATH = ".github/ubiquibot-config.yml";
const KEY_NAME = "private-key-encrypted";
const KEY_PREFIX = "HSK_";
export const getConfigSuperset = async (context: Context, type: "org" | "repo"): Promise<string | undefined> => {
try {
const payload = context.payload as Payload;
const repo = type === "org" ? CONFIG_REPO : payload.repository.name!;
const { data } = await context.octokit.rest.repos.getContent({
owner: payload.organization?.login!,
repo: repo,
path: KEY_PATH,
mediaType: {
format: "raw",
},
});
return data as unknown as string;
} catch (error: any) {
return undefined;
}
};
export interface WideLabel {
name: string;
weight: number;
value?: number | undefined;
target: string;
}
export interface WideConfig {
"chain-id"?: number;
"base-multiplier"?: number;
"time-labels"?: WideLabel[];
"priority-labels"?: WideLabel[];
"auto-pay-mode"?: boolean;
"analytics-mode"?: boolean;
"max-concurrent-bounties"?: number;
}
export interface WideRepoConfig extends WideConfig {}
export interface WideOrgConfig extends WideConfig {
"private-key-encrypted"?: string;
}
export interface DataConfig {
chainId: number;
privateKey: string;
baseMultiplier: number;
timeLabels: WideLabel[];
priorityLabels: WideLabel[];
autoPayMode: boolean;
analyticsMode: boolean;
bountyHunterMax: number;
}
export const parseYAML = async (data: any): Promise<any | undefined> => {
try {
const parsedData = await YAML.parse(data);
if (parsedData !== null) {
return parsedData;
} else {
return undefined;
}
} catch (error) {
return undefined;
}
};
export const getPrivateKey = async (cipherText: string): Promise<string | undefined> => {
try {
await _sodium.ready;
const sodium = _sodium;
const privateKey = process.env.X25519_PRIVATE_KEY;
const publicKey = await getScalarKey(privateKey);
if (!publicKey || !privateKey) {
return undefined;
}
const binPub = sodium.from_base64(publicKey, sodium.base64_variants.URLSAFE_NO_PADDING);
const binPriv = sodium.from_base64(privateKey, sodium.base64_variants.URLSAFE_NO_PADDING);
const binCipher = sodium.from_base64(cipherText, sodium.base64_variants.URLSAFE_NO_PADDING);
let walletPrivateKey: string | undefined = sodium.crypto_box_seal_open(binCipher, binPub, binPriv, "text");
walletPrivateKey = walletPrivateKey.replace(KEY_PREFIX, "");
return walletPrivateKey;
} catch (error: any) {
return undefined;
}
};
export const getScalarKey = async (X25519_PRIVATE_KEY: string | undefined): Promise<string | undefined> => {
try {
if (X25519_PRIVATE_KEY !== undefined) {
await _sodium.ready;
const sodium = _sodium;
const binPriv = sodium.from_base64(X25519_PRIVATE_KEY, sodium.base64_variants.URLSAFE_NO_PADDING);
const scalerPub = sodium.crypto_scalarmult_base(binPriv, "base64");
return scalerPub;
}
return undefined;
} catch (error: any) {
return undefined;
}
};
export const getWideConfig = async (context: Context): Promise<DataConfig> => {
const orgConfig = await getConfigSuperset(context, "org");
const repoConfig = await getConfigSuperset(context, "repo");
const parsedOrg: WideOrgConfig | undefined = await parseYAML(orgConfig);
const parsedRepo: WideRepoConfig | undefined = await parseYAML(repoConfig);
const privateKeyDecrypted = parsedOrg && parsedOrg[KEY_NAME] ? await getPrivateKey(parsedOrg[KEY_NAME]) : undefined;
const configData: DataConfig = {
chainId: getChainId(parsedRepo, parsedOrg),
// TODO: remove "process.env.UBIQUITY_BOT_EVM_PRIVATE_KEY" when all partners are migrate to org wide config
privateKey: privateKeyDecrypted ?? process.env.UBIQUITY_BOT_EVM_PRIVATE_KEY ?? "",
baseMultiplier: getBaseMultiplier(parsedRepo, parsedOrg),
timeLabels: getTimeLabels(parsedRepo, parsedOrg),
priorityLabels: getPriorityLabels(parsedRepo, parsedOrg),
autoPayMode: getAutoPayMode(parsedRepo, parsedOrg),
analyticsMode: getAnalyticsMode(parsedRepo, parsedOrg),
bountyHunterMax: getBountyHunterMax(parsedRepo, parsedOrg),
};
return configData;
};