WizardConnect/packages/core/src/protocols/hdwalletv1-serialize.ts
Dagur Valberg Johannsson 889dd566d1
Add serialization helpers to @wizardconnect/core
Canonical encoding for BigInt (<bigint: Xn>) and Uint8Array (hex or
<Uint8Array: 0x...>) used in the relay protocol. Provides both
serialization (sourceOutputToRelay, transactionToHex) and deserialization
(parseExtendedJson, toUint8Array, toBigInt) so dapps and wallets
don't have to implement this independently.
2026-03-23 08:59:02 +01:00

90 lines
2.7 KiB
TypeScript

// 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 `<bigint: Xn>`.
*/
export function sourceOutputToRelay(so: SourceOutput): any {
const result: any = {
outpointTransactionHash: binToHex(so.outpointTransactionHash),
outpointIndex: so.outpointIndex,
unlockingBytecode: binToHex(so.unlockingBytecode),
sequenceNumber: so.sequenceNumber,
valueSatoshis: `<bigint: ${so.valueSatoshis}n>`,
lockingBytecode: binToHex(so.lockingBytecode),
};
if (so.token) {
result.token = {
category: binToHex(so.token.category),
amount: `<bigint: ${so.token.amount}n>`,
...(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 }));
}