From 82f1d6a3b978a49ddbd0e81bc0536f7948ef365f Mon Sep 17 00:00:00 2001 From: Szymon Lesisz Date: Thu, 16 Apr 2020 20:29:44 +0200 Subject: [PATCH] blockchainSubscribe: param "accounts" optional (#566) --- src/js/backend/BlockchainLink.js | 11 +++++++---- .../methods/blockchain/BlockchainSubscribe.js | 16 +++++++++------- .../methods/blockchain/BlockchainUnsubscribe.js | 2 +- src/js/types/__tests__/blockchain.js | 12 ++++++++++++ src/js/types/backend/blockchain.js | 4 ++-- src/ts/types/__tests__/blockchain.ts | 12 ++++++++++++ src/ts/types/backend/blockchain.d.ts | 4 ++-- 7 files changed, 45 insertions(+), 16 deletions(-) diff --git a/src/js/backend/BlockchainLink.js b/src/js/backend/BlockchainLink.js index ffcb07afb..183568e46 100644 --- a/src/js/backend/BlockchainLink.js +++ b/src/js/backend/BlockchainLink.js @@ -87,6 +87,7 @@ export default class Blockchain { coin: this.coinInfo, ...info, })); + // TODO: check for 1st block hash (different for btc forks) }); this.link.on('disconnected', () => { @@ -167,7 +168,7 @@ export default class Blockchain { return this.link.estimateFee(request); } - async subscribe(accounts: BlockchainSubscribeAccount[]) { + async subscribe(accounts?: BlockchainSubscribeAccount[]) { // set block listener if it wasn't set before if (this.link.listenerCount('block') === 0) { this.link.on('block', (block: BlockchainBlock) => { @@ -188,7 +189,10 @@ export default class Blockchain { }); } - await this.link.subscribe({ type: 'block' }); + const blockSubscription = await this.link.subscribe({ type: 'block' }); + if (!accounts) { + return blockSubscription; + } return this.link.subscribe({ type: 'accounts', @@ -221,9 +225,8 @@ export default class Blockchain { this.link.removeAllListeners('notification'); // remove all subscriptions - await this.link.unsubscribe({ type: 'block' }); await this.link.unsubscribe({ type: 'fiatRates' }); - return this.link.unsubscribe({ type: 'notification' }); + return this.link.unsubscribe({ type: 'block' }); } // unsubscribe only requested accounts return this.link.unsubscribe({ type: 'accounts', accounts }); diff --git a/src/js/core/methods/blockchain/BlockchainSubscribe.js b/src/js/core/methods/blockchain/BlockchainSubscribe.js index da4eb9a8a..5c0250554 100644 --- a/src/js/core/methods/blockchain/BlockchainSubscribe.js +++ b/src/js/core/methods/blockchain/BlockchainSubscribe.js @@ -9,7 +9,7 @@ import { getCoinInfo } from '../../../data/CoinInfo'; import type { CoreMessage, CoinInfo, BlockchainSubscribeAccount } from '../../../types'; type Params = { - accounts: BlockchainSubscribeAccount[]; + accounts?: BlockchainSubscribeAccount[]; coinInfo: CoinInfo; } @@ -25,15 +25,17 @@ export default class BlockchainSubscribe extends AbstractMethod { // validate incoming parameters validateParams(payload, [ - { name: 'accounts', type: 'array', obligatory: true }, + { name: 'accounts', type: 'array', allowEmpty: true }, { name: 'coin', type: 'string', obligatory: true }, ]); - payload.accounts.forEach(account => { - validateParams(account, [ - { name: 'descriptor', type: 'string', obligatory: true }, - ]); - }); + if (payload.accounts) { + payload.accounts.forEach(account => { + validateParams(account, [ + { name: 'descriptor', type: 'string', obligatory: true }, + ]); + }); + } const coinInfo = getCoinInfo(payload.coin); if (!coinInfo) { diff --git a/src/js/core/methods/blockchain/BlockchainUnsubscribe.js b/src/js/core/methods/blockchain/BlockchainUnsubscribe.js index b0bc691f0..a02d9c4fe 100644 --- a/src/js/core/methods/blockchain/BlockchainUnsubscribe.js +++ b/src/js/core/methods/blockchain/BlockchainUnsubscribe.js @@ -25,7 +25,7 @@ export default class BlockchainUnsubscribe extends AbstractMethod { // validate incoming parameters validateParams(payload, [ - { name: 'accounts', type: 'array' }, + { name: 'accounts', type: 'array', allowEmpty: true }, { name: 'coin', type: 'string', obligatory: true }, ]); diff --git a/src/js/types/__tests__/blockchain.js b/src/js/types/__tests__/blockchain.js index 747581942..75c09cb53 100644 --- a/src/js/types/__tests__/blockchain.js +++ b/src/js/types/__tests__/blockchain.js @@ -96,11 +96,19 @@ export const others = async () => { coin: 'btc', }); + TrezorConnect.blockchainSubscribe({ + coin: 'btc', + }); + TrezorConnect.blockchainUnsubscribe({ accounts, coin: 'btc', }); + TrezorConnect.blockchainUnsubscribe({ + coin: 'btc', + }); + TrezorConnect.blockchainDisconnect({ coin: 'btc' }); TrezorConnect.blockchainSetCustomBackend({ @@ -108,6 +116,10 @@ export const others = async () => { blockchainLink: undefined, }); + TrezorConnect.blockchainSetCustomBackend({ + coin: 'btc', + }); + TrezorConnect.blockchainSetCustomBackend({ coin: 'btc', blockchainLink: { diff --git a/src/js/types/backend/blockchain.js b/src/js/types/backend/blockchain.js index d0da39e32..ff57f2aee 100644 --- a/src/js/types/backend/blockchain.js +++ b/src/js/types/backend/blockchain.js @@ -58,7 +58,7 @@ export type BlockchainSubscribeFiatRates = { } export type BlockchainSubscribe = { - accounts: BlockchainSubscribeAccount[]; + accounts?: BlockchainSubscribeAccount[]; coin: string; } @@ -146,7 +146,7 @@ export type BlockchainEstimatedFee = { export type BlockchainSetCustomBackend = { coin: string; - blockchainLink: $PropertyType; + blockchainLink?: $PropertyType; } export type BlockchainEvent = diff --git a/src/ts/types/__tests__/blockchain.ts b/src/ts/types/__tests__/blockchain.ts index de08d693a..26af3ce17 100644 --- a/src/ts/types/__tests__/blockchain.ts +++ b/src/ts/types/__tests__/blockchain.ts @@ -95,11 +95,19 @@ export const others = async () => { coin: 'btc', }); + TrezorConnect.blockchainSubscribe({ + coin: 'btc', + }); + TrezorConnect.blockchainUnsubscribe({ accounts, coin: 'btc', }); + TrezorConnect.blockchainUnsubscribe({ + coin: 'btc', + }); + TrezorConnect.blockchainDisconnect({ coin: 'btc' }); TrezorConnect.blockchainSetCustomBackend({ @@ -107,6 +115,10 @@ export const others = async () => { blockchainLink: undefined, }); + TrezorConnect.blockchainSetCustomBackend({ + coin: 'btc', + }); + TrezorConnect.blockchainSetCustomBackend({ coin: 'btc', blockchainLink: { diff --git a/src/ts/types/backend/blockchain.d.ts b/src/ts/types/backend/blockchain.d.ts index 28f275067..72b02a0e0 100644 --- a/src/ts/types/backend/blockchain.d.ts +++ b/src/ts/types/backend/blockchain.d.ts @@ -43,7 +43,7 @@ export interface BlockchainSubscribeAccount { } export interface BlockchainSubscribe { - accounts: BlockchainSubscribeAccount[]; + accounts?: BlockchainSubscribeAccount[]; coin: string; } @@ -148,7 +148,7 @@ export interface BlockchainAccountBalanceHistory { export interface BlockchainSetCustomBackend { coin: string; - blockchainLink: CoinInfo['blockchainLink']; + blockchainLink?: CoinInfo['blockchainLink']; } export type BlockchainEvent =