Skip to content

Commit 2fd163f

Browse files
authored
Elliottdenis/defi 105 cant do a sol tx in the dashboard (#60)
* fix sol adapter * optimize code * move sha256
1 parent 391c9da commit 2fd163f

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

apps/solana/src/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ export const parseSolTx = (txRaw: string): ParseSolTxResult => {
329329
if (!isHex(input) && isBase64(input)) {
330330
try {
331331
hexInput = base64ToHex(input);
332-
} catch (error) {
332+
} catch (_error) {
333333
throw new Error('Failed to decode base64 input');
334334
}
335335
}

apps/solana/src/solana-adapter.tsx

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,32 @@
11
import { type ProtocolAdapter, SOL } from '@protocols/shared';
2+
import { Transaction } from '@solana/web3.js';
23
import { Summary } from '@/components/Summary';
34
import { convertToMessage, looksLikeMessage, type MessageLike, type ParseSolTxResult, parseSolTx } from '@/parser';
45
import type { DecodedInstruction } from '@/types';
5-
import { base64ToHex, isBase64, isHex } from '@/utils';
6+
import { base64ToHex, isBase64, isHex, sha256 } from '@/utils';
67

78
const computeSolanaHash = async (rawTx: string): Promise<string> => {
89
try {
910
const input = rawTx.trim();
1011

1112
// If input is a JSON message
1213
if (input.startsWith('{') || input.startsWith('[')) {
13-
try {
14-
const obj = JSON.parse(input);
15-
const msg = (obj.message ?? obj) as MessageLike;
16-
if (looksLikeMessage(msg)) {
17-
const message = convertToMessage(msg);
18-
const messageBytes = message.serialize();
19-
const hexString = messageBytes.toString('hex');
20-
21-
const transactionUint8Array = new Uint8Array(
22-
hexString.match(/.{1,2}/g)?.map((byte) => parseInt(byte, 16)) || [],
23-
);
24-
const hashBuffer = await crypto.subtle.digest('SHA-256', transactionUint8Array);
25-
const hashArray = Array.from(new Uint8Array(hashBuffer));
26-
return hashArray.map((b) => b.toString(16).padStart(2, '0')).join('');
27-
}
28-
} catch {}
14+
const obj = JSON.parse(input);
15+
const msg = (obj.message ?? obj) as MessageLike;
16+
if (looksLikeMessage(msg)) {
17+
const message = convertToMessage(msg);
18+
return sha256(new Uint8Array(message.serialize()));
19+
}
2920
}
3021

22+
// Hex or base64 path
3123
let hexInput = input;
3224
if (!isHex(input) && isBase64(input)) {
3325
hexInput = base64ToHex(input);
3426
}
3527

36-
const transactionUint8Array = new Uint8Array(hexInput.match(/.{1,2}/g)?.map((byte) => parseInt(byte, 16)) || []);
37-
const hashBuffer = await crypto.subtle.digest('SHA-256', transactionUint8Array);
38-
const hashArray = Array.from(new Uint8Array(hashBuffer));
39-
return hashArray.map((b) => b.toString(16).padStart(2, '0')).join('');
28+
const tx = Transaction.from(Buffer.from(hexInput, 'hex'));
29+
return sha256(new Uint8Array(tx.serializeMessage()));
4030
} catch {
4131
throw new Error('Failed to compute Solana hash');
4232
}

apps/solana/src/utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,10 @@ export const base64ToHex = (base64: string): string => {
1919
}
2020
return hex;
2121
};
22+
23+
export const sha256 = async (data: Uint8Array): Promise<string> => {
24+
const hashBuffer = await crypto.subtle.digest('SHA-256', data as BufferSource);
25+
return Array.from(new Uint8Array(hashBuffer))
26+
.map((b) => b.toString(16).padStart(2, '0'))
27+
.join('');
28+
};

0 commit comments

Comments
 (0)