<commit_before>import { TxOutput } from 'bitcoinjs-lib';
import { Network } from '..';
import { toOutputScript } from '../address';
import { UtxoTransactionBuilder } from './UtxoTransactionBuilder';

/**
 * Public unspent data in BitGo-specific representation.
 */
export interface Unspent<TNumber extends number | bigint = number> {
  /**
   * Format: ${txid}:${vout}.
   * Use `parseOutputId(id)` to parse.
   */
  id: string;
  /**
   * The network-specific encoded address.
   * Use `toOutputScript(address, network)` to obtain scriptPubKey.
   */
  address: string;
  /**
   * The amount in satoshi.
   */
  value: TNumber;
}

export interface UnspentWithPrevTx<TNumber extends number | bigint = number> extends Unspent<TNumber> {
  prevTx: Buffer;
}

export function isUnspentWithPrevTx<TNumber extends number | bigint, TUnspent extends Unspent<TNumber>>(
  u: Unspent<TNumber>
): u is TUnspent & { prevTx: Buffer } {
  return Buffer.isBuffer((u as UnspentWithPrevTx<TNumber>).prevTx);
}

/**
 * @return TxOutput from Unspent
 */
export function toOutput<TNumber extends number | bigint>(u: Unspent<TNumber>, network: Network): TxOutput<TNumber> {
  return {
    script: toOutputScript(u.address, network),
    value: u.value,<commit_msg>feat(utxo-lib): add methods fromOutput, fromOutputWithPrevTx<commit_after>import { TxOutput } from 'bitcoinjs-lib';
import { Network } from '..';
import { fromOutputScript, toOutputScript } from '../address';
import { UtxoTransactionBuilder } from './UtxoTransactionBuilder';
import { UtxoTransaction } from './UtxoTransaction';

/**
 * Public unspent data in BitGo-specific representation.
 */
export interface Unspent<TNumber extends number | bigint = number> {
  /**
   * Format: ${txid}:${vout}.
   * Use `parseOutputId(id)` to parse.
   */
  id: string;
  /**
   * The network-specific encoded address.
   * Use `toOutputScript(address, network)` to obtain scriptPubKey.
   */
  address: string;
  /**
   * The amount in satoshi.
   */
  value: TNumber;
}

export interface UnspentWithPrevTx<TNumber extends number | bigint = number> extends Unspent<TNumber> {
  prevTx: Buffer;
}

export function isUnspentWithPrevTx<TNumber extends number | bigint, TUnspent extends Unspent<TNumber>>(
  u: Unspent<TNumber>
): u is TUnspent & { prevTx: Buffer } {
  return Buffer.isBuffer((u as UnspentWithPrevTx<TNumber>).prevTx);
}

/**
 * @return TxOutput from Unspent
 */
export function toOutput<TNumber extends number | bigint>(u: Unspent<TNumber>, network: Network): TxOutput<TNumber> {
  return {
    script: toOutputScript(u.address, network),
    value: u.value,
  };
}

/**
 * @return Unspent from TxOutput
 */
export function fromOutput<TNumber extends number | bigint>(
  tx: UtxoTransaction<TNumber>,
  vout: number
): Unspent<TNumber> {
  const o = tx.outs[vout];
  if (!o) {
    throw new Error(`invalid vout`);
  }
  return {
    id: formatOutputId({ txid: tx.getId(), vout }),
    address: fromOutputScript(o.script, tx.network),
    value: o.value,
  };
}

export function fromOutputWithPrevTx<TNumber extends number | bigint>(
  tx: UtxoTransaction<TNumber>,
  vout: number
): UnspentWithPrevTx<TNumber> {
  return {
    ...fromOutput(tx, vout),
    prevTx: tx.toBuffer(),
{'fraction_non_alphanumeric': 0.13267203967761934, 'fraction_numerical': 0.0, 'mean_word_length': 4.188102893890675, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7134131', 'n_tokens_mistral': 1065, 'n_tokens_neox': 1065, 'n_words': 269}