1# frozen_string_literal: true
2
3# This is meant to be run with the output of detect_duplicate_addrs on stdin
4# The assumption is that some logging will dump that, and then someone will
5# run this after looking into why
6# Theoretically they could be piped together directly for automated fixing
7
8require 'redis'
9
10redis = Redis.new
11
12customer_id = ENV['DEFAULT_CUSTOMER_ID']
13unless customer_id
14 puts "The env-var DEFAULT_CUSTOMER_ID must be set to the ID of the customer who will receive the duplicated addrs, preferably a support customer or something linked to notifications when stray money is sent to these addresses"
15 exit 1
16end
17
18
19STDIN.each_line do |line|
20 match = line.match(/^(\w+) is used by the following \d+ keys: (.*)/)
21 unless match
22 puts "The following line can't be understood and is being ignored"
23 puts " #{line}"
24 next
25 end
26
27 addr = match[1]
28 keys = match[2].split(" ")
29
30 # This is the customer ID of the support chat
31 # All duplicates are moved to the support addr so we still hear when people
32 # send money there
33 redis.sadd("jmp_customer_btc_addresses-#{customer_id}", addr)
34
35 keys.each do |key|
36 redis.srem(key, addr)
37 end
38end