Skip to content

Commit

Permalink
Merge pull request #1392 from aeternity/feature/aepp-wallet
Browse files Browse the repository at this point in the history
Convert repp-wallet schema to TypeScript
  • Loading branch information
davidyuk authored Jan 27, 2022
2 parents 72a75f1 + 1277b5b commit da6a868
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 147 deletions.
11 changes: 1 addition & 10 deletions src/utils/aepp-wallet-communication/helpers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/**
* Browser helper functions
*/
import { isAccountBase } from '../../account/base'
import { TypeError, NoBrowserFoundError } from '../errors'
import { NoBrowserFoundError } from '../errors'

export const getBrowserAPI = (force = false) => {
const { chrome, browser } = window
Expand Down Expand Up @@ -61,11 +60,3 @@ export const sendResponseMessage = (client) => (id, method, data) =>
export const voidFn = () => undefined

export const isValidAccounts = (accounts) => (['', 'connected', 'current'].filter(k => typeof (k ? accounts[k] : accounts) === 'object')).length === 3

export const resolveOnAccount = (addresses, onAccount, opt = {}) => {
if (!addresses.find(a => a === onAccount)) {
if (typeof opt.onAccount !== 'object' || !isAccountBase(opt.onAccount)) throw new TypeError('Provided onAccount should be an AccountBase')
onAccount = opt.onAccount
}
return onAccount
}
24 changes: 12 additions & 12 deletions src/utils/aepp-wallet-communication/rpc/aepp-rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
} from '../../errors'

const NOTIFICATIONS = {
[METHODS.wallet.updateAddress]: (instance) =>
[METHODS.updateAddress]: (instance) =>
({ params }) => {
instance.rpcClient.accounts = params
instance.onAddressChange(params)
Expand All @@ -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) {
Expand All @@ -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])
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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() }
)
},
Expand All @@ -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
Expand All @@ -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 })
Expand Down Expand Up @@ -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() }
)
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/aepp-wallet-communication/rpc/rpc-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
},
/**
Expand Down
33 changes: 21 additions & 12 deletions src/utils/aepp-wallet-communication/rpc/wallet-rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,22 @@ import verifyTransaction from '../../../tx/validator'
import AccountMultiple from '../../../account/multiple'
import TxObject from '../../../tx/tx-object'
import RpcClient from './rpc-client'
import { getBrowserAPI, getHandler, isValidAccounts, message, resolveOnAccount, sendResponseMessage } from '../helpers'
import { getBrowserAPI, getHandler, isValidAccounts, message, sendResponseMessage } from '../helpers'
import { ERRORS, METHODS, RPC_STATUS, VERSION, WALLET_TYPE } from '../schema'
import {
IllegalArgumentError,
TypeError,
UnknownRpcClientError
} from '../../errors'
import { isAccountBase } from '../../../account/base'

const resolveOnAccount = (addresses, onAccount, opt = {}) => {
if (!addresses.find(a => a === onAccount)) {
if (typeof opt.onAccount !== 'object' || !isAccountBase(opt.onAccount)) throw new TypeError('Provided onAccount should be an AccountBase')
onAccount = opt.onAccount
}
return onAccount
}

const NOTIFICATIONS = {
[METHODS.closeConnection]: (instance, { client }) =>
Expand All @@ -33,7 +42,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() }

Expand All @@ -60,7 +69,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() }

Expand All @@ -83,13 +92,13 @@ const REQUESTS = {
}
} catch (e) {
if (instance.debug) console.error(e)
return { error: ERRORS.internalError({ msg: e.message }) }
return { error: ERRORS.internalError(e.message) }
}
},
(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() }
Expand All @@ -104,7 +113,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
Expand All @@ -113,7 +122,7 @@ const REQUESTS = {
if (!client.isConnected()) return { error: ERRORS.notAuthorize() }
// Account permission check
if (!client.hasAccessToAccount(address)) {
return { error: ERRORS.permissionDeny({ account: address }) }
return { error: ERRORS.permissionDeny(address) }
}
// NetworkId check
if (!networkId || networkId !== instance.getNetworkId()) {
Expand All @@ -129,7 +138,7 @@ const REQUESTS = {
onAcc = resolveOnAccount(instance.addresses(), address, opt)
} catch (e) {
if (instance.debug) console.error(e)
return { error: ERRORS.internalError({ msg: e.message }) }
return { error: ERRORS.internalError(e.message) }
}
try {
const t = rawTx || tx
Expand All @@ -151,12 +160,12 @@ 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
if (!client.hasAccessToAccount(address)) {
return { error: ERRORS.permissionDeny({ account: address }) }
return { error: ERRORS.permissionDeny(address) }
}

return callInstance(
Expand All @@ -175,7 +184,7 @@ const REQUESTS = {
}
} catch (e) {
if (instance.debug) console.error(e)
return { error: ERRORS.internalError({ msg: e.message }) }
return { error: ERRORS.internalError(e.message) }
}
},
(error) => ({ error: ERRORS.rejectedByUser(error) })
Expand Down Expand Up @@ -358,7 +367,7 @@ export default Ae.compose(AccountMultiple, {
shareWalletInfo (postFn) {
postFn({
jsonrpc: '2.0',
...message(METHODS.wallet.readyToConnect, this.getWalletInfo())
...message(METHODS.readyToConnect, this.getWalletInfo())
})
},
/**
Expand Down
108 changes: 0 additions & 108 deletions src/utils/aepp-wallet-communication/schema.js

This file was deleted.

Loading

0 comments on commit da6a868

Please sign in to comment.