Skip to content

Commit

Permalink
feat(@fireblocks/recovery-relay): ✨ updated wallets to use configurab…
Browse files Browse the repository at this point in the history
…le RPCs
  • Loading branch information
a0ngo committed Oct 23, 2024
1 parent fc90716 commit af041e2
Show file tree
Hide file tree
Showing 49 changed files with 299 additions and 240 deletions.
5 changes: 5 additions & 0 deletions .changeset/tidy-shirts-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@fireblocks/recovery-relay': patch
---

Updated wallets to use configurable RPCs
10 changes: 7 additions & 3 deletions apps/recovery-relay/lib/wallets/ADA/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,23 @@ export class Cardano extends BaseCardano implements LateInitConnectedWallet {

private bkfClient: BlockFrostAPI | undefined = undefined;

private endpoint: string | undefined = undefined;
public rpcURL: string | undefined;

constructor(input: Input) {
super(input);
this.isLateInit = () => true;
}

public setRPCUrl(url: string): void {
this.rpcURL = url;
}

public getLateInitLabel(): string {
return 'Blockfrost Project ID or GraphQL Url';
}

public updateDataEndpoint(endpoint: string): void {
this.endpoint = endpoint;
this.rpcURL = endpoint;
if (endpoint.startsWith('http')) {
this.gqlClient = new ApolloClient({
uri: endpoint,
Expand Down Expand Up @@ -120,7 +124,7 @@ export class Cardano extends BaseCardano implements LateInitConnectedWallet {
const preparedData = {
utxos,
balance: balance / 1_000_000,
endpoint: this.endpoint,
endpoint: this.rpcURL,
insufficientBalance: balance / 1_000_000 < 0.001,
};

Expand Down
22 changes: 14 additions & 8 deletions apps/recovery-relay/lib/wallets/ALGO/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { Algorand as BaseALGO } from '@fireblocks/wallet-derivation';
import { Algodv2 } from 'algosdk';
import { AccountData } from '../types';
import { LateInitConnectedWallet } from '../LateInitConnectedWallet';

import { Algodv2 } from 'algosdk';

export class Algorand extends BaseALGO implements LateInitConnectedWallet {
private endpoint: string | undefined;
public rpcURL: string | undefined;

private algoClient: Algodv2 | undefined;

public updateDataEndpoint(endpoint: string): void {
this.endpoint = endpoint;
this.rpcURL = endpoint;
}

public setRPCUrl(url: string): void {
this.rpcURL = url;
}

public getLateInitLabel(): string {
Expand All @@ -24,6 +28,7 @@ export class Algorand extends BaseALGO implements LateInitConnectedWallet {
}
return accountInfo.amount;
}

public async prepare(): Promise<AccountData> {
this.createClient();

Expand All @@ -42,24 +47,25 @@ export class Algorand extends BaseALGO implements LateInitConnectedWallet {
return {
balance,
extraParams,
endpoint: this.endpoint,
endpoint: this.rpcURL,
};
}

public broadcastTx(tx: string): Promise<string> {
throw new Error('Method not implemented.');
}

private createClient() {
if (!this.endpoint) {
if (!this.rpcURL) {
throw new Error('Wallet not initialized yet');
}

if (this.algoClient) {
return;
}

const [host, port, apiToken] = this.endpoint.split(':');
const [host, port, apiToken] = this.rpcURL.split(':');

this.algoClient = new Algodv2(apiToken, host, parseInt(port));
this.algoClient = new Algodv2(apiToken, host, parseInt(port, 10));
}
}
14 changes: 8 additions & 6 deletions apps/recovery-relay/lib/wallets/ATOM/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Cosmos as BaseCosmos, Input } from '@fireblocks/wallet-derivation';
import { Tendermint34Client } from '@cosmjs/tendermint-rpc';
import { StargateClient } from '@cosmjs/stargate';
import { SignDoc, TxRaw } from 'cosmjs-types/cosmos/tx/v1beta1/tx';
import { TxRaw } from 'cosmjs-types/cosmos/tx/v1beta1/tx';
import { AccountData } from '../types';
import { ConnectedWallet } from '../ConnectedWallet';

Expand All @@ -10,13 +10,15 @@ export class Cosmos extends BaseCosmos implements ConnectedWallet {

private stargateClient: StargateClient | undefined;

private rpcURL: string;
public rpcURL: string | undefined;

constructor(input: Input) {
super(input);

this.tendermintClient = undefined;
this.rpcURL = input.isTestnet ? 'https://rpc.sentry-01.theta-testnet.polypore.xyz' : 'https://cosmos-rpc.publicnode.com';
}

public setRPCUrl(url: string): void {
this.rpcURL = url;
}

public async getBalance(): Promise<number> {
Expand Down Expand Up @@ -56,8 +58,8 @@ export class Cosmos extends BaseCosmos implements ConnectedWallet {

private async prepareClients(): Promise<void> {
if (!this.tendermintClient || !this.stargateClient) {
this.tendermintClient = await Tendermint34Client.connect(this.rpcURL);
this.stargateClient = await StargateClient.connect(this.rpcURL);
this.tendermintClient = await Tendermint34Client.connect(this.rpcURL!);
this.stargateClient = await StargateClient.connect(this.rpcURL!);
}
}
}
19 changes: 11 additions & 8 deletions apps/recovery-relay/lib/wallets/BTCBased/BCH.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ import { LateInitConnectedWallet } from '../LateInitConnectedWallet';
import { BCHUTXO } from './types';

export class BitcoinCash extends BCHBase implements LateInitConnectedWallet {
private endpoint: string;

private network: Networks.Network;

public rpcURL: string | undefined;

constructor(input: Input) {
super(input);
this.endpoint = 'https://rest.bch.actorforth.org/v2';
this.network = Networks.mainnet;
}

public setRPCUrl(url: string): void {
this.rpcURL = url;
}

// eslint-disable-next-line class-methods-use-this
public getLateInitLabel() {
return '';
Expand All @@ -25,18 +28,18 @@ export class BitcoinCash extends BCHBase implements LateInitConnectedWallet {
}

public updateDataEndpoint(endpoint: string): void {
this.endpoint = endpoint;
this.rpcURL = endpoint;
}

public async getBalance(): Promise<number> {
if (this.isLateInit() && this.endpoint === '') {
if (this.isLateInit() && (this.rpcURL === '' || this.rpcURL === undefined)) {
throw new Error('Endpoint not initialized yet');
}
return (await this.prepare()).balance;
}

public async prepare(): Promise<AccountData> {
if (this.isLateInit() && this.endpoint === '') {
if (this.isLateInit() && (this.rpcURL === '' || this.rpcURL === undefined)) {
throw new Error('Endpoint not initialized yet');
}

Expand Down Expand Up @@ -73,15 +76,15 @@ export class BitcoinCash extends BCHBase implements LateInitConnectedWallet {
}

private async _get<T>(path: string) {
const res = await fetch(`${this.endpoint}${path}`);
const res = await fetch(`${this.rpcURL}${path}`);

const data: Promise<T> = res.json();

return data;
}

private async _post(path: string) {
const res = await fetch(`${this.endpoint}${path}`, {
const res = await fetch(`${this.rpcURL}${path}`, {
method: 'POST',
});

Expand Down
12 changes: 5 additions & 7 deletions apps/recovery-relay/lib/wallets/BTCBased/BSV.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ type BSVAddressSummary = {
};

export class BitcoinSV extends BSVBase implements ConnectedWallet {
private baseUrl: string = '';
public rpcURL: string | undefined;

private utils;

constructor(input: Input) {
super(input);
this.baseUrl = input.isTestnet ? 'https://api.whatsonchain.com/v1/bsv/test' : 'https://api.whatsonchain.com/v1/bsv/main';
private utils: unknown | undefined;

public setRPCUrl(url: string): void {
this.rpcURL = url;
// When calling any custom function provided as part of the relay wallet utils
// we bind it to `this` from the BTCRelayWallet class, thus every internal reference to this
// within the custom functions must be considered as a call to the BTCRelayWalletUtils and not
Expand Down Expand Up @@ -107,7 +105,7 @@ export class BitcoinSV extends BSVBase implements ConnectedWallet {

return txBroadcastRes;
}
})(this.baseUrl);
})(this.rpcURL!);
}

public async getBalance(): Promise<number> {
Expand Down
7 changes: 5 additions & 2 deletions apps/recovery-relay/lib/wallets/BTCBased/BTC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ import { AddressSummary, FullUTXO, StandardUTXO, UTXOSummary } from './types';
export class Bitcoin extends BaseBTC implements ConnectedWallet {
private static readonly satsPerBtc = 100000000;

private readonly baseUrl: string;
public rpcURL: string | undefined;

private utils: BTCRelayWalletUtils | undefined;

constructor(input: Input) {
super(input);
this.baseUrl = input.isTestnet ? 'https://api.blockchair.com/bitcoin/testnet' : 'https://api.blockchair.com/bitcoin';
// Legacy requires a custom site
if (this.isLegacy) {
// When calling any custom function provided as part of the relay wallet utils
Expand Down Expand Up @@ -104,6 +103,10 @@ export class Bitcoin extends BaseBTC implements ConnectedWallet {
}
}

public setRPCUrl(url: string): void {
this.rpcURL = url;
}

public async getBalance(): Promise<number> {
return BTCRelayWallet.prototype.getBalance.bind(this)();
}
Expand Down
10 changes: 5 additions & 5 deletions apps/recovery-relay/lib/wallets/BTCBased/BTCRelayWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class BTCRelayWallet {

public async getBalance(): Promise<number> {
// @ts-ignore
const utils = (this.utils as BTCRelayWalletUtils) || new StandardBTCRelayWalletUtils(this.baseUrl);
const utils = (this.utils as BTCRelayWalletUtils) || new StandardBTCRelayWalletUtils(this.rpcURL);
// @ts-ignore
const balance = await utils.getAddressBalance(this.address);
const btcBalance = BTCRelayWallet._satsToBtc(balance);
Expand All @@ -19,10 +19,10 @@ export class BTCRelayWallet {

public async prepare(): Promise<AccountData> {
// @ts-ignore
const { isLegacy, relayLogger: logger, baseUrl, address } = this;
const { isLegacy, relayLogger: logger, rpcURL, address } = this;

// @ts-ignore
const utils = (this.utils as BTCRelayWalletUtils) || new StandardBTCRelayWalletUtils(baseUrl);
const utils = (this.utils as BTCRelayWalletUtils) || new StandardBTCRelayWalletUtils(rpcURL);
const balance = await BTCRelayWallet.prototype.getBalance.bind(this)();

if (balance === 0) {
Expand Down Expand Up @@ -57,10 +57,10 @@ export class BTCRelayWallet {
// const tx = Psbt.fromHex(txHex, { network: this.network });

// @ts-ignore
const { relayLogger: logger, baseUrl } = this;
const { relayLogger: logger, rpcURL } = this;

// @ts-ignore
const utils = (this.utils as BTCRelayWalletUtils) || new StandardBTCRelayWalletUtils(baseUrl);
const utils = (this.utils as BTCRelayWalletUtils) || new StandardBTCRelayWalletUtils(rpcURL);

// @ts-ignore
return utils.broadcastTx(txHex, logger);
Expand Down
6 changes: 4 additions & 2 deletions apps/recovery-relay/lib/wallets/BTCBased/DASH.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import { ConnectedWallet } from '../ConnectedWallet';
import { BTCRelayWallet } from './BTCRelayWallet';

export class DASH extends BaseDASH implements ConnectedWallet {
private readonly baseUrl: string;
public rpcURL: string | undefined;

constructor(input: Input) {
super(input);

if (input.isTestnet) {
throw new Error('No Dash testnet support.');
}
}

this.baseUrl = 'https://api.blockchair.com/dash';
public setRPCUrl(url: string): void {
this.rpcURL = url;
}

public async getBalance(): Promise<number> {
Expand Down
7 changes: 3 additions & 4 deletions apps/recovery-relay/lib/wallets/BTCBased/DOGE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import { BTCRelayWallet } from './BTCRelayWallet';
export class DOGE extends BaseDOGE implements ConnectedWallet {
private static readonly satsPerBtc = 100000000;

private readonly baseUrl: string;
public rpcURL: string | undefined;

constructor(input: Input) {
super(input);
this.baseUrl = 'https://api.blockchair.com/dogecoin';
public setRPCUrl(url: string): void {
this.rpcURL = url;
}

public async prepare(): Promise<AccountData> {
Expand Down
9 changes: 4 additions & 5 deletions apps/recovery-relay/lib/wallets/BTCBased/LTC.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { LiteCoin as BaseLTC, Input } from '@fireblocks/wallet-derivation';
import { LiteCoin as BaseLTC } from '@fireblocks/wallet-derivation';
import { AccountData } from '../types';
import { ConnectedWallet } from '../ConnectedWallet';
import { BTCRelayWallet } from './BTCRelayWallet';

export class LTC extends BaseLTC implements ConnectedWallet {
private static readonly satsPerBtc = 100000000;

private readonly baseUrl: string;
public rpcURL: string | undefined;

constructor(input: Input) {
super(input);
this.baseUrl = 'https://api.blockchair.com/litecoin';
public setRPCUrl(url: string): void {
this.rpcURL = url;
}

public async getBalance(): Promise<number> {
Expand Down
11 changes: 5 additions & 6 deletions apps/recovery-relay/lib/wallets/BTCBased/ZEC.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ZCash as BaseZEC, Input } from '@fireblocks/wallet-derivation';
import { ZCash as BaseZEC } from '@fireblocks/wallet-derivation';
import { AccountData } from '../types';
import { ConnectedWallet } from '../ConnectedWallet';
import { BTCRelayWallet } from './BTCRelayWallet';
Expand All @@ -7,11 +7,10 @@ import { StandardBTCRelayWalletUtils } from './BTCRelayWalletUtils';
export class ZEC extends BaseZEC implements ConnectedWallet {
private static readonly satsPerBtc = 100000000;

private readonly baseUrl: string;
public rpcURL: string | undefined;

constructor(input: Input) {
super(input);
this.baseUrl = 'https://api.blockchair.com/zcash';
public setRPCUrl(url: string): void {
this.rpcURL = url;
}

public async prepare(): Promise<AccountData> {
Expand All @@ -31,7 +30,7 @@ export class ZEC extends BaseZEC implements ConnectedWallet {
}

private async _getCurrentBlockHeight() {
const utils = new StandardBTCRelayWalletUtils(this.baseUrl);
const utils = new StandardBTCRelayWalletUtils(this.rpcURL!);
const stats = await utils.requestJson<{
data: {
blocks: number;
Expand Down
Loading

0 comments on commit af041e2

Please sign in to comment.