/
channels.go
133 lines (126 loc) · 3.8 KB
/
channels.go
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
package servicer
import (
"encoding/hex"
"fmt"
"github.com/buger/jsonparser"
"github.com/hacash/channelpay/chanpay"
"github.com/hacash/channelpay/payroutes"
"github.com/hacash/core/stores"
"github.com/hacash/core/sys"
"io/ioutil"
"os"
"path"
"strings"
)
// Initialize password
func (s *Servicer) setupPasswordSettings() {
var passwordstr string = s.config.SignatureMachinePrivateKeySetupList
if path.Ext(s.config.SignatureMachinePrivateKeySetupList) == ".txt" {
// read file
fpth := sys.AbsDir(s.config.SignatureMachinePrivateKeySetupList)
bts, e := ioutil.ReadFile(fpth)
if e == nil {
passwordstr = string(bts)
//fmt.Println(passwordstr, "**************************")
}
}
s.settlenoderChgLock.Lock()
defer s.settlenoderChgLock.Unlock()
// Take out line breaks and spaces
passwordstr = strings.Replace(passwordstr, " ", "", -1)
//passwordstr = strings.Replace(passwordstr, "\n", "", -1)
// Parse password
//fmt.Println(passwordstr)
for _, v := range strings.Split(passwordstr, "\n") {
if len(v) < 6 {
continue
}
s.signmachine.TemporaryStoragePrivateKeyForSign(v)
}
}
// Initialize settlement channel
func (s *Servicer) setupRelaySettlementChannelDataSettings() {
var jsonfilecon []byte = []byte(s.config.RelaySettlementChannelsJsonFile)
if path.Ext(s.config.RelaySettlementChannelsJsonFile) == ".json" {
// read file
fpth := sys.AbsDir(s.config.RelaySettlementChannelsJsonFile)
bts, e := ioutil.ReadFile(fpth)
if e == nil {
jsonfilecon = bts
}
}
s.settlenoderChgLock.Lock()
defer s.settlenoderChgLock.Unlock()
// Parsing JSON
var chnumcount = 0
fmt.Printf("[InitializeChannelSide]")
jsonparser.ObjectEach(jsonfilecon, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
k := string(key)
if payroutes.IsValidServicerIdentificationName(k) {
if s.settlenoder[k] == nil {
s.settlenoder[k] = make([]*chanpay.RelayPaySettleNoder, 0)
}
fmt.Printf(" " + k + ":")
// Cycle multiple channels
jsonparser.ArrayEach(value, func(item []byte, dataType jsonparser.ValueType, offset int, err error) {
if len(item) == 32+2 {
cidstr := string(item[2:])
cid, _ := hex.DecodeString(cidstr)
if len(cid) != stores.ChannelIdLength {
return // error
}
weLeft := true
if item[0] == 'r' {
weLeft = false
}
// New channel
sise := chanpay.NewChannelSideById(cid)
e := s.InitializeChannelSide(sise, nil, weLeft)
if e != nil {
// Error initializing settlement channel
fmt.Printf("InitializeChannelSide %s error:\n", cidstr)
fmt.Println(e.Error())
os.Exit(0)
return
}
fmt.Printf(" %s", cidstr)
node := chanpay.NewRelayPayNodeConnect(k, cid, weLeft, sise)
// Add a channel
s.settlenoder[k] = append(s.settlenoder[k], node)
chnumcount++
}
})
}
fmt.Printf(" all %d ok.\n", chnumcount)
return nil
})
}
// Modify channel data settings
func (s *Servicer) modifyChannelDataSettings() {
// Delete customer service channel
sccs := strings.Split(strings.Replace(s.config.ServiceCustomerChannelsCancel, " ", "", -1), ",")
sccssn := 0
for _, v := range sccs {
cid, e := hex.DecodeString(v)
if e == nil && len(cid) == stores.ChannelIdLength {
s.chanset.CancelCustomerPayChannel(cid) // Cancel service
sccssn++
}
}
if sccssn > 0 {
fmt.Printf("[Config] ServiceCustomerChannels Cancel %d channels.\n", sccssn)
}
// Add customer service channel
scas := strings.Split(strings.Replace(s.config.ServiceCustomerChannelsAdd, " ", "", -1), ",")
scassn := 0
for _, v := range scas {
cid, e := hex.DecodeString(v)
if e == nil && len(cid) == stores.ChannelIdLength {
s.chanset.SetupCustomerPayChannel(cid) // Add service
scassn++
}
}
if scassn > 0 {
fmt.Printf("[Config] ServiceCustomerChannels Add %d channels.\n", scassn)
}
}