Skip to content
This repository has been archived by the owner on Jul 7, 2021. It is now read-only.

Commit

Permalink
feat(sdk): implement Request#withCacheStore (#555)
Browse files Browse the repository at this point in the history
  • Loading branch information
faustbrian committed Jul 21, 2020
1 parent 7068a7b commit b1e1ea3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
58 changes: 41 additions & 17 deletions packages/platform-sdk-profiles/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,25 @@ export class Wallet {
return this;
}

/**
* @TODO
*
* Think about how to remove this method. We need async methods instead of a
* constructor because of the network requests and different ways to import wallets.
*/
public async setIdentity(mnemonic: string): Promise<Wallet> {
this.#address = await this.#coin.identity().address().fromMnemonic(mnemonic);
this.#publicKey = await this.#coin.identity().publicKey().fromMnemonic(mnemonic);

return this.setAddress(this.#address);
}

/**
* @TODO
*
* Think about how to remove this method. We need async methods instead of a
* constructor because of the network requests and different ways to import wallets.
*/
public async setAddress(address: string): Promise<Wallet> {
const isValidAddress: boolean = await this.coin().identity().address().validate(address);

Expand All @@ -61,23 +73,7 @@ export class Wallet {

this.#address = address;

try {
this.#wallet = await this.#coin.client().wallet(address);

if (!this.#publicKey) {
this.#publicKey = this.#wallet.publicKey();
}

this.data().set(WalletAttribute.Balance, this.#wallet.balance());
this.data().set(WalletAttribute.Sequence, this.#wallet.nonce());
} catch {
/**
* TODO: decide what to do if the wallet couldn't be found
*
* A missing wallet could mean that the wallet is legitimate
* but has no transactions or that the address is wrong.
*/
}
await this.syncIdentity();

this.setAvatar(Avatar.make(this.address()));

Expand Down Expand Up @@ -264,6 +260,34 @@ export class Wallet {
return this.#coin.transaction();
}

/**
* These methods serve as helpers to keep the wallet data updated.
*/

public async syncIdentity(): Promise<void> {
try {
this.#wallet = await this.#coin.client().wallet(this.address());

if (!this.#publicKey) {
this.#publicKey = this.#wallet.publicKey();
}

this.data().set(WalletAttribute.Balance, this.#wallet.balance());
this.data().set(WalletAttribute.Sequence, this.#wallet.nonce());
} catch {
/**
* TODO: decide what to do if the wallet couldn't be found
*
* A missing wallet could mean that the wallet is legitimate
* but has no transactions or that the address is wrong.
*/
}
}

/**
* These methods serve as helpers to interact with the underlying coin.
*/

public async transactions(
query: Contracts.ClientTransactionsInput,
): Promise<Contracts.CollectionResponse<Coins.TransactionDataCollection>> {
Expand Down
2 changes: 2 additions & 0 deletions packages/platform-sdk/src/contracts/coins/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export interface HttpClient {

withHeaders(headers: object): HttpClient;

withCacheStore(cache: object): HttpClient;

timeout(seconds: number): HttpClient;

retry(times: number, sleep?: number): HttpClient;
Expand Down
6 changes: 6 additions & 0 deletions packages/platform-sdk/src/http/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ export abstract class Request implements HttpClient {
return this;
}

public withCacheStore(cache: object): HttpClient {
this._options.cache = cache;

return this;
}

public timeout(seconds: number): HttpClient {
this._options.timeout = seconds;

Expand Down

0 comments on commit b1e1ea3

Please sign in to comment.