Skip to content

Commit

Permalink
Modified client account functions
Browse files Browse the repository at this point in the history
  • Loading branch information
marcvelmer committed Jan 15, 2024
1 parent 1fd0a16 commit 0962239
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 75 deletions.
117 changes: 44 additions & 73 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import {
AnonymousVote,
ArchivedAccount,
CensusType,
CreateAccountOptions,
CspVote,
ElectionStatus,
ElectionStatusReady,
FetchAccountOptions,
HasAlreadyVotedOptions,
InvalidElection,
IsAbleToVoteOptions,
Expand Down Expand Up @@ -163,39 +165,19 @@ export class VocdoniSDKClient {
this.electionId = electionId;
}

/**
* Fetches account information.
*
* @param {string} address The account address to fetch the information
* @returns {Promise<AccountData | ArchivedAccount>}
*/
async fetchAccountInfo(address?: string): Promise<Account | ArchivedAccount> {
let account;
if (!this.wallet && !address) {
throw Error('No account set');
} else if (address) {
account = await this.accountService.fetchAccount(address);
} else {
account = await this.wallet.getAddress().then((address) => this.accountService.fetchAccount(address));
}
return account;
}

/**
* Fetches account.
*
* @param {string} address The account address to fetch the information
* @param {FetchAccountOptions} options The account address to fetch the information
* @returns {Promise<Account>}
*/
async fetchAccount(address?: string): Promise<Account> {
let account;
if (!this.wallet && !address) {
throw Error('No account set');
} else if (address) {
account = await this.accountService.fetchAccount(address);
} else {
account = await this.wallet.getAddress().then((address) => this.accountService.fetchAccount(address));
}
async fetchAccount(options?: FetchAccountOptions): Promise<Account> {
const settings = {
address: options?.address ?? (await this.wallet.getAddress()),
};
invariant(settings.address, 'No account address set');

const account = await this.accountService.fetchAccount(settings.address);

if (account instanceof ArchivedAccount) {
throw Error('Account is archived');
Expand Down Expand Up @@ -306,33 +288,38 @@ export class VocdoniSDKClient {
* like extra information of the account, or the faucet package string.
* @returns {Promise<Account>}
*/
async createAccountInfo(options: {
accountData: AccountData;
faucetPackage?: string;
signedSikPayload?: string;
password?: string;
}): Promise<Account> {
invariant(this.wallet, 'No wallet or signer set');
invariant(options.accountData, 'No account data');
private async setAccount(options: Partial<CreateAccountOptions>): Promise<Account> {
const settings = {
wallet: this.wallet,
data: AccountData.build({}),
faucetPackage: null,
setSecretIdentity: true,
secretPayload: null,
secretPassword: '0',
...options,
};
invariant(settings.wallet, 'No wallet or signer set or given');

const faucetPayload =
options.faucetPackage ??
(await this.wallet.getAddress().then((address) => this.faucetService.fetchPayload(address)));
const faucetPackage = this.faucetService.parseFaucetPackage(faucetPayload);
if (settings.setSecretIdentity && !settings.secretPayload) {
settings.secretPayload = await this.anonymousService.signSIKPayload(this.wallet);
}

const address = await settings.wallet.getAddress();

const address = await this.wallet.getAddress();
const faucetPayload = settings.faucetPackage ?? (await this.faucetService.fetchPayload(address));
const faucetPackage = this.faucetService.parseFaucetPackage(faucetPayload);

const calculatedSik = options?.signedSikPayload
? await AnonymousService.calcSik(address, options.signedSikPayload, options.password)
const calculatedSik = settings.secretPayload
? await AnonymousService.calcSik(address, settings.secretPayload, settings.secretPassword)
: null;

const accountData = Promise.all([
this.fetchChainId(),
this.fileService.calculateCID(JSON.stringify(options.accountData.generateMetadata())),
this.fileService.calculateCID(JSON.stringify(settings.data.generateMetadata())),
]).then((data) =>
AccountCore.generateCreateAccountTransaction(
address,
JSON.stringify(options.accountData.generateMetadata()),
JSON.stringify(settings.data.generateMetadata()),
data[1],
faucetPackage.payload,
faucetPackage.signature,
Expand Down Expand Up @@ -391,41 +378,25 @@ export class VocdoniSDKClient {
/**
* Registers an account against vochain, so it can create new elections.
*
* @param {{account: AccountData | null, faucetPackage: string | null, sik: boolean | null}} options Additional
* options, like extra information of the account, or the faucet package string
* @param {CreateAccountOptions} options Create account options
* @returns {Promise<Account>}
*/
createAccount(options?: {
accountData?: AccountData;
faucetPackage?: string;
sik?: boolean;
password?: string;
}): Promise<Account> {
invariant(this.wallet, 'No wallet or signer set');
createAccount(options?: Partial<CreateAccountOptions>): Promise<Account> {
const settings = {
account: null,
wallet: this.wallet,
data: AccountData.build({}),
faucetPackage: null,
sik: true,
password: '0',
setSecretIdentity: true,
secretPayload: null,
secretPassword: '0',
...options,
};
invariant(settings.wallet, 'No wallet or signer set or given');

return this.fetchAccount().catch(() => {
if (settings?.sik) {
return this.anonymousService.signSIKPayload(this.wallet).then((signedPayload) =>
this.createAccountInfo({
accountData: settings?.accountData ?? AccountData.build({}),
faucetPackage: settings?.faucetPackage,
signedSikPayload: signedPayload,
password: settings.password,
})
);
}
return this.createAccountInfo({
accountData: settings?.accountData ?? AccountData.build({}),
faucetPackage: settings?.faucetPackage,
});
});
return settings.wallet
.getAddress()
.then((address) => this.fetchAccount({ address }))
.catch(() => this.setAccount(settings));
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/types/client/account.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import { Wallet } from '@ethersproject/wallet';
import { Signer } from '@ethersproject/abstract-signer';
import { AccountData } from '../account';

export type WalletOption = { wallet: Wallet | Signer };
export type ElectionIdOption = { electionId: string };
export type VoteIdOption = { voteId: string };
export type AddressOption = { address: string };

export type FetchAccountOptions = Partial<AddressOption>;
export type CreateAccountOptions = Partial<WalletOption> & {
data: AccountData;
faucetPackage: string;
setSecretIdentity: boolean;
secretPayload: string;
secretPassword: string;
};
export type SendTokensOptions = Partial<WalletOption> & { to: string; amount: number };
export type IsInCensusOptions = Partial<WalletOption & ElectionIdOption>;
export type HasAlreadyVotedOptions = Partial<WalletOption & ElectionIdOption & VoteIdOption>;
Expand Down
4 changes: 2 additions & 2 deletions test/integration/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('Account integration tests', () => {
}, 75000);
it('should set information for an account', async () => {
const account = await client.createAccount({
accountData: AccountData.build({
data: AccountData.build({
languages: ['es'],
name: {
es: 'test',
Expand Down Expand Up @@ -79,7 +79,7 @@ describe('Account integration tests', () => {
}, 75000);
it('should set information for an account and then update it', async () => {
await client.createAccount({
accountData: AccountData.build({
data: AccountData.build({
languages: ['es'],
name: {
es: 'test',
Expand Down

0 comments on commit 0962239

Please sign in to comment.