/
privacy.go
1276 lines (1161 loc) · 42.5 KB
/
privacy.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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package wallet
import (
"bytes"
"encoding/hex"
"errors"
"sort"
"strings"
"sync"
"time"
"unsafe"
"github.com/33cn/chain33/system/dapp"
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/common/address"
"github.com/33cn/chain33/common/crypto"
"github.com/33cn/chain33/common/db"
"github.com/33cn/chain33/types"
wcom "github.com/33cn/chain33/wallet/common"
privacy "github.com/33cn/plugin/plugin/dapp/privacy/crypto"
privacytypes "github.com/33cn/plugin/plugin/dapp/privacy/types"
)
func (policy *privacyPolicy) rescanAllTxAddToUpdateUTXOs() {
accounts, err := policy.getWalletOperate().GetWalletAccounts()
if err != nil {
bizlog.Debug("rescanAllTxToUpdateUTXOs", "walletOperate.GetWalletAccounts error", err)
return
}
bizlog.Debug("rescanAllTxToUpdateUTXOs begin!")
for _, acc := range accounts {
//从blockchain模块同步Account.Addr对应的所有交易详细信息
policy.rescanwg.Add(1)
go policy.rescanReqTxDetailByAddr(acc.Addr, policy.rescanwg)
}
policy.rescanwg.Wait()
bizlog.Debug("rescanAllTxToUpdateUTXOs success!")
}
//从blockchain模块同步addr参与的所有交易详细信息
func (policy *privacyPolicy) rescanReqTxDetailByAddr(addr string, wg *sync.WaitGroup) {
defer wg.Done()
policy.reqTxDetailByAddr(addr)
}
//从blockchain模块同步addr参与的所有交易详细信息
func (policy *privacyPolicy) reqTxDetailByAddr(addr string) {
if len(addr) == 0 {
bizlog.Error("reqTxDetailByAddr input addr is nil!")
return
}
var txInfo types.ReplyTxInfo
i := 0
operater := policy.getWalletOperate()
for {
//首先从blockchain模块获取地址对应的所有交易hashs列表,从最新的交易开始获取
var ReqAddr types.ReqAddr
ReqAddr.Addr = addr
ReqAddr.Flag = 0
ReqAddr.Direction = 0
ReqAddr.Count = int32(MaxTxHashsPerTime)
if i == 0 {
ReqAddr.Height = -1
ReqAddr.Index = 0
} else {
ReqAddr.Height = txInfo.GetHeight()
ReqAddr.Index = txInfo.GetIndex()
}
i++
ReplyTxInfos, err := operater.GetAPI().GetTransactionByAddr(&ReqAddr)
if err != nil {
bizlog.Error("reqTxDetailByAddr", "GetTransactionByAddr error", err, "addr", addr)
return
}
if ReplyTxInfos == nil {
bizlog.Info("reqTxDetailByAddr ReplyTxInfos is nil")
return
}
txcount := len(ReplyTxInfos.TxInfos)
var ReqHashes types.ReqHashes
ReqHashes.Hashes = make([][]byte, len(ReplyTxInfos.TxInfos))
for index, ReplyTxInfo := range ReplyTxInfos.TxInfos {
ReqHashes.Hashes[index] = ReplyTxInfo.GetHash()
txInfo.Hash = ReplyTxInfo.GetHash()
txInfo.Height = ReplyTxInfo.GetHeight()
txInfo.Index = ReplyTxInfo.GetIndex()
}
operater.GetTxDetailByHashs(&ReqHashes)
if txcount < int(MaxTxHashsPerTime) {
return
}
}
}
func (policy *privacyPolicy) isRescanUtxosFlagScaning() (bool, error) {
if privacytypes.UtxoFlagScaning == policy.GetRescanFlag() {
return true, privacytypes.ErrRescanFlagScaning
}
return false, nil
}
func (policy *privacyPolicy) parseViewSpendPubKeyPair(in string) (viewPubKey, spendPubKey []byte, err error) {
src, err := common.FromHex(in)
if err != nil {
return nil, nil, err
}
if 64 != len(src) {
bizlog.Error("parseViewSpendPubKeyPair", "pair with len", len(src))
return nil, nil, types.ErrPubKeyLen
}
viewPubKey = src[:32]
spendPubKey = src[32:]
return
}
func (policy *privacyPolicy) getPrivKeyByAddr(addr string) (crypto.PrivKey, error) {
//获取指定地址在钱包里的账户信息
Accountstor, err := policy.store.getAccountByAddr(addr)
if err != nil {
bizlog.Error("ProcSendToAddress", "GetAccountByAddr err:", err)
return nil, err
}
//通过password解密存储的私钥
prikeybyte, err := common.FromHex(Accountstor.GetPrivkey())
if err != nil || len(prikeybyte) == 0 {
bizlog.Error("ProcSendToAddress", "FromHex err", err)
return nil, err
}
operater := policy.getWalletOperate()
password := []byte(operater.GetPassword())
privkey := wcom.CBCDecrypterPrivkey(password, prikeybyte)
//通过privkey生成一个pubkey然后换算成对应的addr
cr, err := crypto.Load(types.GetSignName("privacy", operater.GetSignType()), -1)
if err != nil {
bizlog.Error("ProcSendToAddress", "err", err)
return nil, err
}
priv, err := cr.PrivKeyFromBytes(privkey)
if err != nil {
bizlog.Error("ProcSendToAddress", "PrivKeyFromBytes err", err)
return nil, err
}
return priv, nil
}
func (policy *privacyPolicy) getPrivacykeyPair(addr string) (*privacy.Privacy, error) {
if accPrivacy, _ := policy.store.getWalletAccountPrivacy(addr); accPrivacy != nil {
privacyInfo := &privacy.Privacy{}
password := []byte(policy.getWalletOperate().GetPassword())
copy(privacyInfo.ViewPubkey[:], accPrivacy.ViewPubkey)
decrypteredView := wcom.CBCDecrypterPrivkey(password, accPrivacy.ViewPrivKey)
copy(privacyInfo.ViewPrivKey[:], decrypteredView)
copy(privacyInfo.SpendPubkey[:], accPrivacy.SpendPubkey)
decrypteredSpend := wcom.CBCDecrypterPrivkey(password, accPrivacy.SpendPrivKey)
copy(privacyInfo.SpendPrivKey[:], decrypteredSpend)
return privacyInfo, nil
}
_, err := policy.getPrivKeyByAddr(addr)
if err != nil {
return nil, err
}
return nil, privacytypes.ErrPrivacyNotEnabled
}
func (policy *privacyPolicy) savePrivacykeyPair(addr string) (*privacy.Privacy, error) {
priv, err := policy.getPrivKeyByAddr(addr)
if err != nil {
return nil, err
}
newPrivacy, err := privacy.NewPrivacyWithPrivKey((*[privacy.KeyLen32]byte)(unsafe.Pointer(&priv.Bytes()[0])))
if err != nil {
return nil, err
}
password := []byte(policy.getWalletOperate().GetPassword())
encrypteredView := wcom.CBCEncrypterPrivkey(password, newPrivacy.ViewPrivKey.Bytes())
encrypteredSpend := wcom.CBCEncrypterPrivkey(password, newPrivacy.SpendPrivKey.Bytes())
walletPrivacy := &privacytypes.WalletAccountPrivacy{
ViewPubkey: newPrivacy.ViewPubkey[:],
ViewPrivKey: encrypteredView,
SpendPubkey: newPrivacy.SpendPubkey[:],
SpendPrivKey: encrypteredSpend,
}
//save the privacy created to wallet db
policy.store.setWalletAccountPrivacy(addr, walletPrivacy)
return newPrivacy, nil
}
func (policy *privacyPolicy) enablePrivacy(req *privacytypes.ReqEnablePrivacy) (*privacytypes.RepEnablePrivacy, error) {
var addrs []string
if 0 == len(req.Addrs) {
WalletAccStores, err := policy.store.getAccountByPrefix("Account")
if err != nil || len(WalletAccStores) == 0 {
bizlog.Info("enablePrivacy", "GetAccountByPrefix:err", err)
return nil, types.ErrNotFound
}
for _, WalletAccStore := range WalletAccStores {
addrs = append(addrs, WalletAccStore.Addr)
}
} else {
addrs = append(addrs, req.Addrs...)
}
var rep privacytypes.RepEnablePrivacy
for _, addr := range addrs {
str := ""
isOK := true
_, err := policy.getPrivacykeyPair(addr)
if err != nil {
_, err = policy.savePrivacykeyPair(addr)
if err != nil {
isOK = false
str = err.Error()
}
}
priAddrResult := &privacytypes.PriAddrResult{
Addr: addr,
IsOK: isOK,
Msg: str,
}
rep.Results = append(rep.Results, priAddrResult)
}
return &rep, nil
}
func (policy *privacyPolicy) showPrivacyKeyPair(reqAddr *types.ReqString) (*privacytypes.ReplyPrivacyPkPair, error) {
privacyInfo, err := policy.getPrivacykeyPair(reqAddr.GetData())
if err != nil {
bizlog.Error("showPrivacyKeyPair", "getPrivacykeyPair error ", err)
return nil, err
}
//pair := privacyInfo.ViewPubkey[:]
//pair = append(pair, privacyInfo.SpendPubkey[:]...)
replyPrivacyPkPair := &privacytypes.ReplyPrivacyPkPair{
ShowSuccessful: true,
Pubkeypair: makeViewSpendPubKeyPairToString(privacyInfo.ViewPubkey[:], privacyInfo.SpendPubkey[:]),
}
return replyPrivacyPkPair, nil
}
func (policy *privacyPolicy) getPrivacyAccountInfo(req *privacytypes.ReqPrivacyAccount) (*privacytypes.ReplyPrivacyAccount, error) {
addr := strings.Trim(req.GetAddr(), " ")
token := req.GetToken()
reply := &privacytypes.ReplyPrivacyAccount{}
reply.Displaymode = req.Displaymode
if len(addr) == 0 {
return nil, errors.New("Address is empty")
}
// 搜索可用余额
privacyDBStore, err := policy.store.listAvailableUTXOs(req.GetAssetExec(), token, addr)
if err != nil {
bizlog.Error("getPrivacyAccountInfo", "listAvailableUTXOs")
return nil, err
}
utxos := make([]*privacytypes.UTXO, 0)
for _, ele := range privacyDBStore {
utxoBasic := &privacytypes.UTXOBasic{
UtxoGlobalIndex: &privacytypes.UTXOGlobalIndex{
Outindex: ele.OutIndex,
Txhash: ele.Txhash,
},
OnetimePubkey: ele.OnetimePublicKey,
}
utxo := &privacytypes.UTXO{
Amount: ele.Amount,
UtxoBasic: utxoBasic,
}
utxos = append(utxos, utxo)
}
reply.Utxos = &privacytypes.UTXOs{Utxos: utxos}
// 搜索冻结余额
utxos = make([]*privacytypes.UTXO, 0)
ftxoslice, err := policy.store.listFrozenUTXOs(req.GetAssetExec(), token, addr)
if err == nil && ftxoslice != nil {
for _, ele := range ftxoslice {
utxos = append(utxos, ele.Utxos...)
}
}
reply.Ftxos = &privacytypes.UTXOs{Utxos: utxos}
return reply, nil
}
// 修改选择UTXO的算法
// 优先选择UTXO高度与当前高度建个12个区块以上的UTXO
// 如果选择还不够则再从老到新选择12个区块内的UTXO
// 当该地址上的可用UTXO比较多时,可以考虑改进算法,优先选择币值小的,花掉小票,然后再选择币值接近的,减少找零,最后才选择大面值的找零
func (policy *privacyPolicy) selectUTXO(assetExec, token, addr string, amount int64) ([]*txOutputInfo, error) {
if len(token) == 0 || len(addr) == 0 || amount <= 0 {
return nil, types.ErrInvalidParam
}
wutxos, err := policy.store.getPrivacyTokenUTXOs(assetExec, token, addr)
if err != nil {
return nil, types.ErrInsufficientBalance
}
operater := policy.getWalletOperate()
curBlockHeight := operater.GetBlockHeight()
var confirmUTXOs, unconfirmUTXOs []*walletUTXO
var balance int64
for _, wutxo := range wutxos.utxos {
if curBlockHeight < wutxo.height {
continue
}
if curBlockHeight-wutxo.height > privacytypes.UtxoMaturityDegree {
balance += wutxo.outinfo.amount
confirmUTXOs = append(confirmUTXOs, wutxo)
} else {
unconfirmUTXOs = append(unconfirmUTXOs, wutxo)
}
}
if balance < amount && len(unconfirmUTXOs) > 0 {
// 已经确认的UTXO还不够支付,则需要按照从老到新的顺序,从可能回退的队列中获取
// 高度从低到高获取
sort.Slice(unconfirmUTXOs, func(i, j int) bool {
return unconfirmUTXOs[i].height < unconfirmUTXOs[j].height
})
for _, wutxo := range unconfirmUTXOs {
confirmUTXOs = append(confirmUTXOs, wutxo)
balance += wutxo.outinfo.amount
if balance >= amount {
break
}
}
}
if balance < amount {
return nil, types.ErrInsufficientBalance
}
balance = 0
var selectedOuts []*txOutputInfo
for balance < amount {
index := operater.GetRandom().Intn(len(confirmUTXOs))
selectedOuts = append(selectedOuts, confirmUTXOs[index].outinfo)
balance += confirmUTXOs[index].outinfo.amount
// remove selected utxo
confirmUTXOs = append(confirmUTXOs[:index], confirmUTXOs[index+1:]...)
}
return selectedOuts, nil
}
/*
buildInput 构建隐私交易的输入信息
操作步骤
1.从当前钱包中选择可用并且足够支付金额的UTXO列表
2.如果需要混淆(mixcout>0),则根据UTXO的金额从数据库中获取足够数量的UTXO,与当前UTXO进行混淆
3.通过公式 x=Hs(aR)+b,计算出一个整数,因为 xG = Hs(ar)G+bG = Hs(aR)G+B,所以可以继续使用这笔交易
*/
func (policy *privacyPolicy) buildInput(privacykeyParirs *privacy.Privacy, buildInfo *buildInputInfo) (*privacytypes.PrivacyInput, []*privacytypes.UTXOBasics, []*privacytypes.RealKeyInput, []*txOutputInfo, error) {
operater := policy.getWalletOperate()
//挑选满足额度的utxo
selectedUtxo, err := policy.selectUTXO(buildInfo.assetExec, buildInfo.assetSymbol, buildInfo.sender, buildInfo.amount)
if err != nil {
bizlog.Error("buildInput", "Failed to selectOutput for amount", buildInfo.amount,
"Due to cause", err)
return nil, nil, nil, nil, err
}
sort.Slice(selectedUtxo, func(i, j int) bool {
return selectedUtxo[i].amount <= selectedUtxo[j].amount
})
reqGetGlobalIndex := privacytypes.ReqUTXOGlobalIndex{
AssetExec: buildInfo.assetExec,
AssetSymbol: buildInfo.assetSymbol,
MixCount: 0,
}
if buildInfo.mixcount > 0 {
reqGetGlobalIndex.MixCount = common.MinInt32(int32(privacytypes.PrivacyMaxCount), common.MaxInt32(buildInfo.mixcount, 0))
}
for _, out := range selectedUtxo {
reqGetGlobalIndex.Amount = append(reqGetGlobalIndex.Amount, out.amount)
}
// 混淆数大于0时候才向blockchain请求
var resUTXOGlobalIndex *privacytypes.ResUTXOGlobalIndex
if buildInfo.mixcount > 0 {
query := &types.ChainExecutor{
Driver: "privacy",
FuncName: "GetUTXOGlobalIndex",
Param: types.Encode(&reqGetGlobalIndex),
}
//向blockchain请求相同额度的不同utxo用于相同额度的混淆作用
data, err := operater.GetAPI().QueryChain(query)
if err != nil {
bizlog.Error("buildInput BlockChainQuery", "err", err)
return nil, nil, nil, nil, err
}
resUTXOGlobalIndex = data.(*privacytypes.ResUTXOGlobalIndex)
if resUTXOGlobalIndex == nil {
bizlog.Info("buildInput EventBlockChainQuery is nil")
return nil, nil, nil, nil, err
}
sort.Slice(resUTXOGlobalIndex.UtxoIndex4Amount, func(i, j int) bool {
return resUTXOGlobalIndex.UtxoIndex4Amount[i].Amount <= resUTXOGlobalIndex.UtxoIndex4Amount[j].Amount
})
if len(selectedUtxo) != len(resUTXOGlobalIndex.UtxoIndex4Amount) {
bizlog.Error("buildInput EventBlockChainQuery get not the same count for mix",
"len(selectedUtxo)", len(selectedUtxo),
"len(resUTXOGlobalIndex.UtxoIndex4Amount)", len(resUTXOGlobalIndex.UtxoIndex4Amount))
}
}
//构造输入PrivacyInput
privacyInput := &privacytypes.PrivacyInput{}
utxosInKeyInput := make([]*privacytypes.UTXOBasics, len(selectedUtxo))
realkeyInputSlice := make([]*privacytypes.RealKeyInput, len(selectedUtxo))
for i, utxo2pay := range selectedUtxo {
var utxoIndex4Amount *privacytypes.UTXOIndex4Amount
if nil != resUTXOGlobalIndex && i < len(resUTXOGlobalIndex.UtxoIndex4Amount) && utxo2pay.amount == resUTXOGlobalIndex.UtxoIndex4Amount[i].Amount {
utxoIndex4Amount = resUTXOGlobalIndex.UtxoIndex4Amount[i]
for j, utxo := range utxoIndex4Amount.Utxos {
//查找自身这条UTXO是否存在,如果存在则将其从其中删除
if bytes.Equal(utxo.OnetimePubkey, utxo2pay.onetimePublicKey) {
utxoIndex4Amount.Utxos = append(utxoIndex4Amount.Utxos[:j], utxoIndex4Amount.Utxos[j+1:]...)
break
}
}
}
if utxoIndex4Amount == nil {
utxoIndex4Amount = &privacytypes.UTXOIndex4Amount{}
}
if utxoIndex4Amount.Utxos == nil {
utxoIndex4Amount.Utxos = make([]*privacytypes.UTXOBasic, 0)
}
//如果请求返回的用于混淆的utxo不包含自身且达到mix的上限,则将最后一条utxo删除,保证最后的混淆度不大于设置
if len(utxoIndex4Amount.Utxos) > int(buildInfo.mixcount) {
utxoIndex4Amount.Utxos = utxoIndex4Amount.Utxos[:len(utxoIndex4Amount.Utxos)-1]
}
utxo := &privacytypes.UTXOBasic{
UtxoGlobalIndex: utxo2pay.utxoGlobalIndex,
OnetimePubkey: utxo2pay.onetimePublicKey,
}
//将真实的utxo添加到最后一个
utxoIndex4Amount.Utxos = append(utxoIndex4Amount.Utxos, utxo)
positions := operater.GetRandom().Perm(len(utxoIndex4Amount.Utxos))
utxos := make([]*privacytypes.UTXOBasic, len(utxoIndex4Amount.Utxos))
for k, position := range positions {
utxos[position] = utxoIndex4Amount.Utxos[k]
}
utxosInKeyInput[i] = &privacytypes.UTXOBasics{Utxos: utxos}
//x = Hs(aR) + b
onetimePriv, err := privacy.RecoverOnetimePriKey(utxo2pay.txPublicKeyR, privacykeyParirs.ViewPrivKey, privacykeyParirs.SpendPrivKey, int64(utxo2pay.utxoGlobalIndex.Outindex))
if err != nil {
bizlog.Error("transPri2Pri", "Failed to RecoverOnetimePriKey", err)
return nil, nil, nil, nil, err
}
realkeyInput := &privacytypes.RealKeyInput{
Realinputkey: int32(positions[len(positions)-1]),
Onetimeprivkey: onetimePriv.Bytes(),
}
realkeyInputSlice[i] = realkeyInput
keyImage, err := privacy.GenerateKeyImage(onetimePriv, utxo2pay.onetimePublicKey)
if err != nil {
return nil, nil, nil, nil, err
}
keyInput := &privacytypes.KeyInput{
Amount: utxo2pay.amount,
KeyImage: keyImage[:],
}
for _, utxo := range utxos {
keyInput.UtxoGlobalIndex = append(keyInput.UtxoGlobalIndex, utxo.UtxoGlobalIndex)
}
//完成一个input的构造,包括基于其环签名的生成,keyImage的生成,
//必须要注意的是,此处要添加用于混淆的其他utxo添加到最终keyinput的顺序必须和生成环签名时提供pubkey的顺序一致
//否则会导致环签名验证的失败
privacyInput.Keyinput = append(privacyInput.Keyinput, keyInput)
}
return privacyInput, utxosInKeyInput, realkeyInputSlice, selectedUtxo, nil
}
func (policy *privacyPolicy) createTransaction(req *privacytypes.ReqCreatePrivacyTx) (*types.Transaction, error) {
switch req.ActionType {
case privacytypes.ActionPublic2Privacy:
return policy.createPublic2PrivacyTx(req)
case privacytypes.ActionPrivacy2Privacy:
return policy.createPrivacy2PrivacyTx(req)
case privacytypes.ActionPrivacy2Public:
return policy.createPrivacy2PublicTx(req)
}
return nil, types.ErrInvalidParam
}
func (policy *privacyPolicy) createPublic2PrivacyTx(req *privacytypes.ReqCreatePrivacyTx) (*types.Transaction, error) {
cfg := policy.getWalletOperate().GetAPI().GetConfig()
viewPubSlice, spendPubSlice, err := parseViewSpendPubKeyPair(req.GetPubkeypair())
if err != nil {
bizlog.Error("createPublic2PrivacyTx", "parse view spend public key pair failed. err ", err)
return nil, err
}
amount := req.GetAmount()
viewPublic := (*[32]byte)(unsafe.Pointer(&viewPubSlice[0]))
spendPublic := (*[32]byte)(unsafe.Pointer(&spendPubSlice[0]))
privacyOutput, err := generateOuts(viewPublic, spendPublic, nil, nil, amount, amount, 0, cfg.GetCoinPrecision())
if err != nil {
bizlog.Error("createPublic2PrivacyTx", "generate output failed. err ", err)
return nil, err
}
value := &privacytypes.Public2Privacy{
Tokenname: req.Tokenname,
Amount: amount,
Note: req.GetNote(),
Output: privacyOutput,
AssetExec: req.GetAssetExec(),
}
action := &privacytypes.PrivacyAction{
Ty: privacytypes.ActionPublic2Privacy,
Value: &privacytypes.PrivacyAction_Public2Privacy{Public2Privacy: value},
}
tx := &types.Transaction{
Execer: []byte(cfg.ExecName(privacytypes.PrivacyX)),
Payload: types.Encode(action),
Nonce: policy.getWalletOperate().Nonce(),
To: address.ExecAddress(cfg.ExecName(privacytypes.PrivacyX)),
ChainID: cfg.GetChainID(),
}
tx.SetExpire(cfg, time.Duration(req.Expire))
tx.Signature = &types.Signature{
Signature: types.Encode(&privacytypes.PrivacySignatureParam{
ActionType: action.Ty,
}),
}
tx.Fee, err = tx.GetRealFee(cfg.GetMinTxFeeRate())
if err != nil {
bizlog.Error("createPublic2PrivacyTx", "calc fee failed", err)
return nil, err
}
return tx, nil
}
func (policy *privacyPolicy) createPrivacy2PrivacyTx(req *privacytypes.ReqCreatePrivacyTx) (*types.Transaction, error) {
//需要燃烧的utxo
var utxoBurnedAmount int64
cfg := policy.getWalletOperate().GetAPI().GetConfig()
isMainetCoins := !cfg.IsPara() && (req.AssetExec == cfg.GetCoinExec())
if isMainetCoins {
utxoBurnedAmount = privacytypes.PrivacyTxFee * cfg.GetCoinPrecision()
}
buildInfo := &buildInputInfo{
assetExec: req.GetAssetExec(),
assetSymbol: req.GetTokenname(),
sender: req.GetFrom(),
amount: req.GetAmount() + utxoBurnedAmount,
mixcount: req.GetMixcount(),
}
privacyInfo, err := policy.getPrivacykeyPair(req.GetFrom())
if err != nil {
bizlog.Error("createPrivacy2PrivacyTx", "getPrivacykeyPair error", err)
return nil, err
}
//step 1,buildInput
privacyInput, utxosInKeyInput, realkeyInputSlice, selectedUtxo, err := policy.buildInput(privacyInfo, buildInfo)
if err != nil {
return nil, err
}
//step 2,generateOuts
viewPublicSlice, spendPublicSlice, err := parseViewSpendPubKeyPair(req.GetPubkeypair())
if err != nil {
bizlog.Error("createPrivacy2PrivacyTx", "parseViewSpendPubKeyPair ", err)
return nil, err
}
viewPub4change, spendPub4change := privacyInfo.ViewPubkey.Bytes(), privacyInfo.SpendPubkey.Bytes()
viewPublic := (*[32]byte)(unsafe.Pointer(&viewPublicSlice[0]))
spendPublic := (*[32]byte)(unsafe.Pointer(&spendPublicSlice[0]))
viewPub4chgPtr := (*[32]byte)(unsafe.Pointer(&viewPub4change[0]))
spendPub4chgPtr := (*[32]byte)(unsafe.Pointer(&spendPub4change[0]))
selectedAmounTotal := int64(0)
for _, input := range privacyInput.Keyinput {
selectedAmounTotal += input.Amount
}
//构造输出UTXO
privacyOutput, err := generateOuts(viewPublic, spendPublic, viewPub4chgPtr, spendPub4chgPtr, req.GetAmount(), selectedAmounTotal, utxoBurnedAmount, cfg.GetCoinPrecision())
if err != nil {
return nil, err
}
value := &privacytypes.Privacy2Privacy{
Tokenname: req.GetTokenname(),
Amount: req.GetAmount(),
Note: req.GetNote(),
Input: privacyInput,
Output: privacyOutput,
AssetExec: req.GetAssetExec(),
}
action := &privacytypes.PrivacyAction{
Ty: privacytypes.ActionPrivacy2Privacy,
Value: &privacytypes.PrivacyAction_Privacy2Privacy{Privacy2Privacy: value},
}
tx := &types.Transaction{
Execer: []byte(cfg.ExecName(privacytypes.PrivacyX)),
Payload: types.Encode(action),
Fee: privacytypes.PrivacyTxFee * cfg.GetCoinPrecision(),
Nonce: policy.getWalletOperate().Nonce(),
To: address.ExecAddress(cfg.ExecName(privacytypes.PrivacyX)),
ChainID: cfg.GetChainID(),
}
tx.SetExpire(cfg, time.Duration(req.Expire))
if !isMainetCoins {
tx.Fee, err = tx.GetRealFee(cfg.GetMinTxFeeRate())
if err != nil {
bizlog.Error("createPrivacy2PrivacyTx", "calc fee failed", err)
return nil, err
}
}
// 创建交易成功,将已经使用掉的UTXO冻结,需要注意此处获取的txHash和交易发送时的一致
policy.saveFTXOInfo(tx.GetExpire(), req.GetAssetExec(), req.Tokenname, req.GetFrom(), hex.EncodeToString(tx.Hash()), selectedUtxo)
tx.Signature = &types.Signature{
Signature: types.Encode(&privacytypes.PrivacySignatureParam{
ActionType: action.Ty,
Utxobasics: utxosInKeyInput,
RealKeyInputs: realkeyInputSlice,
}),
}
return tx, nil
}
func (policy *privacyPolicy) createPrivacy2PublicTx(req *privacytypes.ReqCreatePrivacyTx) (*types.Transaction, error) {
//需要燃烧的utxo
//需要燃烧的utxo
var utxoBurnedAmount int64
cfg := policy.getWalletOperate().GetAPI().GetConfig()
isMainetCoins := !cfg.IsPara() && (req.AssetExec == cfg.GetCoinExec())
if isMainetCoins {
utxoBurnedAmount = privacytypes.PrivacyTxFee * cfg.GetCoinPrecision()
}
buildInfo := &buildInputInfo{
assetExec: req.GetAssetExec(),
assetSymbol: req.GetTokenname(),
sender: req.GetFrom(),
amount: req.GetAmount() + utxoBurnedAmount,
mixcount: req.GetMixcount(),
}
privacyInfo, err := policy.getPrivacykeyPair(req.GetFrom())
if err != nil {
bizlog.Error("createPrivacy2PublicTx failed to getPrivacykeyPair")
return nil, err
}
//step 1,buildInput
privacyInput, utxosInKeyInput, realkeyInputSlice, selectedUtxo, err := policy.buildInput(privacyInfo, buildInfo)
if err != nil {
bizlog.Error("createPrivacy2PublicTx failed to buildInput")
return nil, err
}
viewPub4change, spendPub4change := privacyInfo.ViewPubkey.Bytes(), privacyInfo.SpendPubkey.Bytes()
viewPub4chgPtr := (*[32]byte)(unsafe.Pointer(&viewPub4change[0]))
spendPub4chgPtr := (*[32]byte)(unsafe.Pointer(&spendPub4change[0]))
selectedAmounTotal := int64(0)
for _, input := range privacyInput.Keyinput {
if input.Amount <= 0 {
return nil, errors.New("")
}
selectedAmounTotal += input.Amount
}
changeAmount := selectedAmounTotal - req.GetAmount()
//step 2,generateOuts
//构造输出UTXO,只生成找零的UTXO
privacyOutput, err := generateOuts(nil, nil, viewPub4chgPtr, spendPub4chgPtr, 0, changeAmount, utxoBurnedAmount, cfg.GetCoinPrecision())
if err != nil {
return nil, err
}
value := &privacytypes.Privacy2Public{
Tokenname: req.GetTokenname(),
Amount: req.GetAmount(),
Note: req.GetNote(),
Input: privacyInput,
Output: privacyOutput,
To: req.GetTo(),
AssetExec: req.GetAssetExec(),
}
action := &privacytypes.PrivacyAction{
Ty: privacytypes.ActionPrivacy2Public,
Value: &privacytypes.PrivacyAction_Privacy2Public{Privacy2Public: value},
}
tx := &types.Transaction{
Execer: []byte(cfg.ExecName(privacytypes.PrivacyX)),
Payload: types.Encode(action),
Fee: privacytypes.PrivacyTxFee * cfg.GetCoinPrecision(),
Nonce: policy.getWalletOperate().Nonce(),
To: address.ExecAddress(cfg.ExecName(privacytypes.PrivacyX)),
ChainID: cfg.GetChainID(),
}
tx.SetExpire(cfg, time.Duration(req.Expire))
if !isMainetCoins {
tx.Fee, err = tx.GetRealFee(cfg.GetMinTxFeeRate())
if err != nil {
bizlog.Error("createPrivacy2PublicTx", "calc fee failed", err)
return nil, err
}
}
// 创建交易成功,将已经使用掉的UTXO冻结,需要注意此处获取的txHash和交易发送时的一致
policy.saveFTXOInfo(tx.GetExpire(), req.GetAssetExec(), req.Tokenname, req.GetFrom(), hex.EncodeToString(tx.Hash()), selectedUtxo)
tx.Signature = &types.Signature{
Signature: types.Encode(&privacytypes.PrivacySignatureParam{
ActionType: action.Ty,
Utxobasics: utxosInKeyInput,
RealKeyInputs: realkeyInputSlice,
}),
}
return tx, nil
}
func (policy *privacyPolicy) saveFTXOInfo(expire int64, assetExec, assetSymbol, sender, txhash string, selectedUtxos []*txOutputInfo) {
//将已经作为本次交易输入的utxo进行冻结,防止产生双花交易
policy.store.moveUTXO2FTXO(expire, assetExec, assetSymbol, sender, txhash, selectedUtxos)
//TODO:需要加入超时处理,需要将此处的txhash写入到数据库中,以免钱包瞬间奔溃后没有对该笔隐私交易的记录,
//TODO:然后当该交易得到执行之后,没法将FTXO转化为STXO,added by hezhengjun on 2018.6.5
}
func (policy *privacyPolicy) getPrivacyKeyPairs() ([]addrAndprivacy, error) {
//通过Account前缀查找获取钱包中的所有账户信息
WalletAccStores, err := policy.store.getAccountByPrefix("Account")
if err != nil || len(WalletAccStores) == 0 {
bizlog.Info("getPrivacyKeyPairs", "store getAccountByPrefix error", err)
return nil, err
}
var infoPriRes []addrAndprivacy
for _, AccStore := range WalletAccStores {
if len(AccStore.Addr) != 0 {
if privacyInfo, err := policy.getPrivacykeyPair(AccStore.Addr); err == nil {
var priInfo addrAndprivacy
priInfo.Addr = &AccStore.Addr
priInfo.PrivacyKeyPair = privacyInfo
infoPriRes = append(infoPriRes, priInfo)
}
}
}
if 0 == len(infoPriRes) {
return nil, privacytypes.ErrPrivacyNotEnabled
}
return infoPriRes, nil
}
func (policy *privacyPolicy) rescanUTXOs(req *privacytypes.ReqRescanUtxos) (*privacytypes.RepRescanUtxos, error) {
if req.Flag != 0 {
return policy.store.getRescanUtxosFlag4Addr(req)
}
// Rescan请求
var repRescanUtxos privacytypes.RepRescanUtxos
repRescanUtxos.Flag = req.Flag
operater := policy.getWalletOperate()
if operater.IsWalletLocked() {
return nil, types.ErrWalletIsLocked
}
if ok, err := policy.isRescanUtxosFlagScaning(); ok {
return nil, err
}
_, err := policy.getPrivacyKeyPairs()
if err != nil {
return nil, err
}
policy.SetRescanFlag(privacytypes.UtxoFlagScaning)
operater.GetWaitGroup().Add(1)
go policy.rescanReqUtxosByAddr(req.Addrs)
return &repRescanUtxos, nil
}
//从blockchain模块同步addr参与的所有交易详细信息
func (policy *privacyPolicy) rescanReqUtxosByAddr(addrs []string) {
defer policy.getWalletOperate().GetWaitGroup().Done()
bizlog.Debug("RescanAllUTXO begin!")
policy.reqUtxosByAddr(addrs)
bizlog.Debug("RescanAllUTXO success!")
}
func (policy *privacyPolicy) reqUtxosByAddr(addrs []string) {
// 更新数据库存储状态
var storeAddrs []string
if len(addrs) == 0 {
WalletAccStores, err := policy.store.getAccountByPrefix("Account")
if err != nil || len(WalletAccStores) == 0 {
bizlog.Info("reqUtxosByAddr", "getAccountByPrefix error", err)
return
}
for _, WalletAccStore := range WalletAccStores {
storeAddrs = append(storeAddrs, WalletAccStore.Addr)
}
} else {
storeAddrs = append(storeAddrs, addrs...)
}
policy.store.saveREscanUTXOsAddresses(storeAddrs, privacytypes.UtxoFlagScaning)
cfg := policy.getWalletOperate().GetAPI().GetConfig()
reqAddr := address.ExecAddress(cfg.ExecName(privacytypes.PrivacyX))
var txInfo types.ReplyTxInfo
i := 0
operater := policy.getWalletOperate()
for {
select {
case <-operater.GetWalletDone():
return
default:
}
//首先从execs模块获取地址对应的所有UTXOs,
// 1 先获取隐私合约地址相关交易
var ReqAddr types.ReqAddr
ReqAddr.Addr = reqAddr
ReqAddr.Flag = 0
ReqAddr.Direction = 0
ReqAddr.Count = int32(MaxTxHashsPerTime)
if i == 0 {
ReqAddr.Height = -1
ReqAddr.Index = 0
} else {
ReqAddr.Height = txInfo.GetHeight()
ReqAddr.Index = txInfo.GetIndex()
if !cfg.IsDappFork(ReqAddr.Height, privacytypes.PrivacyX, "ForkV21Privacy") { // 小于隐私分叉高度不做扫描
break
}
}
i++
//请求交易信息
msg, err := operater.GetAPI().Query(privacytypes.PrivacyX, "GetTxsByAddr", &ReqAddr)
if err != nil {
bizlog.Error("reqUtxosByAddr", "GetTxsByAddr error", err, "addr", reqAddr)
break
}
ReplyTxInfos := msg.(*types.ReplyTxInfos)
if ReplyTxInfos == nil {
bizlog.Info("privacy ReqTxInfosByAddr ReplyTxInfos is nil")
break
}
txcount := len(ReplyTxInfos.TxInfos)
var ReqHashes types.ReqHashes
ReqHashes.Hashes = make([][]byte, len(ReplyTxInfos.TxInfos))
for index, ReplyTxInfo := range ReplyTxInfos.TxInfos {
ReqHashes.Hashes[index] = ReplyTxInfo.GetHash()
}
if txcount > 0 {
txInfo.Hash = ReplyTxInfos.TxInfos[txcount-1].GetHash()
txInfo.Height = ReplyTxInfos.TxInfos[txcount-1].GetHeight()
txInfo.Index = ReplyTxInfos.TxInfos[txcount-1].GetIndex()
}
policy.getPrivacyTxDetailByHashs(&ReqHashes, addrs)
if txcount < int(MaxTxHashsPerTime) {
break
}
}
// 扫描完毕
policy.SetRescanFlag(privacytypes.UtxoFlagNoScan)
// 删除privacyInput
policy.deleteScanPrivacyInputUtxo()
policy.store.saveREscanUTXOsAddresses(storeAddrs, privacytypes.UtxoFlagScanEnd)
}
//TODO:input也可能时混淆的utxo, 需要增加判定实际的utxo
func (policy *privacyPolicy) deleteScanPrivacyInputUtxo() {
maxUTXOsPerTime := 1000
for {
utxoGlobalIndexs := policy.store.setScanPrivacyInputUTXO(int32(maxUTXOsPerTime))
policy.store.updateScanInputUTXOs(utxoGlobalIndexs)
if len(utxoGlobalIndexs) < maxUTXOsPerTime {
break
}
}
}
func (policy *privacyPolicy) getPrivacyTxDetailByHashs(ReqHashes *types.ReqHashes, addrs []string) {
//通过txhashs获取对应的txdetail
TxDetails, err := policy.getWalletOperate().GetAPI().GetTransactionByHash(ReqHashes)
if err != nil {
bizlog.Error("getPrivacyTxDetailByHashs", "GetTransactionByHash error", err)
return
}
var privacyInfo []addrAndprivacy
if len(addrs) > 0 {
for _, addr := range addrs {
if privacy, err := policy.getPrivacykeyPair(addr); err == nil {
priInfo := &addrAndprivacy{
Addr: &addr,
PrivacyKeyPair: privacy,
}
privacyInfo = append(privacyInfo, *priInfo)
}
}
} else {
privacyInfo, _ = policy.getPrivacyKeyPairs()
}
policy.store.selectPrivacyTransactionToWallet(TxDetails, privacyInfo)
}
func (policy *privacyPolicy) showPrivacyAccountsSpend(req *privacytypes.ReqPrivBal4AddrToken) (*privacytypes.UTXOHaveTxHashs, error) {
if ok, err := policy.isRescanUtxosFlagScaning(); ok {
return nil, err
}
utxoHaveTxHashs, err := policy.store.listSpendUTXOs(req.GetAssetExec(), req.GetToken(), req.GetAddr())
if err != nil {
return nil, err
}
return utxoHaveTxHashs, nil
}
func (policy *privacyPolicy) signatureTx(tx *types.Transaction, privacyInput *privacytypes.PrivacyInput, utxosInKeyInput []*privacytypes.UTXOBasics, realkeyInputSlice []*privacytypes.RealKeyInput) (err error) {
tx.Signature = nil
data := types.Encode(tx)
ringSign := &types.RingSignature{}
ringSign.Items = make([]*types.RingSignatureItem, len(privacyInput.Keyinput))
for i, input := range privacyInput.Keyinput {
utxos := utxosInKeyInput[i]
h := common.BytesToHash(data)
item, err := privacy.GenerateRingSignature(h.Bytes(),
utxos.Utxos,
realkeyInputSlice[i].Onetimeprivkey,
int(realkeyInputSlice[i].Realinputkey),
input.KeyImage)
if err != nil {
return err
}
ringSign.Items[i] = item
}
cfg := policy.getWalletOperate().GetAPI().GetConfig()
ringSignData := types.Encode(ringSign)
tx.Signature = &types.Signature{
Ty: privacytypes.RingBaseonED25519,
Signature: ringSignData,
// 这里填的是隐私合约的公钥,让框架保持一致
Pubkey: address.ExecPubKey(cfg.ExecName(privacytypes.PrivacyX)),
}
return nil
}
func (policy *privacyPolicy) buildAndStoreWalletTxDetail(param *buildStoreWalletTxDetailParam) {
txInfo := param.txInfo
heightstr := dapp.HeightIndexStr(txInfo.blockHeight, int64(txInfo.txIndex))
bizlog.Debug("buildAndStoreWalletTxDetail", "heightstr", heightstr, "isRollBack", txInfo.isRollBack)
if !txInfo.isRollBack {
var txdetail types.WalletTxDetail
key := calcTxKey(heightstr)
txdetail.Tx = txInfo.tx
txdetail.Height = txInfo.blockHeight
txdetail.Index = int64(txInfo.txIndex)
txdetail.Receipt = txInfo.blockDetail.Receipts[txInfo.txIndex]
txdetail.Blocktime = txInfo.blockDetail.Block.BlockTime
txdetail.ActionName = txInfo.actionName
txdetail.Amount, _ = txInfo.tx.Amount()
txdetail.Fromaddr = param.addr
txdetailbyte := types.Encode(&txdetail)
txInfo.batch.Set(key, txdetailbyte)
//额外存储可以快速定位到接收隐私的交易
if sendTx == param.sendRecvFlag {
txInfo.batch.Set(calcSendPrivacyTxKey(txInfo.assetExec, txInfo.assetSymbol, param.addr, heightstr), key)
} else if recvTx == param.sendRecvFlag {
txInfo.batch.Set(calcRecvPrivacyTxKey(txInfo.assetExec, txInfo.assetSymbol, param.addr, heightstr), key)
}
} else {
txInfo.batch.Delete(calcTxKey(heightstr))
if sendTx == param.sendRecvFlag {
txInfo.batch.Delete(calcSendPrivacyTxKey(txInfo.assetExec, txInfo.assetSymbol, param.addr, heightstr))
} else if recvTx == param.sendRecvFlag {
txInfo.batch.Delete(calcRecvPrivacyTxKey(txInfo.assetExec, txInfo.assetSymbol, param.addr, heightstr))