@vocdoni/davinci-sdk 0.0.7 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +48 -28
- package/dist/index.d.ts +78 -88
- package/dist/index.js +125 -146
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +122 -144
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +125 -146
- package/package.json +1 -1
package/dist/index.umd.js
CHANGED
|
@@ -219,34 +219,12 @@
|
|
|
219
219
|
}
|
|
220
220
|
}
|
|
221
221
|
|
|
222
|
-
var CensusType = /* @__PURE__ */ ((CensusType2) => {
|
|
223
|
-
CensusType2["PLAIN"] = "plain";
|
|
224
|
-
CensusType2["WEIGHTED"] = "weighted";
|
|
225
|
-
CensusType2["CSP"] = "csp";
|
|
226
|
-
return CensusType2;
|
|
227
|
-
})(CensusType || {});
|
|
228
222
|
class Census {
|
|
229
|
-
constructor(
|
|
223
|
+
constructor(censusOrigin) {
|
|
230
224
|
this._censusId = null;
|
|
231
225
|
this._censusRoot = null;
|
|
232
226
|
this._censusURI = null;
|
|
233
|
-
this.
|
|
234
|
-
this._type = type;
|
|
235
|
-
if (censusOrigin !== void 0) {
|
|
236
|
-
this._censusOrigin = censusOrigin;
|
|
237
|
-
} else {
|
|
238
|
-
switch (type) {
|
|
239
|
-
case "plain" /* PLAIN */:
|
|
240
|
-
case "weighted" /* WEIGHTED */:
|
|
241
|
-
this._censusOrigin = CensusOrigin.OffchainStatic;
|
|
242
|
-
break;
|
|
243
|
-
case "csp" /* CSP */:
|
|
244
|
-
this._censusOrigin = CensusOrigin.CSP;
|
|
245
|
-
break;
|
|
246
|
-
default:
|
|
247
|
-
throw new Error(`Unknown census type: ${type}`);
|
|
248
|
-
}
|
|
249
|
-
}
|
|
227
|
+
this._censusOrigin = censusOrigin;
|
|
250
228
|
}
|
|
251
229
|
get censusId() {
|
|
252
230
|
return this._censusId;
|
|
@@ -257,12 +235,6 @@
|
|
|
257
235
|
get censusURI() {
|
|
258
236
|
return this._censusURI;
|
|
259
237
|
}
|
|
260
|
-
get type() {
|
|
261
|
-
return this._type;
|
|
262
|
-
}
|
|
263
|
-
get size() {
|
|
264
|
-
return this._size;
|
|
265
|
-
}
|
|
266
238
|
get isPublished() {
|
|
267
239
|
return this._censusRoot !== null && this._censusURI !== null;
|
|
268
240
|
}
|
|
@@ -272,91 +244,41 @@
|
|
|
272
244
|
get censusOrigin() {
|
|
273
245
|
return this._censusOrigin;
|
|
274
246
|
}
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
class PlainCensus extends Census {
|
|
278
|
-
/**
|
|
279
|
-
* Creates a new PlainCensus
|
|
280
|
-
* @param censusOrigin - The census origin (defaults to OffchainStatic for backward compatibility)
|
|
281
|
-
*/
|
|
282
|
-
constructor(censusOrigin) {
|
|
283
|
-
super(CensusType.PLAIN, censusOrigin);
|
|
284
|
-
this._participants = /* @__PURE__ */ new Set();
|
|
285
|
-
}
|
|
286
247
|
/**
|
|
287
|
-
*
|
|
288
|
-
*
|
|
248
|
+
* Check if this census requires publishing via the Census API
|
|
249
|
+
* Merkle censuses (OffchainStatic, OffchainDynamic) need to be published
|
|
250
|
+
* Onchain and CSP censuses are ready immediately upon construction
|
|
289
251
|
*/
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
for (const address of toAdd) {
|
|
293
|
-
this.validateAddress(address);
|
|
294
|
-
this._participants.add(address.toLowerCase());
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
/**
|
|
298
|
-
* Remove participant by address
|
|
299
|
-
*/
|
|
300
|
-
remove(address) {
|
|
301
|
-
this._participants.delete(address.toLowerCase());
|
|
302
|
-
}
|
|
303
|
-
/**
|
|
304
|
-
* Get all participants as CensusParticipant array (for API)
|
|
305
|
-
* All participants have weight="1"
|
|
306
|
-
*/
|
|
307
|
-
get participants() {
|
|
308
|
-
return Array.from(this._participants).map((key) => ({
|
|
309
|
-
key,
|
|
310
|
-
weight: "1"
|
|
311
|
-
// Everyone has weight=1 in plain census
|
|
312
|
-
}));
|
|
313
|
-
}
|
|
314
|
-
/**
|
|
315
|
-
* Get addresses only
|
|
316
|
-
*/
|
|
317
|
-
get addresses() {
|
|
318
|
-
return Array.from(this._participants);
|
|
319
|
-
}
|
|
320
|
-
validateAddress(address) {
|
|
321
|
-
if (!address || typeof address !== "string") {
|
|
322
|
-
throw new Error("Address is required and must be a string");
|
|
323
|
-
}
|
|
324
|
-
if (!/^(0x)?[0-9a-fA-F]{40}$/i.test(address)) {
|
|
325
|
-
throw new Error(`Invalid Ethereum address format: ${address}`);
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
/**
|
|
329
|
-
* Internal method called after publishing
|
|
330
|
-
* @internal
|
|
331
|
-
*/
|
|
332
|
-
_setPublishedData(root, uri, size, censusId) {
|
|
333
|
-
this._censusRoot = root;
|
|
334
|
-
this._censusURI = uri;
|
|
335
|
-
this._size = size;
|
|
336
|
-
if (censusId) this._censusId = censusId;
|
|
252
|
+
get requiresPublishing() {
|
|
253
|
+
return this._censusOrigin === CensusOrigin.OffchainStatic || this._censusOrigin === CensusOrigin.OffchainDynamic;
|
|
337
254
|
}
|
|
338
255
|
}
|
|
339
256
|
|
|
340
|
-
class
|
|
341
|
-
/**
|
|
342
|
-
* Creates a new WeightedCensus
|
|
343
|
-
* @param censusOrigin - The census origin (defaults to OffchainStatic for backward compatibility)
|
|
344
|
-
*/
|
|
257
|
+
class MerkleCensus extends Census {
|
|
345
258
|
constructor(censusOrigin) {
|
|
346
|
-
super(
|
|
259
|
+
super(censusOrigin);
|
|
347
260
|
this._participants = /* @__PURE__ */ new Map();
|
|
348
261
|
}
|
|
349
262
|
/**
|
|
350
|
-
* Add participant(s)
|
|
351
|
-
*
|
|
352
|
-
*
|
|
263
|
+
* Add participant(s) - supports both plain addresses and weighted participants
|
|
264
|
+
* @param data - Can be:
|
|
265
|
+
* - string: single address (weight=1)
|
|
266
|
+
* - string[]: array of addresses (weight=1 for all)
|
|
267
|
+
* - {key: string, weight: string|number|bigint}: single weighted participant
|
|
268
|
+
* - Array of weighted participants
|
|
353
269
|
*/
|
|
354
|
-
add(
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
270
|
+
add(data) {
|
|
271
|
+
if (typeof data === "string") {
|
|
272
|
+
this.addAddress(data, "1");
|
|
273
|
+
} else if (Array.isArray(data)) {
|
|
274
|
+
if (data.length === 0) return;
|
|
275
|
+
if (typeof data[0] === "string") {
|
|
276
|
+
data.forEach((addr) => this.addAddress(addr, "1"));
|
|
277
|
+
} else {
|
|
278
|
+
data.forEach((p) => this.addParticipant(p));
|
|
279
|
+
}
|
|
280
|
+
} else {
|
|
281
|
+
this.addParticipant(data);
|
|
360
282
|
}
|
|
361
283
|
}
|
|
362
284
|
/**
|
|
@@ -386,6 +308,21 @@
|
|
|
386
308
|
getWeight(address) {
|
|
387
309
|
return this._participants.get(address.toLowerCase());
|
|
388
310
|
}
|
|
311
|
+
/**
|
|
312
|
+
* Internal method to add a plain address with a given weight
|
|
313
|
+
*/
|
|
314
|
+
addAddress(address, weight) {
|
|
315
|
+
this.validateAddress(address);
|
|
316
|
+
this._participants.set(address.toLowerCase(), weight);
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Internal method to add a weighted participant
|
|
320
|
+
*/
|
|
321
|
+
addParticipant(participant) {
|
|
322
|
+
this.validateAddress(participant.key);
|
|
323
|
+
const weight = this.normalizeWeight(participant.weight);
|
|
324
|
+
this._participants.set(participant.key.toLowerCase(), weight);
|
|
325
|
+
}
|
|
389
326
|
/**
|
|
390
327
|
* Normalizes weight from string, number, or bigint to string
|
|
391
328
|
*/
|
|
@@ -410,32 +347,63 @@
|
|
|
410
347
|
}
|
|
411
348
|
throw new Error(`Invalid weight type. Must be string, number, or bigint.`);
|
|
412
349
|
}
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
if (
|
|
418
|
-
throw new Error(
|
|
350
|
+
/**
|
|
351
|
+
* Validates Ethereum address format
|
|
352
|
+
*/
|
|
353
|
+
validateAddress(address) {
|
|
354
|
+
if (!address || typeof address !== "string") {
|
|
355
|
+
throw new Error("Address is required and must be a string");
|
|
419
356
|
}
|
|
420
|
-
if (
|
|
421
|
-
throw new Error(
|
|
357
|
+
if (!/^(0x)?[0-9a-fA-F]{40}$/i.test(address)) {
|
|
358
|
+
throw new Error(`Invalid Ethereum address format: ${address}`);
|
|
422
359
|
}
|
|
423
360
|
}
|
|
424
361
|
/**
|
|
425
362
|
* Internal method called after publishing
|
|
426
363
|
* @internal
|
|
427
364
|
*/
|
|
428
|
-
_setPublishedData(root, uri,
|
|
365
|
+
_setPublishedData(root, uri, censusId) {
|
|
429
366
|
this._censusRoot = root;
|
|
430
367
|
this._censusURI = uri;
|
|
431
|
-
this._size = size;
|
|
432
368
|
if (censusId) this._censusId = censusId;
|
|
433
369
|
}
|
|
434
370
|
}
|
|
435
371
|
|
|
372
|
+
class OffchainCensus extends MerkleCensus {
|
|
373
|
+
constructor() {
|
|
374
|
+
super(CensusOrigin.OffchainStatic);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
class OffchainDynamicCensus extends MerkleCensus {
|
|
379
|
+
constructor() {
|
|
380
|
+
super(CensusOrigin.OffchainDynamic);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
class OnchainCensus extends Census {
|
|
385
|
+
/**
|
|
386
|
+
* Creates an OnchainCensus
|
|
387
|
+
* @param contractAddress - The address of the smart contract (e.g., ERC20, ERC721)
|
|
388
|
+
* @param uri - Optional URI with census information
|
|
389
|
+
*/
|
|
390
|
+
constructor(contractAddress, uri) {
|
|
391
|
+
super(CensusOrigin.Onchain);
|
|
392
|
+
if (!/^(0x)?[0-9a-fA-F]{40}$/i.test(contractAddress)) {
|
|
393
|
+
throw new Error("Contract address is missing or invalid");
|
|
394
|
+
}
|
|
395
|
+
this._contractAddress = contractAddress;
|
|
396
|
+
this._censusRoot = contractAddress;
|
|
397
|
+
this._censusURI = uri || `contract://${contractAddress}`;
|
|
398
|
+
}
|
|
399
|
+
get contractAddress() {
|
|
400
|
+
return this._contractAddress;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
436
404
|
class CspCensus extends Census {
|
|
437
|
-
constructor(publicKey, cspURI
|
|
438
|
-
super(
|
|
405
|
+
constructor(publicKey, cspURI) {
|
|
406
|
+
super(CensusOrigin.CSP);
|
|
439
407
|
if (!/^(0x)?[0-9a-fA-F]+$/.test(publicKey)) {
|
|
440
408
|
throw new Error("Public key is missing or invalid");
|
|
441
409
|
}
|
|
@@ -448,7 +416,6 @@
|
|
|
448
416
|
this._cspURI = cspURI;
|
|
449
417
|
this._censusRoot = publicKey;
|
|
450
418
|
this._censusURI = cspURI;
|
|
451
|
-
this._size = size;
|
|
452
419
|
}
|
|
453
420
|
get publicKey() {
|
|
454
421
|
return this._publicKey;
|
|
@@ -461,17 +428,14 @@
|
|
|
461
428
|
class PublishedCensus extends Census {
|
|
462
429
|
/**
|
|
463
430
|
* Creates a PublishedCensus from existing census data
|
|
464
|
-
* @param
|
|
431
|
+
* @param censusOrigin - The census origin (OffchainStatic, OffchainDynamic, Onchain, or CSP)
|
|
465
432
|
* @param root - The census root
|
|
466
433
|
* @param uri - The census URI
|
|
467
|
-
* @param size - The census size (number of participants)
|
|
468
|
-
* @param censusOrigin - The census origin (optional - defaults based on type if not provided)
|
|
469
434
|
*/
|
|
470
|
-
constructor(
|
|
471
|
-
super(
|
|
435
|
+
constructor(censusOrigin, root, uri) {
|
|
436
|
+
super(censusOrigin);
|
|
472
437
|
this._censusRoot = root;
|
|
473
438
|
this._censusURI = uri;
|
|
474
|
-
this._size = size;
|
|
475
439
|
}
|
|
476
440
|
}
|
|
477
441
|
|
|
@@ -480,7 +444,7 @@
|
|
|
480
444
|
this.censusService = censusService;
|
|
481
445
|
}
|
|
482
446
|
/**
|
|
483
|
-
* Publishes a
|
|
447
|
+
* Publishes a MerkleCensus (OffchainCensus, OffchainDynamicCensus, or OnchainCensus)
|
|
484
448
|
* Creates a working census, adds participants, and publishes it
|
|
485
449
|
*/
|
|
486
450
|
async publish(census) {
|
|
@@ -496,23 +460,24 @@
|
|
|
496
460
|
census._setPublishedData(
|
|
497
461
|
publishResponse.root,
|
|
498
462
|
publishResponse.uri,
|
|
499
|
-
publishResponse.participantCount,
|
|
500
463
|
censusId
|
|
501
464
|
);
|
|
502
465
|
}
|
|
503
466
|
/**
|
|
504
467
|
* Gets census data for process creation
|
|
505
|
-
* Throws if census is not published
|
|
468
|
+
* Throws if census is not ready (published for Merkle/CSP, or constructed for Onchain)
|
|
506
469
|
*/
|
|
507
470
|
getCensusData(census) {
|
|
508
|
-
if (!census.isPublished) {
|
|
509
|
-
throw new Error("
|
|
471
|
+
if (census.requiresPublishing && !census.isPublished) {
|
|
472
|
+
throw new Error("Merkle census must be published before creating a process");
|
|
473
|
+
}
|
|
474
|
+
if (!census.censusRoot || !census.censusURI) {
|
|
475
|
+
throw new Error("Census data is incomplete");
|
|
510
476
|
}
|
|
511
477
|
return {
|
|
512
478
|
type: census.censusOrigin,
|
|
513
479
|
root: census.censusRoot,
|
|
514
|
-
uri: census.censusURI
|
|
515
|
-
size: census.size
|
|
480
|
+
uri: census.censusURI
|
|
516
481
|
};
|
|
517
482
|
}
|
|
518
483
|
}
|
|
@@ -1190,26 +1155,24 @@
|
|
|
1190
1155
|
*/
|
|
1191
1156
|
async handleCensus(census) {
|
|
1192
1157
|
if ("isPublished" in census) {
|
|
1193
|
-
if (census
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
);
|
|
1200
|
-
}
|
|
1201
|
-
await this.censusOrchestrator.publish(census);
|
|
1158
|
+
if (census.requiresPublishing && !census.isPublished) {
|
|
1159
|
+
const censusBaseURL = this.apiService.census?.["axios"]?.defaults?.baseURL;
|
|
1160
|
+
if (!censusBaseURL || censusBaseURL === "" || censusBaseURL === "undefined") {
|
|
1161
|
+
throw new Error(
|
|
1162
|
+
'Census API URL is required to publish Merkle censuses (OffchainCensus, OffchainDynamicCensus). Please provide "censusUrl" when initializing DavinciSDK, or use a pre-published census.'
|
|
1163
|
+
);
|
|
1202
1164
|
}
|
|
1165
|
+
await this.censusOrchestrator.publish(census);
|
|
1203
1166
|
}
|
|
1204
1167
|
const censusData = this.censusOrchestrator.getCensusData(census);
|
|
1205
1168
|
return {
|
|
1206
1169
|
type: censusData.type,
|
|
1207
1170
|
root: censusData.root,
|
|
1208
|
-
size: censusData.size,
|
|
1209
1171
|
uri: censusData.uri
|
|
1210
1172
|
};
|
|
1211
1173
|
}
|
|
1212
|
-
|
|
1174
|
+
const { size, ...censusWithoutSize } = census;
|
|
1175
|
+
return censusWithoutSize;
|
|
1213
1176
|
}
|
|
1214
1177
|
/**
|
|
1215
1178
|
* Gets user-friendly process information by transforming raw contract data
|
|
@@ -1417,7 +1380,22 @@
|
|
|
1417
1380
|
ballotMode,
|
|
1418
1381
|
signature
|
|
1419
1382
|
});
|
|
1420
|
-
|
|
1383
|
+
let maxVoters;
|
|
1384
|
+
if (config.maxVoters !== void 0) {
|
|
1385
|
+
maxVoters = config.maxVoters;
|
|
1386
|
+
} else if ("isPublished" in config.census && config.census.isPublished) {
|
|
1387
|
+
if ("participants" in config.census) {
|
|
1388
|
+
maxVoters = config.census.participants.length;
|
|
1389
|
+
} else {
|
|
1390
|
+
throw new Error(
|
|
1391
|
+
"maxVoters is required when using OnchainCensus, CspCensus, or PublishedCensus. It can only be auto-calculated for published MerkleCensus (OffchainCensus/OffchainDynamicCensus)."
|
|
1392
|
+
);
|
|
1393
|
+
}
|
|
1394
|
+
} else {
|
|
1395
|
+
throw new Error(
|
|
1396
|
+
"maxVoters is required. It can only be omitted when using a published MerkleCensus (OffchainCensus/OffchainDynamicCensus), in which case it defaults to the participant count."
|
|
1397
|
+
);
|
|
1398
|
+
}
|
|
1421
1399
|
const census = {
|
|
1422
1400
|
censusOrigin: censusConfig.type,
|
|
1423
1401
|
censusRoot,
|
|
@@ -3546,7 +3524,6 @@
|
|
|
3546
3524
|
exports.CensusNotUpdatable = CensusNotUpdatable;
|
|
3547
3525
|
exports.CensusOrchestrator = CensusOrchestrator;
|
|
3548
3526
|
exports.CensusOrigin = CensusOrigin;
|
|
3549
|
-
exports.CensusType = CensusType;
|
|
3550
3527
|
exports.CircomProof = CircomProof;
|
|
3551
3528
|
exports.ContractServiceError = ContractServiceError;
|
|
3552
3529
|
exports.CspCensus = CspCensus;
|
|
@@ -3554,12 +3531,15 @@
|
|
|
3554
3531
|
exports.DavinciSDK = DavinciSDK;
|
|
3555
3532
|
exports.ElectionMetadataTemplate = ElectionMetadataTemplate;
|
|
3556
3533
|
exports.ElectionResultsTypeNames = ElectionResultsTypeNames;
|
|
3534
|
+
exports.MerkleCensus = MerkleCensus;
|
|
3535
|
+
exports.OffchainCensus = OffchainCensus;
|
|
3536
|
+
exports.OffchainDynamicCensus = OffchainDynamicCensus;
|
|
3537
|
+
exports.OnchainCensus = OnchainCensus;
|
|
3557
3538
|
exports.OrganizationAdministratorError = OrganizationAdministratorError;
|
|
3558
3539
|
exports.OrganizationCreateError = OrganizationCreateError;
|
|
3559
3540
|
exports.OrganizationDeleteError = OrganizationDeleteError;
|
|
3560
3541
|
exports.OrganizationRegistryService = OrganizationRegistryService;
|
|
3561
3542
|
exports.OrganizationUpdateError = OrganizationUpdateError;
|
|
3562
|
-
exports.PlainCensus = PlainCensus;
|
|
3563
3543
|
exports.ProcessCensusError = ProcessCensusError;
|
|
3564
3544
|
exports.ProcessCreateError = ProcessCreateError;
|
|
3565
3545
|
exports.ProcessDurationError = ProcessDurationError;
|
|
@@ -3577,7 +3557,6 @@
|
|
|
3577
3557
|
exports.VocdoniSequencerService = VocdoniSequencerService;
|
|
3578
3558
|
exports.VoteOrchestrationService = VoteOrchestrationService;
|
|
3579
3559
|
exports.VoteStatus = VoteStatus;
|
|
3580
|
-
exports.WeightedCensus = WeightedCensus;
|
|
3581
3560
|
exports.assertCSPCensusProof = assertCSPCensusProof;
|
|
3582
3561
|
exports.assertMerkleCensusProof = assertMerkleCensusProof;
|
|
3583
3562
|
exports.createProcessSignatureMessage = createProcessSignatureMessage;
|