-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauth.go
More file actions
577 lines (551 loc) · 18.7 KB
/
Copy pathauth.go
File metadata and controls
577 lines (551 loc) · 18.7 KB
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
// Copyright 2018-2025 Celer Network
package cnode
import (
"bytes"
"fmt"
"math/big"
"sync"
"time"
"github.com/celer-network/agent-pay/common"
"github.com/celer-network/agent-pay/common/structs"
"github.com/celer-network/agent-pay/config"
"github.com/celer-network/agent-pay/ctype"
"github.com/celer-network/agent-pay/entity"
"github.com/celer-network/agent-pay/fsm"
"github.com/celer-network/agent-pay/ledgerview"
"github.com/celer-network/agent-pay/rpc"
"github.com/celer-network/agent-pay/storage"
"github.com/celer-network/agent-pay/utils"
"github.com/celer-network/goutils/eth"
"github.com/celer-network/goutils/log"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
)
// WARNING: current auth implmentation will always sync the multi-login devices to the correct
// cosigned state and balance, but may lead to incorrect pay state in local db in corner cases.
// Example: device 1 and 2 share the same user account, latest cosigned seq from user is s.
// step 1: device 1 tried to send payment p1 with seq s+1, but p1 was not delivered to OSP.
// step 2: device 2 auth sync to seq s, then sent p2 with seq s+1, acked by OSP.
// step 3: device 1 auth sync to seq s+1. It would think p1 succeeded, but actually p1 was
// never delivered. It was p2 from device 2 which reused the same seq (s+1) succeed.
// clientAuthTsMap has address to last authreq ts from client
// new authReq must have a ts that's larger than last to avoid replay attack
// also due to ts is in seconds, we automatically restrict multiple connections within one second
// also user who adjust their clock forward then backward won't be able to connect
var (
clientAuthTsMap = make(map[ctype.Addr]uint64)
tsMapLock sync.RWMutex
)
func (c *CNode) getAuthReq(peerAddr ctype.Addr) (*rpc.AuthReq, error) {
ts, tsSig := utils.GetTsAndSig(func(b []byte) []byte {
sig, err := c.signer.SignEthMessage(b)
if err != nil {
log.Error(err)
return nil
}
return sig
})
if tsSig == nil {
return nil, fmt.Errorf("auth ts sig error")
}
authReq := &rpc.AuthReq{
MyAddr: c.EthAddress.Bytes(),
Timestamp: ts,
MySig: tsSig,
ExpectPeer: peerAddr.Bytes(),
ProtocolVersion: config.AuthProtocolVersion, // >= 1 means support sync in auth
}
channels, err := c.dal.GetChannelsForAuthReq(peerAddr)
if err == nil && len(channels) > 0 {
authReq.OpenedChannels = channels
}
return authReq, nil
}
// HandleAuthReq verifies AuthReq and return msg to send back or error
// if error is nil, guarantee *rpc.CelerMsg isn't nil
func (c *CNode) HandleAuthReq(req *rpc.AuthReq) (*rpc.CelerMsg, error) {
srcAddr := ctype.Bytes2Addr(req.GetMyAddr())
if !isTimestampValid(srcAddr, req.GetTimestamp()) {
return nil, fmt.Errorf("invalid timestamp, src addr %s", ctype.Addr2Hex(srcAddr))
}
tsByte := utils.Uint64ToBytes(req.Timestamp)
if !eth.IsSignatureValid(srcAddr, tsByte, req.GetMySig()) {
return nil, fmt.Errorf("invalid signature, src addr %s", ctype.Addr2Hex(srcAddr))
}
// only update ts after verify sig
putTs(srcAddr, req.GetTimestamp())
ret := new(rpc.CelerMsg)
// if protocol version >=1 , means support sync in auth
if req.GetProtocolVersion() >= 1 {
syncChans := c.getSyncChannelsForAuthAck(srcAddr, req.GetOpenedChannels())
ret.Message = &rpc.CelerMsg_AuthAck{
AuthAck: &rpc.AuthAck{
SyncChannels: syncChans,
},
}
}
return ret, nil
}
// HandleAuthAck tries to update db based on sync info in AuthAck msg
// NOTE this func doesn't return error as it's best effort to sync state
// some code are similar to open_channel logic but couldn't re-use due to openchan assumes init state
func (c *CNode) HandleAuthAck(peer ctype.Addr, ack *rpc.AuthAck) {
if ack == nil { // do nothing
return
}
var err error
for _, ch := range ack.SyncChannels {
chjson, _ := utils.PbToJSONHexBytes(ch)
log.Infoln("HandleAuthAck, syncing channel:", chjson)
chState := int(ch.ChannelState)
cid := ctype.Bytes2Cid(ch.Cid)
if chState == structs.ChanState_NULL || chState == structs.ChanState_CLOSED {
log.Errorln("invalid channel state in msg:", ch)
continue
}
mySimplex := ch.AuthreqSimplex
peerSimplex := ch.AuthackSimplex
// if simplex isn't nil, means osp sees need to sync
if peerSimplex != nil {
err = c.checkSignedSimplex(peerSimplex, cid, peer, c.EthAddress)
if err != nil {
log.Error(err)
continue
}
}
if mySimplex != nil {
err = c.checkSignedSimplex(mySimplex, cid, c.EthAddress, peer)
if err != nil {
log.Error(err)
continue
}
}
shouldUsePeerLedger := false
peerLedgerAddr := ctype.Bytes2Addr(ch.GetLedgerAddr())
peerLedgerContract := c.nodeConfig.GetLedgerContractOn(peerLedgerAddr)
if peerLedgerContract == nil {
log.Errorf("Sync channel %x error, doesn't have ledger %x in profile.", ch.GetCid(), ch.GetLedgerAddr())
continue
}
peepMyLedgerAddr, peepFound, peepErr := c.dal.GetChanLedger(cid)
if peepErr == nil && peepFound && peepMyLedgerAddr != peerLedgerAddr {
// Only check operability if my ledger is different to peer ledger and
// we'll only update local ledger addr if peer ledger addr is operable.
chanStatus, stateErr := ledgerview.GetOnChainChannelStatusOnLedger(cid, c.nodeConfig, peerLedgerAddr)
if stateErr != nil {
log.Errorln(stateErr)
continue
}
if chanStatus != ledgerview.OnChainStatus_OPERABLE {
continue
}
shouldUsePeerLedger = true
}
myChState, found, err := c.dal.GetChanState(cid)
if err != nil {
log.Error(err)
continue
}
var txF storage.TxFunc
if !found { // no local view, just save everything
if mySimplex == nil || peerSimplex == nil {
// not found case require both simplex to be non-nill
log.Errorf("unexpected nil simplex my: %v, peer: %v", mySimplex, peerSimplex)
continue
}
seqN, tkInfo, err2 := getSeqAndToken(mySimplex)
if err2 != nil {
log.Error(err2)
continue
}
bal, err2 := c.getOnChainBalanceForAuth(cid, chState, ch.OpenChannelResponse.GetChannelInitializer(), peerLedgerAddr)
if err2 != nil {
log.Error(err2)
continue
}
txF = func(tx *storage.DALTx, args ...interface{}) error {
err3 := tx.InsertChan(cid, peer, tkInfo, peerLedgerAddr, chState, ch.OpenChannelResponse, bal, seqN, seqN, seqN, 0 /*lastNackedSeqNum*/, mySimplex, peerSimplex)
if err3 != nil {
return err3
}
err3 = tx.UpdatePeerCid(peer, cid, true)
if err3 != nil {
return err3
}
for _, pay := range ch.AuthackPays { // from peer to me. ok to not check if in simplex because no harm
err3 = insertPaymentFromAuth(tx, pay.Pay, pay.Note, cid, int(pay.State), true)
if err3 != nil {
log.Error(err3) // only log no early return for best effort adding pays
}
}
for _, pay := range ch.AuthreqPays { // my pays
err3 = insertPaymentFromAuth(tx, pay.Pay, pay.Note, cid, int(pay.State), false)
if err3 != nil {
log.Error(err3)
}
}
return nil
} // txF Ends
} else { // has local view, need to compare local view vs. AuthAck msg
// it's possible OSP has a newer state eg. from trust_opened to opened
// but the new state transition must be valid
if !fsm.IsChanStateChangeValid(myChState, chState) {
log.Errorf("invalid chState, my: %s, recved: %s", fsm.ChanStateName(myChState), fsm.ChanStateName(chState))
continue
}
txF = func(tx *storage.DALTx, args ...interface{}) error {
var err3 error
if myChState != chState {
err3 = tx.UpdateChanState(cid, chState)
if err3 != nil {
return err3
}
}
myLedgerAddr, foundLedger, err3 := tx.GetChanLedger(cid)
if err3 != nil {
return err3
}
if !foundLedger {
// This shouldn't happen as we UpdateChanState above making sure existence of cid.
log.Errorf("Can't find cid %x", cid)
return fmt.Errorf("Can't find cid %x", cid)
}
if peepMyLedgerAddr == myLedgerAddr && shouldUsePeerLedger {
// make sure local ledger doesn't change from time of peep to now.
err3 = tx.UpdateChanLedger(cid, peerLedgerAddr)
if err3 != nil {
return err3
}
}
if mySimplex != nil {
mh := c.celerMsgDispatcher.NewMsgHandler()
err3 = mh.HandleMysimplexFromAuthAck(tx, cid, mySimplex)
if err3 != nil {
log.Error(err3) // don't return b/c pays and peerSimplex could still be updated
}
// update last added seqNum
var mySimplexCh entity.SimplexPaymentChannel
err3 = proto.Unmarshal(mySimplex.GetSimplexState(), &mySimplexCh)
if err3 != nil {
log.Error(err3)
}
_, lastUsedSeq, lastAckedSeq, lastNackedSeq, found, err4 := tx.GetChanSeqNums(cid)
if err4 != nil || !found {
log.Error(err4, found)
} else if mySimplexCh.GetSeqNum() > lastUsedSeq {
err3 = tx.UpdateChanSeqNums(cid, mySimplexCh.GetSeqNum(), mySimplexCh.GetSeqNum(), lastAckedSeq, lastNackedSeq)
if err3 != nil {
log.Error(err3)
}
}
for _, pay := range ch.AuthreqPays { // my pays
err3 = insertPaymentFromAuth(tx, pay.Pay, pay.Note, cid, int(pay.State), false)
if err3 != nil {
log.Error(err3) // only log err to try adding more pays
}
}
} // mySimplex done
if peerSimplex != nil {
// need to sync peer
peerSeqN, _, err3 := getSeqAndToken(peerSimplex)
if err3 != nil {
return err3
}
peerChannel, _, found, err3 := tx.GetPeerSimplex(cid)
if err3 != nil {
return err3
}
if found {
// peerSeqN must be larger
if peerSeqN <= peerChannel.GetSeqNum() {
return common.ErrInvalidSeqNum
}
}
err3 = tx.UpdateChanForRecvRequest(cid, peerSimplex)
if err3 != nil {
return err3
}
for _, pay := range ch.AuthackPays { // peer pays
err3 = insertPaymentFromAuth(tx, pay.Pay, pay.Note, cid, int(pay.State), true)
if err3 != nil {
log.Error(err3) // only log err to try adding more pays
}
}
} // peerSimplex done
return nil
} // txF Ends
}
// exec txF
if err = c.dal.Transactional(txF); err != nil {
log.Error(err)
continue
}
}
}
// helper struct for seq num in AuthRequest
type seqNum struct {
MySeq, PeerSeq uint64
}
// get all opened channels with this peer and compare with input, add to return if my seq numbers are bigger or input doesn't have same cid
// this is to support client side missing newly opened channels
func (c *CNode) getSyncChannelsForAuthAck(peer ctype.Addr, input []*rpc.ChannelSummary) []*rpc.ChannelInAuth {
// save channel Summary list as a map for easy lookup later
inMap := make(map[ctype.CidType]*seqNum)
for _, c := range input {
inMap[ctype.Bytes2Cid(c.GetChannelId())] = &seqNum{
MySeq: c.GetMySeqNum(),
PeerSeq: c.GetPeerSeqNum(),
}
}
// channels is type []*storage.chanForAuthAck
channels, err := c.dal.GetChannelsForAuthAck(peer)
if err != nil {
log.Warnln(err)
}
var ret []*rpc.ChannelInAuth
// only return channels if my seq is higher or client doesn't have it
for _, ch := range channels {
var toAdd *rpc.ChannelInAuth // default nil, only malloc when need to add
cid := ctype.Hex2Cid(ch.Cid)
if req, ok := inMap[cid]; ok {
// compare seq and decide whether to add chan
if ch.MySeq > req.PeerSeq || ch.PeerSeq > req.MySeq { // note the swap of my vs. peer
// need to add, so malloc now
toAdd = &rpc.ChannelInAuth{
Cid: cid.Bytes(),
ChannelState: ch.State,
OpenChannelResponse: ch.OpenChanResp, // could be nil
LedgerAddr: ch.LedgerAddr.Bytes(),
}
log.Infof("sync cid %s peer %s mysimplex [%d, %d] peersimplex [%d, %d]",
ctype.Cid2Hex(cid), ctype.Addr2Hex(peer), ch.MySeq, req.PeerSeq, ch.PeerSeq, req.MySeq)
}
if ch.MySeq > req.PeerSeq {
toAdd.AuthackSimplex = ch.MySigned
}
if ch.PeerSeq > req.MySeq {
toAdd.AuthreqSimplex = ch.PeerSigned
}
} else {
// client didn't include this cid, should return
toAdd = &rpc.ChannelInAuth{
Cid: cid.Bytes(),
ChannelState: ch.State,
OpenChannelResponse: ch.OpenChanResp, // could be nil
AuthackSimplex: ch.MySigned,
AuthreqSimplex: ch.PeerSigned,
LedgerAddr: ch.LedgerAddr.Bytes(),
}
log.Infof("sync cid %s peer %s due to not in AuthReq", ctype.Cid2Hex(cid), ctype.Addr2Hex(peer))
}
if toAdd != nil {
// add pays to toAdd
c.addPays(toAdd)
ret = append(ret, toAdd)
}
}
return ret
}
// go over self/peer simplex in ch and add pays
func (c *CNode) addPays(ch *rpc.ChannelInAuth) {
if ch.AuthackSimplex != nil {
payIDs := getPayIDs(ch.AuthackSimplex)
mypays, err := c.dal.GetPaysForAuthAck(payIDs, true) // pay in my simplex, use outState
if err != nil {
log.Warn(err)
} else {
ch.AuthackPays = mypays
}
}
if ch.AuthreqSimplex != nil {
payIDs := getPayIDs(ch.AuthreqSimplex)
// CANNOT reuse mypays because the actual serialization happens later by grpc/proto runtime
peerpays, err := c.dal.GetPaysForAuthAck(payIDs, false) // peer's simplex, use instate
if err != nil {
log.Warn(err)
} else {
ch.AuthreqPays = peerpays
}
}
}
// check and handle special seqNum 0 case
// if stateChannel seq is 0, add my sig and remove peer's
func (c *CNode) checkSignedSimplex(ss *rpc.SignedSimplexState, expCid ctype.CidType, expFrom, expTo ctype.Addr) error {
var ch entity.SimplexPaymentChannel
err := proto.Unmarshal(ss.GetSimplexState(), &ch)
if err != nil {
return err
}
if ctype.Bytes2Cid(ch.ChannelId) != expCid {
return common.ErrInvalidChannelID
}
if ctype.Bytes2Addr(ch.PeerFrom) != expFrom {
return fmt.Errorf("mismatch peerfrom got %s, exp %s", ctype.Bytes2Hex(ch.PeerFrom), ctype.Addr2Hex(expFrom))
}
if ch.SeqNum != 0 {
// must be correctly co-signed by both
if !eth.IsSignatureValid(expFrom, ss.GetSimplexState(), ss.GetSigOfPeerFrom()) ||
!eth.IsSignatureValid(expTo, ss.GetSimplexState(), ss.GetSigOfPeerTo()) {
return common.ErrInvalidSig
}
return nil
}
// seq num 0 case, simplexChannel must match emptySimplex
amtBytes := ch.TransferToPeer.GetReceiver().GetAmt()
pendingBytes := ch.TotalPendingAmount
if !bytes.Equal(zeroAmtBytes, amtBytes) || !bytes.Equal(zeroAmtBytes, pendingBytes) {
return common.ErrInvalidAmount
}
if len(ch.PendingPayIds.GetPayIds()) != 0 || len(ch.PendingPayIds.GetNextListHash()) != 0 {
return common.ErrInvalidPendingPays
}
mysig, err := c.signer.SignEthMessage(ss.GetSimplexState())
if err != nil {
return err
}
if expFrom == c.EthAddress {
ss.SigOfPeerFrom = mysig
ss.SigOfPeerTo = nil
} else {
ss.SigOfPeerFrom = nil
ss.SigOfPeerTo = mysig
}
return nil
}
// getOnChainBalanceForAuth query onchain if channel is opened or populate per init distribution if tcb opened
func (c *CNode) getOnChainBalanceForAuth(cid ctype.CidType, chState int, chanInitBytes []byte, ledgerAddr ctype.Addr) (*structs.OnChainBalance, error) {
// onchain
if chState == structs.ChanState_OPENED || chState == structs.ChanState_SETTLING {
return ledgerview.GetOnChainChannelBalance(cid, ledgerAddr, c.nodeConfig)
}
// tcb, don't expect osp has ChanState_INSTANTIATING but add here for completeness
if chState == structs.ChanState_TRUST_OPENED || chState == structs.ChanState_INSTANTIATING {
var chanInit entity.PaymentChannelInitializer
err := proto.Unmarshal(chanInitBytes, &chanInit)
if err != nil {
return nil, err
}
ret := new(structs.OnChainBalance)
for _, aap := range chanInit.GetInitDistribution().GetDistribution() {
addr := ctype.Bytes2Addr(aap.Account)
amt := new(big.Int).SetBytes(aap.Amt)
if addr == c.EthAddress {
ret.MyDeposit = amt
} else {
ret.PeerDeposit = amt
}
}
ret.MyWithdrawal = ctype.ZeroBigInt
ret.PeerWithdrawal = ctype.ZeroBigInt
return ret, nil
}
return nil, common.ErrInvalidChannelState
}
func insertPaymentFromAuth(tx *storage.DALTx, payBytes []byte, note *anypb.Any, cid ctype.CidType, state int, isIngress bool) error {
pay := new(entity.ConditionalPay)
err := proto.Unmarshal(payBytes, pay)
if err != nil {
return err
}
payID := ctype.Pay2PayID(pay)
if isIngress { // use inCid and inState
inCid, myState, found, err2 := tx.GetPayIngress(payID)
if err2 != nil {
return err2
}
if !found { // not found, just insert
return tx.InsertPayment(payID, payBytes, pay, note, cid, state, ctype.ZeroCid, structs.PayState_NULL)
}
// if found, check data is valid and update if state is diff, otherwise no-op
if inCid != cid {
return common.ErrInvalidChannelID
}
if myState != state {
return tx.UpdatePayIngressState(payID, state)
}
return nil
}
// outCid/outState
outCid, myState, found, err := tx.GetPayEgress(payID)
if err != nil {
return err
}
if !found {
return tx.InsertPayment(payID, payBytes, pay, note, ctype.ZeroCid, structs.PayState_NULL, cid, state)
}
if outCid != cid {
return common.ErrInvalidChannelID
}
if myState != state {
return tx.UpdatePayEgressState(payID, state)
}
return nil
}
func getSeqAndToken(ss *rpc.SignedSimplexState) (uint64, *entity.TokenInfo, error) {
simplexChan := new(entity.SimplexPaymentChannel)
err := proto.Unmarshal(ss.SimplexState, simplexChan)
if err != nil {
return 0, nil, err
}
return simplexChan.SeqNum, simplexChan.GetTransferToPeer().GetToken(), nil
}
type msgOrErr struct {
msg *rpc.CelerMsg
err error
}
// waitRecvWithTimeout blocks until receive a msg from grpc stream or timeout
// currently this is only used for client waiting AuthAck
func waitRecvWithTimeout(s rpc.CelerStream, timeout time.Duration) (*rpc.CelerMsg, error) {
ch := make(chan *msgOrErr, 1)
t := time.NewTimer(timeout)
go func() {
m, e := s.Recv()
ch <- &msgOrErr{
msg: m,
err: e,
}
}()
select {
case <-t.C:
return nil, common.ErrRecvCelerMsgTimeout
case recv := <-ch:
t.Stop()
return recv.msg, recv.err
}
}
// util to return payID list from SignedSimplexState
func getPayIDs(simplex *rpc.SignedSimplexState) (payIDs []ctype.PayIDType) {
simplexChan := new(entity.SimplexPaymentChannel)
err := proto.Unmarshal(simplex.SimplexState, simplexChan)
if err != nil {
log.Warn(err)
return
}
for _, payid := range simplexChan.GetPendingPayIds().GetPayIds() {
payIDs = append(payIDs, ctype.Bytes2PayID(payid))
}
return
}
func isTimestampValid(addr ctype.Addr, ts uint64) bool {
// we used to require client side ts is within certain window like osp, but some client time is really off
// now := uint64(time.Now().Unix())
// return ts >= now-config.AllowedTimeWindow && ts <= now+config.AllowedTimeWindow
// now we just require ts is larger if we have this client in memory
lastTs, ok := getTs(addr)
if ok && ts <= lastTs {
// found last entry and new req ts isn't larger
return false
}
return true
}
func getTs(key ctype.Addr) (uint64, bool) {
tsMapLock.RLock()
t, ok := clientAuthTsMap[key]
tsMapLock.RUnlock()
return t, ok
}
func putTs(key ctype.Addr, ts uint64) {
tsMapLock.Lock()
clientAuthTsMap[key] = ts
tsMapLock.Unlock()
}