Skip to content

Commit b0873bf

Browse files
committed
Fix async for login
1 parent 2823ada commit b0873bf

File tree

2 files changed

+69
-61
lines changed

2 files changed

+69
-61
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ module.exports = {
191191
'@typescript-eslint/prefer-string-starts-ends-with': 'warn',
192192
'@typescript-eslint/promise-function-async': 'off',
193193
'@typescript-eslint/require-array-sort-compare': 'error',
194-
'@typescript-eslint/require-await': 'error',
194+
'@typescript-eslint/require-await': 'off',
195195
'@typescript-eslint/explicit-function-return-type': 'off',
196196
// '@typescript-eslint/strict-boolean-expressions': 'error',
197197
'@typescript-eslint/type-annotation-spacing': 'error',

src/Signer.ts

Lines changed: 68 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
11
import { DEFAULT_OPTIONS } from './constants';
22
import {
3-
TypedData,
4-
UserData,
5-
Provider,
3+
AliasArgs,
4+
AuthEvents,
65
Balance,
7-
IssueArgs,
8-
TransferArgs,
9-
ReissueArgs,
6+
BroadcastedTx,
7+
BroadcastOptions,
108
BurnArgs,
11-
LeaseArgs,
129
CancelLeaseArgs,
13-
AliasArgs,
14-
MassTransferArgs,
1510
DataArgs,
16-
SetScriptArgs,
17-
SponsorshipArgs,
1811
ExchangeArgs,
19-
SetAssetScriptArgs,
12+
Handler,
2013
InvokeArgs,
21-
SignerTx,
22-
SignerIssueTx,
23-
SignerTransferTx,
24-
SignerReissueTx,
14+
IssueArgs,
15+
LeaseArgs,
16+
MassTransferArgs,
17+
Provider,
18+
ReissueArgs,
19+
SetAssetScriptArgs,
20+
SetScriptArgs,
21+
SignedTx,
22+
SignerAliasTx,
2523
SignerBurnTx,
26-
SignerLeaseTx,
27-
SignerExchangeTx,
2824
SignerCancelLeaseTx,
29-
SignerAliasTx,
30-
SignerMassTransferTx,
3125
SignerDataTx,
32-
SignerSponsorshipTx,
26+
SignerExchangeTx,
3327
SignerInvokeTx,
28+
SignerIssueTx,
29+
SignerLeaseTx,
30+
SignerMassTransferTx,
31+
SignerOptions,
32+
SignerReissueTx,
3433
SignerSetAssetScriptTx,
3534
SignerSetScriptTx,
36-
BroadcastOptions,
37-
SignerOptions,
38-
SignedTx,
39-
BroadcastedTx,
40-
AuthEvents,
41-
Handler,
35+
SignerSponsorshipTx,
36+
SignerTransferTx,
37+
SignerTx,
38+
SponsorshipArgs,
39+
TransferArgs,
40+
TypedData,
41+
UserData,
4242
} from './types';
4343
import { IConsole, makeConsole, makeOptions } from '@waves/client-logs';
4444
import { fetchBalanceDetails } from '@waves/node-api-js/cjs/api-node/addresses';
@@ -48,18 +48,18 @@ import broadcast from '@waves/node-api-js/cjs/tools/transactions/broadcast';
4848
import getNetworkByte from '@waves/node-api-js/cjs/tools/blocks/getNetworkByte';
4949
import { ChainApi1stCall } from './types/api';
5050
import {
51-
TRANSACTION_TYPE,
5251
Transaction,
52+
TRANSACTION_TYPE,
5353
TransactionType,
5454
} from '@waves/ts-types';
5555
import {
5656
argsValidators,
57-
validateSignerOptions,
5857
validateProviderInterface,
58+
validateSignerOptions,
5959
} from './validation';
6060
import { ERRORS } from './SignerError';
6161
import { ErrorHandler, errorHandlerFactory } from './helpers';
62-
import { ensureProvider, checkAuth, catchProviderError } from './decorators';
62+
import { catchProviderError, checkAuth, ensureProvider } from './decorators';
6363

6464
export * from './types';
6565

@@ -219,7 +219,7 @@ export class Signer {
219219
NODE_URL: this._options.NODE_URL,
220220
})
221221
.then(() => {
222-
this._logger.info('Provider has conneced to node.');
222+
this._logger.info('Provider has connected to node.');
223223

224224
return provider;
225225
})
@@ -292,32 +292,32 @@ export class Signer {
292292

293293
/**
294294
* Получаем информацию о пользователе
295-
*
295+
* В данном методе НЕЛЬЗЯ использовать асинхронность.
296+
* Метод login провайдера должен вызываться синхронно в контексте вызова метода!
296297
* ```ts
297298
* await waves.login(); // Авторизуемся. Возвращает адрес и публичный ключ
298299
* ```
299300
*/
300301
@ensureProvider
301-
public async login(): Promise<UserData> {
302-
await this._connectPromise;
303-
304-
try {
305-
this._userData = await this.currentProvider!.login();
306-
307-
this._logger.info('Logged in.');
308-
309-
return this._userData;
310-
} catch (err) {
311-
if (err === 'Error: User rejection!') {
312-
throw err;
313-
}
302+
public login(): Promise<UserData> {
303+
return this.currentProvider!.login()
304+
.then((data) => {
305+
this._logger.info('Logged in.');
306+
this._userData = data;
307+
308+
return data;
309+
})
310+
.catch((err) => {
311+
if (err === 'Error: User rejection!') {
312+
throw err;
313+
}
314314

315-
const error = this._handleError(ERRORS.PROVIDER_INTERNAL, [
316-
err.message,
317-
]);
315+
const error = this._handleError(ERRORS.PROVIDER_INTERNAL, [
316+
err.message,
317+
]);
318318

319-
throw error;
320-
}
319+
throw error;
320+
});
321321
}
322322

323323
/**
@@ -626,13 +626,16 @@ export class Signer {
626626
} as any),
627627
sign: () => this._sign<T>(txs as any),
628628
broadcast: function(options?: BroadcastOptions) {
629-
if (_this.currentProvider?.isSignAndBroadcastByProvider === true) {
630-
return _this.currentProvider
631-
.sign(txs);
629+
if (
630+
_this.currentProvider?.isSignAndBroadcastByProvider === true
631+
) {
632+
return _this.currentProvider.sign(txs);
632633
} else {
633-
return this.sign()
634-
// @ts-ignore
635-
.then((txs) => _this.broadcast(txs, options)) as any;
634+
return (
635+
this.sign()
636+
// @ts-ignore
637+
.then((txs) => _this.broadcast(txs, options)) as any
638+
);
636639
}
637640
},
638641
};
@@ -688,10 +691,15 @@ export class Signer {
688691
const validation = this._validate(toSign);
689692

690693
if (this.currentProvider?.isSignAndBroadcastByProvider === true) {
691-
const error = this._handleError(ERRORS.PROVIDER_SIGN_NOT_SUPPORTED, [{
692-
error: 'PROVIDER_SIGN_NOT_SUPPORTED',
693-
node: this._options.NODE_URL,
694-
}]);
694+
const error = this._handleError(
695+
ERRORS.PROVIDER_SIGN_NOT_SUPPORTED,
696+
[
697+
{
698+
error: 'PROVIDER_SIGN_NOT_SUPPORTED',
699+
node: this._options.NODE_URL,
700+
},
701+
]
702+
);
695703

696704
throw error;
697705
}

0 commit comments

Comments
 (0)