-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoinmarket-log-parser.mjs
336 lines (308 loc) · 12.2 KB
/
joinmarket-log-parser.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import { readdir, writeFile } from 'fs/promises'
import { createReadStream } from 'fs'
import { createInterface } from 'readline'
import path from 'path'
// read directory from argv
const directoryPath = process.argv[2]
// events
const getInfo = line => {
if (line.startsWith('Added utxos=')) return { type: 'utxos_added', content: '' }
else if (line.startsWith('Removed utxos=')) return { type: 'utxos_removed', content: '' }
else if (line.startsWith('chosen orders =')) return { type: 'chosen_orders', content: '' }
else if (line.startsWith('cj amount =')) return { type: 'cj_amount', content: line }
else if (line.startsWith('total cj fee =')) return { type: 'cj_fee', content: line }
else if (line.startsWith('total coinjoin fee =')) return { type: 'cj_fee', content: line }
else if (line.startsWith('obtained tx')) return { type: 'tx_obtained', content: '' }
else if (line.startsWith('txid = ')) return { type: 'tx_send', content: line }
else if (line.startsWith('schedule item was: ')) return { type: 'schedule_item', content: line }
else if (line.startsWith('filling offer')) return { type: 'filling_offer', content: line }
else if (line.startsWith('sending output to address=')) return { type: 'sending_output', content: line }
else if (line.startsWith('tx in a block')) return { type: 'tx_confirmed', content: line }
else if (line.startsWith('potentially earned')) return { type: 'cj_earned', content: line }
else if (line.startsWith('mycjaddr, mychange')) return { type: 'cj_info', content: line }
}
async function processFile(filePath) {
const fileStream = createReadStream(filePath)
const rl = createInterface({ input: fileStream, crlfDelay: Infinity })
let entry = null
const result = []
for await (const line of rl) {
const m = line.match(/([\d-:\s]*),\d+\s(?:\[.*?\]\s*)+(.*)/)
const info = m && getInfo(m[2])
if (info) {
const l = result.push({ date: new Date(m[1]), ...info })
entry = result[l-1]
entry.file = path.basename(filePath)
} else if (m && info == null) {
entry = null
} else if (entry != null) {
entry.content += `\n${line}`
}
}
return result
}
;(async function() {
// get and process files
const files = await readdir(directoryPath)
const promises = files.map(file => processFile(path.join(directoryPath, file)))
const allResults = await Promise.all(promises)
// flatten, sort and cleanup
const results = allResults.flat().sort((a, b) => a.date - b.date).map(entry => {
let content = entry.content.trim()
switch (entry.type) {
case 'chosen_orders':
entry.orders = JSON.parse(`[${content.replace(/'/g, '"').replace(/[\s\S]\{/g, ',{')}]`)
delete entry.content
break
case 'cj_amount':
const amt = content.match(/cj amount = (\w+)/)
if (amt) {
entry.amount_sats = parseInt(amt[1])
delete entry.content
}
break
case 'cj_fee':
const fee = content.match(/ = (.*)/)
if (fee) {
if (fee[1].endsWith('%'))
entry.fee_percent = fee[1]
else
entry.fee_sats = parseInt(fee[1])
delete entry.content
}
break
case 'tx_obtained':
if (content.startsWith('[') || content.startsWith('{')) {
try {
const json = JSON.parse(content.replace(/'/g, '"'))
entry = Object.assign(entry, json)
// unify: convert pre-segwit into new format
if (entry.ins) {
entry.inputs = entry.ins.map(i => ({
outpoint: `${i.outpoint.hash}:${i.outpoint.index}`,
scriptSig: i.script,
nSequence: i.sequence,
witness: null
}))
delete entry.ins
}
if (entry.outs) {
entry.outputs = entry.outs.map(o => ({
value_sats: o.value,
scriptPubKey: o.script,
address: null
}))
delete entry.outs
}
delete entry.content
} catch(e) {
console.error(e, content)
}
}
break
case 'tx_send':
const info = content.match(/txid = (\w+)/)
if (info) {
entry.txid = info[1]
delete entry.content
}
break
case 'filling_offer':
const offer = content.match(/filling offer, mixdepth=(\d+), amount=(\d+)/)
if (offer) {
entry.mixdepth = parseInt(offer[1])
entry.amount_sats = parseInt(offer[2])
delete entry.content
}
break
case 'sending_output':
const output = content.match(/sending output to address=(\w+)/)
if (output) {
entry.to_address = output[1]
delete entry.content
}
break
case 'cj_info':
const cjinfo = content.match(/mycjaddr, mychange = (\w+), (\w+)/)
if (cjinfo) {
entry.cjaddr = cjinfo[1]
entry.change = cjinfo[2]
delete entry.content
}
break
case 'utxos_added':
case 'utxos_removed':
const utxos = content.split('\n').map(utxo => {
const info = utxo.match(/(\w+):(\d+) - path: (.*), address: (.*), value: (\d+)/)
return {
outpoint: `${info[1]}:${info[2]}`,
path: info[3],
address: info[4].trim(),
value: parseInt(info[5])
}
})
entry.utxos = utxos
delete entry.content
break
case 'cj_earned':
const earned = content.replace('potentially earned = ', '').replace(/.* BTC \(/, '')
if (earned) {
entry.sats = parseInt(earned)
delete entry.content
}
break
case 'schedule_item':
const schedule = content.match(/schedule item was: (\[.*\])/)
if (schedule) {
const [mixdepth, amount, counterparties, to_address] = JSON.parse(schedule[1].replace(/'/g, '"'))
entry.mixdepth = mixdepth
entry.amount_sats = amount
entry.counterparties = counterparties
entry.to_address = to_address
delete entry.content
}
break
case 'tx_confirmed':
const blockinfo = content.match(/tx in a block: (\w+) with (\d+) confirmations/)
if (blockinfo) {
entry.block = blockinfo[1]
entry.confirmations = parseInt(blockinfo[2])
delete entry.content
}
break
default:
entry.content = content
break
}
return entry
})
// aggregate by date and join related entries
let prev = null
const byDate = results.reduce((acc, entry) => {
let date = entry.date.toISOString()
switch (entry.type) {
// find related entry
case 'utxos_removed':
const { outpoint } = entry.utxos[0]
const removeDate = Object.keys(acc).find(key =>
acc[key].find(e => e.type === 'tx_obtained' && e.inputs.find(i => i.outpoint === outpoint)) &&
!acc[key].find(e => e.type === 'utxos_removed'))
if (removeDate) date = removeDate
break
case 'utxos_added':
const { address } = entry.utxos[0]
const addDate = Object.keys(acc).find(key =>
acc[key].find(e => e.type === 'cj_info' && (e.cjaddr === address || e.change === address)) &&
!acc[key].find(e => e.type === 'utxos_added'))
if (addDate) date = addDate
case 'tx_confirmed':
const blockHash = entry.block
const confDate = Object.keys(acc).find(key =>
acc[key].find(e => e.type === 'utxos_added' && (e.utxos.find(u => u.outpoint.startsWith(blockHash)))) &&
!acc[key].find(e => e.type === 'tx_confirmed'))
if (confDate) date = confDate
break
case 'sending_output':
if (prev.type == 'filling_offer') {
entry.date = prev.date
date = entry.date.toISOString()
}
break
case 'tx_obtained':
if (['sending_output', 'filling_offer', 'cj_fee', 'cj_amount', 'chosen_orders'].includes(prev.type)) {
entry.date = prev.date
date = entry.date.toISOString()
}
break
case 'cj_earned':
if (['utxos_removed', 'tx_obtained'].includes(prev.type)) {
entry.date = prev.date
date = entry.date.toISOString()
}
break
case 'cj_info':
if (['cj_earned', 'tx_obtained'].includes(prev.type)) {
entry.date = prev.date
date = entry.date.toISOString()
}
break
case 'schedule_item':
if (['utxos_removed', 'tx_obtained'].includes(prev.type)) {
entry.date = prev.date
date = entry.date.toISOString()
}
break
case 'tx_send':
if (['schedule_item'].includes(prev.type)) {
entry.date = prev.date
date = entry.date.toISOString()
}
break
}
if (acc[date] == null) acc[date] = []
acc[date].push(entry)
prev = entry
return acc
}, {})
// write result
await writeFile('joinmarket.json', JSON.stringify(byDate, null, 2))
// BIP 329 labels
// { "type": "tx", "ref": "52a8a8317c8836ad11e0a3402196db97bdac51566739b8a2d4dff526edb7fd5d", "label": "cj" }
// { "type": "addr", "ref": "bc1q34aq5drpuwy3wgl9lhup9892qp6svr8ldzyy7c", "label": "Address" }
// { "type": "input", "ref": "f91d0a8a78462bc59398f2c5d7a84fcff491c26ba54c4833478b202796c8aafd:0", "label": "Input" }
// { "type": "output", "ref": "f91d0a8a78462bc59398f2c5d7a84fcff491c26ba54c4833478b202796c8aafd:1", "label": "Output" , "spendable" : "false" }
const bip329 = Object.values(byDate).reduce((acc, entries) => {
const fillingOffer = entries.find(entry => entry.type === 'filling_offer')
const sendingOutput = entries.find(entry => entry.type === 'sending_output')
const chosenOrders = entries.find(entry => entry.type === 'chosen_orders')
const txSend = entries.find(entry => entry.type === 'tx_send')
const scheduleItem = entries.find(entry => entry.type === 'schedule_item')
const utxosAdded = entries.find(entry => entry.type === 'utxos_added')
const utxosRemoved = entries.find(entry => entry.type === 'utxos_removed')
const cjInfo = entries.find(entry => entry.type === 'cj_info')
const fundingLabel = 'Funding'
let cjLabel = ''
if (fillingOffer && utxosAdded) {
cjLabel = `Mixdepth ${fillingOffer.mixdepth} Maker`
acc.push({ type: 'tx', ref: utxosAdded.utxos[0].outpoint.split(':')[0], label: cjLabel })
} else if (utxosRemoved && txSend) {
const md = scheduleItem && scheduleItem.mixdepth
cjLabel = `Mixdepth${md ? ` ${md}` : ''} Taker`
acc.push({ type: 'tx', ref: txSend.txid, label: cjLabel })
}
if (cjInfo) {
acc.push({ type: 'addr', ref: cjInfo.cjaddr, label: (cjLabel + ' Coinjoined').trim() })
acc.push({ type: 'addr', ref: cjInfo.change, label: (cjLabel + ' Change').trim() })
}
if (utxosAdded) {
if (cjInfo) {
utxosAdded.utxos.forEach(utxo => {
acc.push({ type: 'output', ref: utxo.outpoint, label: utxo.address === cjInfo.cjaddr ? (cjLabel + ' Coinjoined').trim() : (cjLabel + ' Change').trim() })
})
} else {
utxosAdded.utxos.forEach(utxo => {
acc.push({ type: 'output', ref: utxo.outpoint, label: fundingLabel })
})
}
}
if (utxosRemoved) {
if (cjInfo) {
utxosRemoved.utxos.forEach(utxo => {
const prevOutput = acc.find(e => e.type === 'output' && e.ref === utxo.outpoint && e.label != fundingLabel)
const thisLabel = (cjLabel + ' Coinjoined').trim()
const label = prevOutput ? [prevOutput.label, thisLabel].join(', ') : thisLabel
acc.push({ type: 'input', ref: utxo.outpoint, label })
})
} else {
utxosRemoved.utxos.forEach(utxo => {
const prevOutput = acc.find(e => e.type === 'output' && e.ref === utxo.outpoint && e.label != fundingLabel)
const thisLabel = 'Withdrawal'
const label = prevOutput ? [prevOutput.label, thisLabel].join(', ') : thisLabel
acc.push({ type: 'input', ref: utxo.outpoint, label })
})
}
}
return acc
}, []).map(JSON.stringify).join('\n')
await writeFile('joinmarket-bip329.json', bip329)
})()