forked from mhcrnl/javacard-simple-wallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleWalletClient.java
120 lines (98 loc) · 5.1 KB
/
SimpleWalletClient.java
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
package fitpay.javacard.simplewallet;
import java.nio.ByteBuffer;
import java.util.List;
import javax.smartcardio.Card;
import javax.smartcardio.CardException;
import javax.smartcardio.CardTerminal;
import javax.smartcardio.CardChannel;
import javax.smartcardio.TerminalFactory;
import javax.smartcardio.CommandAPDU;
import javax.smartcardio.ResponseAPDU;
import com.licel.jcardsim.utils.AIDUtil;
import jnasmartcardio.Smartcardio;
@SuppressWarnings("restriction")
public class SimpleWalletClient {
@SuppressWarnings("restriction")
public static void main(String[] args) throws Exception {
TerminalFactory tf = TerminalFactory.getInstance("PC/SC", null, new Smartcardio());
System.out.println("default type: " + TerminalFactory.getDefaultType());
System.out.println("terminal count: " + tf.terminals().list().size());
List<CardTerminal> terminals = tf.terminals().list();
CardTerminal ct = terminals.get(0);
System.out.println("terminal: " + ct);
System.out.println("is card present: " + ct.isCardPresent());
if (ct.isCardPresent()) {
Card c = ct.connect("T=0");
System.out.println("connected to card: " + c);
CardChannel ch = c.getBasicChannel();
ResponseAPDU response = ch.transmit(new CommandAPDU(AIDUtil.select("FITPAYRULEZ!")));
System.out.println("select response: " + response);
System.out.println("current balance: " + getBalance(ch));
System.out.println("issuing credit");
issueCredit(ch, 5);
System.out.println("updated balance: " + getBalance(ch));
} else {
System.out.println("waiting for card");
while (true) {
try {
if (ct.isCardPresent()) {
System.out.println("card found... connecting....");
if (ct.isCardPresent()) {
Card c = ct.connect("T=0");
System.out.println("connected to card: " + c);
CardChannel ch = c.getBasicChannel();
ResponseAPDU response = ch.transmit(new CommandAPDU(AIDUtil.select("FITPAYRULEZ!")));
if (response.getSW() != 0x9000) {
System.out.println("no fitpay wallet found, is it installed?");
} else {
System.out.println("current balance: " + getBalance(ch));
System.out.println("spending some mula");
try {
issueDebit(ch, 5);
System.out.println("spending completed, updated balance: " + getBalance(ch));
} catch (NotEnoughMoneyException e) {
System.out.println("sorry pal, you don't have enough money!");
}
}
while (ct.isCardPresent()) {
Thread.sleep(250);
}
System.out.println("session completed");
}
} else {
Thread.sleep(250);
}
} catch (Exception e) {
System.err.println("uh oh scoobie: " + e.getMessage());
}
}
}
}
@SuppressWarnings("restriction")
protected static void issueDebit(CardChannel ch, int amount) throws CardException {
byte[] request = new byte[]{(byte)0xb0, (byte)0x30, (byte)0x00, (byte)0x00, (byte)0x01, (byte)amount};
ResponseAPDU response = ch.transmit(new CommandAPDU(request));
if (response.getSW() != 0x9000) {
if (response.getSW() == 0xff85) {
throw new NotEnoughMoneyException();
}
throw new RuntimeException("issue debit failed: " + response);
}
}
@SuppressWarnings("restriction")
protected static void issueCredit(CardChannel ch, int amount) throws CardException {
byte[] request = new byte[]{(byte)0xb0, (byte)0x40, (byte)0x00, (byte)0x00, (byte)0x01, (byte)amount};
ResponseAPDU response = ch.transmit(new CommandAPDU(request));
if (response.getSW() != 0x9000) {
throw new RuntimeException("issue credit failed: " + response);
}
}
@SuppressWarnings("restriction")
protected static short getBalance(CardChannel ch) throws CardException {
byte[] brequest = new byte[] { (byte)0xb0, (byte)0x50, (byte)0x00, (byte)0x00, (byte)0x02 };
ResponseAPDU response = ch.transmit(new CommandAPDU(brequest));
//assertEquals("get balance failed", 0x9000, response.getSW());
ByteBuffer buf = ByteBuffer.wrap(response.getData());
return buf.asShortBuffer().get();
}
}