forked from decred/politeiagui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.js
151 lines (140 loc) · 3.56 KB
/
generate.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
import faker from "faker";
import { User } from "../users/generate";
import {
bufferToBase64String,
getIndexMdFromText,
digestPayload,
objectToBuffer
} from "./utils";
import compose from "lodash/fp/compose";
import entries from "lodash/fp/entries";
import reduce from "lodash/fp/reduce";
import range from "lodash/fp/range";
import map from "lodash/fp/map";
import chunk from "lodash/fp/chunk";
import get from "lodash/fp/get";
// Record
export const Token = (isShort) =>
faker.git.shortSha().slice(0, isShort ? 7 : 16);
export function Metadata(pluginid, streamid, payload) {
this.pluginid = pluginid;
this.streamid = streamid;
this.payload = JSON.stringify(payload);
}
export function UserMetadata({ userid, publickey, signature }) {
return new Metadata("usermd", faker.datatype.number(3), {
userid,
publickey,
signature
});
}
export function RecordMetadata({
token,
version,
status,
publickey,
signature,
timestamp
}) {
return new Metadata("usermd", faker.datatype.number(3), {
token,
version,
status,
publickey,
signature,
timestamp
});
}
export const fileImage = {
name: `image_${faker.name.title()}.png`,
mime: "image/png",
payload: bufferToBase64String(faker.image.image())
};
export const fileIndex = getIndexMdFromText(
`# ${faker.name.title()}\n\n${faker.lorem.paragraphs()}`
);
export function fileJson({ name, object }) {
return {
name,
mime: "text/plain; charset=utf-8",
payload: bufferToBase64String(objectToBuffer(object))
};
}
export function File({ name, mime, payload }) {
this.name = name;
this.mime = mime;
this.payload = payload;
this.digest = digestPayload(payload);
}
/**
* Record instantiates a new Record structure customizeable by the recordProps
* parameter.
* @param {Object} { author, token, status, state, vesion, files }
*/
export function Record({
author,
token: recordToken,
status: recordStatus,
state: recordState,
version: recordVersion,
publickey,
signature: recordSignature,
files = []
} = {}) {
const token = recordToken || Token();
const status = recordStatus || faker.datatype.number(3) + 1;
const state = recordState || faker.datatype.number(1) + 1;
const user = author instanceof User || new User();
const timestamp = Date.now() / 1000;
const version = recordVersion || faker.datatype.number(6) + 1;
const signature = faker.datatype.hexaDecimal(128, false, /[0-9a-z]/);
this.status = status;
this.state = state;
this.timestamp = timestamp;
this.username = user.username;
this.version = version;
this.censorshiprecord = {
token,
signature,
merkle: faker.datatype.hexaDecimal(64, false, /[0-9a-z]/)
};
if (files.length == 2) {
this.files = files;
} else {
this.files = [new File(fileIndex), ...files.map((f) => new File(f))];
}
this.metadata = [
UserMetadata(user),
RecordMetadata({
token,
version,
status,
publickey: publickey || user.publickey,
recordSignature,
timestamp
})
];
}
/**
* Inventory returns a new inventory map with random tokens according to
* the amountByStatus ({ [status]: amount }) object.
* @param { Object } amountByStatus
* @param { Object } { pageLimit, page }
*/
export function Inventory(amountByStatus = {}, { pageLimit = 20, page = 1 }) {
return compose(
reduce(
(acc, [status, amount]) => ({
...acc,
[status]: compose(
get(page - 1),
chunk(pageLimit),
map(() => Token()),
range(amount)
)(0)
}),
{}
),
entries
)(amountByStatus);
}