Repository navigation
Expand file tree
/
Copy pathZenWallet.java
More file actions
157 lines (136 loc) · 4.76 KB
/
Copy pathZenWallet.java
File metadata and controls
157 lines (136 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package org.tron.core.zen;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.tron.core.zen.address.IncomingViewingKey;
import org.tron.core.zen.address.PaymentAddress;
import org.tron.core.zen.merkle.IncrementalMerkleVoucherContainer;
import org.tron.core.zen.note.BaseNote.Note;
import org.tron.core.zen.note.BaseNotePlaintext.NotePlaintext;
import org.tron.core.zen.note.NoteData;
import org.tron.core.zen.note.NoteEntry;
import org.tron.core.zen.transaction.BaseOutPoint.OutPoint;
import org.tron.core.zen.walletdb.CKeyMetadata;
import org.tron.core.zen.zip32.ExtendedSpendingKey;
import org.tron.protos.Contract.ReceiveDescription;
public class ZenWallet {
public static Map<IncomingViewingKey, CKeyMetadata> mapSaplingZKeyMetadata = Maps.newHashMap();
public static Map<OutPoint, NoteData> mapNoteData = Maps.newHashMap();
public static Lock csWallet = new ReentrantLock();
public static void getNoteVouchers(List<OutPoint> ops,
List<Optional<IncrementalMerkleVoucherContainer>> voucher, byte[] anchor) {
}
public static Optional<ExtendedSpendingKey> getSpendingKeyForPaymentAddress(
PaymentAddress zaddr) {
return KeyStore.getExtendedSpendingKey(zaddr);
}
//
// public static int ScanForWalletTransactions(CBlockIndex*pindexStart, bool fUpdate) {
//
// BOOST_FOREACH(CTransaction & tx, block.vtx)
// {
// if (AddToWalletIfInvolvingMe(tx, & block,fUpdate)){
// myTxHashes.push_back(tx.GetHash());
// ret++;
// }
// }
// }
//
// public static bool AddToWalletIfInvolvingMe(const CTransaction&tx, const CBlock*pblock,
// bool fUpdate) {
// auto saplingNoteDataAndAddressesToAdd = FindMySaplingNotes(tx);
// }
//
// public static pair<mapSaplingNoteData_t, SaplingIncomingViewingKeyMap> FindMySaplingNotes(const
// CTransaction &tx) const
//
// {
// // Protocol Spec: 4.19 Block Chain Scanning (Sapling)
// for (uint32_t i = 0; i < tx.vShieldedOutput.size(); ++i) {
// const OutputDescription output = tx.vShieldedOutput[i];
// for (auto it = mapFullViewingKeys.begin(); it != mapFullViewingKeys.end();
// ++it) {
// SaplingIncomingViewingKey ivk = it -> first;
// auto result = NotePlaintext::decrypt
// (output.encCiphertext, ivk, output.ephemeralKey, output.cm);·
// if (!result) {
// continue;
// }
//
//
// }
//
//
// }
// }
public static List<NoteEntry> GetFilteredNotes(
PaymentAddress filterAddress,
boolean ignoreSpent,
boolean requireSpendingKey) {
List<NoteEntry> saplingEntries = Lists.newArrayList();
for (Entry<OutPoint, NoteData> entry : mapNoteData.entrySet()) {
OutPoint op = entry.getKey();
NoteData nd = entry.getValue();
//todo: tx.vShieldedOutput[op.n]
ReceiveDescription description = null;
Optional<NotePlaintext> maybe_pt = NotePlaintext.decrypt(
description.getCEnc().toByteArray(),
nd.ivk.value,
description.getEpk().toByteArray(),
description.getNoteCommitment().toByteArray());
if (!maybe_pt.isPresent()) {
throw new RuntimeException("");
}
NotePlaintext notePt = maybe_pt.get();
Optional<PaymentAddress> maybe_pa = nd.ivk.address(notePt.d);
if (!maybe_pa.isPresent()) {
throw new RuntimeException("");
}
PaymentAddress pa = maybe_pa.get();
if (!filterAddress.equals(pa)) {
continue;
}
if (ignoreSpent && nd.nullifier.isPresent() && IsSaplingSpent(nd.nullifier.get())) {
continue;
}
// skip notes which cannot be spent
if (requireSpendingKey) {
if (!KeyStore.getExtendedSpendingKey(pa).isPresent()) {
continue;
}
}
Note note = notePt.note(nd.ivk).get();
saplingEntries.add(new NoteEntry(op, pa, note, notePt.memo));
}
return saplingEntries;
}
//todo: check db
private static boolean IsSaplingSpent(byte[] nullifier) {
return false;
}
// Add spending key to keystore
public static boolean AddSaplingZKey(
ExtendedSpendingKey sk,
PaymentAddress defaultAddr) {
// AssertLockHeld(cs_wallet); // mapSaplingZKeyMetadata
//
// if (!CCryptoKeyStore::AddSaplingSpendingKey (sk, defaultAddr)){
// return false;
// }
//
// if (!fFileBacked) {
// return true;
// }
//
// if (!IsCrypted()) {
// auto ivk = sk.expsk.fullViewingKey().in_viewing_key();
// return CWalletDB(strWalletFile).WriteSaplingZKey(ivk, sk, mapSaplingZKeyMetadata[ivk]);
// }
return true;
}
}