forked from etotheipi/BitcoinArmory
-
Notifications
You must be signed in to change notification settings - Fork 11
/
armorymodels.py
executable file
·1169 lines (989 loc) · 44.8 KB
/
armorymodels.py
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 (C) 2011-2012, Alan C. Reiner <alan.reiner@gmail.com>
# Distributed under the GNU Affero General Public License (AGPL v3)
# See LICENSE or http://www.gnu.org/licenses/agpl.html
#
################################################################################
import os
import platform
import sys
from os import path
from PyQt4.QtCore import *
from PyQt4.QtGui import *
sys.path.append('..')
sys.path.append('../cppForSwig')
from armoryengine import *
from CppBlockUtils import *
from qtdefines import *
from armorycolors import Colors, htmlColor
WLTVIEWCOLS = enum('ID', 'Name', 'Secure', 'Bal')
LEDGERCOLS = enum('NumConf', 'UnixTime', 'DateStr', 'TxDir', 'WltName', 'Comment', \
'Amount', 'isOther', 'WltID', 'TxHash', 'isCoinbase', 'toSelf', 'DoubleSpend')
ADDRESSCOLS = enum('Address', 'Comment', 'NumTx', 'Imported', 'Balance')
ADDRBOOKCOLS = enum('Address', 'WltID', 'NumSent', 'Comment')
TXINCOLS = enum('WltID', 'Sender', 'Btc', 'OutPt', 'OutIdx', 'FromBlk', \
'ScrType', 'Sequence', 'Script')
TXOUTCOLS = enum('WltID', 'Recip', 'Btc', 'ScrType', 'Script')
class AllWalletsDispModel(QAbstractTableModel):
# The columns enumeration
def __init__(self, mainWindow):
super(AllWalletsDispModel, self).__init__()
self.main = mainWindow
def rowCount(self, index=QModelIndex()):
return len(self.main.walletMap)
def columnCount(self, index=QModelIndex()):
return 4
def data(self, index, role=Qt.DisplayRole):
COL = WLTVIEWCOLS
row,col = index.row(), index.column()
wlt = self.main.walletMap[self.main.walletIDList[row]]
wltID = wlt.uniqueIDB58
if role==Qt.DisplayRole:
if col==COL.ID:
return QVariant(wltID)
if col==COL.Name:
return QVariant(wlt.labelName.ljust(32))
if col==COL.Secure:
wtype,typestr = determineWalletType(wlt, self.main)
return QVariant(typestr)
if col==COL.Bal:
bal = wlt.getBalance('Total')
if bal==-1:
return QVariant('(...)')
else:
dispStr = coin2strX(wlt.color, bal, maxZeros=2)
return QVariant(dispStr)
elif role==Qt.TextAlignmentRole:
if col in (COL.ID, COL.Name):
return QVariant(int(Qt.AlignLeft | Qt.AlignVCenter))
elif col in (COL.Secure,):
return QVariant(int(Qt.AlignHCenter | Qt.AlignVCenter))
elif col in (COL.Bal,):
return QVariant(int(Qt.AlignLeft | Qt.AlignVCenter))
elif role==Qt.BackgroundColorRole:
t = determineWalletType(wlt, self.main)[0]
if t==WLTTYPES.WatchOnly:
return QVariant( Colors.TblWltOther )
elif t==WLTTYPES.Offline:
return QVariant( Colors.TblWltOffline )
else:
return QVariant( Colors.TblWltMine )
elif role==Qt.FontRole:
if col==COL.Bal:
return GETFONT('Fixed')
return QVariant()
def headerData(self, section, orientation, role=Qt.DisplayRole):
colLabels = ['ID', 'Name', 'Security', 'Balance']
if role==Qt.DisplayRole:
if orientation==Qt.Horizontal:
return QVariant( colLabels[section])
elif role==Qt.TextAlignmentRole:
return QVariant( int(Qt.AlignHCenter | Qt.AlignVCenter) )
# This might work for checkbox-in-tableview
#QStandardItemModel* tableModel = new QStandardItemModel();
#// create text item
#tableModel->setItem(0, 0, new QStandardItem("text item"));
#// create check box item
#QStandardItem* item0 = new QStandardItem(true);
#item0->setCheckable(true);
#item0->setCheckState(Qt::Checked);
#item0->setText("some text");
#tableModel->setItem(0, 1, item0);
#// set model
#ui->tableView->setModel(tableModel);
# Perhaps delegate for rich text in QTableViews
#void SpinBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
#{
#QTextDocument document;
#QVariant value = index.data(Qt::DisplayRole);
#if (value.isValid() && !value.isNull()) {
#QString text("<span style='background-color: lightgreen'>This</span> is highlighted.");
#text.append(" (");
#text.append(value.toString());
#text.append(")");
#document.setHtml(text);
#painter->translate(option.rect.topLeft());
#document.drawContents(painter);
#painter->translate(-option.rect.topLeft());
#}
################################################################################
class LedgerDispModelSimple(QAbstractTableModel):
""" Displays an Nx10 table of pre-formatted/processed ledger entries """
def __init__(self, ledgerTable, parent=None, main=None):
super(LedgerDispModelSimple, self).__init__()
self.parent = parent
self.main = main
self.ledger = ledgerTable
def rowCount(self, index=QModelIndex()):
return len(self.ledger)
def columnCount(self, index=QModelIndex()):
return 13
def data(self, index, role=Qt.DisplayRole):
COL = LEDGERCOLS
row,col = index.row(), index.column()
rowData = self.ledger[row]
nConf = rowData[LEDGERCOLS.NumConf]
wltID = rowData[LEDGERCOLS.WltID]
wtype = determineWalletType(self.main.walletMap[wltID], self.main)[0]
#LEDGERCOLS = enum('NumConf', 'UnixTime','DateStr', 'TxDir', 'WltName', 'Comment', \
#'Amount', 'isOther', 'WltID', 'TxHash', 'isCoinbase', 'toSelf', 'DoubleSpend')
if role==Qt.DisplayRole:
return QVariant(rowData[col])
elif role==Qt.TextAlignmentRole:
if col in (COL.NumConf, COL.TxDir):
return QVariant(int(Qt.AlignHCenter | Qt.AlignVCenter))
elif col in (COL.Comment, COL.DateStr):
return QVariant(int(Qt.AlignLeft | Qt.AlignVCenter))
elif col in (COL.Amount,):
return QVariant(int(Qt.AlignRight | Qt.AlignVCenter))
elif role==Qt.DecorationRole:
pass
elif role==Qt.BackgroundColorRole:
if wtype==WLTTYPES.WatchOnly:
return QVariant( Colors.TblWltOther )
elif wtype==WLTTYPES.Offline:
return QVariant( Colors.TblWltOffline )
else:
return QVariant( Colors.TblWltMine )
elif role==Qt.ForegroundRole:
#if self.index(index.row(),COL.DoubleSpend).data().toBool():
if rowData[COL.DoubleSpend]:
return QVariant(Colors.TextRed)
if nConf <= 2:
return QVariant(Colors.TextNoConfirm)
elif nConf <= 4:
return QVariant(Colors.TextSomeConfirm)
if col==COL.Amount:
#toSelf = self.index(index.row(), COL.toSelf).data().toBool()
toSelf = rowData[COL.toSelf]
if toSelf:
return QVariant(Colors.Mid)
amt = float(rowData[COL.Amount])
if amt>0: return QVariant(Colors.TextGreen)
elif amt<0: return QVariant(Colors.TextRed)
else: return QVariant(Colors.Foreground)
elif role==Qt.FontRole:
if col==COL.Amount:
f = GETFONT('Fixed')
f.setWeight(QFont.Bold)
return f
elif role==Qt.ToolTipRole:
if col in (COL.NumConf, COL.DateStr):
nConf = rowData[COL.NumConf]
isCB = rowData[COL.isCoinbase]
isConfirmed = (nConf>119 if isCB else nConf>5)
if isConfirmed:
return QVariant('Transaction confirmed!\n(%d confirmations)'%nConf)
else:
tooltipStr = ''
if isCB:
tooltipStr = '%d/120 confirmations'%nConf
tooltipStr += ( '\n\nThis is a "generation" transaction from\n'
'Bitcoin mining. These transactions take\n'
'120 confirmations (approximately one day)\n'
'before they are available to be spent.')
else:
tooltipStr = '%d/6 confirmations'%rowData[COL.NumConf]
tooltipStr += ( '\n\nFor small transactions, 2 or 3\n'
'confirmations is usually acceptable.\n'
'For larger transactions, you should\n'
'wait for 6 confirmations before\n'
'trusting that the transaction valid.')
return QVariant(tooltipStr)
if col==COL.TxDir:
#toSelf = self.index(index.row(), COL.toSelf).data().toBool()
toSelf = rowData[COL.toSelf]
if toSelf:
return QVariant('Bitcoins sent and received by the same wallet')
else:
#txdir = str(index.model().data(index).toString()).strip()
txdir = rowData[COL.TxDir]
if rowData[COL.isCoinbase]:
return QVariant('You mined these Bitcoins!')
if txdir[0].startswith('-'):
return QVariant('Bitcoins sent')
else:
return QVariant('Bitcoins received')
if col==COL.Amount:
if self.main.settings.get('DispRmFee'):
return QVariant('The net effect on the balance of this wallet '
'<b>not including transaction fees.</b> '
'You can change this behavior in the Armory '
'preferences window.')
else:
return QVariant('The net effect on the balance of this wallet, '
'including transaction fees.')
return QVariant()
def headerData(self, section, orientation, role=Qt.DisplayRole):
COL = LEDGERCOLS
if role==Qt.DisplayRole:
if orientation==Qt.Horizontal:
if section==COL.NumConf: return QVariant()
if section==COL.DateStr: return QVariant('Date')
if section==COL.WltName: return QVariant('Wallet')
if section==COL.Comment: return QVariant('Comments')
if section==COL.TxDir: return QVariant()
if section==COL.Amount: return QVariant('Amount')
if section==COL.isOther: return QVariant('Other Owner')
if section==COL.WltID: return QVariant('Wallet ID')
if section==COL.TxHash: return QVariant('Tx Hash (LE)')
elif role==Qt.TextAlignmentRole:
return QVariant( int(Qt.AlignHCenter | Qt.AlignVCenter) )
################################################################################
class LedgerDispSortProxy(QSortFilterProxyModel):
"""
Acts as a proxy that re-maps indices to the table view so that data
appears sorted, without actually touching the model
"""
def lessThan(self, idxLeft, idxRight):
COL = LEDGERCOLS
thisCol = self.sortColumn()
def getDouble(idx, col):
return float(self.sourceModel().ledger[idx.row()][col])
def getInt(idx, col):
return int(self.sourceModel().ledger[idx.row()][col])
#LEDGERCOLS = enum('NumConf', 'UnixTime', 'DateStr', 'TxDir', 'WltName', 'Comment', \
#'Amount', 'isOther', 'WltID', 'TxHash', 'toSelf', 'DoubleSpend')
if thisCol==COL.NumConf:
lConf = getInt(idxLeft, COL.NumConf)
rConf = getInt(idxRight, COL.NumConf)
if lConf==rConf:
tLeft = getDouble(idxLeft, COL.UnixTime)
tRight = getDouble(idxRight, COL.UnixTime)
return (tLeft<tRight)
return (lConf>rConf)
if thisCol==COL.DateStr:
tLeft = getDouble(idxLeft, COL.UnixTime)
tRight = getDouble(idxRight, COL.UnixTime)
return (tLeft<tRight)
if thisCol==COL.Amount:
btcLeft = getDouble(idxLeft, COL.Amount)
btcRight = getDouble(idxRight, COL.Amount)
return (abs(btcLeft) < abs(btcRight))
else:
return super(LedgerDispSortProxy, self).lessThan(idxLeft, idxRight)
################################################################################
class LedgerDispDelegate(QStyledItemDelegate):
COL = LEDGERCOLS
def __init__(self, parent=None):
super(LedgerDispDelegate, self).__init__(parent)
def paint(self, painter, option, index):
bgcolor = QColor(index.model().data(index, Qt.BackgroundColorRole))
if option.state & QStyle.State_Selected:
bgcolor = QApplication.palette().highlight().color()
#bgcolor = Colors.Background
#if option.state & QStyle.State_Selected:
#bgcolor = Colors.Highlight
if index.column() == self.COL.NumConf:
nConf = index.model().data(index).toInt()[0]
isCoinbase = index.model().index(index.row(), self.COL.isCoinbase).data().toBool()
image=None
if isCoinbase:
if nConf<120:
effectiveNConf = int(6*float(nConf)/120.)
image = QImage(':/conf%dt_nonum.png'%effectiveNConf)
else:
image = QImage(':/conf6t.png')
else:
if nConf<6:
image = QImage(':/conf%dt.png'%nConf)
else:
image = QImage(':/conf6t.png')
painter.fillRect(option.rect, bgcolor)
pixmap = QPixmap.fromImage(image)
#pixmap.scaled(70, 30, Qt.KeepAspectRatio)
painter.drawPixmap(option.rect, pixmap)
elif index.column() == self.COL.TxDir:
# This is frustrating... QVariant doesn't support 64-bit ints
# So I have to pass the amt as string, then convert here to long
toSelf = index.model().index(index.row(), self.COL.toSelf).data().toBool()
isCoinbase = index.model().index(index.row(), self.COL.isCoinbase).data().toBool()
image = QImage()
# isCoinbase still needs to be flagged in the C++ utils
if isCoinbase:
image = QImage(':/moneyCoinbase.png')
elif toSelf:
image = QImage(':/moneySelf.png')
else:
txdir = str(index.model().data(index).toString()).strip()
if txdir[0].startswith('-'):
image = QImage(':/moneyOut.png')
else:
image = QImage(':/moneyIn.png')
painter.fillRect(option.rect, bgcolor)
pixmap = QPixmap.fromImage(image)
#pixmap.scaled(70, 30, Qt.KeepAspectRatio)
painter.drawPixmap(option.rect, pixmap)
else:
QStyledItemDelegate.paint(self, painter, option, index)
def sizeHint(self, option, index):
if index.column()==self.COL.NumConf:
return QSize(28,28)
elif index.column()==self.COL.TxDir:
return QSize(28,28)
return QStyledItemDelegate.sizeHint(self, option, index)
################################################################################
class WalletAddrDispModel(QAbstractTableModel):
def __init__(self, wlt, mainWindow):
super(WalletAddrDispModel, self).__init__()
self.main = mainWindow
self.wlt = wlt
self.addr160List = [a.getAddr160() for a in self.wlt.getLinearAddrList()]
def reset(self):
self.addr160List = [a.getAddr160() for a in self.wlt.getLinearAddrList()]
QAbstractTableModel.reset(self)
def rowCount(self, index=QModelIndex()):
return len(self.addr160List)
def columnCount(self, index=QModelIndex()):
return 5
def data(self, index, role=Qt.DisplayRole):
COL = ADDRESSCOLS
row,col = index.row(), index.column()
if row>=len(self.addr160List):
return QVariant('')
addr = self.wlt.addrMap[self.addr160List[row]]
addr160 = addr.getAddr160()
addrB58 = addr.getAddrStr()
if role==Qt.DisplayRole:
if col==COL.Address:
return QVariant( addrB58 )
if col==COL.Comment:
if addr160 in self.wlt.commentsMap:
return QVariant( self.wlt.commentsMap[addr160] )
else:
return QVariant('')
if col==COL.NumTx:
ledg = self.wlt.getAddrTxLedger(addr160, 'Full')
return QVariant( len(ledg) )
if col==COL.Imported:
if self.wlt.addrMap[addr160].chainIndex==-2:
return QVariant('Imported')
else:
return QVariant()
if col==COL.Balance:
val = self.wlt.getAddrBalance(addr160, 'Full')
return QVariant( coin2strX(self.wlt.color, val, maxZeros=2) )
elif role==Qt.TextAlignmentRole:
if col in (COL.Address, COL.Comment):
return QVariant(int(Qt.AlignLeft | Qt.AlignVCenter))
elif col in (COL.NumTx,COL.Imported):
return QVariant(int(Qt.AlignHCenter | Qt.AlignVCenter))
elif col in (COL.Balance,):
return QVariant(int(Qt.AlignRight | Qt.AlignVCenter))
elif role==Qt.ForegroundRole:
if col==COL.Balance:
val = self.wlt.getAddrBalance(addr160, 'Full')
if val>0: return QVariant(Colors.TextGreen)
else: return QVariant(Colors.Foreground)
elif role==Qt.FontRole:
hasTx = len(self.wlt.getAddrTxLedger(addr160, 'blk'))>0
cmt = str(self.index(index.row(),COL.Comment).data().toString())
isChange = (cmt==CHANGE_ADDR_DESCR_STRING)
if col==COL.Balance:
return GETFONT('Fixed',bold=hasTx)
else:
doBold = hasTx and not isChange
doItalic = isChange
return GETFONT('Var',bold=doBold, italic=doItalic)
elif role==Qt.ToolTipRole:
cmt = str(self.index(index.row(),COL.Comment).data().toString())
if cmt==CHANGE_ADDR_DESCR_STRING:
return QVariant('This address was created by Armory to '
'receive change-back-to-self from an oversized '
'transaction.')
elif role==Qt.BackgroundColorRole:
val = self.wlt.getAddrBalance(addr160, 'Full')
if val>0:
return QVariant( Colors.SlightGreen )
else:
return QVariant( Colors.TblWltOther )
return QVariant()
def headerData(self, section, orientation, role=Qt.DisplayRole):
COL = ADDRESSCOLS
if role==Qt.DisplayRole:
if orientation==Qt.Horizontal:
if section==COL.Address: return QVariant( 'Address' )
if section==COL.Comment: return QVariant( 'Comment' )
if section==COL.NumTx: return QVariant( '#Tx' )
if section==COL.Imported: return QVariant( '' )
if section==COL.Balance: return QVariant( 'Balance' )
elif role==Qt.TextAlignmentRole:
if section in (COL.Address, COL.Comment):
return QVariant(int(Qt.AlignLeft | Qt.AlignVCenter))
elif section in (COL.NumTx,):
return QVariant(int(Qt.AlignHCenter | Qt.AlignVCenter))
elif section in (COL.Balance,):
return QVariant(int(Qt.AlignRight | Qt.AlignVCenter))
return QVariant()
################################################################################
class WalletAddrSortProxy(QSortFilterProxyModel):
"""
Acts as a proxy that re-maps indices to the table view so that data
appears sorted, without actually touching the model
"""
def lessThan(self, idxLeft, idxRight):
COL = ADDRESSCOLS
thisCol = self.sortColumn()
strLeft = str(self.sourceModel().data(idxLeft).toString())
strRight = str(self.sourceModel().data(idxRight).toString())
if thisCol==COL.Address:
return (strLeft.lower() < strRight.lower())
elif thisCol==COL.Comment:
return (strLeft < strRight)
elif thisCol==COL.Balance:
return (float(strLeft.strip()) < float(strRight.strip()))
else:
return super(WalletAddrSortProxy, self).lessThan(idxLeft, idxRight)
################################################################################
class TxInDispModel(QAbstractTableModel):
def __init__(self, pytx, txinListFromBDM=None, main=None):
super(TxInDispModel, self).__init__()
self.main = main
self.txInList = []
self.dispTable = []
# If this is actually a TxDP in here, then let's use that
# We do this to make sure we have somewhere to put txdp-specific
# code, but we don't really need it yet, except to identify
# signed/unsigned in the table
pytxdp = None
if isinstance(pytx, PyTxDistProposal):
pytxdp = pytx
pytx = pytxdp.pytxObj.copy()
self.tx = pytx.copy()
for i,txin in enumerate(self.tx.inputs):
self.dispTable.append([])
wltID = ''
scrType = getTxInScriptType(txin)
if txinListFromBDM and len(txinListFromBDM[i][0])>0:
# We had a BDM to help us get info on each input -- use it
recip160,val,blk,hsh,idx = txinListFromBDM[i]
if main:
wltID = self.main.getWalletForAddr160(recip160)
dispcoin = '' if not val else coin2str(val,maxZeros=1)
self.dispTable[-1].append(wltID)
self.dispTable[-1].append(hash160_to_addrStr(recip160))
self.dispTable[-1].append(dispcoin)
self.dispTable[-1].append(binary_to_hex(hsh))
self.dispTable[-1].append(idx)
self.dispTable[-1].append(blk)
if pytxdp==None:
self.dispTable[-1].append(TXIN_TYPE_NAMES[scrType])
else:
# TODO: Assume NO multi-sig... will be updated in future to use
# PyTxDP::isSigValidForInput which will handle all cases
self.dispTable[-1].append('Signed' if pytxdp.signatures[i][0] else 'Unsigned')
self.dispTable[-1].append(int_to_hex(txin.intSeq, widthBytes=4))
self.dispTable[-1].append(binary_to_hex(txin.binScript))
else:
# We don't have any info from the BDM, display whatever we can
# (which usually isn't much)
recipAddr = '<Unknown>'
if scrType in (TXIN_SCRIPT_STANDARD,):
recipAddr = TxInScriptExtractAddr160IfAvail(txin)
if main:
wltID = self.main.getWalletForAddr160(recip)
self.dispTable[-1].append(wltID)
self.dispTable[-1].append(recipAddr)
self.dispTable[-1].append('<Unknown>')
self.dispTable[-1].append(binary_to_hex(txin.outpoint.txHash))
self.dispTable[-1].append(str(txin.outpoint.txOutIndex))
self.dispTable[-1].append('')
self.dispTable[-1].append(TXIN_TYPE_NAMES[scrType])
self.dispTable[-1].append(int_to_hex(txin.intSeq, widthBytes=4))
self.dispTable[-1].append(binary_to_hex(txin.binScript))
def rowCount(self, index=QModelIndex()):
return len(self.dispTable)
def columnCount(self, index=QModelIndex()):
return 9
#TXINCOLS = enum('WltID', 'Sender', 'Btc', 'OutPt', 'OutIdx', 'FromBlk', 'ScrType', 'Sequence', 'Script')
def data(self, index, role=Qt.DisplayRole):
COLS = TXINCOLS
row,col = index.row(), index.column()
wltID = self.dispTable[row][COLS.WltID]
if role==Qt.DisplayRole:
return QVariant(self.dispTable[row][col])
elif role==Qt.TextAlignmentRole:
if col in (COLS.WltID, COLS.Sender, COLS.OutPt):
return QVariant(int(Qt.AlignLeft | Qt.AlignVCenter))
elif col in (COLS.OutIdx, COLS.FromBlk, COLS.Sequence, COLS.ScrType):
return QVariant(int(Qt.AlignHCenter | Qt.AlignVCenter))
elif col in (COLS.Btc,):
return QVariant(int(Qt.AlignRight | Qt.AlignVCenter))
elif role==Qt.BackgroundColorRole:
if self.dispTable[row][COLS.WltID]:
wtype = determineWalletType(self.main.walletMap[wltID], self.main)[0]
if wtype==WLTTYPES.WatchOnly:
return QVariant( Colors.TblWltOther )
elif wtype==WLTTYPES.Offline:
return QVariant( Colors.TblWltOffline )
else:
return QVariant( Colors.TblWltMine )
elif role==Qt.FontRole:
if col==COLS.Btc:
return GETFONT('Fixed')
return QVariant()
def headerData(self, section, orientation, role=Qt.DisplayRole):
COLS = TXINCOLS
if role==Qt.DisplayRole:
if orientation==Qt.Horizontal:
if section==COLS.WltID: return QVariant('Wallet ID')
if section==COLS.Sender: return QVariant('Sender')
if section==COLS.Btc: return QVariant('Amount')
if section==COLS.OutPt: return QVariant('Prev. Tx Hash')
if section==COLS.OutIdx: return QVariant('Index')
if section==COLS.FromBlk: return QVariant('From Block#')
if section==COLS.ScrType: return QVariant('Script Type')
if section==COLS.Sequence: return QVariant('Sequence')
if section==COLS.Script: return QVariant('Script')
elif role==Qt.TextAlignmentRole:
if orientation==Qt.Horizontal:
if section in (COLS.WltID, COLS.Sender, COLS.OutPt):
return QVariant(int(Qt.AlignLeft | Qt.AlignVCenter))
elif section in (COLS.OutIdx, COLS.FromBlk, COLS.Btc, COLS.Sequence, COLS.ScrType):
return QVariant(int(Qt.AlignHCenter | Qt.AlignVCenter))
return QVariant(int(Qt.AlignHCenter | Qt.AlignVCenter))
################################################################################
class TxOutDispModel(QAbstractTableModel):
def __init__(self, pytx, main=None, idxGray=[]):
super(TxOutDispModel, self).__init__()
self.tx = pytx.copy()
self.main = main
self.txOutList = []
self.wltIDList = []
self.idxGray = idxGray
for i,txout in enumerate(self.tx.outputs):
recip160 = TxOutScriptExtractAddr160(txout.binScript)
self.txOutList.append(txout)
if main:
self.wltIDList.append(main.getWalletForAddr160(recip160))
else:
self.wltIDList.append('')
def rowCount(self, index=QModelIndex()):
return len(self.txOutList)
def columnCount(self, index=QModelIndex()):
return 5
#TXOUTCOLS = enum('WltID', 'Recip', 'Btc', 'ScrType')
def data(self, index, role=Qt.DisplayRole):
COLS = TXOUTCOLS
row,col = index.row(), index.column()
txout = self.txOutList[row]
stype = getTxOutScriptType(txout.binScript)
stypeStr = TXOUT_TYPE_NAMES[stype]
wltID = self.wltIDList[row]
if stype==TXOUT_SCRIPT_MULTISIG:
mstype = getTxOutMultiSigInfo(txout.binScript)[0]
stypeStr = 'Multi-Signature (%d-of-%d)' % mstype
if role==Qt.DisplayRole:
if col==COLS.WltID: return QVariant(wltID)
if col==COLS.ScrType: return QVariant(stypeStr)
if col==COLS.Script: return QVariant(binary_to_hex(txout.binScript))
if stype==TXOUT_SCRIPT_STANDARD:
if col==COLS.Recip: return QVariant(TxOutScriptExtractAddrStr(txout.binScript))
if col==COLS.Btc: return QVariant(coin2str(txout.getValue(),maxZeros=2))
if stype==TXOUT_SCRIPT_COINBASE:
if col==COLS.Recip: return QVariant(TxOutScriptExtractAddrStr(txout.binScript))
if col==COLS.Btc: return QVariant(coin2str(txout.getValue(),maxZeros=2))
if stype==TXOUT_SCRIPT_MULTISIG:
if col==COLS.Recip: return QVariant('[[Multiple]]')
if col==COLS.Btc: return QVariant(coin2str(txout.getValue(),maxZeros=2))
if stype==TXOUT_SCRIPT_UNKNOWN:
if col==COLS.Recip: return QVariant('[[Non-Standard]]')
if col==COLS.Btc: return QVariant(coin2str(txout.getValue(),maxZeros=2))
if stype==TXOUT_SCRIPT_OP_EVAL:
if col==COLS.Recip: return QVariant('[[OP-EVAL]]')
if col==COLS.Btc: return QVariant(coin2str(txout.getValue(),maxZeros=2))
elif role==Qt.TextAlignmentRole:
if col==COLS.Recip: return QVariant(int(Qt.AlignLeft | Qt.AlignVCenter))
if col==COLS.Btc: return QVariant(int(Qt.AlignRight | Qt.AlignVCenter))
if col==COLS.ScrType: return QVariant(int(Qt.AlignHCenter | Qt.AlignVCenter))
elif role==Qt.ForegroundRole:
if row in self.idxGray:
return QVariant(Colors.Mid)
elif role==Qt.BackgroundColorRole:
if wltID:
wtype = determineWalletType(self.main.walletMap[wltID], self.main)[0]
if wtype==WLTTYPES.WatchOnly:
return QVariant( Colors.TblWltOther )
if wtype==WLTTYPES.Offline:
return QVariant( Colors.TblWltOffline )
else:
return QVariant( Colors.TblWltMine )
elif role==Qt.FontRole:
if col==COLS.Btc:
return GETFONT('Fixed')
return QVariant()
def headerData(self, section, orientation, role=Qt.DisplayRole):
COLS = TXOUTCOLS
if role==Qt.DisplayRole:
if orientation==Qt.Horizontal:
if section==COLS.WltID: return QVariant('Wallet ID')
if section==COLS.Recip: return QVariant('Recipient')
if section==COLS.Btc: return QVariant('Amount')
if section==COLS.ScrType: return QVariant('Script Type')
elif role==Qt.TextAlignmentRole:
if orientation==Qt.Horizontal:
if section==COLS.WltID: return QVariant(Qt.AlignLeft | Qt.AlignVCenter)
if section==COLS.Recip: return QVariant(Qt.AlignLeft | Qt.AlignVCenter)
if section==COLS.Btc: return QVariant(Qt.AlignHCenter | Qt.AlignVCenter)
if section==COLS.ScrType: return QVariant(Qt.AlignHCenter | Qt.AlignVCenter)
return QVariant(int(Qt.AlignHCenter | Qt.AlignVCenter))
################################################################################
class SentToAddrBookModel(QAbstractTableModel):
def __init__(self, wltID, main):
super(SentToAddrBookModel, self).__init__()
self.wltID = wltID
self.main = main
self.wlt = self.main.walletMap[wltID]
# Get a vector of "AddressBookEntry" objects sorted by first-sent-to
self.addrBook = self.wlt.cppWallet.createAddressBook()
# Delete entries that are our own addr in other wallets
otherAddr = []
for abe in self.addrBook:
if not self.main.getWalletForAddr160(abe.getAddr160()):
otherAddr.append(abe)
self.addrBook = otherAddr
def rowCount(self, index=QModelIndex()):
return len(self.addrBook)
def columnCount(self, index=QModelIndex()):
return 4
def data(self, index, role=Qt.DisplayRole):
COL = ADDRBOOKCOLS
row,col = index.row(), index.column()
abe = self.addrBook[row]
addr160 = abe.getAddr160()
addrB58 = hash160_to_addrStr(addr160)
wltID = self.main.getWalletForAddr160(addr160)
numSent = len(abe.getTxList())
comment = self.wlt.getCommentForAddrBookEntry(abe)
#ADDRBOOKCOLS = enum('Address', 'WltID', 'NumSent', 'Comment')
if role==Qt.DisplayRole:
if col==COL.Address:
return QVariant( addrB58 )
if col==COL.NumSent:
return QVariant( numSent )
if col==COL.Comment:
return QVariant( comment )
if col==COL.WltID:
return QVariant( wltID )
elif role==Qt.TextAlignmentRole:
if col in (COL.Address, COL.Comment, COL.WltID):
return QVariant(int(Qt.AlignLeft | Qt.AlignVCenter))
elif col in (COL.NumSent,):
return QVariant(int(Qt.AlignHCenter | Qt.AlignVCenter))
elif role==Qt.FontRole:
isFreqAddr = (numSent>1)
return GETFONT('Var', bold=isFreqAddr)
return QVariant()
def headerData(self, section, orientation, role=Qt.DisplayRole):
COL = ADDRBOOKCOLS
if role==Qt.DisplayRole:
if orientation==Qt.Horizontal:
if section==COL.Address: return QVariant( 'Address' )
if section==COL.WltID: return QVariant( 'Ownership' )
if section==COL.NumSent: return QVariant( 'Times Used' )
if section==COL.Comment: return QVariant( 'Comment' )
elif role==Qt.TextAlignmentRole:
if section in (COL.Address, COL.Comment, COL.WltID):
return QVariant(int(Qt.AlignLeft | Qt.AlignVCenter))
elif section in (COL.NumSent,):
return QVariant(int(Qt.AlignHCenter | Qt.AlignVCenter))
return QVariant()
################################################################################
class SentAddrSortProxy(QSortFilterProxyModel):
def lessThan(self, idxLeft, idxRight):
COL = ADDRBOOKCOLS
thisCol = self.sortColumn()
strLeft = str(self.sourceModel().data(idxLeft).toString())
strRight = str(self.sourceModel().data(idxRight).toString())
#ADDRBOOKCOLS = enum('Address', 'WltID', 'NumSent', 'Comment')
if thisCol==COL.Address:
return (strLeft.lower() < strRight.lower())
else:
return super(SentAddrSortProxy, self).lessThan(idxLeft, idxRight)
"""
class HeaderDataModel(QAbstractTableModel):
def __init__(self):
super(HeaderDataModel, self).__init__()
self.bdm = BlockDataManager().getBDM()
def rowCount(self, index=QModelIndex()):
def columnCount(self, index=QModelIndex()):
def data(self, index, role=Qt.DisplayRole):
nHead = self.rowCount()
row,col = index.row(), index.column()
if role==Qt.DisplayRole:
if col== HEAD_DATE: return QVariant(someStr)
elif role==Qt.TextAlignmentRole:
if col in (HEAD_BLKNUM, HEAD_DIFF, HEAD_NUMTX, HEAD_BTC):
return QVariant(int(Qt.AlignRight | Qt.AlignVCenter))
else:
return QVariant(int(Qt.AlignLeft | Qt.AlignVCenter))
elif role==Qt.BackgroundColorRole:
return QVariant( QColor(235,235,255) )
return QVariant()
def headerData(self, section, orientation, role=Qt.DisplayRole):
if role==Qt.DisplayRole:
if orientation==Qt.Horizontal:
return QVariant( headColLabels[section] )
elif role==Qt.TextAlignmentRole:
return QVariant( int(Qt.AlignHCenter | Qt.AlignVCenter) )
################################################################################
# Below is PyBtcEngine testing code -- A blockchain explorer may be a nice
# feature in the future, but not right now -- IGNORE THIS SECTION
################################################################################
HEAD_BLKNUM, HEAD_HASH, HEAD_DIFF, HEAD_NUMTX, HEAD_BTC, HEAD_DATE = range(6)
TX_INDEX, TX_HASH, TX_BTC, TX_SRC, TX_RECIP, TX_SIZE = range(6)
TXIN_SRC, TXIN_BTC, TXIN_SBLK, TXIN_STYPE, TXIN_SEQ, = range(5)
TXOUT_RECIP, TXOUT_BTC, TXOUT_STYPE, = range(3)
headColLabels = {HEAD_BLKNUM: 'Block#', \
HEAD_HASH: 'Hash', \
HEAD_DIFF: 'Difficulty', \
HEAD_NUMTX: '#Tx', \
HEAD_BTC: 'Total BTC', \
HEAD_DATE: 'Date & Time'}
txColLabels = {TX_INDEX: 'Index', \
TX_HASH: 'Hash', \
TX_BTC: 'BTC', \
TX_SRC: 'Source', \
TX_RECIP: 'Recipient', \
TX_SIZE: 'Size' }
txinColLabels = {TXIN_SRC: 'Sender', \
TXIN_BTC: 'BTC', \
TXIN_SBLK: 'From#', \
TXIN_STYPE: 'Script Type'}
#TXIN_SEQ: 'Sequence'}
txoutColLabels ={TXOUT_RECIP: 'Recipient',\
TXOUT_BTC: 'BTC', \
TXOUT_STYPE: 'Script Type'}
b2h = lambda x: binary_to_hex(x)
h2b = lambda x: hex_to_binary(x)
class HeaderDataModel(QAbstractTableModel):
def __init__(self):
super(HeaderDataModel, self).__init__()
self.bdm = BlockDataManager().getBDM()
self.headerData = []
self.endianSelect = BIGENDIAN
def rowCount(self, index=QModelIndex()):
return self.bdm.getTopBlockHeader().getBlockHeight()+1
def columnCount(self, index=QModelIndex()):
return len(headColLabels)
def sumBtcInHeader(self, header):
txlist = header.getTxRefPtrList()
total = 0
for tx in txlist:
txcopy = tx.getTxCopy()
total += txcopy.getSumOfOutputs()
return total
def data(self, index, role=Qt.DisplayRole):
nHead = self.rowCount()
row,col = index.row(), index.column()
if not index.isValid() or not (0 <= row < nHead):
return QVariant()
if role==Qt.DisplayRole:
h = nHead-row-1
cppHeader = self.bdm.getHeaderByHeight(h)
if cppHeader == None or not h>0:
return QVariant()
if col == HEAD_BLKNUM:
return QVariant(cppHeader.getBlockHeight())
elif col== HEAD_HASH:
return QVariant(binary_to_hex(cppHeader.getThisHash(), self.endianSelect))
elif col== HEAD_DIFF:
return QVariant("%0.2f" % cppHeader.getDifficulty())
elif col== HEAD_NUMTX:
return QVariant("%d " % cppHeader.getNumTx())
elif col== HEAD_BTC:
return QVariant("%0.4f" % float(self.sumBtcInHeader(cppHeader)/1e8))
elif col== HEAD_DATE:
return QVariant(unixTimeToFormatStr(cppHeader.getTimestamp()))
elif role==Qt.TextAlignmentRole:
if col in (HEAD_BLKNUM, HEAD_DIFF, HEAD_NUMTX, HEAD_BTC):
return QVariant(int(Qt.AlignRight | Qt.AlignVCenter))
else:
return QVariant(int(Qt.AlignLeft | Qt.AlignVCenter))
elif role==Qt.BackgroundColorRole:
return QVariant( QColor(235,235,255) )
return QVariant()
def headerData(self, section, orientation, role=Qt.DisplayRole):
if role==Qt.TextAlignmentRole:
return QVariant( int(Qt.AlignHCenter | Qt.AlignVCenter) )
if role==Qt.DisplayRole:
if self.endianSelect==BIGENDIAN:
headColLabels[HEAD_HASH] = 'Hash (BE)'
else:
headColLabels[HEAD_HASH] = 'Hash (LE)'
if orientation==Qt.Horizontal:
return QVariant( headColLabels[section] )
#def getInfoDict(self, header):
class TxDataModel(QAbstractTableModel):
def __init__(self):
super(TxDataModel, self).__init__()
self.bdm = BlockDataManager().getBDM()
self.endianSelect = BIGENDIAN
self.txHashList = None
def rowCount(self, index=QModelIndex()):
if self.txHashList:
return len(self.txHashList)
return 0
def columnCount(self, index=QModelIndex()):
return len(txColLabels)
def getTxSrcStr(self, tx):
if tx.getNumTxIn() > 1:
return '<%d sources>' % tx.getNumTxIn()
else:
txin = tx.getTxIn(0)
if txin.isCoinbase():
return '<COINBASE>'
else:
return hash160_to_addrStr(self.bdm.getSenderAddr20(txin))
def getTxDstStr(self, tx):
if tx.getNumTxOut() > 1:
return '<%d recipients>' % tx.getNumTxOut()
else:
return hash160_to_addrStr(tx.getTxOut(0).getRecipientAddr())
def data(self, index, role=Qt.DisplayRole):
nTx = self.rowCount()
row,col = index.row(), index.column()
if not index.isValid() or not (0 <= row < nTx):
return QVariant()
if role==Qt.DisplayRole:
cppTx = self.bdm.getTxByHash(self.txHashList[row])
if cppTx == None:
return QVariant()