@@ -2008,49 +2008,158 @@ export async function logTransaction(
20082008 txid : HexString
20092009) : Promise < string > {
20102010 let amount : SatoshiValue = 0
2011- let log = `txid: ${ txid } \n`
2012- const rt = await storage . findTransactions ( { partial : { txid } } )
2013- for ( const t of rt ) {
2014- log += `status: ${ t . status } \n`
2015- log += `description: ${ t . description } \n`
2016- const ro = await storage . findOutputs ( {
2017- partial : { transactionId : t . transactionId }
2011+ let log = `\n==== Transaction Log ====\ntxid: ${ txid } \n`
2012+
2013+ const transactions = await storage . findTransactions ( { partial : { txid } } )
2014+ for ( const tx of transactions ) {
2015+ log += `Status: ${ tx . status } \n`
2016+ log += `Description: ${ tx . description } \n`
2017+
2018+ const txLabelMaps = await storage . findTxLabelMaps ( {
2019+ partial : { transactionId : tx . transactionId }
20182020 } )
2019- for ( const o of ro ) {
2020- log += `${ await logOutput ( storage , o ) } `
2021- amount += o . spendable ? o . satoshis : 0
2021+ if ( txLabelMaps . length > 0 ) {
2022+ log += `Labels:\n`
2023+ for ( const txLabelMap of txLabelMaps ) {
2024+ const labels = await storage . findTxLabels ( {
2025+ partial : { txLabelId : txLabelMap . txLabelId }
2026+ } )
2027+ if ( labels . length > 0 ) {
2028+ log += ` - ${ labels [ 0 ] . label } \n`
2029+ }
2030+ }
2031+ } else {
2032+ log += `Labels: N/A\n`
2033+ }
2034+
2035+ const inputs = await storage . findOutputs ( {
2036+ partial : { transactionId : tx . transactionId }
2037+ } )
2038+ for ( const input of inputs ) {
2039+ log += await logInput ( storage , input . txid ! , input . vout )
2040+ }
2041+
2042+ const outputs = await storage . findOutputs ( {
2043+ partial : { transactionId : tx . transactionId }
2044+ } )
2045+ for ( const output of outputs ) {
2046+ log += await logOutput ( storage , output )
2047+ amount += output . spendable ? output . satoshis : 0
2048+ }
2049+
2050+ const beef = await storage . getBeefForTransaction ( txid , { } )
2051+ if ( beef ) {
2052+ log += `Beef Data:\n${ beef . toLogString ( ) } \n${ beef . toHex ( ) } \n`
2053+ } else {
2054+ log += `Beef Data: N/A\n`
20222055 }
20232056 }
2024- log += `------------------\namount: ${ amount } \n`
2057+
2058+ log += `-------------\nTotal Amount: ${ amount } satoshis\n=============\n`
20252059 return log
20262060}
20272061
20282062export async function logOutput (
20292063 storage : StorageKnex ,
20302064 output : table . Output
20312065) : Promise < string > {
2032- let log = `satoshis: ${ output . satoshis } \n`
2033- log += `spendable: ${ output . spendable } \n`
2034- log += `change: ${ output . change } \n`
2035- log += `providedBy: ${ output . providedBy } \n`
2036- log += `spentBy: ${ output . providedBy } \n`
2066+ let log = `\n-- Output --\n`
2067+ log += `Outpoint: ${ output . txid } :${ output . vout } \n`
2068+ log += `Satoshis: ${ output . satoshis } \n`
2069+ log += `Spendable: ${ output . spendable } \n`
2070+ log += `Change: ${ output . change } \n`
2071+ log += `Provided By: ${ output . providedBy } \n`
2072+ log += `Spent By: ${ output . spentBy ?? 'Unspent' } \n`
2073+
20372074 if ( output . basketId ) {
2038- const rb = await storage . findOutputBaskets ( {
2075+ const baskets = await storage . findOutputBaskets ( {
20392076 partial : { basketId : output . basketId }
20402077 } )
2041- log += `basket:${ await logBasket ( storage , rb [ 0 ] ) } \n`
2078+ if ( baskets . length === 1 ) {
2079+ log += `Basket: ${ logBasket ( baskets [ 0 ] ) } \n`
2080+ } else {
2081+ log += '*** PROBLEM WITH BASKET ***'
2082+ }
20422083 }
2084+
2085+ const outputTags = await storage . findOutputTagMaps ( {
2086+ partial : { outputId : output . outputId }
2087+ } )
2088+ if ( outputTags . length > 0 ) {
2089+ log += `Tags:\n`
2090+ for ( const outputTag of outputTags ) {
2091+ const tags = await storage . findOutputTags ( {
2092+ partial : { outputTagId : outputTag . outputTagId }
2093+ } )
2094+ if ( tags . length > 0 ) {
2095+ log += ` - ${ tags [ 0 ] . tag } \n`
2096+ }
2097+ }
2098+ } else {
2099+ log += `Tags: N/A\n`
2100+ }
2101+
20432102 return log
20442103}
20452104
2046- export function logBasket (
2105+ export async function logInput (
20472106 storage : StorageKnex ,
2048- basket : table . OutputBasket
2049- ) : string {
2050- let log = `${ basket . name } \n`
2107+ prevOutputTxid : HexString ,
2108+ prevOutputVout : number ,
2109+ indentLevel = 1
2110+ ) : Promise < string > {
2111+ const indent = ' ' . repeat ( indentLevel )
2112+ let log = `\n${ indent } -- Input (Previous Output) --\n`
2113+
2114+ const prevOutputs = await storage . findOutputs ( {
2115+ partial : { txid : prevOutputTxid , vout : prevOutputVout }
2116+ } )
2117+
2118+ if ( prevOutputs . length === 0 ) {
2119+ log += `${ indent } Previous Output Not Found (Outpoint: ${ prevOutputTxid } :${ prevOutputVout } )\n`
2120+ return log
2121+ }
2122+
2123+ for ( const prevOutput of prevOutputs ) {
2124+ const outpoint = `${ prevOutputTxid } :${ prevOutput . vout } `
2125+
2126+ log += `${ indent } Source Outpoint: ${ outpoint } \n`
2127+ log += `${ indent } Satoshis: ${ prevOutput . satoshis } \n`
2128+ log += `${ indent } Spendable: ${ prevOutput . spendable } \n`
2129+ log += `${ indent } Change: ${ prevOutput . change } \n`
2130+ log += `${ indent } Provided By: ${ prevOutput . providedBy } \n`
2131+ log += `${ indent } Spent By: ${ prevOutput . spentBy ?? 'Unspent' } \n`
2132+ log += `${ indent } Locking Script: ${ prevOutput . lockingScript } \n`
2133+
2134+ // If this output was spent, recursively log its inputs
2135+ if ( prevOutput . spentBy ) {
2136+ const spendingTx = await storage . findTransactions ( {
2137+ partial : { transactionId : prevOutput . spentBy }
2138+ } )
2139+
2140+ if ( spendingTx . length > 0 ) {
2141+ const spentByTxid = spendingTx [ 0 ] . txid
2142+
2143+ log += `${ indent } ↳ Spent By TXID: ${ spentByTxid } \n`
2144+ log += await logInput (
2145+ storage ,
2146+ spentByTxid ! ,
2147+ prevOutput . vout ,
2148+ indentLevel + 2
2149+ )
2150+ } else {
2151+ log += `${ indent } ↳ Spent By TXID Unknown (transactionId: ${ prevOutput . spentBy } )\n`
2152+ }
2153+ }
2154+ }
2155+
20512156 return log
20522157}
20532158
2159+ export function logBasket ( basket : table . OutputBasket ) : string {
2160+ return `\n-- Basket --\nName: ${ basket . name } \n`
2161+ }
2162+
20542163export function hexStringToNumberArray ( hexString : string ) : number [ ] {
20552164 const sanitizedHex = hexString . replace ( / [ ^ a - f A - F 0 - 9 ] / g, '' )
20562165 const result : number [ ] = [ ]
@@ -2059,16 +2168,3 @@ export function hexStringToNumberArray(hexString: string): number[] {
20592168 }
20602169 return result
20612170}
2062-
2063- async function getOutputByTxIdAndVout (
2064- storage : StorageProvider ,
2065- txid : string ,
2066- vout : number
2067- ) : Promise < table . Output | null > {
2068- const results = await storage . findOutputs ( {
2069- partial : { txid }
2070- } )
2071-
2072- // Return the first matching result or null if no match is found
2073- return results . length > 0 ? results [ 0 ] : null
2074- }
0 commit comments