Skip to content

Commit

Permalink
Added new refactored file service
Browse files Browse the repository at this point in the history
  • Loading branch information
marcvelmer committed Sep 7, 2023
1 parent a00c4c5 commit 0d1b71f
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { keccak256 } from '@ethersproject/keccak256';
import { Wallet } from '@ethersproject/wallet';
import { Buffer } from 'buffer';
import invariant from 'tiny-invariant';
import { AccountAPI, FaucetAPI, FileAPI } from './api';
import { AccountAPI, FaucetAPI } from './api';
import { AccountCore } from './core/account';
import { ElectionCore } from './core/election';
import { VoteCore } from './core/vote';
Expand Down Expand Up @@ -34,6 +34,7 @@ import {
CspCensusProof,
CspService,
ElectionService,
FileService,
VoteService,
ZkProof,
} from './services';
Expand Down Expand Up @@ -132,6 +133,7 @@ export class VocdoniSDKClient {
public cspService: CspService;
public electionService: ElectionService;
public voteService: VoteService;
public fileService: FileService;

public url: string;
public wallet: Wallet | Signer | null;
Expand Down Expand Up @@ -164,6 +166,7 @@ export class VocdoniSDKClient {
};
this.explorerUrl = EXPLORER_URL[opts.env];
this.censusService = new CensusService({ url: this.url });
this.fileService = new FileService({ url: this.url });
this.chainService = new ChainService({ url: this.url });
this.anonymousService = new AnonymousService({ url: this.url });
this.electionService = new ElectionService({
Expand Down Expand Up @@ -218,16 +221,6 @@ export class VocdoniSDKClient {
return this.accountData;
}

/**
* Fetches the CID expected for the specified data content.
*
* @param {string} data The data of which we want the CID of
* @returns {Promise<string>} Resulting CID
*/
calculateCID(data: string): Promise<string> {
return FileAPI.cid(this.url, data).then((data) => data.cid);
}

/**
* Fetches a faucet payload. Only for development.
*
Expand Down Expand Up @@ -390,7 +383,7 @@ export class VocdoniSDKClient {

const accountData = Promise.all([
this.fetchChainId(),
this.calculateCID(Buffer.from(JSON.stringify(options.account.generateMetadata()), 'utf8').toString('base64')),
this.fileService.calculateCID(JSON.stringify(options.account.generateMetadata())),
]).then((data) =>
AccountCore.generateCreateAccountTransaction(address, options.account, data[1], faucetPackage, calculatedSik)
);
Expand All @@ -411,7 +404,7 @@ export class VocdoniSDKClient {
const accountData = Promise.all([
this.fetchAccountInfo(),
this.fetchChainId(),
this.calculateCID(Buffer.from(JSON.stringify(account.generateMetadata()), 'utf8').toString('base64')),
this.fileService.calculateCID(JSON.stringify(account.generateMetadata())),
]).then((data) => AccountCore.generateUpdateAccountTransaction(data[0], account, data[2]));

return this.setAccountInfo(accountData);
Expand Down Expand Up @@ -529,7 +522,7 @@ export class VocdoniSDKClient {

const electionData = Promise.all([
this.fetchAccountInfo(),
this.calculateCID(Buffer.from(JSON.stringify(election.generateMetadata()), 'utf8').toString('base64')),
this.fileService.calculateCID(JSON.stringify(election.generateMetadata())),
]).then((data) =>
ElectionCore.generateNewElectionTransaction(election, data[1], this.chainService.chainData, data[0])
);
Expand Down Expand Up @@ -920,4 +913,14 @@ export class VocdoniSDKClient {
public calculateElectionCost(election: UnpublishedElection): Promise<number> {
return this.electionService.calculateElectionCost(election);
}

/**
* Fetches the CID expected for the specified data content.
*
* @param {string} data The data of which we want the CID of
* @returns {Promise<string>} Resulting CID
*/
calculateCID(data: string): Promise<string> {
return this.fileService.calculateCID(data);
}
}
3 changes: 3 additions & 0 deletions src/services/anonymous.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ export class AnonymousService extends Service implements AnonymousServicePropert
}

async fetchAccountSIK(address: string) {
invariant(this.url, 'No URL set');
return ZkAPI.sik(this.url, address);
}

async fetchZKProof(address: string) {
invariant(this.url, 'No URL set');
return ZkAPI.proof(this.url, address);
}

Expand Down Expand Up @@ -137,6 +139,7 @@ export class AnonymousService extends Service implements AnonymousServicePropert
return Promise.resolve(this.chainCircuits);
} catch (e) {}
}
invariant(this.url, 'No URL set');

const setCircuitInfo = this.chainCircuits
? Promise.resolve(this.chainCircuits)
Expand Down
5 changes: 5 additions & 0 deletions src/services/census.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Service, ServiceProperties } from './service';
import { CensusType, PlainCensus, WeightedCensus } from '../types';
import { CensusAPI, WalletAPI } from '../api';
import { Wallet } from '@ethersproject/wallet';
import invariant from 'tiny-invariant';

