Skip to content

Commit

Permalink
fix: solana connection 403 fix
Browse files Browse the repository at this point in the history
  • Loading branch information
a0ngo committed Dec 13, 2023
1 parent f4c3099 commit e0891d5
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 8 deletions.
7 changes: 7 additions & 0 deletions .changeset/rotten-mayflies-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@fireblocks/wallet-derivation': minor
'@fireblocks/recovery-utility': minor
'@fireblocks/recovery-relay': minor
---

fix: Solana connection 403 fix
20 changes: 14 additions & 6 deletions apps/recovery-relay/lib/wallets/SOL/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
import { Buffer } from 'buffer';
import * as web3 from '@solana/web3.js';
import { ipcRenderer } from 'electron';
import { Solana as BaseSolana, Input } from '@fireblocks/wallet-derivation';
import { AccountData, TxPayload } from '../types';
import { AccountData } from '../types';
import { ConnectedWallet } from '../ConnectedWallet';

export class Solana extends BaseSolana implements ConnectedWallet {
private readonly connection: web3.Connection;
private readonly solConnection: web3.Connection;

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

const endpoint = input.isTestnet ? web3.clusterApiUrl('devnet') : 'https://try-rpc.mainnet.solana.blockdaemon.tech';
const endpoint = input.isTestnet ? web3.clusterApiUrl('devnet') : web3.clusterApiUrl('mainnet-beta');

this.connection = new web3.Connection(endpoint, 'confirmed');
this.solConnection = new web3.Connection(endpoint, {
commitment: 'confirmed',
//@ts-ignore
fetch: async (input: string | URL | Request, init) => {
const res = await ipcRenderer.invoke('sol_web_request', input, init);
return new Response(res);
},
});
}

public async getBalance() {
const lamports = await this.connection.getBalance(this.web3PubKey);
const lamports = await this.solConnection.getBalance(this.web3PubKey);
const balance = lamports / web3.LAMPORTS_PER_SOL;
return balance;
}

public async broadcastTx(tx: string): Promise<string> {
try {
const txHash = await this.connection.sendRawTransaction(Buffer.from(tx, 'hex'));
const txHash = await this.solConnection.sendRawTransaction(Buffer.from(tx, 'hex'));
this.relayLogger.debug(`Solana: Tx broadcasted: ${txHash}`);
return txHash;
} catch (e) {
Expand Down
1 change: 1 addition & 0 deletions apps/recovery-utility/main/ipc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ import './restoreSettings';
import './saveSettings';
import './getDeployment';
import './useDeployment';
import './solanaWebRequest';
48 changes: 48 additions & 0 deletions apps/recovery-utility/main/ipc/solanaWebRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import axios, { AxiosHeaders } from 'axios';
import { ipcMain } from 'electron';

ipcMain.handle('sol_web_request', async (event, input: string | Request | URL, init?: RequestInit) => {
try {
let res: any = undefined;
if (!init) {
if (typeof input === 'string') {
res = (await axios.get(input)).data;
} else if (input instanceof URL) {
res = (await axios.get(input.toString())).data;
} else {
res = (
await axios({
url: input.url,
data: input.body,
method: input.method,
})
).data;
}
} else {
const headers = new AxiosHeaders();
if (init.headers) {
Object.keys(init.headers).forEach(
(header: string) =>
headers.set(header, (init.headers! as { [key: string]: string })[header as string] as unknown as string), // ew
);
}

res = (
await axios({
url: input as string,
method: init.method,
data: init.body,
headers: headers,
})
).data;
}

if (res !== undefined) {
return JSON.stringify(res);
} else {
throw new Error('No result');
}
} catch (e) {
console.error('Failed to query solana information', e);
}
});
2 changes: 1 addition & 1 deletion apps/recovery-utility/renderer/lib/wallets/SOL/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class Solana extends BaseSolana implements SigningWallet {
constructor(input: Input) {
super(input);

const endpoint = input.isTestnet ? web3.clusterApiUrl('devnet') : 'https://try-rpc.mainnet.solana.blockdaemon.tech';
const endpoint = input.isTestnet ? web3.clusterApiUrl('devnet') : web3.clusterApiUrl('mainnet-beta');

this.connection = new web3.Connection(endpoint, 'confirmed');
}
Expand Down
2 changes: 1 addition & 1 deletion packages/wallet-derivation/wallets/chains/SOL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class Solana extends EdDSAWallet {
constructor(input: Input) {
super(input, 501);

this.web3PubKey = new PublicKey(Buffer.from(this.publicKey, 'hex'));
this.web3PubKey = new PublicKey(Buffer.from(this.publicKey.replace('0x', ''), 'hex'));
}

protected getAddress() {
Expand Down

0 comments on commit e0891d5

Please sign in to comment.