From a4a13889e9f005b5d2c41f54c7280df37854aedf Mon Sep 17 00:00:00 2001 From: Denis Davidyuk Date: Tue, 20 Apr 2021 19:39:46 +0300 Subject: [PATCH] Inline receive helper that is used once --- src/utils/aepp-wallet-communication/helpers.js | 8 -------- .../aepp-wallet-communication/rpc/rpc-clients.js | 13 +++++++++++-- test/integration/rpc.js | 10 +--------- 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/src/utils/aepp-wallet-communication/helpers.js b/src/utils/aepp-wallet-communication/helpers.js index 126ae978c4..e5948a4933 100644 --- a/src/utils/aepp-wallet-communication/helpers.js +++ b/src/utils/aepp-wallet-communication/helpers.js @@ -40,14 +40,6 @@ export const sendMessage = (connection) => { } } -export const receive = (handler) => (msg, origin) => { - if (!msg || !msg.jsonrpc || msg.jsonrpc !== '2.0' || !msg.method) { - console.warn('Receive invalid message', msg) - return - } - handler(msg, origin) -} - export const getHandler = (schema, msg, { debug = false } = {}) => { const handler = schema[msg.method] if (!handler || typeof handler !== 'function') { diff --git a/src/utils/aepp-wallet-communication/rpc/rpc-clients.js b/src/utils/aepp-wallet-communication/rpc/rpc-clients.js index bd78033fa8..4a5aa30e5a 100644 --- a/src/utils/aepp-wallet-communication/rpc/rpc-clients.js +++ b/src/utils/aepp-wallet-communication/rpc/rpc-clients.js @@ -9,7 +9,7 @@ import stampit from '@stamp/it' import { METHODS, RPC_STATUS, SUBSCRIPTION_TYPES } from '../schema' -import { receive, sendMessage, message, isValidAccounts } from '../helpers' +import { sendMessage, message, isValidAccounts } from '../helpers' /** * Contain functionality for managing multiple RPC clients (RpcClient stamp) @@ -160,11 +160,20 @@ export const RpcClient = stampit({ this.accounts = {} this.sendMessage = sendMessage(this.connection) + + const handleMessage = (msg, origin) => { + if (!msg || !msg.jsonrpc || msg.jsonrpc !== '2.0' || !msg.method) { + throw new Error(`Received invalid message: ${msg}`) + } + onMessage(msg, origin) + } + const disconnect = (aepp, connection) => { this.disconnect(true) typeof onDisconnect === 'function' && onDisconnect(connection, this) } - connection.connect(receive(onMessage), disconnect) + + connection.connect(handleMessage, disconnect) }, propertyDescriptors: { currentAccount: { diff --git a/test/integration/rpc.js b/test/integration/rpc.js index ac8c5bdd86..92e7b2e2f3 100644 --- a/test/integration/rpc.js +++ b/test/integration/rpc.js @@ -20,12 +20,7 @@ import { MemoryAccount, Node, RpcAepp, RpcWallet } from '../../src' import { unpackTx } from '../../src/tx/builder' import { decode } from '../../src/tx/builder/helpers' import BrowserWindowMessageConnection from '../../src/utils/aepp-wallet-communication/connection/browser-window-message' -import { - getBrowserAPI, - getHandler, - isInIframe, - receive -} from '../../src/utils/aepp-wallet-communication/helpers' +import { getBrowserAPI, getHandler } from '../../src/utils/aepp-wallet-communication/helpers' import { METHODS, RPC_STATUS } from '../../src/utils/aepp-wallet-communication/schema' import { generateKeyPair, verify } from '../../src/utils/crypto' import { compilerUrl, genesisAccount, internalUrl, networkId, publicKey, url } from './' @@ -550,9 +545,6 @@ describe('Aepp<->Wallet', function () { }) describe('Rpc helpers', () => { - it('Receive invalid message', () => { - (!receive(() => true)(false)).should.be.equal(true) - }) it('receive unknown method', async () => { (await getHandler({}, { method: 'hey' })()()).should.be.equal(true) })