-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathadmin.ts
162 lines (154 loc) · 4.23 KB
/
admin.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import * as mvc from 'mvc-lib';
import axios from 'axios';
import { signMessage } from '../lib/hash';
const getNodeInfo = async (host: string) => {
const url = `${host}/admin/info`;
return (await axios.get(url)).data;
};
const commitInit = async (host: string, pubkey: string, sig: string) => {
const data = {
pubkey,
sig,
};
const body = JSON.stringify(data);
const config = {
method: 'post',
maxBodyLength: Infinity,
url: `${host}/admin/commitInit`,
headers: {
'Content-Type': 'application/json',
},
data: body,
};
return (await axios.request(config)).data;
};
const updateAdminPub = async (host: string, data: any) => {
const body = JSON.stringify(data);
const config = {
method: 'post',
maxBodyLength: Infinity,
url: `${host}/admin/updateAdminPub`,
headers: {
'Content-Type': 'application/json',
},
data: body,
};
return (await axios.request(config)).data;
};
const updateConfig = async (host: string, data: any) => {
const body = JSON.stringify(data);
const config = {
method: 'post',
maxBodyLength: Infinity,
url: `${host}/admin/updateConfig`,
headers: {
'Content-Type': 'application/json',
},
data: body,
};
return (await axios.request(config)).data;
};
const addAuthPubkey = async (host: string, data: any) => {
const body = JSON.stringify(data);
const config = {
method: 'post',
maxBodyLength: Infinity,
url: `${host}/admin/addAuthPubkey`,
headers: {
'Content-Type': 'application/json',
},
data: body,
};
return (await axios.request(config)).data;
};
const addListenPubkey = async (host: string, data: any) => {
const body = JSON.stringify(data);
const config = {
method: 'post',
maxBodyLength: Infinity,
url: `${host}/admin/addListenPubkey`,
headers: {
'Content-Type': 'application/json',
},
data: body,
};
return (await axios.request(config)).data;
};
const main = async function () {
// set INDEXER_HOST
// set INDEXER_ADMIN_WIF
const host = process.env.INDEXER_HOST;
const privateKey = mvc.PrivateKey(process.env.INDEXER_ADMIN_WIF);
const pubkey = privateKey.publicKey.toString();
console.log('pubkey', pubkey);
{
// init admin
const nodeInfo: {
data: { status: string; inGroup: boolean; adminPub: string };
} = await getNodeInfo(host);
console.log('nodeInfo', host, nodeInfo);
if (nodeInfo.data.status === 'Uninitialized') {
// demo init admin
const sig = signMessage(privateKey, pubkey);
const commitInfo = await commitInit(host, pubkey, sig);
console.log(commitInfo);
} else {
// demo update admin
const newPrivateKey = privateKey;
const newPubkey = pubkey;
const newAdminSig = signMessage(newPrivateKey, newPubkey);
const data = {
newPubkey: newPubkey,
newAdminSig: newAdminSig,
};
const sig = signMessage(privateKey, JSON.stringify(data));
const body = {
sig,
data: data,
};
const resp = await updateAdminPub(host, body);
console.log('admin update', resp);
}
}
{
// set callback url
const data = {
key: 'callbackUrl',
value: 'http://127.0.0.1:15001/callback',
};
const sig = signMessage(privateKey, JSON.stringify(data));
const body = {
sig,
data: data,
};
const resp = await updateConfig(host, body);
console.log('updateConfig:', resp);
}
{
// add auth pubkey
// demo address is random
const authPubkey =
'0277130230821766ee72f50dcaa3d68bba99d20a700b6747879f904c232000684a';
const dto = {
notifyPubkey: authPubkey,
publicKey: pubkey,
publicKeySign: signMessage(privateKey, authPubkey),
};
const resp = await addAuthPubkey(host, dto);
console.log('authPubkey resp:', resp);
}
{
// add listen pubkey
// demo address is f2 pool miner
const listenPubkey =
'025dc17b4f5ac34e0c3e179c04df18eab375312f762a12eb15e6188e46e584b26c';
const dto = {
notifyPubkey: listenPubkey,
publicKey: pubkey,
publicKeySign: signMessage(privateKey, listenPubkey),
};
const resp = await addListenPubkey(host, dto);
console.log('addListenPubkey resp:', resp);
}
};
main().then();