Skip to content
This repository has been archived by the owner on Apr 25, 2024. It is now read-only.

Configurable client #365

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@
"@mui/material": "^5.5.0",
"@mui/styles": "^5.5.0",
"@trezor/transport": "^1.1.15",
"@types/redux": "^3.6.0",
"@vitejs/plugin-react": "^3.1.0",
"axios": "^0.21.2",
"base58check": "^2.0.0",
Expand Down
13 changes: 6 additions & 7 deletions src/actions/braidActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {
updateDepositSliceAction,
updateChangeSliceAction,
} from "./walletActions";
import { fetchAddressUTXOs, getAddressStatus } from "../blockchain";
import { setErrorNotification } from "./errorNotificationActions";
import { getBlockchainClientFromStore } from "./clientActions";

export const UPDATE_BRAID_SLICE = "UPDATE_BRAID_SLICE";

Expand All @@ -15,10 +15,9 @@ export const UPDATE_BRAID_SLICE = "UPDATE_BRAID_SLICE";
* @param {array<object>} slices - array of slices from one or more braids
*/
export const fetchSliceData = async (slices) => {
return async (dispatch, getState) => {
const { network } = getState().settings;
const { client } = getState();

return async (dispatch) => {
const blockchainClient = await dispatch(getBlockchainClientFromStore());
if (!blockchainClient) return;
try {
// Create a list of the async calls for updating the slice data.
// This lets us run these requests in parallel with a Promise.all
Expand All @@ -27,8 +26,8 @@ export const fetchSliceData = async (slices) => {
// creating a tuple of async calls that will need to be resolved
// for each slice we're querying for
return Promise.all([
fetchAddressUTXOs(address, network, client),
getAddressStatus(address, network, client),
blockchainClient.fetchAddressUTXOs(address),
blockchainClient.getAddressStatus(address),
]);
});

Expand Down
8 changes: 0 additions & 8 deletions src/actions/clientActions.js

This file was deleted.

50 changes: 50 additions & 0 deletions src/actions/clientActions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Dispatch } from "react";
import { BlockchainClient, ClientType } from "../clients/client";

export const SET_CLIENT_TYPE = "SET_CLIENT_TYPE";
export const SET_CLIENT_URL = "SET_CLIENT_URL";
export const SET_CLIENT_USERNAME = "SET_CLIENT_USERNAME";
export const SET_CLIENT_PASSWORD = "SET_CLIENT_PASSWORD";

export const SET_CLIENT_URL_ERROR = "SET_CLIENT_URL_ERROR";
export const SET_CLIENT_USERNAME_ERROR = "SET_CLIENT_USERNAME_ERROR";
export const SET_CLIENT_PASSWORD_ERROR = "SET_CLIENT_PASSWORD_ERROR";

export const SET_BLOCKCHAIN_CLIENT = "SET_BLOCKCHAIN_CLIENT";

// TODO: use this to add more flexibility to client support
// For example, this defaults to blockstream for public client
// but can also support mempool.space as an option
export const getBlockchainClientFromStore = async () => {
return async (
dispatch: Dispatch<any>,
getState: () => { settings: any; client: any }
) => {
const { network } = getState().settings;
const { client } = getState();
if (!client) return;
let clientType: ClientType;

switch (client.type) {
case "public":
clientType = ClientType.BLOCKSTREAM;
break;
case "private":
clientType = ClientType.PRIVATE;
break;
default:
// this allows us to support other clients in the future
// like mempool.space
clientType = client.type;
}

const blockchainClient = new BlockchainClient({
client,
type: clientType,
network,
throttled: true,
});
dispatch({ type: SET_BLOCKCHAIN_CLIENT, value: blockchainClient });
return blockchainClient;
};
};
8 changes: 3 additions & 5 deletions src/actions/walletActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
} from "unchained-bitcoin";

import BigNumber from "bignumber.js";
import { fetchAddressUTXOs } from "../blockchain";
import { isChange } from "../utils/slices";
import { naiveCoinSelection } from "../utils";
import {
Expand All @@ -16,6 +15,7 @@ import {
} from "./transactionActions";
import { setErrorNotification } from "./errorNotificationActions";
import { getSpendableSlices } from "../selectors/wallet";
import { getBlockchainClientFromStore } from "./clientActions";

export const UPDATE_DEPOSIT_SLICE = "UPDATE_DEPOSIT_SLICE";
export const UPDATE_CHANGE_SLICE = "UPDATE_CHANGE_SLICE";
Expand Down Expand Up @@ -173,8 +173,6 @@ export function updateTxSlices(
// eslint-disable-next-line consistent-return
return async (dispatch, getState) => {
const {
settings: { network },
client,
spend: {
transaction: { changeAddress, inputs, txid },
},
Expand All @@ -183,11 +181,11 @@ export function updateTxSlices(
change: { nodes: changeSlices },
},
} = getState();

const client = await dispatch(getBlockchainClientFromStore());
// utility function for getting utxo set of an address
// and formatting the result in a way we can use
const fetchSliceStatus = async (address, bip32Path) => {
const utxos = await fetchAddressUTXOs(address, network, client);
const utxos = await client.fetchAddressUtxos(address);
return {
addressUsed: true,
change: isChange(bip32Path),
Expand Down
2 changes: 1 addition & 1 deletion src/bitcoind.js → src/clients/bitcoind.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios from "axios";
import BigNumber from "bignumber.js";
import { bitcoinsToSatoshis } from "unchained-bitcoin";

async function callBitcoind(url, auth, method, params = []) {
export async function callBitcoind(url, auth, method, params = []) {
// FIXME
// eslint-disable-next-line no-async-promise-executor
return new Promise(async (resolve, reject) => {
Expand Down
File renamed without changes.
File renamed without changes.
Loading
Loading