-
Notifications
You must be signed in to change notification settings - Fork 0
/
composites.ts
255 lines (220 loc) · 8.01 KB
/
composites.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import { readFileSync, writeFileSync } from "fs";
import { CeramicClient } from "@ceramicnetwork/http-client";
import {
createComposite,
readEncodedComposite,
writeEncodedComposite,
writeEncodedCompositeRuntime,
} from "@composedb/devtools-node";
import { Composite } from "@composedb/devtools";
import { DID } from "dids";
import { Ed25519Provider } from "key-did-provider-ed25519";
import { getResolver } from "key-did-resolver";
import { fromString } from "uint8arrays/from-string";
import { Ora } from "ora";
const ceramic = new CeramicClient(
process.env.CERAMIC_ENDPOINT || "http://localhost:7007",
);
const ENCODED_PATH = "./src/__generated__/definition.json";
const ENCODED_RUNTIME_PATH = "./src/__generated__/definition.js";
type Models = {
profile?: string;
socialHandle?: string;
researchObject?: string;
researchField?: string;
claim?: string;
attestation?: string;
researchComponent?: string;
referenceRelation?: string;
contributorRelation?: string;
researchFieldRelation?: string;
annotation?: string;
};
export const writeComposite = async (seed: string, spinner?: Ora) => {
if (!spinner) {
spinner = { info: console.log, succeed: console.log } as Ora;
}
await authenticateAdmin(seed);
spinner.info("writing composite to Ceramic");
/** Collect streamID of each composite as it is created */
const modelIDs: Models = {};
const profileComposite = await createComposite(
ceramic,
"./composites/1-profile.graphql",
);
modelIDs.profile = profileComposite.modelIDs[0];
const socialHandleComposite = await createComposite(
ceramic,
"./composites/1-socialHandle.graphql",
);
modelIDs.socialHandle = socialHandleComposite.modelIDs[0];
const researchObjectComposite = await createComposite(
ceramic,
"./composites/1-researchObject.graphql",
);
modelIDs.researchObject = researchObjectComposite.modelIDs[0];
const researchFieldComposite = await createComposite(
ceramic,
"./composites/1-researchField.graphql",
);
modelIDs.researchField = researchFieldComposite.modelIDs[0];
const claimComposite = await createComposite(
ceramic,
"./composites/1-claim.graphql",
);
modelIDs.claim = claimComposite.modelIDs[0];
const attestationSchema = readFileSync("./composites/2-attestation.graphql", {
encoding: "utf-8",
}).replace("$CLAIM_ID", modelIDs.claim);
const attestationComposite = await Composite.create({
ceramic,
schema: attestationSchema,
});
modelIDs.attestation = attestationComposite.modelIDs[1];
const researchComponentSchema = readFileSync(
"./composites/2-researchComponent.graphql",
{ encoding: "utf-8" },
).replace("$RESEARCH_OBJECT_ID", modelIDs.researchObject);
const researchComponentComposite = await Composite.create({
ceramic,
schema: researchComponentSchema,
});
modelIDs.researchComponent = researchComponentComposite.modelIDs[1];
const referenceRelationSchema = readFileSync(
"./composites/2-referenceRelation.graphql",
{ encoding: "utf-8" },
).replace("$RESEARCH_OBJECT_ID", modelIDs.researchObject);
const referenceRelationComposite = await Composite.create({
ceramic,
schema: referenceRelationSchema,
});
modelIDs.referenceRelation = referenceRelationComposite.modelIDs[1];
const contributorRelationSchema = readFileSync(
"./composites/2-contributorRelation.graphql",
{ encoding: "utf-8" },
)
.replace("$RESEARCH_OBJECT_ID", modelIDs.researchObject)
.replace("$PROFILE_ID", modelIDs.profile);
const contributorRelationComposite = await Composite.create({
ceramic,
schema: contributorRelationSchema,
});
modelIDs.contributorRelation = contributorRelationComposite.modelIDs[2];
const researchFieldRelationSchema = readFileSync(
"./composites/2-researchFieldRelation.graphql",
{ encoding: "utf-8" },
)
.replace("$RESEARCH_OBJECT_ID", modelIDs.researchObject)
.replace("$RESEARCH_FIELD_ID", modelIDs.researchField);
const researchFieldRelationComposite = await Composite.create({
ceramic,
schema: researchFieldRelationSchema,
});
modelIDs.researchFieldRelation = researchFieldRelationComposite.modelIDs[2];
const annotationSchema = readFileSync("./composites/2-annotation.graphql", {
encoding: "utf-8",
})
.replace("$CLAIM_ID", modelIDs.claim)
.replace("$RESEARCH_OBJECT_ID", modelIDs.researchObject);
const annotationComposite = await Composite.create({
ceramic,
schema: annotationSchema,
});
modelIDs.annotation = annotationComposite.modelIDs[2];
const additionalRelationsSchema = readFileSync(
"./composites/3-additionalRelations.graphql",
{ encoding: "utf-8" },
)
.replace("$ATTESTATION_ID", modelIDs.attestation)
.replace("$CLAIM_ID", modelIDs.claim)
.replace("$RESEARCH_OBJECT_ID", modelIDs.researchObject)
.replace("$PROFILE_ID", modelIDs.profile)
.replace("$RESEARCH_COMPONENT_ID", modelIDs.researchComponent)
.replace("$CONTRIBUTOR_RELATION_ID", modelIDs.contributorRelation)
.replace("$REFERENCE_RELATION_ID", modelIDs.referenceRelation)
.replace("$RESEARCH_FIELD_ID", modelIDs.researchField)
.replace("$RESEARCH_FIELD_RELATION_ID", modelIDs.researchFieldRelation)
.replace("$ANNOTATION_ID", modelIDs.annotation);
const additionalRelationsComposite = await Composite.create({
ceramic,
schema: additionalRelationsSchema,
});
const composite = Composite.from([
profileComposite,
socialHandleComposite,
researchObjectComposite,
claimComposite,
attestationComposite,
researchComponentComposite,
additionalRelationsComposite,
contributorRelationComposite,
referenceRelationComposite,
researchFieldComposite,
researchFieldRelationComposite,
annotationComposite,
]);
await writeEncodedComposite(composite, ENCODED_PATH);
spinner.info("creating composite for runtime usage");
await writeEncodedCompositeRuntime(
ceramic,
ENCODED_PATH,
ENCODED_RUNTIME_PATH,
);
// Fix non-determinism due to arbitrarily sorted keys in files
await orderCompositeFileKeys();
spinner.info("deploying composite");
const deployComposite = await readEncodedComposite(ceramic, ENCODED_PATH);
await deployComposite.startIndexingOn(ceramic);
spinner.succeed("composite deployed & ready for use");
};
/**
* Authenticating DID for publishing composite
*/
const authenticateAdmin = async (seed: string): Promise<void> => {
const key = fromString(seed, "base16");
const did = new DID({
resolver: getResolver(),
provider: new Ed25519Provider(key),
});
await did.authenticate();
await ceramic.setDID(did);
};
/**
* Repeated runs yield diffs in generated composite definition files, even
* if they are semantically the same, because the keys aren't serialized
* in order. This function fixes that, making it clear in git when they
* actually have changed.
*/
const orderCompositeFileKeys = async () => {
/* eslint-disable @typescript-eslint/no-explicit-any*/
/**
* Recursively order object keys to get deterministic serialization
* https://gist.github.com/davidfurlong/463a83a33b70a3b6618e97ec9679e490
*/
const orderedReplacer = (_key: any, value: any) =>
value instanceof Object && !(value instanceof Array)
? Object.keys(value)
.sort()
.reduce((sorted, key) => {
sorted[key] = value[key];
return sorted;
}, {} as any)
: value;
const encoded = JSON.parse(readFileSync(ENCODED_PATH, { encoding: "ascii" }));
writeFileSync(ENCODED_PATH, JSON.stringify(encoded, orderedReplacer));
const { definition } = await import(
process.cwd() + "/" + ENCODED_RUNTIME_PATH
);
const encoded_runtime_ordered = `
export const definition = ${JSON.stringify(definition, orderedReplacer)}
`;
writeFileSync(ENCODED_RUNTIME_PATH, encoded_runtime_ordered);
};
const runAsScript =
process.argv[0].includes("/bin/node") &&
process.argv[1].includes("scripts/composites.ts");
if (runAsScript) {
const seed = process.env.ADMIN_SEED;
if (!seed) throw new Error("No ADMIN_SEED environment variable set!");
await writeComposite(seed);
}