-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
362 lines (306 loc) · 10.5 KB
/
index.ts
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* eslint-disable no-await-in-loop */
/* eslint-disable no-restricted-syntax */
import algosdk, { ABIMethod, ABIResult } from 'algosdk';
import { ApplicationClient } from '@algorandfoundation/algokit-utils/types/app-client';
import { AppCallTransactionResult } from '@algorandfoundation/algokit-utils/types/app';
import { expect } from '@jest/globals';
import { SuggestedParamsWithMinFee } from 'algosdk/dist/types/types/transactions/base';
import appSpec from '../contracts/artifacts/AlgoDID.json';
const COST_PER_BYTE = 400;
const COST_PER_BOX = 2500;
const MAX_BOX_SIZE = 32768;
const BYTES_PER_CALL = 2048
- 4 // 4 bytes for the method selector
- 34 // 34 bytes for the key
- 8 // 8 bytes for the box index
- 8; // 8 bytes for the offset
export type Metadata = {start: bigint, end: bigint, status: bigint, endSize: bigint};
export async function resolveDID(did: string, algodClient: algosdk.Algodv2): Promise<Buffer> {
const splitDid = did.split(':');
if (splitDid[0] !== 'did') throw new Error(`Invalid protocol. Expected 'did', got ${splitDid[0]}`);
if (splitDid[1] !== 'algo') throw new Error(`Invalid DID method. Expected 'algod', got ${splitDid[1]}`);
const splitID = splitDid[2].split('-');
let pubKey: Uint8Array;
try {
pubKey = algosdk.decodeAddress(splitID[0]).publicKey;
} catch (e) {
throw new Error(`Invalid public key. Expected Algorand address, got ${splitID[0]}`);
}
let appID: bigint;
try {
appID = BigInt(splitID[1]);
algosdk.encodeUint64(appID);
} catch (e) {
throw new Error(`Invalid app ID. Expected uint64, got ${splitID[1]}`);
}
const appClient = new ApplicationClient({
resolveBy: 'id',
id: appID,
sender: algosdk.generateAccount(),
app: JSON.stringify(appSpec),
}, algodClient);
const boxValue = (await appClient.getBoxValueFromABIType(pubKey, algosdk.ABIType.from('(uint64,uint64,uint8,uint64,uint64)'))).valueOf() as bigint[];
const metadata: Metadata = {
start: boxValue[0], end: boxValue[1], status: boxValue[2], endSize: boxValue[3],
};
if (metadata.status === BigInt(0)) throw new Error('DID document is still being uploaded');
if (metadata.status === BigInt(2)) throw new Error('DID document is being deleted');
const boxPromises = [];
for (let i = metadata.start; i <= metadata.end; i += 1n) {
boxPromises.push(appClient.getBoxValue(algosdk.encodeUint64(i)));
}
const boxValues = await Promise.all(boxPromises);
return Buffer.concat(boxValues);
}
/**
*
* @param algodClient
* @param abiMethod
* @param pubKey
* @param boxes
* @param boxIndex
* @param suggestedParams
* @param sender
* @param appID
* @param group
* @returns
*/
export async function sendTxGroup(
algodClient: algosdk.Algodv2,
abiMethod: ABIMethod,
bytesOffset: number,
pubKey: Uint8Array,
boxes: algosdk.BoxReference[],
boxIndex: bigint,
suggestedParams: SuggestedParamsWithMinFee,
sender: algosdk.Account,
appID: number,
group: Buffer[],
): Promise<string[]> {
const atc = new algosdk.AtomicTransactionComposer();
group.forEach((chunk, i) => {
atc.addMethodCall({
method: abiMethod!,
methodArgs: [pubKey, boxIndex, BYTES_PER_CALL * (i + bytesOffset), chunk],
boxes,
suggestedParams,
sender: sender.addr,
signer: algosdk.makeBasicAccountTransactionSigner(sender),
appID,
});
});
await new Promise((r) => setTimeout(r, 2000));
return (await atc.execute(algodClient, 3)).txIDs;
}
/**
*
* @param atc
* @param algodClient
* @param retryCount
*/
async function tryExecute(
atc: algosdk.AtomicTransactionComposer,
algodClient: algosdk.Algodv2,
retryCount = 1,
): Promise<void> {
try {
await atc.execute(algodClient, 3);
} catch (e) {
if (retryCount === 3) {
// TODO: SDK bugfix
// const execTraceConfig = new algosdk.modelsv2.SimulateTraceConfig({
// enable: true,
// stackChange: true,
// stateChange: true,
// scratchChange: true,
// });
// const simReq = new algosdk.modelsv2.SimulateRequest({
// txnGroups: [],
// execTraceConfig,
// });
// const result = await atc.simulate(algodClient, simReq);
// console.warn(result.simulateResponse.txnGroups[0].txnResults[0].execTrace);
throw e;
}
// eslint-disable-next-line no-console
console.warn(`Failed to send transaction group. Retrying in ${500 * retryCount}ms (${retryCount / 3})`);
}
}
/**
*
* @param data
* @param appID
* @param pubKey
* @param sender
* @param algodClient
* @returns
*/
export async function uploadDIDDocument(
data: Buffer,
appID: number,
pubKey: Uint8Array,
sender: algosdk.Account,
algodClient: algosdk.Algodv2,
): Promise<Metadata> {
const appClient = new ApplicationClient({
resolveBy: 'id',
id: appID,
sender,
app: JSON.stringify(appSpec),
}, algodClient);
const ceilBoxes = Math.ceil(data.byteLength / MAX_BOX_SIZE);
const endBoxSize = data.byteLength % MAX_BOX_SIZE;
const totalCost = ceilBoxes * COST_PER_BOX // cost of data boxes
+ (ceilBoxes - 1) * MAX_BOX_SIZE * COST_PER_BYTE // cost of data
+ ceilBoxes * 8 * COST_PER_BYTE // cost of data keys
+ endBoxSize * COST_PER_BYTE // cost of last data box
+ COST_PER_BOX + (8 + 8 + 1 + 8 + 32 + 8) * COST_PER_BYTE; // cost of metadata box
const mbrPayment = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
from: sender.addr,
to: (await appClient.getAppReference()).appAddress,
amount: totalCost,
suggestedParams: await algodClient.getTransactionParams().do(),
});
const appCallResult: AppCallTransactionResult = await appClient.call({
method: 'startUpload',
methodArgs: [pubKey, ceilBoxes, endBoxSize, mbrPayment],
boxes: [
pubKey,
],
sendParams: { suppressLog: true },
});
expect(appCallResult).toBeDefined();
const boxValue = (await appClient.getBoxValueFromABIType(pubKey, algosdk.ABIType.from('(uint64,uint64,uint8,uint64,uint64)'))).valueOf() as bigint[];
const metadata: Metadata = {
start: boxValue[0], end: boxValue[1], status: boxValue[2], endSize: boxValue[3],
};
const numBoxes = Math.floor(data.byteLength / MAX_BOX_SIZE);
const boxData: Buffer[] = [];
for (let i = 0; i < numBoxes; i += 1) {
const box = data.subarray(i * MAX_BOX_SIZE, (i + 1) * MAX_BOX_SIZE);
boxData.push(box);
}
boxData.push(data.subarray(numBoxes * MAX_BOX_SIZE, data.byteLength));
const suggestedParams: SuggestedParamsWithMinFee = await algodClient.getTransactionParams().do();
const boxPromises = boxData.map(async (box, boxIndexOffset) => {
const boxIndex = metadata.start + BigInt(boxIndexOffset);
const numChunks = Math.ceil(box.byteLength / BYTES_PER_CALL);
const chunks: Buffer[] = [];
for (let i = 0; i < numChunks; i += 1) {
chunks.push(box.subarray(i * BYTES_PER_CALL, (i + 1) * BYTES_PER_CALL));
}
const boxRef = { appIndex: 0, name: algosdk.encodeUint64(boxIndex) };
const boxes: algosdk.BoxReference[] = new Array(7).fill(boxRef);
boxes.push({ appIndex: 0, name: pubKey });
const firstGroup = chunks.slice(0, 8);
const secondGroup = chunks.slice(8);
await sendTxGroup(algodClient, appClient.getABIMethod('upload')!, 0, pubKey, boxes, boxIndex, suggestedParams, sender, appID, firstGroup);
if (secondGroup.length === 0) return;
await sendTxGroup(algodClient, appClient.getABIMethod('upload')!, 8, pubKey, boxes, boxIndex, suggestedParams, sender, appID, secondGroup);
});
await Promise.all(boxPromises);
if (Buffer.concat(boxData).toString('hex') !== data.toString('hex')) throw new Error('Data validation failed!');
await appClient.call({
method: 'finishUpload',
methodArgs: [pubKey],
boxes: [
pubKey,
],
sendParams: { suppressLog: true },
});
return metadata;
}
/*
export async function uploadDIDDocument(
data: Buffer,
appID: number,
pubKey: Uint8Array,
sender: algosdk.Account,
algodClient: algosdk.Algodv2,
): Promise<Metadata> {
*/
export async function deleteDIDDocument(
appID: number,
pubKey: Uint8Array,
sender: algosdk.Account,
algodClient: algosdk.Algodv2,
): Promise<void> {
const appClient = new ApplicationClient({
resolveBy: 'id',
id: appID,
sender: algosdk.generateAccount(),
app: JSON.stringify(appSpec),
}, algodClient);
const boxValue = (await appClient.getBoxValueFromABIType(pubKey, algosdk.ABIType.from('(uint64,uint64,uint8,uint64,uint64)'))).valueOf() as bigint[];
const metadata: Metadata = {
start: boxValue[0], end: boxValue[1], status: boxValue[2], endSize: boxValue[3],
};
await appClient.call({
method: 'startDelete',
methodArgs: [pubKey],
boxes: [
pubKey,
],
sender,
sendParams: { suppressLog: true },
});
const suggestedParams = await algodClient.getTransactionParams().do();
const atcs: {boxIndex: bigint, atc: algosdk.AtomicTransactionComposer}[] = [];
for (let boxIndex = metadata.start; boxIndex <= metadata.end; boxIndex += 1n) {
const atc = new algosdk.AtomicTransactionComposer();
const boxIndexRef = { appIndex: appID, name: algosdk.encodeUint64(boxIndex) };
atc.addMethodCall({
appID,
method: appClient.getABIMethod('deleteData')!,
methodArgs: [pubKey, boxIndex],
boxes: [
{ appIndex: appID, name: pubKey },
boxIndexRef,
boxIndexRef,
boxIndexRef,
boxIndexRef,
boxIndexRef,
boxIndexRef,
boxIndexRef,
],
suggestedParams: { ...suggestedParams, fee: 2000, flatFee: true },
sender: sender.addr,
signer: algosdk.makeBasicAccountTransactionSigner(sender),
});
for (let i = 0; i < 4; i += 1) {
atc.addMethodCall({
appID,
method: appClient.getABIMethod('dummy')!,
methodArgs: [],
boxes: [
boxIndexRef,
boxIndexRef,
boxIndexRef,
boxIndexRef,
boxIndexRef,
boxIndexRef,
boxIndexRef,
boxIndexRef,
],
suggestedParams,
sender: sender.addr,
signer: algosdk.makeBasicAccountTransactionSigner(sender),
note: new Uint8Array(Buffer.from(`dummy ${i}`)),
});
}
atcs.push({ atc, boxIndex });
}
for await (const atcAndIndex of atcs) {
await tryExecute(atcAndIndex.atc, algodClient);
}
}
export async function updateDIDDocument(
data: Buffer,
appID: number,
pubKey: Uint8Array,
sender: algosdk.Account,
algodClient: algosdk.Algodv2,
): Promise<Metadata> {
await deleteDIDDocument(appID, pubKey, sender, algodClient);
return uploadDIDDocument(data, appID, pubKey, sender, algodClient);
}