Skip to content

Commit f88ab9b

Browse files
author
Ivan
authored
Merge pull request #65 from wavesplatform/metamask
Sign and broadcast via provider
2 parents b3b7a4b + de68831 commit f88ab9b

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

src/Signer.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -620,11 +620,14 @@ export class Signer {
620620
} as any),
621621
sign: () => this._sign<T>(txs as any),
622622
broadcast: function(options?: BroadcastOptions) {
623-
return (
624-
this.sign()
623+
if (_this.currentProvider?.isSignAndBroadcastByProvider === true) {
624+
return _this.currentProvider
625+
.sign(txs);
626+
} else {
627+
return this.sign()
625628
// @ts-ignore
626-
.then((txs) => _this.broadcast(txs, options)) as any
627-
);
629+
.then((txs) => _this.broadcast(txs, options)) as any;
630+
}
628631
},
629632
};
630633
}
@@ -678,6 +681,15 @@ export class Signer {
678681
private _sign<T extends SignerTx>(toSign: T[]): Promise<SignedTx<T>[]> {
679682
const validation = this._validate(toSign);
680683

684+
if (this.currentProvider?.isSignAndBroadcastByProvider === true) {
685+
const error = this._handleError(ERRORS.PROVIDER_SIGN_NOT_SUPPORTED, [{
686+
error: 'PROVIDER_SIGN_NOT_SUPPORTED',
687+
node: this._options.NODE_URL,
688+
}]);
689+
690+
throw error;
691+
}
692+
681693
if (validation.isValid) {
682694
return this._connectPromise.then(
683695
(provider) => provider.sign(toSign as any)

src/SignerError.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const ERRORS = {
1010
PROVIDER_INTERNAL: 1006 as 1006,
1111
API_ARGUMENTS: 1007 as 1007,
1212
NETWORK_ERROR: 1008 as 1008,
13+
PROVIDER_SIGN_NOT_SUPPORTED: 1009 as 1009,
1314
};
1415

1516
type ErrorDetails = {
@@ -125,6 +126,19 @@ export class SignerProviderConnectError extends SignerError {
125126
}
126127
}
127128

129+
export class SignerProviderSignIsNotSupport extends SignerError {
130+
constructor({ error, node }: { error: string; node: string }) {
131+
super({
132+
code: ERRORS.PROVIDER_SIGN_NOT_SUPPORTED,
133+
title: 'Method sign is not support for this provider. Use broadcats instead',
134+
type: 'validation',
135+
errorArgs: { error, node },
136+
});
137+
138+
Object.setPrototypeOf(this, SignerProviderSignIsNotSupport.prototype);
139+
}
140+
}
141+
128142
export class SignerEnsureProviderError extends SignerError {
129143
constructor(method: string) {
130144
super({

src/helpers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
SignerNetworkError,
99
SignerOptionsError,
1010
SignerProviderConnectError,
11+
SignerProviderSignIsNotSupport,
1112
SignerProviderInterfaceError,
1213
SignerProviderInternalError,
1314
} from './SignerError';
@@ -19,6 +20,7 @@ const ERRORS_MAP = {
1920
[ERRORS.PROVIDER_INTERFACE]: SignerProviderInterfaceError,
2021
[ERRORS.API_ARGUMENTS]: SignerApiArgumentsError,
2122
[ERRORS.PROVIDER_CONNECT]: SignerProviderConnectError,
23+
[ERRORS.PROVIDER_SIGN_NOT_SUPPORTED]: SignerProviderSignIsNotSupport,
2224
[ERRORS.ENSURE_PROVIDER]: SignerEnsureProviderError,
2325
[ERRORS.PROVIDER_INTERNAL]: SignerProviderInternalError,
2426
[ERRORS.NOT_AUTHORIZED]: SignerAuthError,

src/types/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ export interface TypedData {
4343
export interface Provider {
4444
user: UserData | null;
4545

46+
isSignAndBroadcastByProvider?: boolean;
47+
4648
on<EVENT extends keyof AuthEvents>(
4749
event: EVENT,
4850
handler: Handler<AuthEvents[EVENT]>,

0 commit comments

Comments
 (0)