diff --git a/electron/src/lite-node/createGetAccountBalancePayload.ts b/electron/src/lite-node/createGetAccountBalancePayload.ts new file mode 100644 index 0000000..921a1ae --- /dev/null +++ b/electron/src/lite-node/createGetAccountBalancePayload.ts @@ -0,0 +1,27 @@ +import bs58 from 'bs58'; +import { Buffer } from 'buffer'; + +const ADDRESS_LENGTH = 25; + +/** + * Creates the payload for GET_ACCOUNT_BALANCE. + * This function assumes you'll frame it separately using `sendMessage(type, payload, id?)`. + */ +export function createGetAccountBalancePayload( + address: string, + assetId: number +): Buffer { + const addressBytes = bs58.decode(address); + + if (addressBytes.length !== ADDRESS_LENGTH) { + throw new Error( + `Invalid address length. Expected ${ADDRESS_LENGTH}, got ${addressBytes.length}` + ); + } + + const assetIdBigInt = BigInt(assetId); + const assetIdBuffer = Buffer.alloc(8); + assetIdBuffer.writeBigUInt64BE(assetIdBigInt); + + return Buffer.concat([Buffer.from(addressBytes), assetIdBuffer]); // ✅ Just the payload +} diff --git a/electron/src/lite-node/index.ts b/electron/src/lite-node/index.ts index 4cc4817..83db7e2 100644 --- a/electron/src/lite-node/index.ts +++ b/electron/src/lite-node/index.ts @@ -5,15 +5,20 @@ import { sha512 } from '@noble/hashes/sha512'; import util from 'util'; import fs from 'fs'; import path from 'path'; +import bs58 from 'bs58'; + +import { createGetAccountBalancePayload } from './createGetAccountBalancePayload'; const readFile = util.promisify(fs.readFile); -export const SEED_PEERS = ['127.0.0.1']; +export const SEED_PEERS = ['78.72.193.212']; export enum MessageType { HELLO = 0, CHALLENGE = 2, RESPONSE = 3, PING = 11, + ACCOUNT_BALANCE = 170, + GET_ACCOUNT_BALANCE = 171, } const memory = new WebAssembly.Memory({ initial: 256, maximum: 256 }); @@ -393,6 +398,36 @@ export class LiteNodeClient { private async handleResponse(payload: Buffer) { this.startPinging(); + try { + const message = createGetAccountBalancePayload( + 'QP9Jj4S3jpCgvPnaABMx8VWzND3qpji6rP', + 0 + ); + this.sendMessage(MessageType.GET_ACCOUNT_BALANCE, message); + } catch (error) { + console.error(error); + } + } + + private async handleGetAccountBalance(payload: Buffer) { + console.log('payload100', payload); + if (payload.length < 41) { + console.error('❌ Invalid payload length for AccountBalanceMessage'); + return; + } + + const addressBytes = payload.subarray(0, 25); + const address = bs58.encode(addressBytes); + + const assetId = payload.readBigUInt64BE(25); // offset = 25 + const balance = payload.readBigUInt64BE(33); // offset = 33 + + console.log('📬 Received Account Balance:'); + console.log('🏷️ Address:', address); + console.log('🪙 Asset ID:', assetId.toString()); + console.log('💰 Balance:', balance.toString()); + + // Optionally store or use the data here } private lastHandledPingIds = new Set(); @@ -470,6 +505,9 @@ export class LiteNodeClient { case MessageType.PING: this.handlePing(id); break; + case MessageType.ACCOUNT_BALANCE: + this.handleGetAccountBalance(payload); + break; default: console.warn(`⚠️ Unhandled message type: ${messageType}`); }