@@ -1990,49 +1990,158 @@ export async function logTransaction(
19901990 txid : HexString
19911991) : Promise < string > {
19921992 let amount : SatoshiValue = 0
1993- let log = `txid: ${ txid } \n`
1994- const rt = await storage . findTransactions ( { partial : { txid } } )
1995- for ( const t of rt ) {
1996- log += `status: ${ t . status } \n`
1997- log += `description: ${ t . description } \n`
1998- const ro = await storage . findOutputs ( {
1999- partial : { transactionId : t . transactionId }
1993+ let log = `\n==== Transaction Log ====\ntxid: ${ txid } \n`
1994+
1995+ const transactions = await storage . findTransactions ( { partial : { txid } } )
1996+ for ( const tx of transactions ) {
1997+ log += `Status: ${ tx . status } \n`
1998+ log += `Description: ${ tx . description } \n`
1999+
2000+ const txLabelMaps = await storage . findTxLabelMaps ( {
2001+ partial : { transactionId : tx . transactionId }
20002002 } )
2001- for ( const o of ro ) {
2002- log += `${ await logOutput ( storage , o ) } `
2003- amount += o . spendable ? o . satoshis : 0
2003+ if ( txLabelMaps . length > 0 ) {
2004+ log += `Labels:\n`
2005+ for ( const txLabelMap of txLabelMaps ) {
2006+ const labels = await storage . findTxLabels ( {
2007+ partial : { txLabelId : txLabelMap . txLabelId }
2008+ } )
2009+ if ( labels . length > 0 ) {
2010+ log += ` - ${ labels [ 0 ] . label } \n`
2011+ }
2012+ }
2013+ } else {
2014+ log += `Labels: N/A\n`
2015+ }
2016+
2017+ const inputs = await storage . findOutputs ( {
2018+ partial : { transactionId : tx . transactionId }
2019+ } )
2020+ for ( const input of inputs ) {
2021+ log += await logInput ( storage , input . txid ! , input . vout )
2022+ }
2023+
2024+ const outputs = await storage . findOutputs ( {
2025+ partial : { transactionId : tx . transactionId }
2026+ } )
2027+ for ( const output of outputs ) {
2028+ log += await logOutput ( storage , output )
2029+ amount += output . spendable ? output . satoshis : 0
2030+ }
2031+
2032+ const beef = await storage . getBeefForTransaction ( txid , { } )
2033+ if ( beef ) {
2034+ log += `Beef Data:\n${ beef . toLogString ( ) } \n${ beef . toHex ( ) } \n`
2035+ } else {
2036+ log += `Beef Data: N/A\n`
20042037 }
20052038 }
2006- log += `------------------\namount: ${ amount } \n`
2039+
2040+ log += `-------------\nTotal Amount: ${ amount } satoshis\n=============\n`
20072041 return log
20082042}
20092043
20102044export async function logOutput (
20112045 storage : StorageKnex ,
20122046 output : table . Output
20132047) : Promise < string > {
2014- let log = `satoshis: ${ output . satoshis } \n`
2015- log += `spendable: ${ output . spendable } \n`
2016- log += `change: ${ output . change } \n`
2017- log += `providedBy: ${ output . providedBy } \n`
2018- log += `spentBy: ${ output . providedBy } \n`
2048+ let log = `\n-- Output --\n`
2049+ log += `Outpoint: ${ output . txid } :${ output . vout } \n`
2050+ log += `Satoshis: ${ output . satoshis } \n`
2051+ log += `Spendable: ${ output . spendable } \n`
2052+ log += `Change: ${ output . change } \n`
2053+ log += `Provided By: ${ output . providedBy } \n`
2054+ log += `Spent By: ${ output . spentBy ?? 'Unspent' } \n`
2055+
20192056 if ( output . basketId ) {
2020- const rb = await storage . findOutputBaskets ( {
2057+ const baskets = await storage . findOutputBaskets ( {
20212058 partial : { basketId : output . basketId }
20222059 } )
2023- log += `basket:${ await logBasket ( storage , rb [ 0 ] ) } \n`
2060+ if ( baskets . length === 1 ) {
2061+ log += `Basket: ${ logBasket ( baskets [ 0 ] ) } \n`
2062+ } else {
2063+ log += '*** PROBLEM WITH BASKET ***'
2064+ }
20242065 }
2066+
2067+ const outputTags = await storage . findOutputTagMaps ( {
2068+ partial : { outputId : output . outputId }
2069+ } )
2070+ if ( outputTags . length > 0 ) {
2071+ log += `Tags:\n`
2072+ for ( const outputTag of outputTags ) {
2073+ const tags = await storage . findOutputTags ( {
2074+ partial : { outputTagId : outputTag . outputTagId }
2075+ } )
2076+ if ( tags . length > 0 ) {
2077+ log += ` - ${ tags [ 0 ] . tag } \n`
2078+ }
2079+ }
2080+ } else {
2081+ log += `Tags: N/A\n`
2082+ }
2083+
20252084 return log
20262085}
20272086
2028- export function logBasket (
2087+ export async function logInput (
20292088 storage : StorageKnex ,
2030- basket : table . OutputBasket
2031- ) : string {
2032- let log = `${ basket . name } \n`
2089+ prevOutputTxid : HexString ,
2090+ prevOutputVout : number ,
2091+ indentLevel = 1
2092+ ) : Promise < string > {
2093+ const indent = ' ' . repeat ( indentLevel )
2094+ let log = `\n${ indent } -- Input (Previous Output) --\n`
2095+
2096+ const prevOutputs = await storage . findOutputs ( {
2097+ partial : { txid : prevOutputTxid , vout : prevOutputVout }
2098+ } )
2099+
2100+ if ( prevOutputs . length === 0 ) {
2101+ log += `${ indent } Previous Output Not Found (Outpoint: ${ prevOutputTxid } :${ prevOutputVout } )\n`
2102+ return log
2103+ }
2104+
2105+ for ( const prevOutput of prevOutputs ) {
2106+ const outpoint = `${ prevOutputTxid } :${ prevOutput . vout } `
2107+
2108+ log += `${ indent } Source Outpoint: ${ outpoint } \n`
2109+ log += `${ indent } Satoshis: ${ prevOutput . satoshis } \n`
2110+ log += `${ indent } Spendable: ${ prevOutput . spendable } \n`
2111+ log += `${ indent } Change: ${ prevOutput . change } \n`
2112+ log += `${ indent } Provided By: ${ prevOutput . providedBy } \n`
2113+ log += `${ indent } Spent By: ${ prevOutput . spentBy ?? 'Unspent' } \n`
2114+ log += `${ indent } Locking Script: ${ prevOutput . lockingScript } \n`
2115+
2116+ // If this output was spent, recursively log its inputs
2117+ if ( prevOutput . spentBy ) {
2118+ const spendingTx = await storage . findTransactions ( {
2119+ partial : { transactionId : prevOutput . spentBy }
2120+ } )
2121+
2122+ if ( spendingTx . length > 0 ) {
2123+ const spentByTxid = spendingTx [ 0 ] . txid
2124+
2125+ log += `${ indent } ↳ Spent By TXID: ${ spentByTxid } \n`
2126+ log += await logInput (
2127+ storage ,
2128+ spentByTxid ! ,
2129+ prevOutput . vout ,
2130+ indentLevel + 2
2131+ )
2132+ } else {
2133+ log += `${ indent } ↳ Spent By TXID Unknown (transactionId: ${ prevOutput . spentBy } )\n`
2134+ }
2135+ }
2136+ }
2137+
20332138 return log
20342139}
20352140
2141+ export function logBasket ( basket : table . OutputBasket ) : string {
2142+ return `\n-- Basket --\nName: ${ basket . name } \n`
2143+ }
2144+
20362145export function hexStringToNumberArray ( hexString : string ) : number [ ] {
20372146 const sanitizedHex = hexString . replace ( / [ ^ a - f A - F 0 - 9 ] / g, '' )
20382147 const result : number [ ] = [ ]
@@ -2041,16 +2150,3 @@ export function hexStringToNumberArray(hexString: string): number[] {
20412150 }
20422151 return result
20432152}
2044-
2045- async function getOutputByTxIdAndVout (
2046- storage : StorageProvider ,
2047- txid : string ,
2048- vout : number
2049- ) : Promise < table . Output | null > {
2050- const results = await storage . findOutputs ( {
2051- partial : { txid }
2052- } )
2053-
2054- // Return the first matching result or null if no match is found
2055- return results . length > 0 ? results [ 0 ] : null
2056- }
0 commit comments