|
| 1 | +package org.tron.core; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertArrayEquals; |
| 4 | +import static org.junit.Assert.assertEquals; |
| 5 | +import static org.junit.Assert.assertFalse; |
| 6 | +import static org.junit.Assert.assertThrows; |
| 7 | +import static org.mockito.Mockito.doReturn; |
| 8 | +import static org.mockito.Mockito.spy; |
| 9 | + |
| 10 | +import com.google.protobuf.Any; |
| 11 | +import com.google.protobuf.ByteString; |
| 12 | +import java.util.Arrays; |
| 13 | +import org.junit.AfterClass; |
| 14 | +import org.junit.BeforeClass; |
| 15 | +import org.junit.Test; |
| 16 | +import org.tron.api.GrpcAPI; |
| 17 | +import org.tron.common.crypto.Hash; |
| 18 | +import org.tron.common.utils.ByteArray; |
| 19 | +import org.tron.core.capsule.TransactionCapsule; |
| 20 | +import org.tron.core.config.args.Args; |
| 21 | +import org.tron.core.exception.ZksnarkException; |
| 22 | +import org.tron.core.zen.address.FullViewingKey; |
| 23 | +import org.tron.core.zen.address.KeyIo; |
| 24 | +import org.tron.core.zen.address.PaymentAddress; |
| 25 | +import org.tron.core.zen.address.SpendingKey; |
| 26 | +import org.tron.core.zen.note.Note; |
| 27 | +import org.tron.core.zen.note.NoteEncryption; |
| 28 | +import org.tron.core.zen.note.NoteEncryption.Encryption; |
| 29 | +import org.tron.core.zen.note.NoteEncryption.Encryption.EncCiphertext; |
| 30 | +import org.tron.core.zen.note.NoteEncryption.Encryption.EncPlaintext; |
| 31 | +import org.tron.core.zen.note.OutgoingPlaintext; |
| 32 | +import org.tron.protos.Protocol.Block; |
| 33 | +import org.tron.protos.Protocol.Transaction; |
| 34 | +import org.tron.protos.Protocol.Transaction.Contract; |
| 35 | +import org.tron.protos.Protocol.TransactionInfo; |
| 36 | +import org.tron.protos.contract.ShieldContract.ReceiveDescription; |
| 37 | +import org.tron.protos.contract.ShieldContract.ShieldedTransferContract; |
| 38 | + |
| 39 | +public class WalletShieldedNoteScanTest { |
| 40 | + |
| 41 | + private static boolean previousAllow; |
| 42 | + private static PaymentAddress address; |
| 43 | + private static byte[] ivk; |
| 44 | + private static byte[] ovk; |
| 45 | + |
| 46 | + @BeforeClass |
| 47 | + public static void setUpClass() throws Exception { |
| 48 | + previousAllow = Args.getInstance().isAllowShieldedTransactionApi(); |
| 49 | + Args.getInstance().setAllowShieldedTransactionApi(true); |
| 50 | + SpendingKey key = new SpendingKey(new byte[32]); |
| 51 | + FullViewingKey viewingKey = key.fullViewingKey(); |
| 52 | + address = key.defaultAddress(); |
| 53 | + ivk = viewingKey.inViewingKey().getValue(); |
| 54 | + ovk = viewingKey.getOvk(); |
| 55 | + } |
| 56 | + |
| 57 | + @AfterClass |
| 58 | + public static void tearDownClass() { |
| 59 | + Args.getInstance().setAllowShieldedTransactionApi(previousAllow); |
| 60 | + } |
| 61 | + |
| 62 | + private static Note note(long value) throws Exception { |
| 63 | + Note note = new Note(address, value); |
| 64 | + note.setMemo(new byte[]{7, 8, 9}); |
| 65 | + return note; |
| 66 | + } |
| 67 | + |
| 68 | + private static class EncryptedNote { |
| 69 | + |
| 70 | + private final Note note; |
| 71 | + private final NoteEncryption encryptor; |
| 72 | + private final EncCiphertext ciphertext; |
| 73 | + private final byte[] commitment; |
| 74 | + private final byte[] cout; |
| 75 | + |
| 76 | + private EncryptedNote(Note note, EncPlaintext plaintext) throws Exception { |
| 77 | + this.note = note; |
| 78 | + commitment = note.cm(); |
| 79 | + encryptor = NoteEncryption.fromDiversifier(address.getD()).get(); |
| 80 | + ciphertext = encryptor.encryptToRecipient(address.getPkD(), plaintext).get(); |
| 81 | + cout = new OutgoingPlaintext(address.getPkD(), encryptor.getEsk()) |
| 82 | + .encrypt(ovk, new byte[32], commitment, encryptor).getData(); |
| 83 | + // A malformed authenticated plaintext, rather than damaged ciphertext, reaches the bug. |
| 84 | + assertArrayEquals(plaintext.getData(), Encryption.attemptEncDecryption( |
| 85 | + ciphertext.getData(), ivk, encryptor.getEpk()).get().getData()); |
| 86 | + } |
| 87 | + |
| 88 | + private ReceiveDescription description() { |
| 89 | + return ReceiveDescription.newBuilder() |
| 90 | + .setNoteCommitment(ByteString.copyFrom(commitment)) |
| 91 | + .setValueCommitment(ByteString.copyFrom(new byte[32])) |
| 92 | + .setEpk(ByteString.copyFrom(encryptor.getEpk())) |
| 93 | + .setCEnc(ByteString.copyFrom(ciphertext.getData())) |
| 94 | + .setCOut(ByteString.copyFrom(cout)).build(); |
| 95 | + } |
| 96 | + |
| 97 | + private TransactionInfo.Log log(byte[] contractAddress, int position) { |
| 98 | + byte[] data = new byte[800]; |
| 99 | + System.arraycopy(ByteArray.fromLong(position), 0, data, 24, 8); |
| 100 | + System.arraycopy(commitment, 0, data, 32, 32); |
| 101 | + System.arraycopy(encryptor.getEpk(), 0, data, 96, 32); |
| 102 | + System.arraycopy(ciphertext.getData(), 0, data, 128, 580); |
| 103 | + System.arraycopy(cout, 0, data, 708, 80); |
| 104 | + return TransactionInfo.Log.newBuilder() |
| 105 | + .setAddress(ByteString.copyFrom(Arrays.copyOfRange(contractAddress, 1, 21))) |
| 106 | + .addTopics(ByteString.copyFrom(Hash.sha3(ByteArray.fromString( |
| 107 | + "MintNewLeaf(uint256,bytes32,bytes32,bytes32,bytes32[21])")))) |
| 108 | + .setData(ByteString.copyFrom(data)).build(); |
| 109 | + } |
| 110 | + |
| 111 | + private void assertRejected() throws Exception { |
| 112 | + assertFalse(Note.decrypt(ciphertext.getData(), ivk, encryptor.getEpk(), commitment) |
| 113 | + .isPresent()); |
| 114 | + assertFalse(Note.decrypt(ciphertext, encryptor.getEpk(), encryptor.getEsk(), |
| 115 | + address.getPkD(), commitment).isPresent()); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void malformedAuthenticatedPlaintextsAreSkippedByBothDecryptors() throws Exception { |
| 121 | + Note note = note(42); |
| 122 | + for (int header : new int[]{0, 2, 255}) { |
| 123 | + EncPlaintext plaintext = note.encode(); |
| 124 | + plaintext.getData()[0] = (byte) header; |
| 125 | + assertThrows(ZksnarkException.class, () -> Note.decode(plaintext)); |
| 126 | + new EncryptedNote(note, plaintext).assertRejected(); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void negativeAuthenticatedValuesAreSkippedByBothDecryptors() throws Exception { |
| 132 | + Note note = note(42); |
| 133 | + for (long value : new long[]{Long.MIN_VALUE, -1}) { |
| 134 | + Note invalid = new Note(address.getD(), address.getPkD(), value, note.getRcm()); |
| 135 | + assertEquals(value, Note.decode(invalid.encode()).getValue()); |
| 136 | + new EncryptedNote(note, invalid.encode()).assertRejected(); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void validNotesAndExistingRejectionsArePreserved() throws Exception { |
| 142 | + for (long value : new long[]{0, 42, Long.MAX_VALUE}) { |
| 143 | + Note note = note(value); |
| 144 | + EncryptedNote encrypted = new EncryptedNote(note, note.encode()); |
| 145 | + Note incoming = Note.decrypt(encrypted.ciphertext.getData(), ivk, |
| 146 | + encrypted.encryptor.getEpk(), encrypted.commitment).get(); |
| 147 | + Note outgoing = Note.decrypt(encrypted.ciphertext, encrypted.encryptor.getEpk(), |
| 148 | + encrypted.encryptor.getEsk(), address.getPkD(), encrypted.commitment).get(); |
| 149 | + for (Note decrypted : new Note[]{incoming, outgoing}) { |
| 150 | + assertEquals(note.getValue(), decrypted.getValue()); |
| 151 | + assertArrayEquals(note.getD().getData(), decrypted.getD().getData()); |
| 152 | + assertArrayEquals(note.getRcm(), decrypted.getRcm()); |
| 153 | + assertArrayEquals(note.getMemo(), decrypted.getMemo()); |
| 154 | + } |
| 155 | + assertThrows(ZksnarkException.class, () -> Note.decrypt(encrypted.ciphertext.getData(), |
| 156 | + new byte[31], encrypted.encryptor.getEpk(), encrypted.commitment)); |
| 157 | + encrypted.commitment[0] ^= 1; |
| 158 | + encrypted.assertRejected(); |
| 159 | + encrypted.commitment[0] ^= 1; |
| 160 | + encrypted.ciphertext.getData()[0] ^= 1; |
| 161 | + encrypted.assertRejected(); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + private static EncryptedNote[] mixedNotes() throws Exception { |
| 166 | + Note valid = note(42); |
| 167 | + EncPlaintext invalidHeader = valid.encode(); |
| 168 | + invalidHeader.getData()[0] = 2; |
| 169 | + Note invalidValue = new Note(address.getD(), address.getPkD(), -1, valid.getRcm()); |
| 170 | + Note last = note(84); |
| 171 | + return new EncryptedNote[]{new EncryptedNote(valid, valid.encode()), |
| 172 | + new EncryptedNote(valid, invalidHeader), new EncryptedNote(valid, invalidValue.encode()), |
| 173 | + new EncryptedNote(last, last.encode())}; |
| 174 | + } |
| 175 | + |
| 176 | + private static Wallet scanner(Transaction transaction) { |
| 177 | + Wallet scanner = spy(new Wallet()); |
| 178 | + GrpcAPI.BlockList blocks = GrpcAPI.BlockList.newBuilder() |
| 179 | + .addBlock(Block.newBuilder().addTransactions(transaction)).build(); |
| 180 | + doReturn(blocks).when(scanner).getBlocksByLimitNext(0, 1); |
| 181 | + return scanner; |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + public void trc20ScansReturnValidNotesOnBothSidesOfMalformedNotes() throws Exception { |
| 186 | + byte[] contractAddress = new byte[21]; |
| 187 | + contractAddress[0] = Wallet.getAddressPreFixByte(); |
| 188 | + contractAddress[20] = 1; |
| 189 | + EncryptedNote[] notes = mixedNotes(); |
| 190 | + Transaction transaction = Transaction.getDefaultInstance(); |
| 191 | + ByteString txid = new TransactionCapsule(transaction).getTransactionId().getByteString(); |
| 192 | + Wallet scanner = scanner(transaction); |
| 193 | + TransactionInfo.Builder info = TransactionInfo.newBuilder(); |
| 194 | + for (int i = 0; i < notes.length; i++) { |
| 195 | + info.addLog(notes[i].log(contractAddress, i)); |
| 196 | + } |
| 197 | + doReturn(info.build()).when(scanner).getTransactionInfoById(txid); |
| 198 | + |
| 199 | + GrpcAPI.DecryptNotesTRC20 incoming = scanner.scanShieldedTRC20NotesByIvk( |
| 200 | + 0, 1, contractAddress, ivk, null, null); |
| 201 | + GrpcAPI.DecryptNotesTRC20 outgoing = scanner.scanShieldedTRC20NotesByOvk( |
| 202 | + 0, 1, ovk, contractAddress); |
| 203 | + for (GrpcAPI.DecryptNotesTRC20 result : new GrpcAPI.DecryptNotesTRC20[]{incoming, outgoing}) { |
| 204 | + assertEquals(2, result.getNoteTxsCount()); |
| 205 | + for (int i = 0; i < 2; i++) { |
| 206 | + int index = i * 3; |
| 207 | + GrpcAPI.DecryptNotesTRC20.NoteTx actual = result.getNoteTxs(i); |
| 208 | + assertEquals(txid, actual.getTxid()); |
| 209 | + assertEquals(index, actual.getIndex()); |
| 210 | + assertEquals(notes[index].note.getValue(), actual.getNote().getValue()); |
| 211 | + assertEquals(ByteString.copyFrom(notes[index].note.getRcm()), actual.getNote().getRcm()); |
| 212 | + assertEquals(ByteString.copyFrom(new byte[]{7, 8, 9}), actual.getNote().getMemo()); |
| 213 | + assertEquals(KeyIo.encodePaymentAddress(address), actual.getNote().getPaymentAddress()); |
| 214 | + } |
| 215 | + } |
| 216 | + assertEquals(0, incoming.getNoteTxs(0).getPosition()); |
| 217 | + assertEquals(3, incoming.getNoteTxs(1).getPosition()); |
| 218 | + } |
| 219 | + |
| 220 | + @Test |
| 221 | + public void nativeScansReturnValidNotesOnBothSidesOfMalformedNotes() throws Exception { |
| 222 | + EncryptedNote[] notes = mixedNotes(); |
| 223 | + ShieldedTransferContract.Builder shielded = ShieldedTransferContract.newBuilder(); |
| 224 | + for (EncryptedNote note : notes) { |
| 225 | + shielded.addReceiveDescription(note.description()); |
| 226 | + } |
| 227 | + Transaction transaction = Transaction.newBuilder() |
| 228 | + .setRawData(Transaction.raw.newBuilder().addContract(Contract.newBuilder() |
| 229 | + .setType(Contract.ContractType.ShieldedTransferContract) |
| 230 | + .setParameter(Any.pack(shielded.build())))).build(); |
| 231 | + Wallet scanner = scanner(transaction); |
| 232 | + GrpcAPI.DecryptNotes incoming = scanner.scanNoteByIvk(0, 1, ivk); |
| 233 | + GrpcAPI.DecryptNotes outgoing = scanner.scanNoteByOvk(0, 1, ovk); |
| 234 | + for (GrpcAPI.DecryptNotes result : new GrpcAPI.DecryptNotes[]{incoming, outgoing}) { |
| 235 | + assertEquals(2, result.getNoteTxsCount()); |
| 236 | + assertEquals(0, result.getNoteTxs(0).getIndex()); |
| 237 | + assertEquals(3, result.getNoteTxs(1).getIndex()); |
| 238 | + assertEquals(42, result.getNoteTxs(0).getNote().getValue()); |
| 239 | + assertEquals(84, result.getNoteTxs(1).getNote().getValue()); |
| 240 | + assertEquals(ByteString.copyFrom(notes[0].note.getRcm()), |
| 241 | + result.getNoteTxs(0).getNote().getRcm()); |
| 242 | + assertEquals(ByteString.copyFrom(notes[3].note.getRcm()), |
| 243 | + result.getNoteTxs(1).getNote().getRcm()); |
| 244 | + } |
| 245 | + } |
| 246 | +} |
0 commit comments