1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 
17 package com.example.android.asymmetricfingerprintdialog.server;
18 
19 import java.io.ByteArrayOutputStream;
20 import java.io.DataOutputStream;
21 import java.io.IOException;
22 import java.util.Objects;
23 
24 /**
25  * An entity that represents a single transaction (purchase) of an item.
26  */
27 public class Transaction {
28 
29     /** The unique ID of the item of the purchase */
30     private final Long mItemId;
31 
32     /** The unique user ID who made the transaction */
33     private final String mUserId;
34 
35     /**
36      * The random long value that will be also signed by the private key and verified in the server
37      * that the same nonce can't be reused to prevent replay attacks.
38      */
39     private final Long mClientNonce;
40 
Transaction(String userId, long itemId, long clientNonce)41     public Transaction(String userId, long itemId, long clientNonce) {
42         mItemId = itemId;
43         mUserId = userId;
44         mClientNonce = clientNonce;
45     }
46 
getUserId()47     public String getUserId() {
48         return mUserId;
49     }
50 
toByteArray()51     public byte[] toByteArray() {
52         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
53         DataOutputStream dataOutputStream = null;
54         try {
55             dataOutputStream = new DataOutputStream(byteArrayOutputStream);
56             dataOutputStream.writeLong(mItemId);
57             dataOutputStream.writeUTF(mUserId);
58             dataOutputStream.writeLong(mClientNonce);
59             return byteArrayOutputStream.toByteArray();
60         } catch (IOException e) {
61             throw new RuntimeException(e);
62         } finally {
63             try {
64                 if (dataOutputStream != null) {
65                     dataOutputStream.close();
66                 }
67             } catch (IOException ignore) {
68             }
69             try {
70                 byteArrayOutputStream.close();
71             } catch (IOException ignore) {
72             }
73         }
74     }
75 
76     @Override
equals(Object o)77     public boolean equals(Object o) {
78         if (this == o) {
79             return true;
80         }
81         if (o == null || getClass() != o.getClass()) {
82             return false;
83         }
84 
85         Transaction that = (Transaction) o;
86         return Objects.equals(mItemId, that.mItemId) && Objects.equals(mUserId, that.mUserId) &&
87                 Objects.equals(mClientNonce, that.mClientNonce);
88     }
89 
90     @Override
hashCode()91     public int hashCode() {
92         return Objects.hash(mItemId, mUserId, mClientNonce);
93     }
94 }
95