// Copyright (C) 2026 Whiterun LLC, // This software is licensed under the GNU Lesser General Public License (LGPL), version 3.0 or later. // A copy of the license can be found in the LICENSE file or at https://www.gnu.org/licenses/lgpl-3.0.html /** * Serialization helpers specific to the hdwalletv1 sign-transaction flow. * * These convert between native libauth transaction types and the relay-safe * JSON used in SignTransactionRequest / SignTransactionResponse messages. */ import { binToHex, encodeTransaction } from "@bitauth/libauth"; // --------------------------------------------------------------------------- // Interfaces (mirror libauth transaction shapes) // --------------------------------------------------------------------------- export interface SourceOutput { outpointTransactionHash: Uint8Array; outpointIndex: number; unlockingBytecode: Uint8Array; sequenceNumber: number; valueSatoshis: bigint; lockingBytecode: Uint8Array; token?: { category: Uint8Array; amount: bigint; nft?: { capability?: string; commitment?: Uint8Array }; }; } interface TxInput { outpointTransactionHash: Uint8Array; outpointIndex: number; unlockingBytecode: Uint8Array; sequenceNumber: number; } interface TxOutput { valueSatoshis: bigint; lockingBytecode: Uint8Array; } // --------------------------------------------------------------------------- // Serialization (native types → relay JSON) // --------------------------------------------------------------------------- /** * Convert a source output to relay-safe JSON format. * Uint8Array fields become hex strings, BigInt becomes ``. */ export function sourceOutputToRelay(so: SourceOutput): any { const result: any = { outpointTransactionHash: binToHex(so.outpointTransactionHash), outpointIndex: so.outpointIndex, unlockingBytecode: binToHex(so.unlockingBytecode), sequenceNumber: so.sequenceNumber, valueSatoshis: ``, lockingBytecode: binToHex(so.lockingBytecode), }; if (so.token) { result.token = { category: binToHex(so.token.category), amount: ``, ...(so.token.nft && { nft: { ...(so.token.nft.capability !== undefined && { capability: so.token.nft.capability, }), ...(so.token.nft.commitment !== undefined && { commitment: binToHex(so.token.nft.commitment), }), }, }), }; } return result; } /** * Encode a transaction to hex for relay transport. */ export function transactionToHex( inputs: TxInput[], outputs: TxOutput[], version = 2, locktime = 0, ): string { return binToHex(encodeTransaction({ inputs, outputs, version, locktime })); }