From 457cfe29661dc7625b25d0d52c1af85782d70d76 Mon Sep 17 00:00:00 2001 From: Denis Davidyuk Date: Sat, 22 Jul 2023 19:07:51 +0600 Subject: [PATCH 1/5] docs(aepp,wallet): fix disconnect in examples --- examples/browser/aepp/src/App.vue | 2 - examples/browser/aepp/src/Connect.vue | 75 ++++++++++------ examples/browser/aepp/src/store.js | 1 - examples/browser/aepp/src/styles.scss | 2 +- examples/browser/wallet-iframe/src/App.vue | 90 ++++++++++++++----- .../browser/wallet-iframe/src/styles.scss | 2 +- .../wallet-web-extension/src/styles.scss | 2 +- 7 files changed, 119 insertions(+), 55 deletions(-) diff --git a/examples/browser/aepp/src/App.vue b/examples/browser/aepp/src/App.vue index 32bfef5240..9c3e88ffdc 100644 --- a/examples/browser/aepp/src/App.vue +++ b/examples/browser/aepp/src/App.vue @@ -41,7 +41,6 @@ diff --git a/examples/browser/aepp/src/Connect.vue b/examples/browser/aepp/src/Connect.vue index 5ec9bcbf63..3d335e6292 100644 --- a/examples/browser/aepp/src/Connect.vue +++ b/examples/browser/aepp/src/Connect.vue @@ -13,18 +13,26 @@
+ + + @@ -34,6 +42,7 @@
{{ (walletConnected && 'Wallet connected') + || (cancelWalletDetection && 'Wallet detection') || (walletConnecting && 'Wallet connecting') || 'Ready to connect to wallet' }} @@ -48,7 +57,7 @@ diff --git a/examples/browser/wallet-iframe/src/styles.scss b/examples/browser/wallet-iframe/src/styles.scss index e595669c68..1e921cc8fb 100644 --- a/examples/browser/wallet-iframe/src/styles.scss +++ b/examples/browser/wallet-iframe/src/styles.scss @@ -15,7 +15,7 @@ body { } button { - @extend .w-32, .p-2, .m-2, .rounded-full, .bg-purple-500, .text-white, .text-xs; + @extend .w-40, .p-2, .m-2, .rounded-full, .bg-purple-500, .text-white, .text-xs; &:disabled { @extend .bg-purple-300, .cursor-not-allowed; diff --git a/examples/browser/wallet-web-extension/src/styles.scss b/examples/browser/wallet-web-extension/src/styles.scss index 28df27f2a0..6130689684 100644 --- a/examples/browser/wallet-web-extension/src/styles.scss +++ b/examples/browser/wallet-web-extension/src/styles.scss @@ -7,7 +7,7 @@ body { } button { - @extend .w-32, .p-2, .m-2, .rounded-full, .bg-purple-500, .text-white, .text-xs; + @extend .w-40, .p-2, .m-2, .rounded-full, .bg-purple-500, .text-white, .text-xs; &:disabled { @extend .bg-purple-300, .cursor-not-allowed; From e2ffc25021d16b197bba41c88520e0a842b88ba9 Mon Sep 17 00:00:00 2001 From: Denis Davidyuk Date: Sat, 22 Jul 2023 21:53:58 +0600 Subject: [PATCH 2/5] fix(wallet): don't ask to confirm unsubscription --- src/AeSdkWallet.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/AeSdkWallet.ts b/src/AeSdkWallet.ts index 8a46aa61fc..879a2fd916 100644 --- a/src/AeSdkWallet.ts +++ b/src/AeSdkWallet.ts @@ -219,10 +219,10 @@ export default class AeSdkWallet extends AeSdk { [METHODS.subscribeAddress]: async ({ type, value }, origin) => { if (!this._isRpcClientConnected(id)) throw new RpcNotAuthorizeError(); - await this.onSubscription(id, { type, value }, origin); - switch (type) { case SUBSCRIPTION_TYPES.subscribe: + // TODO: remove `type` as it always subscribe + await this.onSubscription(id, { type, value }, origin); client.addressSubscription.add(value); break; case SUBSCRIPTION_TYPES.unsubscribe: From 36920b4234a00be4e3cc2728372e875b361f9409 Mon Sep 17 00:00:00 2001 From: Denis Davidyuk Date: Sat, 22 Jul 2023 21:58:03 +0600 Subject: [PATCH 3/5] fix(wallet): don't require to be subscribed to request addresses --- src/AeSdkWallet.ts | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/AeSdkWallet.ts b/src/AeSdkWallet.ts index 879a2fd916..a4ac0ec0c6 100644 --- a/src/AeSdkWallet.ts +++ b/src/AeSdkWallet.ts @@ -109,9 +109,10 @@ export default class AeSdkWallet extends AeSdk { _pushAccountsToApps(): void { if (this._clients == null) return; Array.from(this._clients.keys()) - .filter((clientId) => this._isRpcClientSubscribed(clientId)) - .map((clientId) => this._getClient(clientId).rpc) - .forEach((client) => client.notify(METHODS.updateAddress, this.getAccounts())); + .filter((clientId) => this._isRpcClientConnected(clientId)) + .map((clientId) => this._getClient(clientId)) + .filter((client) => client.addressSubscription.size !== 0) + .forEach((client) => client.rpc.notify(METHODS.updateAddress, this.getAccounts())); } override selectAccount(address: Encoded.AccountAddress): void { @@ -149,11 +150,6 @@ export default class AeSdkWallet extends AeSdk { return client; } - _isRpcClientSubscribed(clientId: string): boolean { - return this._isRpcClientConnected(clientId) - && this._getClient(clientId).addressSubscription.size !== 0; - } - _isRpcClientConnected(clientId: string): boolean { return RPC_STATUS.CONNECTED === this._getClient(clientId).status && this._getClient(clientId).rpc.connection.isConnected(); @@ -238,7 +234,7 @@ export default class AeSdkWallet extends AeSdk { }; }, [METHODS.address]: async (params, origin) => { - if (!this._isRpcClientSubscribed(id)) throw new RpcNotAuthorizeError(); + if (!this._isRpcClientConnected(id)) throw new RpcNotAuthorizeError(); await this.onAskAccounts(id, params, origin); return this.addresses(); }, From fa900c0481cc898211a3d99d22d57d34cac02177 Mon Sep 17 00:00:00 2001 From: Denis Davidyuk Date: Sat, 22 Jul 2023 22:03:28 +0600 Subject: [PATCH 4/5] fix(wallet): return accounts according to subscription --- src/AeSdkWallet.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/AeSdkWallet.ts b/src/AeSdkWallet.ts index a4ac0ec0c6..a0217d54ac 100644 --- a/src/AeSdkWallet.ts +++ b/src/AeSdkWallet.ts @@ -106,13 +106,23 @@ export default class AeSdkWallet extends AeSdk { this._type = type; } + _getAccountsForClient({ addressSubscription }: RpcClientsInfo): Accounts { + const { current, connected } = this.getAccounts(); + return { + current: addressSubscription.has('current') || addressSubscription.has('connected') + ? current : {}, + connected: addressSubscription.has('connected') ? connected : {}, + }; + } + _pushAccountsToApps(): void { if (this._clients == null) return; Array.from(this._clients.keys()) .filter((clientId) => this._isRpcClientConnected(clientId)) .map((clientId) => this._getClient(clientId)) .filter((client) => client.addressSubscription.size !== 0) - .forEach((client) => client.rpc.notify(METHODS.updateAddress, this.getAccounts())); + .forEach((client) => client.rpc + .notify(METHODS.updateAddress, this._getAccountsForClient(client))); } override selectAccount(address: Encoded.AccountAddress): void { @@ -230,7 +240,7 @@ export default class AeSdkWallet extends AeSdk { return { subscription: Array.from(client.addressSubscription), - address: this.getAccounts(), + address: this._getAccountsForClient(client), }; }, [METHODS.address]: async (params, origin) => { From 1d848437498a60d36532abd1a8ab51613e35b99a Mon Sep 17 00:00:00 2001 From: Denis Davidyuk Date: Sun, 23 Jul 2023 16:08:10 +0600 Subject: [PATCH 5/5] test(aepp,wallet): use `assertNotNull` instead throwing `UnexpectedTsError` --- test/integration/rpc.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/integration/rpc.ts b/test/integration/rpc.ts index 65f631bb39..4659dbbf6c 100644 --- a/test/integration/rpc.ts +++ b/test/integration/rpc.ts @@ -24,7 +24,6 @@ import { verify, NoWalletConnectedError, UnAuthorizedAccountError, - UnexpectedTsError, UnknownRpcClientError, UnsubscribedAccountError, AccountBase, @@ -38,6 +37,7 @@ import { getSdk, ignoreVersion, networkId, url, compilerUrl, } from '.'; import { Accounts, Network } from '../../src/aepp-wallet-communication/rpc/types'; +import { assertNotNull } from '../utils'; const WindowPostMessageFake = ( name: string, @@ -149,7 +149,6 @@ describe('Aepp<->Wallet', function aeppWallet() { it('Should receive `announcePresence` message from wallet', async () => { const isReceived = new Promise((resolve) => { - if (connections.aeppWindow.addEventListener == null) throw new UnexpectedTsError(); connections.aeppWindow.addEventListener('message', (msg) => { resolve(msg.data.method === 'connection.announcePresence'); }); @@ -321,7 +320,7 @@ describe('Aepp<->Wallet', function aeppWallet() { it('Try to sign using unpermited account', async () => { const { publicKey: pub } = generateKeyPair(); - if (aepp.rpcClient == null) throw new UnexpectedTsError(); + assertNotNull(aepp.rpcClient); await expect(aepp.rpcClient.request(METHODS.sign, { tx: 'tx_+NkLAfhCuECIIeWttRUiZ32uriBdmM1t+dCg90KuG2ABxOiuXqzpAul6uTWvsyfx3EFJDah6trudrityh+6XSX3mkPEimhgGuJH4jzIBoQELtO15J/l7UeG8teE0DRIzWyorEsi8UiHWPEvLOdQeYYgbwW1nTsgAAKEB6bv2BOYRtUYKOzmZ6Xcbb2BBfXPOfFUZ4S9+EnoSJcqIG8FtZ07IAACIAWNFeF2KAAAKAIYSMJzlQADAoDBrIcoop8JfZ4HOD9p3nDTiNthj7jjl+ArdHwEMUrvQgitwOr/v3Q==', onAccount: pub, @@ -351,8 +350,9 @@ describe('Aepp<->Wallet', function aeppWallet() { payload: encode(Buffer.from('zerospend'), Encoding.Bytearray), }); const res = await aepp.sendTransaction(tx); - if (res.tx?.payload == null || res.blockHeight == null) throw new UnexpectedTsError(); + assertNotNull(res.tx?.payload); decode(res.tx.payload as Encoded.Any).toString().should.be.equal('zerospend2'); + assertNotNull(res.blockHeight); res.blockHeight.should.be.a('number'); }); @@ -454,7 +454,7 @@ describe('Aepp<->Wallet', function aeppWallet() { describe('Subscriptions', () => { it('Add new account to wallet: receive notification for update accounts', async () => { - if (aepp._accounts == null) throw new UnexpectedTsError(); + assertNotNull(aepp._accounts); const connectedLength = Object.keys(aepp._accounts.connected).length; const accountsPromise = new Promise((resolve) => { aepp.onAddressChange = resolve; @@ -464,7 +464,7 @@ describe('Aepp<->Wallet', function aeppWallet() { }); it('Receive update for wallet select account', async () => { - if (aepp._accounts == null) throw new UnexpectedTsError(); + assertNotNull(aepp._accounts); const connectedAccount = Object .keys(aepp._accounts.connected)[0] as Encoded.AccountAddress; const accountsPromise = new Promise((resolve) => { @@ -472,7 +472,6 @@ describe('Aepp<->Wallet', function aeppWallet() { }); wallet.selectAccount(connectedAccount); const { connected, current } = await accountsPromise; - if (current == null || connected == null) throw new UnexpectedTsError(); expect(current[connectedAccount]).to.be.eql({}); expect(Object.keys(connected).includes(connectedAccount)).to.be.equal(false); }); @@ -488,7 +487,7 @@ describe('Aepp<->Wallet', function aeppWallet() { }); it('Try to connect unsupported protocol', async () => { - if (aepp.rpcClient == null) throw new UnexpectedTsError(); + assertNotNull(aepp.rpcClient); await expect(aepp.rpcClient.request(METHODS.connect, { name: 'test-aepp', version: 2 as 1, connectNode: false })).to.be.eventually.rejectedWith('Unsupported Protocol Version').with.property('code', 5); }); @@ -610,8 +609,9 @@ describe('Aepp<->Wallet', function aeppWallet() { payload: encode(Buffer.from('zerospend'), Encoding.Bytearray), }); const res = await aepp.sendTransaction(tx); - if (res.tx?.payload == null || res.blockHeight == null) throw new UnexpectedTsError(); + assertNotNull(res.tx?.payload); decode(res.tx.payload as Encoded.Any).toString().should.be.equal('zerospend2'); + assertNotNull(res.blockHeight); res.blockHeight.should.be.a('number'); }); @@ -621,7 +621,7 @@ describe('Aepp<->Wallet', function aeppWallet() { wallet.addNode('second_node', node, true); }); message.networkId.should.be.equal(networkId); - if (message.node == null) throw new UnexpectedTsError(); + assertNotNull(message.node); message.node.should.be.an('object'); expect(wallet.selectedNodeName).to.be.equal('second_node'); });