/
globalservice.go
624 lines (565 loc) · 21.4 KB
/
globalservice.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
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
package service
import (
"encoding/json"
"errors"
"github.com/asdine/storm/q"
"github.com/shopspring/decimal"
"log"
"obd/bean"
"obd/dao"
"obd/rpc"
"obd/tool"
"time"
"github.com/asdine/storm"
"github.com/tidwall/gjson"
)
var db *storm.DB
var P2PLocalPeerId string
var rpcClient *rpc.Client
//for store the privateKey
var tempAddrPrivateKeyMap = make(map[string]string)
var OnlineUserMap = make(map[string]bool)
func FindUserIsOnline(peerId string) error {
if tool.CheckIsString(&peerId) {
value, exists := OnlineUserMap[peerId]
if exists && value == true {
return nil
}
}
return errors.New(peerId + " not exist or online")
}
type commitmentOutputBean struct {
AmountToRsmc float64
AmountToCounterparty float64
AmountToHtlc float64
RsmcTempPubKey string
HtlcTempPubKey string
OppositeSideChannelPubKey string
OppositeSideChannelAddress string
}
func init() {
var err error
db, err = dao.DBService.GetGlobalDB()
if err != nil {
log.Println(err)
}
rpcClient = rpc.NewClient()
}
func GetHtlcFee() float64 {
return 1
}
func checkBtcFundFinish(address string) error {
result, err := rpcClient.ListUnspent(address)
if err != nil {
return err
}
array := gjson.Parse(result).Array()
log.Println("listunspent", array)
if len(array) < 3 {
return errors.New("btc fund have been not finished")
}
pMoney := rpc.GetOmniDustBtc()
out, _ := decimal.NewFromFloat(rpc.GetMinerFee()).Add(decimal.NewFromFloat(pMoney)).Mul(decimal.NewFromFloat(4.0)).Float64()
count := 0
for _, item := range array {
amount := item.Get("amount").Float()
if amount >= out {
count++
}
}
if count < 3 {
return errors.New("btc amount error, must greater " + tool.FloatToString(out, 8))
}
return nil
}
func getAddressFromPubKey(pubKey string) (address string, err error) {
if tool.CheckIsString(&pubKey) == false {
return "", errors.New("empty pubKey")
}
address, err = tool.GetAddressFromPubKey(pubKey)
if err != nil {
return "", err
}
_, err = rpcClient.ValidateAddress(address)
if err != nil {
return "", err
}
return address, nil
}
func createCommitmentTx(owner string, channelInfo *dao.ChannelInfo, fundingTransaction *dao.FundingTransaction, outputBean commitmentOutputBean, user *bean.User) (*dao.CommitmentTransaction, error) {
commitmentTxInfo := &dao.CommitmentTransaction{}
commitmentTxInfo.PeerIdA = channelInfo.PeerIdA
commitmentTxInfo.PeerIdB = channelInfo.PeerIdB
commitmentTxInfo.ChannelId = channelInfo.ChannelId
commitmentTxInfo.PropertyId = fundingTransaction.PropertyId
commitmentTxInfo.Owner = owner
//input
commitmentTxInfo.InputTxid = fundingTransaction.FundingTxid
commitmentTxInfo.InputVout = fundingTransaction.FundingOutputIndex
commitmentTxInfo.InputAmount = fundingTransaction.AmountA + fundingTransaction.AmountB
//output to rsmc
commitmentTxInfo.RSMCTempAddressPubKey = outputBean.RsmcTempPubKey
multiAddr, err := rpcClient.CreateMultiSig(2, []string{commitmentTxInfo.RSMCTempAddressPubKey, outputBean.OppositeSideChannelPubKey})
if err != nil {
return nil, err
}
commitmentTxInfo.RSMCMultiAddress = gjson.Get(multiAddr, "address").String()
commitmentTxInfo.RSMCRedeemScript = gjson.Get(multiAddr, "redeemScript").String()
json, err := rpcClient.GetAddressInfo(commitmentTxInfo.RSMCMultiAddress)
if err != nil {
return nil, err
}
commitmentTxInfo.RSMCMultiAddressScriptPubKey = gjson.Get(json, "scriptPubKey").String()
if tool.CheckIsString(&outputBean.HtlcTempPubKey) {
commitmentTxInfo.HTLCTempAddressPubKey = outputBean.HtlcTempPubKey
multiAddr, err := rpcClient.CreateMultiSig(2, []string{commitmentTxInfo.HTLCTempAddressPubKey, outputBean.OppositeSideChannelPubKey})
if err != nil {
return nil, err
}
commitmentTxInfo.HTLCMultiAddress = gjson.Get(multiAddr, "address").String()
commitmentTxInfo.HTLCRedeemScript = gjson.Get(multiAddr, "redeemScript").String()
json, err := rpcClient.GetAddressInfo(commitmentTxInfo.HTLCMultiAddress)
if err != nil {
return nil, err
}
commitmentTxInfo.HTLCMultiAddressScriptPubKey = gjson.Get(json, "scriptPubKey").String()
}
commitmentTxInfo.AmountToRSMC = outputBean.AmountToRsmc
commitmentTxInfo.AmountToCounterparty = outputBean.AmountToCounterparty
commitmentTxInfo.AmountToHtlc = outputBean.AmountToHtlc
commitmentTxInfo.CreateBy = user.PeerId
commitmentTxInfo.CreateAt = time.Now()
commitmentTxInfo.LastEditTime = time.Now()
return commitmentTxInfo, nil
}
func createRDTx(owner string, channelInfo *dao.ChannelInfo, commitmentTxInfo *dao.CommitmentTransaction, toAddress string, user *bean.User) (*dao.RevocableDeliveryTransaction, error) {
rda := &dao.RevocableDeliveryTransaction{}
rda.CommitmentTxId = commitmentTxInfo.Id
rda.PeerIdA = channelInfo.PeerIdA
rda.PeerIdB = channelInfo.PeerIdB
rda.ChannelId = channelInfo.ChannelId
rda.PropertyId = commitmentTxInfo.PropertyId
rda.Owner = owner
//input
rda.InputTxid = commitmentTxInfo.RSMCTxid
rda.InputVout = 0
rda.InputAmount = commitmentTxInfo.AmountToRSMC
//output
rda.OutputAddress = toAddress
rda.Sequence = 1000
rda.Amount = commitmentTxInfo.AmountToRSMC
rda.CreateBy = user.PeerId
rda.CreateAt = time.Now()
rda.LastEditTime = time.Now()
return rda, nil
}
func createBRTxObj(owner string, channelInfo *dao.ChannelInfo, brType dao.BRType, commitmentTxInfo *dao.CommitmentTransaction, user *bean.User) (*dao.BreachRemedyTransaction, error) {
breachRemedyTransaction := &dao.BreachRemedyTransaction{}
breachRemedyTransaction.CommitmentTxId = commitmentTxInfo.Id
breachRemedyTransaction.PeerIdA = channelInfo.PeerIdA
breachRemedyTransaction.PeerIdB = channelInfo.PeerIdB
breachRemedyTransaction.ChannelId = channelInfo.ChannelId
breachRemedyTransaction.PropertyId = commitmentTxInfo.PropertyId
breachRemedyTransaction.Owner = owner
breachRemedyTransaction.Type = brType
//input
breachRemedyTransaction.TempPubKey = commitmentTxInfo.RSMCTempAddressPubKey
breachRemedyTransaction.InputAddress = commitmentTxInfo.RSMCMultiAddress
breachRemedyTransaction.InputAddressScriptPubKey = commitmentTxInfo.RSMCMultiAddressScriptPubKey
breachRemedyTransaction.InputTxHex = commitmentTxInfo.RSMCTxHex
breachRemedyTransaction.InputTxid = commitmentTxInfo.RSMCTxid
breachRemedyTransaction.InputVout = 0
breachRemedyTransaction.InputAmount = commitmentTxInfo.AmountToRSMC
//output
breachRemedyTransaction.Amount = commitmentTxInfo.AmountToRSMC
breachRemedyTransaction.CreateBy = user.PeerId
breachRemedyTransaction.CreateAt = time.Now()
breachRemedyTransaction.LastEditTime = time.Now()
return breachRemedyTransaction, nil
}
func checkBtcTxHex(btcFeeTxHexDecode string, channelInfo *dao.ChannelInfo, peerId string) (fundingTxid string, amountA float64, fundingOutputIndex uint32, err error) {
jsonFundingTxHexDecode := gjson.Parse(btcFeeTxHexDecode)
fundingTxid = jsonFundingTxHexDecode.Get("txid").String()
//vin
if jsonFundingTxHexDecode.Get("vin").IsArray() == false {
err = errors.New("wrong Tx input vin")
log.Println(err)
return "", 0, 0, err
}
inTxid := jsonFundingTxHexDecode.Get("vin").Array()[0].Get("txid").String()
inputTx, err := rpcClient.GetTransactionById(inTxid)
if err != nil {
err = errors.New("wrong input: " + err.Error())
log.Println(err)
return "", 0, 0, err
}
jsonInputTxDecode := gjson.Parse(inputTx)
flag := false
inputHexDecode, err := rpcClient.DecodeRawTransaction(jsonInputTxDecode.Get("hex").String())
if err != nil {
err = errors.New("wrong input: " + err.Error())
log.Println(err)
return "", 0, 0, err
}
funderAddress := channelInfo.AddressA
if peerId == channelInfo.PeerIdB {
funderAddress = channelInfo.AddressB
}
jsonInputHexDecode := gjson.Parse(inputHexDecode)
if jsonInputHexDecode.Get("vout").IsArray() {
for _, item := range jsonInputHexDecode.Get("vout").Array() {
addresses := item.Get("scriptPubKey").Get("addresses").Array()
for _, subItem := range addresses {
if subItem.String() == funderAddress {
flag = true
break
}
}
if flag {
break
}
}
}
if flag == false {
err = errors.New("wrong vin " + jsonFundingTxHexDecode.Get("vin").String())
log.Println(err)
return "", 0, 0, err
}
//vout
flag = false
if jsonFundingTxHexDecode.Get("vout").IsArray() == false {
err = errors.New("wrong Tx vout")
log.Println(err)
return "", 0, 0, err
}
for _, item := range jsonFundingTxHexDecode.Get("vout").Array() {
addresses := item.Get("scriptPubKey").Get("addresses").Array()
for _, subItem := range addresses {
if subItem.String() == channelInfo.ChannelAddress {
amountA = item.Get("value").Float()
fundingOutputIndex = uint32(item.Get("n").Int())
flag = true
break
}
}
if flag {
break
}
}
if flag == false {
err = errors.New("wrong vout " + jsonFundingTxHexDecode.Get("vout").String())
log.Println(err)
return "", 0, 0, err
}
return fundingTxid, amountA, fundingOutputIndex, err
}
func checkOmniTxHex(fundingTxHexDecode string, channelInfo *dao.ChannelInfo, user string) (fundingTxid string, amountA float64, propertyId int64, err error) {
jsonOmniTxHexDecode := gjson.Parse(fundingTxHexDecode)
fundingTxid = jsonOmniTxHexDecode.Get("txid").String()
funderAddress := channelInfo.AddressA
if user == channelInfo.PeerIdB {
funderAddress = channelInfo.AddressB
}
sendingAddress := jsonOmniTxHexDecode.Get("sendingaddress").String()
if sendingAddress != funderAddress {
err = errors.New("wrong Tx input")
log.Println(err)
return "", 0, 0, err
}
referenceAddress := jsonOmniTxHexDecode.Get("referenceaddress").String()
if referenceAddress != channelInfo.ChannelAddress {
err = errors.New("wrong Tx output")
log.Println(err)
return "", 0, 0, err
}
amountA = jsonOmniTxHexDecode.Get("amount").Float()
propertyId = jsonOmniTxHexDecode.Get("propertyid").Int()
return fundingTxid, amountA, propertyId, err
}
//从未广播的交易hash数据中解析出他的输出,以此作为下个交易的输入
func getInputsForNextTxByParseTxHashVout(hex string, toAddress, scriptPubKey string) (inputs []rpc.TransactionInputItem, err error) {
result, err := rpcClient.DecodeRawTransaction(hex)
if err != nil {
return nil, err
}
jsonHex := gjson.Parse(result)
log.Println(jsonHex)
if jsonHex.Get("vout").IsArray() {
inputs = make([]rpc.TransactionInputItem, 0, 0)
for _, item := range jsonHex.Get("vout").Array() {
if item.Get("scriptPubKey").Get("addresses").Exists() {
addresses := item.Get("scriptPubKey").Get("addresses").Array()
for _, address := range addresses {
if address.String() == toAddress {
node := rpc.TransactionInputItem{}
node.Txid = jsonHex.Get("txid").String()
node.ScriptPubKey = scriptPubKey
node.Vout = uint32(item.Get("n").Uint())
node.Amount = item.Get("value").Float()
if node.Amount > 0 {
inputs = append(inputs, node)
}
}
}
}
}
return inputs, nil
}
return nil, errors.New("no inputs")
}
func getLatestCommitmentTx(channelId string, owner string) (commitmentTxInfo *dao.CommitmentTransaction, err error) {
commitmentTxInfo = &dao.CommitmentTransaction{}
err = db.Select(
q.Eq("ChannelId", channelId),
q.Eq("Owner", owner)).
OrderBy("CreateAt").Reverse().First(commitmentTxInfo)
return commitmentTxInfo, err
}
func getLatestCommitmentTxUseDbTx(tx storm.Node, channelId string, owner string) (commitmentTxInfo *dao.CommitmentTransaction, err error) {
commitmentTxInfo = &dao.CommitmentTransaction{}
err = tx.Select(
q.Eq("ChannelId", channelId),
q.Eq("Owner", owner)).
OrderBy("CreateAt").Reverse().First(commitmentTxInfo)
return commitmentTxInfo, err
}
//根据通道id获取通道信息
func getChannelInfoByChannelId(tx storm.Node, channelId string, userPeerId string) (channelInfo *dao.ChannelInfo) {
channelInfo = &dao.ChannelInfo{}
err := tx.Select(
q.Eq("ChannelId", channelId),
q.Or(
q.Eq("PeerIdA", userPeerId),
q.Eq("PeerIdB", userPeerId)),
q.Eq("CurrState", dao.ChannelState_CanUse)).
First(channelInfo)
if err != nil {
return nil
}
return channelInfo
}
//根据通道id获取通道信息
func getFundingTransactionByChannelId(dbTx storm.Node, channelId string, userPeerId string) (fundingTransaction *dao.FundingTransaction) {
fundingTransaction = &dao.FundingTransaction{}
err := dbTx.Select(
q.Eq("ChannelId", channelId),
q.Eq("CurrState", dao.FundingTransactionState_Accept),
q.Or(
q.Eq("PeerIdA", userPeerId),
q.Eq("PeerIdB", userPeerId))).
OrderBy("CreateAt").
Reverse().First(fundingTransaction)
if err != nil {
return nil
}
return fundingTransaction
}
func signRdTx(tx storm.Node, channelInfo *dao.ChannelInfo, signedRsmcHex string, rdHex string, latestCcommitmentTxInfo dao.CommitmentTransaction, outputAddress string, user *bean.User) (err error) {
inputs, err := getInputsForNextTxByParseTxHashVout(signedRsmcHex, latestCcommitmentTxInfo.RSMCMultiAddress, latestCcommitmentTxInfo.RSMCMultiAddressScriptPubKey)
if err != nil {
log.Println(err)
return err
}
_, signedRdHex, err := rpcClient.OmniSignRawTransactionForUnsend(rdHex, inputs, tempAddrPrivateKeyMap[latestCcommitmentTxInfo.RSMCTempAddressPubKey])
if err != nil {
return err
}
result, err := rpcClient.TestMemPoolAccept(signedRdHex)
if err != nil {
return err
}
if gjson.Parse(result).Array()[0].Get("allowed").Bool() == false {
if gjson.Parse(result).Array()[0].Get("reject-reason").String() != "missing-inputs" {
return errors.New(gjson.Parse(result).Array()[0].Get("reject-reason").String())
}
}
aliceRdTxid := checkHexOutputAddressFromOmniDecode(signedRdHex, inputs, outputAddress)
if aliceRdTxid == "" {
return errors.New("rdtx has wrong output address")
}
rdTransaction, err := createRDTx(user.PeerId, channelInfo, &latestCcommitmentTxInfo, outputAddress, user)
if err != nil {
log.Println(err)
return err
}
rdTransaction.RDType = 0
rdTransaction.TxHex = signedRdHex
rdTransaction.Txid = aliceRdTxid
rdTransaction.CurrState = dao.TxInfoState_CreateAndSign
err = tx.Save(rdTransaction)
if err != nil {
log.Println(err)
return err
}
return nil
}
func signHTD1bTx(tx storm.Node, signedHtlcHex string, htd1bHex string, latestCcommitmentTxInfo dao.CommitmentTransaction, outputAddress string, user *bean.User) (err error) {
inputs, err := getInputsForNextTxByParseTxHashVout(signedHtlcHex, latestCcommitmentTxInfo.HTLCMultiAddress, latestCcommitmentTxInfo.HTLCMultiAddressScriptPubKey)
if err != nil {
log.Println(err)
return err
}
signedHtd1bTxid, signedHtd1bHex, err := rpcClient.OmniSignRawTransactionForUnsend(htd1bHex, inputs, tempAddrPrivateKeyMap[latestCcommitmentTxInfo.HTLCTempAddressPubKey])
if err != nil {
return err
}
result, err := rpcClient.TestMemPoolAccept(signedHtd1bHex)
if err != nil {
return err
}
if gjson.Parse(result).Array()[0].Get("allowed").Bool() == false {
if gjson.Parse(result).Array()[0].Get("reject-reason").String() != "missing-inputs" {
return errors.New(gjson.Parse(result).Array()[0].Get("reject-reason").String())
}
}
owner := latestCcommitmentTxInfo.PeerIdA
if user.PeerId == latestCcommitmentTxInfo.PeerIdA {
owner = latestCcommitmentTxInfo.PeerIdB
}
htlcTimeOut := getHtlcTimeout(latestCcommitmentTxInfo.HtlcChannelPath, latestCcommitmentTxInfo.ChannelId)
htlcTimeoutDeliveryTx := &dao.HTLCTimeoutDeliveryTxB{}
htlcTimeoutDeliveryTx.ChannelId = latestCcommitmentTxInfo.ChannelId
htlcTimeoutDeliveryTx.CommitmentTxId = latestCcommitmentTxInfo.Id
htlcTimeoutDeliveryTx.PropertyId = latestCcommitmentTxInfo.PropertyId
htlcTimeoutDeliveryTx.OutputAddress = outputAddress
htlcTimeoutDeliveryTx.InputHex = latestCcommitmentTxInfo.HtlcTxHex
htlcTimeoutDeliveryTx.OutAmount = latestCcommitmentTxInfo.AmountToHtlc
htlcTimeoutDeliveryTx.Owner = owner
htlcTimeoutDeliveryTx.CurrState = dao.TxInfoState_CreateAndSign
htlcTimeoutDeliveryTx.CreateBy = user.PeerId
htlcTimeoutDeliveryTx.Timeout = htlcTimeOut
htlcTimeoutDeliveryTx.CreateAt = time.Now()
htlcTimeoutDeliveryTx.Txid = signedHtd1bTxid
htlcTimeoutDeliveryTx.TxHex = signedHtd1bHex
err = tx.Save(htlcTimeoutDeliveryTx)
if err != nil {
log.Println(err)
return err
}
return nil
}
func createMultiSig(pubkey1 string, pubkey2 string) (multiAddress, redeemScript, scriptPubKey string, err error) {
aliceRsmcMultiAddr, err := rpcClient.CreateMultiSig(2, []string{pubkey1, pubkey2})
if err != nil {
return "", "", "", err
}
multiAddress = gjson.Get(aliceRsmcMultiAddr, "address").String()
redeemScript = gjson.Get(aliceRsmcMultiAddr, "redeemScript").String()
tempJson, err := rpcClient.GetAddressInfo(multiAddress)
if err != nil {
return "", "", "", err
}
scriptPubKey = gjson.Get(tempJson, "scriptPubKey").String()
return multiAddress, redeemScript, scriptPubKey, nil
}
func createCommitmentTxHex(dbTx storm.Node, isSender bool, reqData *bean.CommitmentTx, channelInfo *dao.ChannelInfo, lastCommitmentTx *dao.CommitmentTransaction, currUser bean.User) (commitmentTxInfo *dao.CommitmentTransaction, err error) {
//1、转账给bob的交易:输入:通道其中一个input,输出:给bob
//2、转账后的余额的交易:输入:通道总的一个input,输出:一个多签地址,这个钱又需要后续的RD才能赎回
// create Cna tx
fundingTransaction := getFundingTransactionByChannelId(dbTx, channelInfo.ChannelId, currUser.PeerId)
if fundingTransaction == nil {
err = errors.New("not found fundingTransaction")
return nil, err
}
var outputBean = commitmentOutputBean{}
outputBean.RsmcTempPubKey = reqData.CurrTempAddressPubKey
if currUser.PeerId == channelInfo.PeerIdA {
//default alice transfer to bob ,then alice minus money
outputBean.AmountToRsmc, _ = decimal.NewFromFloat(fundingTransaction.AmountA).Sub(decimal.NewFromFloat(reqData.Amount)).Float64()
outputBean.AmountToCounterparty, _ = decimal.NewFromFloat(fundingTransaction.AmountB).Add(decimal.NewFromFloat(reqData.Amount)).Float64()
outputBean.OppositeSideChannelAddress = channelInfo.AddressB
outputBean.OppositeSideChannelPubKey = channelInfo.PubKeyB
} else {
outputBean.AmountToRsmc, _ = decimal.NewFromFloat(fundingTransaction.AmountB).Add(decimal.NewFromFloat(reqData.Amount)).Float64()
outputBean.AmountToCounterparty, _ = decimal.NewFromFloat(fundingTransaction.AmountA).Sub(decimal.NewFromFloat(reqData.Amount)).Float64()
outputBean.OppositeSideChannelAddress = channelInfo.AddressA
outputBean.OppositeSideChannelPubKey = channelInfo.PubKeyA
}
if lastCommitmentTx != nil && lastCommitmentTx.Id > 0 {
if isSender {
outputBean.AmountToRsmc, _ = decimal.NewFromFloat(lastCommitmentTx.AmountToRSMC).Sub(decimal.NewFromFloat(reqData.Amount)).Float64()
outputBean.AmountToCounterparty, _ = decimal.NewFromFloat(lastCommitmentTx.AmountToCounterparty).Add(decimal.NewFromFloat(reqData.Amount)).Float64()
} else {
outputBean.AmountToRsmc, _ = decimal.NewFromFloat(lastCommitmentTx.AmountToRSMC).Add(decimal.NewFromFloat(reqData.Amount)).Float64()
outputBean.AmountToCounterparty, _ = decimal.NewFromFloat(lastCommitmentTx.AmountToCounterparty).Sub(decimal.NewFromFloat(reqData.Amount)).Float64()
}
}
if lastCommitmentTx.TxType == dao.CommitmentTransactionType_Htlc {
if lastCommitmentTx.HtlcSender == currUser.PeerId {
outputBean.AmountToCounterparty, _ = decimal.NewFromFloat(lastCommitmentTx.AmountToCounterparty).Add(decimal.NewFromFloat(lastCommitmentTx.AmountToHtlc)).Float64()
} else {
outputBean.AmountToRsmc, _ = decimal.NewFromFloat(lastCommitmentTx.AmountToRSMC).Add(decimal.NewFromFloat(lastCommitmentTx.AmountToHtlc)).Float64()
}
}
commitmentTxInfo, err = createCommitmentTx(currUser.PeerId, channelInfo, fundingTransaction, outputBean, &currUser)
if err != nil {
log.Println(err)
return nil, err
}
commitmentTxInfo.TxType = dao.CommitmentTransactionType_Rsmc
usedTxidTemp := ""
if commitmentTxInfo.AmountToRSMC > 0 {
txid, hex, usedTxid, err := rpcClient.OmniCreateAndSignRawTransactionUseSingleInput(
int(commitmentTxInfo.TxType),
channelInfo.ChannelAddress,
[]string{
reqData.ChannelAddressPrivateKey,
},
commitmentTxInfo.RSMCMultiAddress,
fundingTransaction.PropertyId,
commitmentTxInfo.AmountToRSMC,
0,
0, &channelInfo.ChannelAddressRedeemScript, "")
if err != nil {
log.Println(err)
return nil, err
}
usedTxidTemp = usedTxid
commitmentTxInfo.RsmcInputTxid = usedTxid
commitmentTxInfo.RSMCTxid = txid
commitmentTxInfo.RSMCTxHex = hex
}
//create to other tx
if commitmentTxInfo.AmountToCounterparty > 0 {
txid, hex, err := rpcClient.OmniCreateAndSignRawTransactionUseRestInput(
int(commitmentTxInfo.TxType),
channelInfo.ChannelAddress,
usedTxidTemp,
[]string{
reqData.ChannelAddressPrivateKey,
},
outputBean.OppositeSideChannelAddress,
fundingTransaction.FunderAddress,
fundingTransaction.PropertyId,
commitmentTxInfo.AmountToCounterparty,
0,
0, &channelInfo.ChannelAddressRedeemScript)
if err != nil {
log.Println(err)
return nil, err
}
commitmentTxInfo.ToCounterpartyTxid = txid
commitmentTxInfo.ToCounterpartyTxHex = hex
}
commitmentTxInfo.LastHash = ""
commitmentTxInfo.CurrHash = ""
if lastCommitmentTx != nil && lastCommitmentTx.Id > 0 {
commitmentTxInfo.LastCommitmentTxId = lastCommitmentTx.Id
commitmentTxInfo.LastHash = lastCommitmentTx.CurrHash
}
commitmentTxInfo.CurrState = dao.TxInfoState_Create
err = dbTx.Save(commitmentTxInfo)
if err != nil {
log.Println(err)
return nil, err
}
bytes, err := json.Marshal(commitmentTxInfo)
msgHash := tool.SignMsgWithSha256(bytes)
commitmentTxInfo.CurrHash = msgHash
err = dbTx.Update(commitmentTxInfo)
if err != nil {
log.Println(err)
return nil, err
}
return commitmentTxInfo, nil
}