Skip to content
This repository was archived by the owner on Mar 20, 2024. It is now read-only.

Commit 893f15d

Browse files
committed
Improved init funtion
- Only computeHost and passphrase are reqired as configs - Added specific internal errors for config fields to help with usage
1 parent 780ca46 commit 893f15d

File tree

4 files changed

+54
-38
lines changed

4 files changed

+54
-38
lines changed

src/interfaces/error.interfaces.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ export enum IErrorInternal {
8282
InvalidDRUIDProvided = 'Invalid DRUID value provided',
8383
UnableToFilterIntercomData = 'Unable to filter intercom data',
8484
ClientNotInitialized = 'Client not initialized',
85+
StorageNotInitialized = 'Storage host not initialized',
86+
IntercomNotInitialized = 'Intercom host not initialized',
87+
NotaryNotInitialized = 'Notary host not initialized',
8588
FetchBalanceResponseEmpty = 'Balance object is empty',
8689
NoDRUIDValues = 'DRUID values are null',
8790
AssetsIncompatible = 'Assets are incompatible',
@@ -91,7 +94,9 @@ export enum IErrorInternal {
9194
UnableToSignMessage = 'Unable to sign message',
9295
NoHostsProvided = 'No hosts provided',
9396
NoKeypairsProvided = 'No key-pairs provided',
94-
UnknownError = 'Unknown Error',
97+
NoPassPhraseProvided = 'No passphrase provided',
98+
NoComputeHostProvided = 'No compute host provided',
99+
UnknownError = 'Unknown Error'
95100
}
96101

97102
// Custom `Result` wrapper with string error

src/interfaces/general.interfaces.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
// Config needed for initialization
66
export type IClientConfig = {
7-
computeHost?: string;
7+
computeHost: string; /* Required */
88
storageHost?: string;
99
intercomHost?: string;
1010
notaryHost?: string;
11-
passPhrase: string;
11+
passPhrase: string; /* Required */
1212
};
1313

1414
/* -------------------------------------------------------------------------- */

src/mgmt/key.mgmt.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ export function generateSeed(): IResult<string> {
7575
* @param passphrase {string} - Passphrase as a string
7676
*/
7777
export function getPassphraseBuffer(passphrase: string): IResult<Uint8Array> {
78+
if (passphrase == undefined)
79+
return err(IErrorInternal.NoPassPhraseProvided);
7880
try {
7981
const hash = sha3_256(passphrase);
8082
const val = truncateByBytesUTF8(hash, 32);

src/services/znt.service.ts

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ export class ABlockWallet {
5050
/* -------------------------------------------------------------------------- */
5151
/* Member Variables */
5252
/* -------------------------------------------------------------------------- */
53-
private intercomHost: string | undefined;
5453
private computeHost: string | undefined;
5554
private storageHost: string | undefined;
55+
private intercomHost: string | undefined;
5656
private notaryHost: string | undefined;
5757
private keyMgmt: mgmtClient | undefined;
5858
private computeRoutesPoW: Map<string, number> | undefined;
@@ -62,9 +62,9 @@ export class ABlockWallet {
6262
/* Constructor */
6363
/* -------------------------------------------------------------------------- */
6464
constructor() {
65-
this.intercomHost = undefined;
6665
this.computeHost = undefined;
6766
this.storageHost = undefined;
67+
this.intercomHost = undefined;
6868
this.notaryHost = undefined;
6969
this.keyMgmt = undefined;
7070
this.computeRoutesPoW = undefined;
@@ -184,27 +184,31 @@ export class ABlockWallet {
184184
* Common network initialization (retrieval of PoW list for compute as well as storage)
185185
*
186186
* @private
187-
* @param {IClientConfig} config - Additional configuration parameters
187+
* @param {IClientConfig} config - Configuration parameters
188188
* @memberof ABlockWallet
189189
*/
190-
public async initNetwork(config: IClientConfig): Promise<IClientResponse> {
191-
this.intercomHost = config.intercomHost;
190+
private async initNetwork(config: IClientConfig): Promise<IClientResponse> {
192191
this.computeHost = config.computeHost;
193192
this.storageHost = config.storageHost;
193+
this.intercomHost = config.intercomHost;
194194
this.notaryHost = config.notaryHost;
195-
// Set routes proof-of-work requirements
195+
196+
if (this.computeHost == undefined)
197+
return {
198+
status: 'error',
199+
reason: IErrorInternal.NoComputeHostProvided
200+
} as IClientResponse;
196201

197202
// Initialize routes proof-of-work for compute host
198-
if (this.computeHost !== undefined) {
199-
this.computeRoutesPoW = new Map();
200-
const initComputeResult = await this.initNetworkForHost(
201-
this.computeHost,
202-
this.computeRoutesPoW,
203-
);
204-
if (initComputeResult.status == 'error') return initComputeResult;
205-
}
203+
this.computeRoutesPoW = new Map();
204+
const initComputeResult = await this.initNetworkForHost(
205+
this.computeHost,
206+
this.computeRoutesPoW,
207+
);
208+
if (initComputeResult.status == 'error') return initComputeResult;
206209

207-
// Initialize routes proof-of-work for storage host
210+
211+
// Optional - Initialize routes proof-of-work for storage host
208212
if (this.storageHost !== undefined) {
209213
this.storageRoutesPoW = new Map();
210214
const initStorageResult = await this.initNetworkForHost(
@@ -215,10 +219,10 @@ export class ABlockWallet {
215219
}
216220

217221
if (
218-
this.storageHost === undefined &&
219222
this.computeHost === undefined &&
220-
this.notaryHost === undefined &&
221-
this.intercomHost === undefined
223+
this.storageHost === undefined &&
224+
this.intercomHost === undefined &&
225+
this.notaryHost === undefined
222226
)
223227
return {
224228
status: 'error',
@@ -237,16 +241,10 @@ export class ABlockWallet {
237241
* @param {IClientConfig} config - Additional configuration parameters
238242
* @memberof ABlockWallet
239243
*/
240-
public async initNetworkForHost(
241-
host: string | undefined,
244+
private async initNetworkForHost(
245+
host: string,
242246
routesPow: Map<string, number>,
243247
): Promise<IClientResponse> {
244-
if (!host) {
245-
return {
246-
status: 'error',
247-
reason: IErrorInternal.NoHostsProvided,
248-
} as IClientResponse;
249-
}
250248
// Set routes proof-of-work requirements
251249
const debugData = await this.getDebugData(host);
252250
if (debugData.status === 'error')
@@ -359,8 +357,10 @@ export class ABlockWallet {
359357
*/
360358
public async fetchTransactions(transactionHashes: string[]): Promise<IClientResponse> {
361359
try {
362-
if (!this.storageHost || !this.keyMgmt || !this.storageRoutesPoW)
360+
if (!this.keyMgmt)
363361
throw new Error(IErrorInternal.ClientNotInitialized);
362+
if (!this.storageHost || !this.storageRoutesPoW) /* Storage is optional on wallet init, but must be initialized here */
363+
throw new Error(IErrorInternal.StorageNotInitialized);
364364
const headers = this.getRequestIdAndNonceHeadersForRoute(
365365
this.computeRoutesPoW,
366366
IAPIRoute.FetchBalance,
@@ -509,8 +509,10 @@ export class ABlockWallet {
509509
*/
510510
async getNotaryBurnAddress(): Promise<IClientResponse> {
511511
try {
512-
if (!this.notaryHost || !this.keyMgmt)
512+
if (!this.keyMgmt)
513513
throw new Error(IErrorInternal.ClientNotInitialized);
514+
if (!this.notaryHost)
515+
throw new Error(IErrorInternal.NotaryNotInitialized);
514516
const headers = this.getRequestIdAndNonceHeadersForRoute(
515517
new Map(),
516518
IAPIRoute.GetNotaryBurnAddress,
@@ -554,8 +556,10 @@ export class ABlockWallet {
554556
signatures: IGenericKeyPair<string>,
555557
): Promise<IClientResponse> {
556558
try {
557-
if (!this.notaryHost || !this.keyMgmt)
559+
if (!this.keyMgmt)
558560
throw new Error(IErrorInternal.ClientNotInitialized);
561+
if (!this.notaryHost)
562+
throw new Error(IErrorInternal.NotaryNotInitialized);
559563
const headers = this.getRequestIdAndNonceHeadersForRoute(
560564
new Map(),
561565
IAPIRoute.GetNotarySignature,
@@ -684,8 +688,10 @@ export class ABlockWallet {
684688
receiveAddress: IKeypairEncrypted,
685689
): Promise<IClientResponse> {
686690
try {
687-
if (!this.computeHost || !this.intercomHost || !this.keyMgmt || !this.computeRoutesPoW)
691+
if (!this.computeHost || !this.keyMgmt || !this.computeRoutesPoW)
688692
throw new Error(IErrorInternal.ClientNotInitialized);
693+
if (!this.intercomHost)
694+
throw new Error(IErrorInternal.IntercomNotInitialized);
689695
const senderKeypair = throwIfErr(this.keyMgmt.decryptKeypair(receiveAddress));
690696
const [allAddresses, keyPairMap] = throwIfErr(
691697
this.keyMgmt.getAllAddressesAndKeypairMap(allKeypairs),
@@ -829,8 +835,10 @@ export class ABlockWallet {
829835
allEncryptedTxs: ICreateTransactionEncrypted[],
830836
): Promise<IClientResponse> {
831837
try {
832-
if (!this.computeHost || !this.intercomHost || !this.keyMgmt || !this.computeRoutesPoW)
838+
if (!this.computeHost || !this.keyMgmt || !this.computeRoutesPoW)
833839
throw new Error(IErrorInternal.ClientNotInitialized);
840+
if (!this.intercomHost)
841+
throw new Error(IErrorInternal.IntercomNotInitialized);
834842

835843
// Generate a key-pair map
836844
const [allAddresses, keyPairMap] = throwIfErr(
@@ -1229,8 +1237,10 @@ export class ABlockWallet {
12291237
allKeypairs: IKeypairEncrypted[],
12301238
): Promise<IClientResponse> {
12311239
try {
1232-
if (!this.computeHost || !this.intercomHost || !this.keyMgmt || !this.computeRoutesPoW)
1240+
if (!this.computeHost || !this.keyMgmt || !this.computeRoutesPoW)
12331241
throw new Error(IErrorInternal.ClientNotInitialized);
1242+
if (!this.intercomHost)
1243+
throw new Error(IErrorInternal.IntercomNotInitialized);
12341244
const [allAddresses, keyPairMap] = throwIfErr(
12351245
this.keyMgmt.getAllAddressesAndKeypairMap(allKeypairs),
12361246
);
@@ -1336,13 +1346,12 @@ export class ABlockWallet {
13361346
* Get information regarding the PoW required for all routes
13371347
*
13381348
* @private
1339-
* @param {(string | undefined)} host - Host address to retrieve proof-of-work data from
1349+
* @param {(string)} host - Host address to retrieve proof-of-work data from
13401350
* @return {*} {Promise<IClientResponse>}
13411351
* @memberof ABlockWallet
13421352
*/
1343-
private async getDebugData(host: string | undefined): Promise<IClientResponse> {
1353+
private async getDebugData(host: string): Promise<IClientResponse> {
13441354
try {
1345-
if (!host) throw new Error(IErrorInternal.ClientNotInitialized);
13461355
const routesPow =
13471356
host === this.computeHost ? this.computeRoutesPoW : this.storageRoutesPoW;
13481357
const headers = this.getRequestIdAndNonceHeadersForRoute(

0 commit comments

Comments
 (0)