diff --git a/examples/browser/aepp/src/App.vue b/examples/browser/aepp/src/App.vue index 2c9c6e93ee..8bb57f0151 100644 --- a/examples/browser/aepp/src/App.vue +++ b/examples/browser/aepp/src/App.vue @@ -48,7 +48,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; diff --git a/src/AeSdkWallet.ts b/src/AeSdkWallet.ts index 59aeb4d13e..2c58804202 100644 --- a/src/AeSdkWallet.ts +++ b/src/AeSdkWallet.ts @@ -106,12 +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._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._getAccountsForClient(client))); } override selectAccount(address: Encoded.AccountAddress): void { @@ -149,11 +160,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(); @@ -219,10 +225,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: @@ -234,11 +240,11 @@ export default class AeSdkWallet extends AeSdk { return { subscription: Array.from(client.addressSubscription), - address: this.getAccounts(), + address: this._getAccountsForClient(client), }; }, [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(); }, diff --git a/test/integration/rpc.ts b/test/integration/rpc.ts index c994f73abc..07eb02949d 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'); }); @@ -490,7 +490,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; @@ -500,7 +500,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) => { @@ -508,7 +508,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); }); @@ -524,7 +523,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); }); @@ -646,8 +645,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'); }); @@ -657,7 +657,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'); });