Skip to content

Commit

Permalink
refactor(wallet rpc)!: don't pass networkId from aepp to wallet
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `RpcClient` doesn't contain `networkId` anymore
On wallet side: assume that all aepps uses the same network as the wallet connected to.
On aepp side: use `networkId` that wallet provided.
In case `networkId` is not compatible ask user to switch wallet to a compatible network.
  • Loading branch information
davidyuk committed Apr 22, 2022
1 parent 845dbda commit 153fd89
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 43 deletions.
6 changes: 2 additions & 4 deletions src/account/base.js
Expand Up @@ -119,10 +119,8 @@ async function verifyMessage (message, signature, opt = {}) {
* @return {Object} Account instance
*/
export default stampit({
init ({ networkId }) { // NETWORK_ID using for signing transaction's
if (!this.networkId && networkId) {
this.networkId = networkId
}
init ({ networkId }) {
this.networkId ??= networkId
},
methods: { signTransaction, getNetworkId, signMessage, verifyMessage }
}, required({
Expand Down
3 changes: 1 addition & 2 deletions src/account/rpc.js
Expand Up @@ -40,8 +40,7 @@ export default AccountBase.compose({
...options,
onAccount: this._address,
tx,
returnSigned: true,
networkId: this.getNetworkId(options)
returnSigned: true
})
},
/**
Expand Down
5 changes: 1 addition & 4 deletions src/utils/aepp-wallet-communication/rpc/aepp-rpc.js
Expand Up @@ -32,8 +32,7 @@ const NOTIFICATIONS = {
},
[METHODS.updateNetwork]: (instance) =>
async ({ params }) => {
const { networkId, node } = params
instance.rpcClient.info.networkId = networkId
const { node } = params
if (node) instance.addNode(node.name, await Node(node), true)
instance.onNetworkChange(params)
},
Expand Down Expand Up @@ -158,7 +157,6 @@ export default AccountResolver.compose({
if (this.rpcClient?.isConnected()) throw new AlreadyConnectedError('You are already connected to wallet ' + this.rpcClient)
this.rpcClient = RpcClient({
connection,
networkId: this.getNetworkId({ force: true }),
...connection.connectionInfo,
id: uuid(),
handlers: [handleMessage(this), this.onDisconnect]
Expand Down Expand Up @@ -223,7 +221,6 @@ export default AccountResolver.compose({
METHODS.connect, {
name: this.name,
version: VERSION,
networkId: this.getNetworkId({ force: true }),
connectNode
}
)
Expand Down
4 changes: 2 additions & 2 deletions src/utils/aepp-wallet-communication/rpc/rpc-client.js
Expand Up @@ -31,10 +31,10 @@ import {
* @return {Object}
*/
export default stampit({
init ({ id, name, networkId, icons, connection, handlers: [onMessage, onDisconnect] }) {
init ({ id, name, icons, connection, handlers: [onMessage, onDisconnect] }) {
this.id = id
this.connection = connection
this.info = { name, networkId, icons }
this.info = { name, icons }
// {
// [msg.id]: { resolve, reject }
// }
Expand Down
15 changes: 4 additions & 11 deletions src/utils/aepp-wallet-communication/rpc/wallet-rpc.js
Expand Up @@ -43,14 +43,13 @@ const REQUESTS = {
callInstance,
instance,
client,
{ name, networkId, version, icons, connectNode }) {
{ name, version, icons, connectNode }) {
// Check if protocol and network is compatible with wallet
if (version !== VERSION) return { error: ERRORS.unsupportedProtocol() }
// Store new AEPP and wait for connection approve
client.updateInfo({
status: RPC_STATUS.WAITING_FOR_CONNECTION_APPROVE,
name,
networkId,
icons,
version,
origin: window.location.origin,
Expand All @@ -60,7 +59,7 @@ const REQUESTS = {
// Call onConnection callBack to notice Wallet about new AEPP
return callInstance(
'onConnection',
{ name, networkId, version },
{ name, version },
({ shareNode } = {}) => {
client.updateInfo({ status: shareNode ? RPC_STATUS.NODE_BINDED : RPC_STATUS.CONNECTED })
return {
Expand Down Expand Up @@ -121,20 +120,14 @@ const REQUESTS = {
)
},
[METHODS.sign] (callInstance, instance, client, options) {
const { tx, onAccount, networkId, returnSigned = false } = options
const { tx, onAccount, returnSigned = false } = options
const address = onAccount || client.currentAccount
// Update client with new networkId
networkId && client.updateInfo({ networkId })
// Authorization check
if (!client.isConnected()) return { error: ERRORS.notAuthorize() }
// Account permission check
if (!client.hasAccessToAccount(address)) {
return { error: ERRORS.permissionDeny(address) }
}
// NetworkId check
if (!networkId || networkId !== instance.getNetworkId()) {
return { error: ERRORS.unsupportedNetwork() }
}

return callInstance(
'onSign',
Expand Down Expand Up @@ -308,7 +301,7 @@ export default Ae.compose(AccountMultiple, {
client.sendMessage(
message(METHODS.updateNetwork, {
networkId: this.getNetworkId(),
...(client.info.status === RPC_STATUS.NODE_BINDED && { node: this.selectedNode })
...client.info.status === RPC_STATUS.NODE_BINDED && { node: this.selectedNode }
}), true)
})
}
Expand Down
4 changes: 0 additions & 4 deletions src/utils/aepp-wallet-communication/schema.ts
Expand Up @@ -72,9 +72,5 @@ export const ERRORS = {
unsupportedProtocol: () => ({
code: 5,
message: 'Unsupported Protocol Version'
}),
unsupportedNetwork: () => ({
code: 8,
message: 'Unsupported Network'
})
}
17 changes: 1 addition & 16 deletions test/integration/rpc.js
Expand Up @@ -413,25 +413,10 @@ describe('Aepp<->Wallet', function () {

it('Try to connect unsupported protocol', async () => {
await expect(aepp.rpcClient.request(
METHODS.connect, {
name: 'test-aepp',
version: 2,
networkId: aepp.getNetworkId()
}
METHODS.connect, { name: 'test-aepp', version: 2 }
)).to.be.eventually.rejectedWith('Unsupported Protocol Version').with.property('code', 5)
})

it.skip('Try to connect unsupported network', async () => {
// TODO: fix this assertion
await expect(aepp.rpcClient.request(
METHODS.connect, {
name: 'test-aepp',
version: 1,
networkId: 'ae_test'
}
)).to.be.eventually.rejectedWith('Unsupported Network').with.property('code', 8)
})

it('Process response ', () => {
expect(() => aepp.rpcClient.processResponse({ id: 11, error: {} }))
.to.throw('Can\'t find callback for this messageId ' + 11)
Expand Down

0 comments on commit 153fd89

Please sign in to comment.