Skip to content

Commit

Permalink
refactor(aepp-wallet schema)!: rearrange METHODS enum
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Jan 25, 2022
1 parent 95bf0e9 commit 8a40105
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 51 deletions.
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
12 changes: 6 additions & 6 deletions src/utils/aepp-wallet-communication/rpc/wallet-rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() }

Expand All @@ -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() }

Expand Down Expand Up @@ -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() }
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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())
})
},
/**
Expand Down
37 changes: 9 additions & 28 deletions src/utils/aepp-wallet-communication/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/aepp-wallet-communication/wallet-detector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
6 changes: 3 additions & 3 deletions test/integration/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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'
Expand Down

0 comments on commit 8a40105

Please sign in to comment.