interface CensusServiceProperties {
auth: CensusAuth;
Expand Down Expand Up @@ -63,6 +64,7 @@ export class CensusService extends Service implements CensusServiceProperties {
* @returns {Promise<{size: number, weight: bigint}>}
*/
fetchCensusInfo(censusId: string): Promise<{ size: number; weight: bigint; type: CensusType }> {
invariant(this.url, 'No URL set');
return Promise.all([
CensusAPI.size(this.url, censusId),
CensusAPI.weight(this.url, censusId),
Expand All @@ -88,6 +90,7 @@ export class CensusService extends Service implements CensusServiceProperties {
* @returns {Promise<CensusProof>}
*/
async fetchProof(censusId: string, key: string): Promise<CensusProof> {
invariant(this.url, 'No URL set');
return CensusAPI.proof(this.url, censusId, key).then((censusProof) => ({
type: censusProof.type,
weight: censusProof.weight,
Expand All @@ -105,6 +108,7 @@ export class CensusService extends Service implements CensusServiceProperties {
* @returns {Promise<void>}
*/
createCensus(census: PlainCensus | WeightedCensus): Promise<void> {
invariant(this.url, 'No URL set');
const censusCreation = this.fetchAccountToken().then(() =>
CensusAPI.create(this.url, this.auth.identifier, census.type)
);
Expand Down Expand Up @@ -135,6 +139,7 @@ export class CensusService extends Service implements CensusServiceProperties {
if (this.auth) {
return Promise.resolve();
}
invariant(this.url, 'No URL set');

this.auth = {
identifier: '',
Expand Down
5 changes: 5 additions & 0 deletions src/services/chain.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Service, ServiceProperties } from './service';
import { ChainAPI, IChainGetCostsResponse, IChainTxReference } from '../api';
import invariant from 'tiny-invariant';

interface ChainServiceProperties {
chainCosts: ChainCosts;
Expand Down Expand Up @@ -42,6 +43,7 @@ export class ChainService extends Service implements ChainServiceProperties {
if (this.chainData) {
return Promise.resolve(this.chainData);
}
invariant(this.url, 'No URL set');

return ChainAPI.info(this.url).then((chainData) => {
this.chainData = chainData;
Expand All @@ -58,6 +60,7 @@ export class ChainService extends Service implements ChainServiceProperties {
if (this.chainCosts) {
return Promise.resolve(this.chainCosts);
}
invariant(this.url, 'No URL set');

return ChainAPI.costs(this.url).then((chainCosts) => {
this.chainCosts = chainCosts;
Expand All @@ -72,6 +75,7 @@ export class ChainService extends Service implements ChainServiceProperties {
* @returns {Promise<string>} The transaction hash
*/
submitTx(payload: string): Promise<string> {
invariant(this.url, 'No URL set');
return ChainAPI.submitTx(this.url, payload).then((txData) => txData.hash);
}

Expand All @@ -82,6 +86,7 @@ export class ChainService extends Service implements ChainServiceProperties {
* @returns {Promise<ChainTx>} The chain transaction
*/
txInfo(txHash: string): Promise<ChainTx> {
invariant(this.url, 'No URL set');
return ChainAPI.txInfo(this.url, txHash);
}
}
32 changes: 32 additions & 0 deletions src/services/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Service, ServiceProperties } from './service';
import { FileAPI } from '../api';
import invariant from 'tiny-invariant';
import { Buffer } from 'buffer';

interface FileServiceProperties {}

type FileServiceParameters = ServiceProperties & FileServiceProperties;

export class FileService extends Service implements FileServiceProperties {
/**
* Instantiate the election service.
*
* @param {Partial<FileServiceParameters>} params The service parameters
*/
constructor(params: Partial<FileServiceParameters>) {
super();
Object.assign(this, params);
}

/**
* Fetches the CID expected for the specified data content.
*
* @param {string} data The data of which we want the CID of
* @returns {Promise<string>} Resulting CID
*/
calculateCID(data: string): Promise<string> {
invariant(this.url, 'No URL set');
const b64Data = Buffer.from(data, 'utf8').toString('base64');
return FileAPI.cid(this.url, b64Data).then((response) => response.cid);
}
}
1 change: 1 addition & 0 deletions src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './census';
export * from './chain';
export * from './csp';
export * from './election';
export * from './file';
export * from './service';
export * from './anonymous';
export * from './vote';
2 changes: 2 additions & 0 deletions src/services/vote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class VoteService extends Service implements VoteServiceProperties {
* @returns {Promise<VoteInfo>}
*/
info(address: string, electionId: string): Promise<VoteInfo> {
invariant(this.url, 'No URL set');
return VoteAPI.info(this.url, keccak256(address.toLowerCase() + electionId));
}

Expand All @@ -56,6 +57,7 @@ export class VoteService extends Service implements VoteServiceProperties {
* @returns {Promise<VoteSubmit>}
*/
vote(payload: string): Promise<VoteSubmit> {
invariant(this.url, 'No URL set');
return VoteAPI.submit(this.url, payload);
}
}

0 comments on commit 0d1b71f

Please sign in to comment.