-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHDUser.java
186 lines (141 loc) · 3.99 KB
/
HDUser.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
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
package com.taisys.Slimduet.Applet;
import javacard.framework.*;
import javacard.security.*;
public class HDUser {
private HDUser next; // points next HDUser
private ECPrivateKey masterPrivate; // Root Private
private byte[] userID; // UserID
private byte[] masterChainCode; // Root ChainCode
private short[] mnemonicCode;
protected static byte NO_USER = (byte)0;
protected static byte ONE_USER = (byte)1;
protected static byte MORE_USER = (byte)2;
public HDUser() {
masterPrivate = (ECPrivateKey) KeyBuilder.buildKey(KeyBuilder.TYPE_EC_FP_PRIVATE, KeyBuilder.LENGTH_EC_FP_256, false);
SECP256k1.setCurveParameters(masterPrivate);
userID = new byte[Slimduet.USER_ID_SIZE];
masterChainCode = new byte[Slimduet.CHAIN_CODE_SIZE];
mnemonicCode = new short[Slimduet.MNEMONIC_SIZE];
}
public HDUser getNext() {
return next;
}
public ECPrivateKey getMasterPrivate() {
return masterPrivate;
}
public byte[] getUserID() {
return userID;
}
public byte[] getMasterChainCode() {
return masterChainCode;
}
public short getMnemonicCode(short index){
return mnemonicCode[index];
}
public void setMnemonicCode(short[] value, short Ofs, byte length){
short i;
for(i = (short)0; i < (short)length; i++){
mnemonicCode[i] = value[Ofs++];
}
}
/** Create HD User
* @param root Root node of the list, if null, ROOT node will be created
* @param id current User index
* @remark
* output current nod
**/
static HDUser createUser(HDUser root) {
HDUser nod = null;
HDUser lst = null;
nod = new HDUser();
if (root==null) return nod;
lst = root;
while(lst.next != null){
lst = lst.next;
}
lst.next = nod;
return nod;
}
/** Read id HD User
* @param root Root node of the list, if null, return null
* @param id current User index
* @remark
* output current nod
**/
static HDUser read(HDUser root, byte[] id, short ofs) {
HDUser lst = null;
if (root==null) return null;
lst = root;
while(lst != null) {
if (Util.arrayCompare(id, ofs, lst.userID, (short) 0, Slimduet.USER_ID_SIZE) == 0) {
return lst;
}
lst = lst.next;
}
return null;
}
/** Read all HD User
* @param root Root node of the list, if null, return null
* @param outBuf return all User id
* @ofs Available offset into out buffer
* @remark
* output Available size of out data
**/
static short readAllUser(HDUser root, byte[] outBuf, short ofs) {
short size = (short) 0;
HDUser lst = null;
if (root==null) return (short) 0;
lst = root;
while(lst != null) {
Util.arrayCopy(lst.userID, (short) 0, outBuf, (short) (ofs+size), Slimduet.USER_ID_SIZE);
size += Slimduet.USER_ID_SIZE;
lst = lst.next;
}
return size;
}
/** Read all HD User
* @param root Root node of the list, if null, return null
* @remark
* output Available size of out data
**/
static short readAllUserCount(HDUser root) {
short count = (short) 0;
HDUser lst = null;
if (root==null) return (short) 0;
lst = root;
while(lst != null) {
lst = lst.next;
count++;
}
return count;
}
/** Delete HD User
* @param root Root node of the list, if null, return null
* @param id current User index
* @remark
* output next nod
**/
static HDUser deleteUser(HDUser root, byte[] id, short idOfs, byte[] buf, short bufOfs) {
HDUser lst = null;
HDUser nod = null;
buf[bufOfs] = NO_USER;
if (root==null) return null;
if (Util.arrayCompare(id, idOfs, root.userID, (short) 0, Slimduet.USER_ID_SIZE) == 0) {
if (JCSystem.isObjectDeletionSupported()) JCSystem.requestObjectDeletion();
buf[bufOfs] = ONE_USER;
return root.next;
}
nod = root;
while (nod != null) {
if (Util.arrayCompare(id, idOfs, nod.userID, (short) 0, Slimduet.USER_ID_SIZE) == 0) {
lst.next = nod.next;
if (JCSystem.isObjectDeletionSupported()) JCSystem.requestObjectDeletion();
buf[bufOfs] = MORE_USER;
return nod;
}
lst = nod;
nod = nod.next;
}
return null;
}
}