Skip to content

Commit

Permalink
fix(AEX-2): Handler always as Promise (#1018)
Browse files Browse the repository at this point in the history
* fix(AEX-2): Handler always as Promise

* fix(AEX-2): Fix test

* fix(RPC): Add debug option for `getHandler`
Prevent awaiting of non Promise in WalletRpc

* fix(RPC): Add debug option for `getHandler`
Prevent awaiting of non Promise in WalletRpc
  • Loading branch information
nduchak authored Jun 9, 2020
1 parent 22c452c commit a8b0aab
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion es/utils/aepp-wallet-communication/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const getHandler = (schema, msg, { debug = false } = {}) => {
const handler = schema[msg.method]
if (!handler || typeof handler !== 'function') {
debug && console.log(`Unknown message method ${msg.method}`)
return () => () => true
return () => async () => true
}
return handler
}
Expand Down
20 changes: 11 additions & 9 deletions es/utils/aepp-wallet-communication/rpc/wallet-rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const rpcClients = RpcClients()

const NOTIFICATIONS = {
[METHODS.closeConnection]: (instance, { client }) =>
(msg) => {
async (msg) => {
client.disconnect(true)
instance.onDisconnect(msg.params, client)
}
Expand All @@ -30,7 +30,7 @@ const RESPONSES = {}
const REQUESTS = {
// Store client info and prepare two fn for each client `connect` and `denyConnection`
// which automatically prepare and send response for that client
async [METHODS.aepp.connect] (callInstance, instance, client, { name, networkId, version, icons }) {
[METHODS.aepp.connect] (callInstance, instance, client, { name, networkId, version, icons }) {
// Check if protocol and network is compatible with wallet
if (version !== VERSION) return { error: ERRORS.unsupportedProtocol() }

Expand All @@ -57,7 +57,7 @@ const REQUESTS = {
}
)
},
async [METHODS.aepp.subscribeAddress] (callInstance, instance, client, { type, value }) {
[METHODS.aepp.subscribeAddress] (callInstance, instance, client, { type, value }) {
// Authorization check
if (!client.isConnected()) return { error: ERRORS.notAuthorize() }

Expand All @@ -84,7 +84,7 @@ const REQUESTS = {
(error) => ({ error: ERRORS.rejectedByUser(error) })
)
},
async [METHODS.aepp.address] (callInstance, instance, client) {
[METHODS.aepp.address] (callInstance, instance, client) {
// Authorization check
if (!client.isConnected()) return { error: ERRORS.notAuthorize() }
if (!client.isSubscribed()) return { error: ERRORS.notAuthorize() }
Expand All @@ -96,7 +96,7 @@ const REQUESTS = {
(error) => ({ error: ERRORS.rejectedByUser(error) })
)
},
async [METHODS.aepp.sign] (callInstance, instance, client, { tx, onAccount, networkId, returnSigned = false }) {
[METHODS.aepp.sign] (callInstance, instance, client, { tx, onAccount, networkId, returnSigned = false }) {
const address = onAccount || client.currentAccount
// Update client with new networkId
networkId && rpcClients.updateClientInfo(client.id, { networkId })
Expand Down Expand Up @@ -140,7 +140,7 @@ const REQUESTS = {
(error) => ({ error: ERRORS.rejectedByUser(error) })
)
},
async [METHODS.aepp.signMessage] (callInstance, instance, client, { message, onAccount }) {
[METHODS.aepp.signMessage] (callInstance, instance, client, { message, onAccount }) {
// Authorization check
if (!client.isConnected()) return { error: ERRORS.notAuthorize() }
const address = onAccount || client.currentAccount
Expand Down Expand Up @@ -179,7 +179,7 @@ const handleMessage = (instance, id) => async (msg, origin) => {
return getHandler(RESPONSES, msg, { debug: instance.debug })(instance, { client })(msg, origin)
} else {
const { id, method } = msg
const callInstance = (methodName, params, accept, deny) => new Promise(resolve => {
const callInstance = (methodName, params, accept, deny) => () => new Promise(resolve => {
instance[methodName](
client,
client.addAction({ id, method, params }, [
Expand All @@ -188,7 +188,9 @@ const handleMessage = (instance, id) => async (msg, origin) => {
origin
)
})
const response = await getHandler(REQUESTS, msg, { debug: instance.debug })(callInstance, instance, client, msg.params)
// TODO make one structure for handler functions
const errorObjectOrHandler = getHandler(REQUESTS, msg, { debug: instance.debug })(callInstance, instance, client, msg.params)
const response = typeof errorObjectOrHandler === 'function' ? await errorObjectOrHandler() : errorObjectOrHandler
sendResponseMessage(client)(id, method, response)
}
}
Expand All @@ -209,7 +211,7 @@ const handleMessage = (instance, id) => async (msg, origin) => {
* @return {Object}
*/
export const WalletRpc = Ae.compose(Accounts, Selector, {
init ({ name, onConnection, onSubscription, onSign, onDisconnect, onAskAccounts, onMessageSign, forceValidation = false, debug = false }) {
init ({ name, onConnection, onSubscription, onSign, onDisconnect, onAskAccounts, onMessageSign, forceValidation = false, debug = false } = {}) {
this.debug = debug
const eventsHandlers = ['onConnection', 'onSubscription', 'onSign', 'onDisconnect', 'onMessageSign']
// CallBacks for events
Expand Down
4 changes: 2 additions & 2 deletions test/integration/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,8 @@ describe('Aepp<->Wallet', function () {
it('Receive invalid message', () => {
(!receive(() => true)(false)).should.be.equal(true)
})
it('receive unknown method', () => {
getHandler({}, { method: 'hey' })()().should.be.equal(true)
it('receive unknown method', async () => {
(await getHandler({}, { method: 'hey' })()()).should.be.equal(true)
})
it('getBrowserAPI: not in browser', () => {
global.chrome = null
Expand Down

0 comments on commit a8b0aab

Please sign in to comment.