From 8a40105dc6d0d90bcc1f4c0c52af413547d78893 Mon Sep 17 00:00:00 2001 From: Denis Davidyuk Date: Tue, 25 Jan 2022 20:32:28 +0300 Subject: [PATCH] refactor(aepp-wallet schema)!: rearrange METHODS enum --- .../aepp-wallet-communication/rpc/aepp-rpc.js | 24 ++++++------ .../rpc/rpc-client.js | 2 +- .../rpc/wallet-rpc.js | 12 +++--- src/utils/aepp-wallet-communication/schema.js | 37 +++++-------------- .../wallet-detector.js | 2 +- test/integration/rpc.js | 6 +-- 6 files changed, 32 insertions(+), 51 deletions(-) diff --git a/src/utils/aepp-wallet-communication/rpc/aepp-rpc.js b/src/utils/aepp-wallet-communication/rpc/aepp-rpc.js index b964de0df0..c17be492cb 100644 --- a/src/utils/aepp-wallet-communication/rpc/aepp-rpc.js +++ b/src/utils/aepp-wallet-communication/rpc/aepp-rpc.js @@ -21,7 +21,7 @@ import { } from '../../errors' const NOTIFICATIONS = { - [METHODS.wallet.updateAddress]: (instance) => + [METHODS.updateAddress]: (instance) => ({ params }) => { instance.rpcClient.accounts = params instance.onAddressChange(params) @@ -39,14 +39,14 @@ const NOTIFICATIONS = { } const RESPONSES = { - [METHODS.aepp.address]: (instance) => + [METHODS.address]: (instance) => (msg) => instance.rpcClient.processResponse(msg), - [METHODS.aepp.connect]: (instance) => + [METHODS.connect]: (instance) => (msg) => { if (msg.result) instance.rpcClient.info.status = RPC_STATUS.CONNECTED instance.rpcClient.processResponse(msg) }, - [METHODS.aepp.subscribeAddress]: (instance) => + [METHODS.subscribeAddress]: (instance) => (msg) => { if (msg.result) { if (msg.result.address) { @@ -59,13 +59,13 @@ const RESPONSES = { instance.rpcClient.processResponse(msg, ({ id, result }) => [result]) }, - [METHODS.aepp.sign]: (instance) => + [METHODS.sign]: (instance) => (msg) => { instance.rpcClient.processResponse( msg, ({ id, result }) => [result.signedTransaction || result.transactionHash] ) }, - [METHODS.aepp.signMessage]: (instance) => + [METHODS.signMessage]: (instance) => (msg) => { instance.rpcClient.processResponse(msg, ({ id, result }) => [result.signature]) } @@ -182,7 +182,7 @@ export default Ae.compose({ async askAddresses () { if (!this.rpcClient || !this.rpcClient.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet') if (!this.rpcClient.currentAccount) throw new UnsubscribedAccountError() - return this.rpcClient.request(METHODS.aepp.address) + return this.rpcClient.request(METHODS.address) }, /** * Subscribe for addresses from wallet @@ -195,7 +195,7 @@ export default Ae.compose({ */ async subscribeAddress (type, value) { if (!this.rpcClient || !this.rpcClient.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet') - return this.rpcClient.request(METHODS.aepp.subscribeAddress, { type, value }) + return this.rpcClient.request(METHODS.subscribeAddress, { type, value }) }, /** * Overwriting of `signTransaction` AE method @@ -210,7 +210,7 @@ export default Ae.compose({ if (!this.rpcClient.currentAccount) throw new UnsubscribedAccountError() if (opt.onAccount && !this.rpcClient.hasAccessToAccount(opt.onAccount)) throw new UnAuthorizedAccountError(`You do not have access to account ${opt.onAccount}`) return this.rpcClient.request( - METHODS.aepp.sign, + METHODS.sign, { ...opt, tx, returnSigned: true, networkId: this.getNetworkId() } ) }, @@ -226,7 +226,7 @@ export default Ae.compose({ if (!this.rpcClient || !this.rpcClient.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet') if (!this.rpcClient.currentAccount) throw new UnsubscribedAccountError() if (opt.onAccount && !this.rpcClient.hasAccessToAccount(opt.onAccount)) throw new UnAuthorizedAccountError(`You do not have access to account ${opt.onAccount}`) - return this.rpcClient.request(METHODS.aepp.signMessage, { ...opt, message: msg }) + return this.rpcClient.request(METHODS.signMessage, { ...opt, message: msg }) }, /** * Send connection request to wallet @@ -237,7 +237,7 @@ export default Ae.compose({ */ async sendConnectRequest () { return this.rpcClient.request( - METHODS.aepp.connect, { + METHODS.connect, { name: this.name, version: VERSION, networkId: this.getNetworkId({ force: true }) @@ -267,7 +267,7 @@ export default Ae.compose({ return this.sendTransaction(signed, opt) } return this.rpcClient.request( - METHODS.aepp.sign, + METHODS.sign, { onAccount: opt.onAccount, tx, returnSigned: false, networkId: this.getNetworkId() } ) } diff --git a/src/utils/aepp-wallet-communication/rpc/rpc-client.js b/src/utils/aepp-wallet-communication/rpc/rpc-client.js index 86f07b170c..cf509ed61e 100644 --- a/src/utils/aepp-wallet-communication/rpc/rpc-client.js +++ b/src/utils/aepp-wallet-communication/rpc/rpc-client.js @@ -168,7 +168,7 @@ export default stampit({ this.accounts = accounts if (!forceNotification) { // Sent notification about account updates - this.sendMessage(message(METHODS.wallet.updateAddress, this.accounts), true) + this.sendMessage(message(METHODS.updateAddress, this.accounts), true) } }, /** diff --git a/src/utils/aepp-wallet-communication/rpc/wallet-rpc.js b/src/utils/aepp-wallet-communication/rpc/wallet-rpc.js index 822b97a2a6..9e4c000395 100644 --- a/src/utils/aepp-wallet-communication/rpc/wallet-rpc.js +++ b/src/utils/aepp-wallet-communication/rpc/wallet-rpc.js @@ -33,7 +33,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 - [METHODS.aepp.connect] (callInstance, instance, client, { name, networkId, version, icons }) { + [METHODS.connect] (callInstance, instance, client, { name, networkId, version, icons }) { // Check if protocol and network is compatible with wallet if (version !== VERSION) return { error: ERRORS.unsupportedProtocol() } @@ -60,7 +60,7 @@ const REQUESTS = { } ) }, - [METHODS.aepp.subscribeAddress] (callInstance, instance, client, { type, value }) { + [METHODS.subscribeAddress] (callInstance, instance, client, { type, value }) { // Authorization check if (!client.isConnected()) return { error: ERRORS.notAuthorize() } @@ -89,7 +89,7 @@ const REQUESTS = { (error) => ({ error: ERRORS.rejectedByUser(error) }) ) }, - [METHODS.aepp.address] (callInstance, instance, client) { + [METHODS.address] (callInstance, instance, client) { // Authorization check if (!client.isConnected()) return { error: ERRORS.notAuthorize() } if (!client.isSubscribed()) return { error: ERRORS.notAuthorize() } @@ -104,7 +104,7 @@ const REQUESTS = { (error) => ({ error: ERRORS.rejectedByUser(error) }) ) }, - [METHODS.aepp.sign] (callInstance, instance, client, options) { + [METHODS.sign] (callInstance, instance, client, options) { const { tx, onAccount, networkId, returnSigned = false } = options const address = onAccount || client.currentAccount // Update client with new networkId @@ -151,7 +151,7 @@ const REQUESTS = { (error) => ({ error: ERRORS.rejectedByUser(error) }) ) }, - [METHODS.aepp.signMessage] (callInstance, instance, client, { message, onAccount }) { + [METHODS.signMessage] (callInstance, instance, client, { message, onAccount }) { // Authorization check if (!client.isConnected()) return { error: ERRORS.notAuthorize() } const address = onAccount || client.currentAccount @@ -358,7 +358,7 @@ export default Ae.compose(AccountMultiple, { shareWalletInfo (postFn) { postFn({ jsonrpc: '2.0', - ...message(METHODS.wallet.readyToConnect, this.getWalletInfo()) + ...message(METHODS.readyToConnect, this.getWalletInfo()) }) }, /** diff --git a/src/utils/aepp-wallet-communication/schema.js b/src/utils/aepp-wallet-communication/schema.js index f9399c3ab6..28a7bf80cd 100644 --- a/src/utils/aepp-wallet-communication/schema.js +++ b/src/utils/aepp-wallet-communication/schema.js @@ -9,40 +9,21 @@ export const WALLET_TYPE = asEnum([ 'extension' ]) -export const NOTIFICATIONS = asEnum([ - 'readyToConnect', - 'closeConnection', - 'updateNetwork', - 'updateAddress' -]) - -export const REQUESTS = asEnum([ - 'connect', - 'subscribeAddress', - 'sign', - 'address', - 'signMessage' -]) - export const SUBSCRIPTION_TYPES = asEnum([ 'subscribe', 'unsubscribe' ]) export const METHODS = { - wallet: { - [NOTIFICATIONS.readyToConnect]: 'connection.announcePresence', - [NOTIFICATIONS.updateAddress]: 'address.update' - }, - aepp: { - [REQUESTS.address]: 'address.get', - [REQUESTS.connect]: 'connection.open', - [REQUESTS.sign]: 'transaction.sign', - [REQUESTS.signMessage]: 'message.sign', - [REQUESTS.subscribeAddress]: 'address.subscribe' - }, - [NOTIFICATIONS.updateNetwork]: 'networkId.update', - [NOTIFICATIONS.closeConnection]: 'connection.close' + readyToConnect: 'connection.announcePresence', + updateAddress: 'address.update', + address: 'address.get', + connect: 'connection.open', + sign: 'transaction.sign', + signMessage: 'message.sign', + subscribeAddress: 'address.subscribe', + updateNetwork: 'networkId.update', + closeConnection: 'connection.close' } export const RPC_STATUS = { diff --git a/src/utils/aepp-wallet-communication/wallet-detector.js b/src/utils/aepp-wallet-communication/wallet-detector.js index 2f3a2ccf86..4003ae2ca2 100644 --- a/src/utils/aepp-wallet-communication/wallet-detector.js +++ b/src/utils/aepp-wallet-communication/wallet-detector.js @@ -55,7 +55,7 @@ export default stampit({ this.connection.connect(({ method, params }, origin, source) => { if ( !method || !params || - method !== METHODS.wallet.readyToConnect || wallets[params.id] + method !== METHODS.readyToConnect || wallets[params.id] ) return const wallet = { diff --git a/test/integration/rpc.js b/test/integration/rpc.js index fbf12e2790..f3ea002698 100644 --- a/test/integration/rpc.js +++ b/test/integration/rpc.js @@ -272,7 +272,7 @@ describe('Aepp<->Wallet', function () { it('Try to sing using unpermited account', async () => { const { publicKey: pub } = generateKeyPair() await expect(aepp.rpcClient.request( - METHODS.aepp.sign, { + METHODS.sign, { tx: 'tx_+NkLAfhCuECIIeWttRUiZ32uriBdmM1t+dCg90KuG2ABxOiuXqzpAul6uTWvsyfx3EFJDah6trudrityh+6XSX3mkPEimhgGuJH4jzIBoQELtO15J/l7UeG8teE0DRIzWyorEsi8UiHWPEvLOdQeYYgbwW1nTsgAAKEB6bv2BOYRtUYKOzmZ6Xcbb2BBfXPOfFUZ4S9+EnoSJcqIG8FtZ07IAACIAWNFeF2KAAAKAIYSMJzlQADAoDBrIcoop8JfZ4HOD9p3nDTiNthj7jjl+ArdHwEMUrvQgitwOr/v3Q==', onAccount: pub, returnSigned: true @@ -424,7 +424,7 @@ describe('Aepp<->Wallet', function () { it('Try to connect unsupported protocol', async () => { await expect(aepp.rpcClient.request( - METHODS.aepp.connect, { + METHODS.connect, { name: 'test-aepp', version: 2, networkId: aepp.getNetworkId() @@ -435,7 +435,7 @@ describe('Aepp<->Wallet', function () { it.skip('Try to connect unsupported network', async () => { // TODO: fix this assertion await expect(aepp.rpcClient.request( - METHODS.aepp.connect, { + METHODS.connect, { name: 'test-aepp', version: 1, networkId: 'ae_test'