Repository navigation
Expand file tree
/
Copy pathCookieDecoder.ts
More file actions
307 lines (250 loc) · 11.4 KB
/
Copy pathCookieDecoder.ts
File metadata and controls
307 lines (250 loc) · 11.4 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// tslint:disable no-bitwise no-shadowed-variable
import { WalletInfoEntry, WalletType } from './WalletInfo';
import { Utf8Tools } from '@nimiq/utils';
import { AccountInfoEntry } from './AccountInfo';
import CookieJar from './CookieJar';
import {
ACCOUNT_DEFAULT_LABEL_LEGACY,
ACCOUNT_DEFAULT_LABEL_LEDGER,
LABEL_MAX_LENGTH,
CONTRACT_DEFAULT_LABEL_VESTING,
} from '@/lib/Constants';
import LabelingMachine from './LabelingMachine';
import { ContractInfoEntry, VestingContractInfoEntry } from './ContractInfo';
import AddressUtils from './AddressUtils';
export class CookieDecoder {
public static decode(str: string): WalletInfoEntry[] {
if (!str) return [];
// Convert base64 str into byte array
const bytes: number[] = Array.from(this.base64Decode(str));
// Cookie version
const version = this.readByte(bytes);
if (version !== CookieJar.VERSION) return this.legacyCookie(version, bytes);
const wallets: WalletInfoEntry[] = [];
while (bytes.length > 0) {
wallets.push(this.decodeWallet(bytes));
}
return wallets;
}
private static readByte(bytes: number[]): number {
const byte = bytes.shift();
if (typeof byte === 'undefined') throw new Error('Malformed Cookie');
return byte;
}
private static readBytes(bytes: number[], length: number): number[] {
const result: number[] = [];
for (let i = 0; i < length; i++) {
result.push(this.readByte(bytes));
}
return result;
}
private static decodeWallet(bytes: number[]): WalletInfoEntry {
// Wallet type and label length
const typeAndLabelLength = this.readByte(bytes);
const type = typeAndLabelLength & 0b11;
const labelLength = typeAndLabelLength >> 2; // Can only be < 64 because it's only 6 bit
// Status byte
const statusByte = this.readByte(bytes);
const keyMissing =
(statusByte & CookieJar.StatusFlags.KEY_MISSING) === CookieJar.StatusFlags.KEY_MISSING;
const fileExported =
(statusByte & CookieJar.StatusFlags.FILE_EXPORTED) === CookieJar.StatusFlags.FILE_EXPORTED;
const wordsExported =
(statusByte & CookieJar.StatusFlags.WORDS_EXPORTED) === CookieJar.StatusFlags.WORDS_EXPORTED;
const hasContracts =
(statusByte & CookieJar.StatusFlags.HAS_CONTRACTS) === CookieJar.StatusFlags.HAS_CONTRACTS;
// Wallet ID
let id: string = '';
for (let i = 0; i < 6; i++) {
const idChunk = this.readByte(bytes).toString(16);
id += `${idChunk.length < 2 ? '0' : ''}${idChunk}`;
}
// Handle LEGACY wallet
if (type === WalletType.LEGACY) {
const walletLabel = ACCOUNT_DEFAULT_LABEL_LEGACY;
const accounts = this.decodeAccounts(bytes, labelLength);
const contracts = hasContracts ? this.decodeContracts(bytes) : [];
const walletInfoEntry: WalletInfoEntry = {
id,
keyId: '',
type,
label: walletLabel,
accounts,
contracts,
keyMissing,
fileExported,
wordsExported,
};
return walletInfoEntry;
}
// Handle regular wallet
// Wallet label
const walletLabelBytes = this.readBytes(bytes, labelLength);
const accounts = this.decodeAccounts(bytes);
const contracts = hasContracts ? this.decodeContracts(bytes) : [];
const firstAccount = accounts.values().next().value;
const walletLabel = walletLabelBytes.length > 0
? Utf8Tools.utf8ByteArrayToString(new Uint8Array(walletLabelBytes))
: type === WalletType.LEDGER
? ACCOUNT_DEFAULT_LABEL_LEDGER
: LabelingMachine.labelAccount(AddressUtils.toUserFriendlyAddress(firstAccount.address));
const walletInfoEntry: WalletInfoEntry = {
id,
keyId: '',
type,
label: walletLabel,
accounts,
contracts,
keyMissing,
fileExported,
wordsExported,
};
return walletInfoEntry;
}
private static decodeAccounts(bytes: number[], labelLength?: number): Map<string, AccountInfoEntry> {
let numberAccounts = 1;
if (typeof labelLength === 'undefined') {
// When the labelLength is not passed, it means it is not a LEGACY wallet
// and the number of accounts is encoded before the list
numberAccounts = this.readByte(bytes);
}
const accounts: AccountInfoEntry[] = [];
for (let i = 0; i < numberAccounts; i++) {
accounts.push(this.decodeAccount(bytes, labelLength));
}
const accountsMapArray: Array<[string, AccountInfoEntry]> = accounts.map((account) => {
// Deserialize Nimiq.Address
const userFriendlyAddress = AddressUtils.toUserFriendlyAddress(account.address);
return [userFriendlyAddress, account] as [string, AccountInfoEntry];
});
return new Map(accountsMapArray);
}
private static decodeAccount(bytes: number[], labelLength?: number): AccountInfoEntry {
if (typeof labelLength === 'undefined') {
labelLength = this.readByte(bytes);
}
if (labelLength > LABEL_MAX_LENGTH) {
throw new Error('Malformed Cookie, label too long');
}
// Account label
const labelBytes = this.readBytes(bytes, labelLength);
// Account address
// (iframe does not have Nimiq lib)
const addressBytes = this.readBytes(bytes, 20 /* Nimiq.Address.SERIALIZED_SIZE */);
const accountLabel = labelBytes.length > 0
? Utf8Tools.utf8ByteArrayToString(new Uint8Array(labelBytes))
: LabelingMachine.labelAddress(AddressUtils.toUserFriendlyAddress(new Uint8Array(addressBytes)));
const accountInfoEntry: AccountInfoEntry = {
path: 'not public',
label: accountLabel,
address: new Uint8Array(addressBytes),
};
return accountInfoEntry;
}
private static decodeContracts(bytes: number[]): ContractInfoEntry[] {
const numberContracts = this.readByte(bytes);
const contracts: ContractInfoEntry[] = [];
for (let i = 0; i < numberContracts; i++) {
contracts.push(this.decodeContract(bytes));
}
return contracts;
}
private static decodeContract(bytes: number[]): ContractInfoEntry {
// Contract type and label length
const typeAndLabelLength = this.readByte(bytes);
const type = typeAndLabelLength & 0b11;
const labelLength = typeAndLabelLength >> 2;
const labelBytes = this.readBytes(bytes, labelLength);
// Contract address
// (iframe does not have Nimiq lib)
const addressBytes = this.readBytes(bytes, 20 /* Nimiq.Address.SERIALIZED_SIZE */);
switch (type) {
case 1 /* Nimiq.Account.Type.VESTING */:
const label = labelBytes.length > 0
? Utf8Tools.utf8ByteArrayToString(new Uint8Array(labelBytes))
: CONTRACT_DEFAULT_LABEL_VESTING;
const ownerBytes = this.readBytes(bytes, 20 /* Nimiq.Address.SERIALIZED_SIZE */);
const start = this.fromBase256(this.readBytes(bytes, 4)); // Uint32
const stepAmount = this.fromBase256(this.readBytes(bytes, 8)); // Uint64
const stepBlocks = this.fromBase256(this.readBytes(bytes, 4)); // Uint32
const totalAmount = this.fromBase256(this.readBytes(bytes, 8)); // Uint64
return {
type,
label,
address: new Uint8Array(addressBytes),
owner: new Uint8Array(ownerBytes),
start,
stepAmount,
stepBlocks,
totalAmount,
} as VestingContractInfoEntry;
case 2 /* Nimiq.Account.Type.HTLC */:
throw new Error('HTLC decoding is not yet implemented');
default:
throw new Error('Unknown contract type: ' + type);
}
}
private static legacyCookie(version: number, bytes: number[]): WalletInfoEntry[] {
switch (version) {
default: return [];
}
}
private static fromBase256(numbers: number[]) {
const base2bytes = numbers.map((value) => value.toString(2).padStart(8, '0'));
return parseInt(base2bytes.join(''), 2);
}
private static base64Decode(base64: string): Uint8Array {
/*
Source: https://github.com/danguer/blog-examples/blob/master/js/base64-binary.js
Copyright (c) 2011, Daniel Guerrero
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL DANIEL GUERRERO BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// Use a lookup table to find the index.
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
const lookup = new Uint8Array(256);
for (let i = 0; i < chars.length; i++) {
lookup[chars.charCodeAt(i)] = i;
}
// Decode
let bufferLength = base64.length * 0.75;
let p = 0;
let encoded1: number;
let encoded2: number;
let encoded3: number;
let encoded4: number;
if (base64[base64.length - 1] === '=') {
bufferLength--;
if (base64[base64.length - 2] === '=') {
bufferLength--;
}
}
const bytes = new Uint8Array(bufferLength);
for (let i = 0; i < base64.length; i += 4) {
encoded1 = lookup[base64.charCodeAt(i)];
encoded2 = lookup[base64.charCodeAt(i + 1)];
encoded3 = lookup[base64.charCodeAt(i + 2)];
encoded4 = lookup[base64.charCodeAt(i + 3)];
bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);
}
return bytes;
}
}