From e12d83c4ab65b147d438e85dda74af76e2907c75 Mon Sep 17 00:00:00 2001 From: notEternal Date: Mon, 7 Dec 2020 18:13:21 +0800 Subject: [PATCH 1/4] Remove ts-ignores. Adding interfaces --- src/common/coins/BTC.ts | 14 ++++++---- src/common/coins/GHOST.ts | 13 +++++---- src/common/coins/LTC.ts | 11 ++++---- src/common/coins/NEXT.ts | 13 +++++---- src/common/coins/interfaces.ts | 27 +++++++++++++++++++ src/common/tests/test-getBalance.ts | 2 +- src/core/swap.swaps/SumSwap.ts | 3 +-- src/front/shared/pages/Wallet/Row/Row.tsx | 2 -- src/front/shared/redux/actions/btcmultisig.ts | 3 +-- 9 files changed, 61 insertions(+), 27 deletions(-) create mode 100644 src/common/coins/interfaces.ts diff --git a/src/common/coins/BTC.ts b/src/common/coins/BTC.ts index 86001df010..08b14c1024 100644 --- a/src/common/coins/BTC.ts +++ b/src/common/coins/BTC.ts @@ -1,3 +1,4 @@ +import { RefObject } from 'react'; import bip32 from 'bip32' import bip39 from'bip39' import bitcore from 'bitcore-lib' @@ -7,14 +8,18 @@ import BigNumber from 'bignumber.js' import { networkType } from './../domain/network' import bip44 from './../helpers/bip44' - +import { + ICoin, + ILibAdapter, + IConnector +} from './interfaces' const netNames = { 'mainnet': 'mainnet', 'testnet': 'testnet', } -const BTC = { +const BTC: ICoin = { ticker: 'BTC', name: 'Bitcoin', precision: 8, @@ -80,11 +85,10 @@ export default BTC -const libAdapter = { +const libAdapter: ILibAdapter = { accountFromMnemonic(mnemonic, netName) { const network = BTC[netName] - // @ts-ignore const settings = network.settings // todo: move? @@ -134,7 +138,7 @@ const libAdapter = { -const connector = { +const connector: IConnector = { // bitcore API documentation: // https://github.com/bitpay/bitcore/blob/master/packages/bitcore-node/docs/api-documentation.md diff --git a/src/common/coins/GHOST.ts b/src/common/coins/GHOST.ts index 3433d4475d..a530b65c94 100644 --- a/src/common/coins/GHOST.ts +++ b/src/common/coins/GHOST.ts @@ -6,6 +6,11 @@ import fetch from 'node-fetch' import { networkType } from './../domain/network' import bip44 from './../helpers/bip44' +import { + ICoin, + ILibAdapter, + IConnector +} from './interfaces' const netNames = { @@ -13,7 +18,7 @@ const netNames = { 'testnet': 'testnet', } -const GHOST = { +const GHOST: ICoin = { ticker: 'GHOST', name: 'Ghost', precision: 8, @@ -85,11 +90,10 @@ export default GHOST -const libAdapter = { +const libAdapter: ILibAdapter = { accountFromMnemonic(mnemonic, netName) { const network = GHOST[netName] - //@ts-ignore const settings = network.settings const seed = bip39.mnemonicToSeedSync(mnemonic) @@ -126,7 +130,6 @@ const libAdapter = { const network = GHOST[netName] const addressStr = address.toString() - //@ts-ignore const unspent = await connector.fetchUnspents(network.type, addressStr) const tx = new ghost_bitcore.Transaction() @@ -143,7 +146,7 @@ const libAdapter = { -const connector = { +const connector: IConnector = { getApiUrl(netType) { if (netType === networkType.mainnet) { diff --git a/src/common/coins/LTC.ts b/src/common/coins/LTC.ts index 812c390051..af36f476e7 100644 --- a/src/common/coins/LTC.ts +++ b/src/common/coins/LTC.ts @@ -6,14 +6,17 @@ import fetch from 'node-fetch' import { networkType } from './../domain/network' import bip44 from './../helpers/bip44' - +import { + ICoin, + ILibAdapter +} from './interfaces' const netNames = { 'mainnet': 'mainnet', 'testnet': 'testnet', } -const LTC = { +const LTC: ICoin = { ticker: 'LTC', name: 'Litecoin', precision: 8, // ? @@ -71,17 +74,15 @@ export default LTC -const libAdapter = { +const libAdapter: ILibAdapter = { accountFromMnemonic(mnemonic, netName) { const network = LTC[netName] - //@ts-ignore const settings = network.settings // todo: move? const seed = bip39.mnemonicToSeedSync(mnemonic) - //@ts-ignore const root = bip32.fromSeed(seed, network.bip32settings) const derivePath = bip44.createDerivePath(network) const child = root.derivePath(derivePath) diff --git a/src/common/coins/NEXT.ts b/src/common/coins/NEXT.ts index e18b4ba893..9d1b218b3e 100644 --- a/src/common/coins/NEXT.ts +++ b/src/common/coins/NEXT.ts @@ -6,6 +6,11 @@ import fetch from 'node-fetch' import { networkType } from './../domain/network' import bip44 from './../helpers/bip44' +import { + ICoin, + ILibAdapter, + IConnector +} from './interfaces' const netNames = { @@ -13,7 +18,7 @@ const netNames = { //'testnet': 'testnet', // testnet is down } -const NEXT = { +const NEXT: ICoin = { ticker: 'NEXT', name: 'NEXT.coin', precision: 8, @@ -64,11 +69,10 @@ export default NEXT -const libAdapter = { +const libAdapter: ILibAdapter = { accountFromMnemonic(mnemonic, netName) { const network = NEXT[netName] - //@ts-ignore const settings = network.settings const seed = bip39.mnemonicToSeedSync(mnemonic) @@ -117,7 +121,6 @@ const libAdapter = { const network = NEXT[netName] const addressStr = address.toString() - //@ts-ignore const unspent = await connector.fetchUnspents(network.type, addressStr) const tx = new bitcore.Transaction() @@ -134,7 +137,7 @@ const libAdapter = { -const connector = { +const connector: IConnector = { // next.exhnage API documentation: // https://explore.next.exchange/#/api diff --git a/src/common/coins/interfaces.ts b/src/common/coins/interfaces.ts new file mode 100644 index 0000000000..1564d33879 --- /dev/null +++ b/src/common/coins/interfaces.ts @@ -0,0 +1,27 @@ +export interface ICoin { + ticker: string + name: string + precision: number + networks: { [key: string]: string } +} + +export interface ILibAdapter { + accountFromMnemonic(mnemonic: string, netName: string): { + privateKey: string + publicKey: string + address: string + } + createTx?({ netName, account, amount, to }): Promise +} + +export interface IConnector { + getApiUrl(netwType: string): string + getTxUrl(netType: string, txId: string): string + fetchBalance(networkType: string, address: string): Promise + + fetchUnspents?(netType: string, addr: string): Promise + fetchTx?(tsid: string): Promise + fetchRawTx?(tsid: string): Promise + publishRawTx?(netType: string, rawTx: string): Promise + publishTx?(networkType: string, rawTx: string): Promise +} \ No newline at end of file diff --git a/src/common/tests/test-getBalance.ts b/src/common/tests/test-getBalance.ts index 17b4a27e69..3d0c9502d8 100644 --- a/src/common/tests/test-getBalance.ts +++ b/src/common/tests/test-getBalance.ts @@ -88,7 +88,7 @@ const tests = [ } const { coin, network, address, isCoins } = test; console.log(`Test: getBalance, ${coin} ${network}, balance ${isCoins ? '> 0' : '= 0'}`) - //@ts-ignore + const balance = await coins[coin][network].getBalance(address) //console.log(balance) if ((balance > 0) == isCoins) { diff --git a/src/core/swap.swaps/SumSwap.ts b/src/core/swap.swaps/SumSwap.ts index cc3a8a778c..c9896c34f0 100644 --- a/src/core/swap.swaps/SumSwap.ts +++ b/src/core/swap.swaps/SumSwap.ts @@ -230,7 +230,7 @@ class SumSwap extends SwapInterface { * @param {object|string} data - scriptValues or wallet address * @returns {Promise.} */ - async getBalance(data, hashName) { + async getBalance(data, hashName?) { let address if (typeof data === 'string') { @@ -341,7 +341,6 @@ class SumSwap extends SwapInterface { async checkBalance(data) { const { ownerAddress, expectedValue } = data let balance = await util.helpers.repeatAsyncUntilResult(() => - //@ts-ignore this.getBalance( ownerAddress ) ) diff --git a/src/front/shared/pages/Wallet/Row/Row.tsx b/src/front/shared/pages/Wallet/Row/Row.tsx index c9d785fd18..4a4f35ad5f 100644 --- a/src/front/shared/pages/Wallet/Row/Row.tsx +++ b/src/front/shared/pages/Wallet/Row/Row.tsx @@ -145,14 +145,12 @@ export default class Row extends Component { switch (currency) { case 'BTC (SMS-Protected)': - //@ts-ignore await actions.btcmultisig.getBalance() break case 'BTC (Multisig)': await actions.btcmultisig.getBalanceUser(address) break case 'BTC (PIN-Protected)': - //@ts-ignore await actions.btcmultisig.getBalancePin() break default: diff --git a/src/front/shared/redux/actions/btcmultisig.ts b/src/front/shared/redux/actions/btcmultisig.ts index 4d43fc52be..7b1bcef6b6 100644 --- a/src/front/shared/redux/actions/btcmultisig.ts +++ b/src/front/shared/redux/actions/btcmultisig.ts @@ -885,7 +885,6 @@ const addSMSWallet = async (mnemonicOrKey) => { let btcSmsPublicKeys = [btcSMSServerKey, mnemonicKey] await actions.btcmultisig.login_SMS(privateKey, btcSmsPublicKeys) - //@ts-ignore await getBalance() } @@ -934,7 +933,7 @@ const getBalance = (ownAddress = null, ownDataKey = null) => { }) } -const getBalancePin = (checkAddress) => { +const getBalancePin = (checkAddress = null) => { const { user: { btcMultisigPinData: { address } } } = getState() return getBalance(address, 'btcMultisigPinData') From c10bdd4fdd93bb073e1bb05314945dd8f17a2a03 Mon Sep 17 00:00:00 2001 From: notEternal Date: Mon, 7 Dec 2020 18:20:25 +0800 Subject: [PATCH 2/4] Small improvements --- src/common/coins/BTC.ts | 1 - src/common/coins/interfaces.ts | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/common/coins/BTC.ts b/src/common/coins/BTC.ts index 08b14c1024..0db9bef8c4 100644 --- a/src/common/coins/BTC.ts +++ b/src/common/coins/BTC.ts @@ -1,4 +1,3 @@ -import { RefObject } from 'react'; import bip32 from 'bip32' import bip39 from'bip39' import bitcore from 'bitcore-lib' diff --git a/src/common/coins/interfaces.ts b/src/common/coins/interfaces.ts index 1564d33879..780c0b68b9 100644 --- a/src/common/coins/interfaces.ts +++ b/src/common/coins/interfaces.ts @@ -11,7 +11,7 @@ export interface ILibAdapter { publicKey: string address: string } - createTx?({ netName, account, amount, to }): Promise + createTx?({ netName, account, amount, to }): Promise // not sure what the } export interface IConnector { @@ -20,8 +20,8 @@ export interface IConnector { fetchBalance(networkType: string, address: string): Promise fetchUnspents?(netType: string, addr: string): Promise - fetchTx?(tsid: string): Promise - fetchRawTx?(tsid: string): Promise + fetchTx?(tsid: string): Promise // this method is empty now + fetchRawTx?(tsid: string): Promise // this method is empty now publishRawTx?(netType: string, rawTx: string): Promise publishTx?(networkType: string, rawTx: string): Promise } \ No newline at end of file From aaa53eb2a3878ec5b53ec930aaae755e6bea26f7 Mon Sep 17 00:00:00 2001 From: notEternal Date: Mon, 7 Dec 2020 19:10:27 +0800 Subject: [PATCH 3/4] Fix mistake in the names --- src/common/coins/interfaces.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/common/coins/interfaces.ts b/src/common/coins/interfaces.ts index 780c0b68b9..98c2ddf5d1 100644 --- a/src/common/coins/interfaces.ts +++ b/src/common/coins/interfaces.ts @@ -20,8 +20,8 @@ export interface IConnector { fetchBalance(networkType: string, address: string): Promise fetchUnspents?(netType: string, addr: string): Promise - fetchTx?(tsid: string): Promise // this method is empty now - fetchRawTx?(tsid: string): Promise // this method is empty now + fetchTx?(txid: string): Promise // this method is empty now + fetchRawTx?(txid: string): Promise // this method is empty now publishRawTx?(netType: string, rawTx: string): Promise publishTx?(networkType: string, rawTx: string): Promise } \ No newline at end of file From 109802c22acc5245d0cb1dd1060a72adb3856f18 Mon Sep 17 00:00:00 2001 From: notEternal Date: Mon, 7 Dec 2020 19:12:54 +0800 Subject: [PATCH 4/4] Delete the useless property --- src/front/shared/redux/actions/btcmultisig.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/front/shared/redux/actions/btcmultisig.ts b/src/front/shared/redux/actions/btcmultisig.ts index 7b1bcef6b6..a447f40793 100644 --- a/src/front/shared/redux/actions/btcmultisig.ts +++ b/src/front/shared/redux/actions/btcmultisig.ts @@ -933,7 +933,7 @@ const getBalance = (ownAddress = null, ownDataKey = null) => { }) } -const getBalancePin = (checkAddress = null) => { +const getBalancePin = () => { const { user: { btcMultisigPinData: { address } } } = getState() return getBalance(address, 'btcMultisigPinData')