-
Notifications
You must be signed in to change notification settings - Fork 3
/
api.js
168 lines (154 loc) · 4.3 KB
/
api.js
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
163
164
165
166
167
168
var express = require("express");
var dhive = require("@hiveio/dhive");
var signale = require("signale");
var _ = require("lodash");
var config = require("./config.json");
var router = express.Router();
let client = new dhive.Client(config.rpc);
router.get("/", (req, res) => {
signale.info(new Date(), "- GET request to /api - Source: " + req.ip);
res.json({
owner_account: config.accounts[0].account,
});
});
router.post("/createAccount", (req, res) => {
let auth = req.headers.authority;
if (!auth) {
signale.warn(
new Date(),
"- POST request to /api/createAccount - Refused: Authorization header missing. - Source: " +
req.ip
);
res.status(401).send("Authorization header missing.");
} else {
let provider = _.find(config.auth, { key: auth });
if (!provider) {
signale.warn(
new Date(),
"- POST request to /api/createAccount - Refused: Invalid auth token. - Source: " +
req.ip
);
res.status(401).send("Invalid auth token.");
} else {
signale.info(
new Date(),
"- POST request to /api/createAccount - Provider: " +
provider.label +
" - Source: " +
req.ip
);
let name = req.body.name;
let publicKeys = req.body.publicKeys;
let metaData = req.body.metaData;
let creator = req.body.creator;
let creatorRequested = req.body.creatorRequested;
let account = _.find(config.accounts, { account: creator });
if (!account) {
signale.warn(
new Date(),
"- POST request to /api/createAccount - Error: Invalid creator account. - Source: " +
req.ip
);
res.status(400).send("Invalid creator account.");
} else {
if (
typeof name === "undefined" ||
typeof publicKeys === "undefined" ||
typeof metaData === "undefined"
) {
signale.error(
new Date(),
"- POST request to /api/createAccount - Error: Body data is invalid or missing. - Source: " +
req.ip
);
res.status(400).send("Body data is invalid or missing.");
} else {
res.setHeader("Content-Type", "application/json");
let postingAccountAuth =
account.setPostingAccountAuth &&
typeof creator !== "undefined" &&
creatorRequested
? true
: false;
createAccount(
name,
publicKeys,
metaData,
postingAccountAuth,
account
).then((response) => {
res.send(response);
});
}
}
}
}
});
function createAccount(
name,
publicKeys,
metaData,
postingAccountAuth,
account
) {
const postingAccountAuthArray = postingAccountAuth
? [[account.account, 1]]
: [];
const ownerAuth = {
weight_threshold: 1,
account_auths: [],
key_auths: [[publicKeys.owner, 1]],
};
const activeAuth = {
weight_threshold: 1,
account_auths: [],
key_auths: [[publicKeys.active, 1]],
};
const postingAuth = {
weight_threshold: 1,
account_auths: postingAccountAuthArray,
key_auths: [[publicKeys.posting, 1]],
};
let privateKey = dhive.PrivateKey.fromString(account.key);
let ops = [];
const create_op = [
"create_claimed_account",
{
creator: account.account,
new_account_name: name,
owner: ownerAuth,
active: activeAuth,
posting: postingAuth,
memo_key: publicKeys.memo,
json_metadata: JSON.stringify(metaData),
extensions: [],
},
];
ops.push(create_op);
return client.broadcast.sendOperations(ops, privateKey).then(
function (result) {
signale.success(
new Date(),
"- OP: " +
account.account +
" - create_claimed_account - Success: Account " +
name +
" was created."
);
return_status = JSON.stringify({ created: true, name: name });
return return_status;
},
function (error) {
signale.error(
new Date(),
"- OP: " +
account.account +
" - create_claimed_account - Error: " +
error
);
return_status = JSON.stringify({ created: false, name: name });
return return_status;
}
);
}
module.exports = router;