-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgo-did.test.ts
187 lines (157 loc) · 5.22 KB
/
algo-did.test.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
/* eslint-disable no-plusplus */
import * as algokit from "@algorandfoundation/algokit-utils";
import fs from "fs";
import { ApplicationClient } from "@algorandfoundation/algokit-utils/types/app-client";
import { describe, expect, beforeAll, it, jest } from "@jest/globals";
import algosdk from "algosdk";
import { algodClient, kmdClient } from "./common";
import appSpec from "../contracts/artifacts/AlgoDID.json";
import {
resolveDID,
uploadDIDDocument,
deleteDIDDocument,
updateDIDDocument,
} from "../src/index";
jest.setTimeout(20000);
describe("Algorand DID", () => {
/**
* Large data (> 32k) to simulate a large DID Document
* that needs to be put into multiple boxes
*/
const bigData = fs.readFileSync(`${__dirname}/TEAL.pdf`);
/**
* Small data (< 32k) to simulate a small DID Document
* that can fit into a single box
*/
const smallJSONObject = { keyOne: "foo", keyTwo: "bar" };
/** The public key for the user in the tests that has a big DID Document */
const bigDataUserKey = algosdk.decodeAddress(
algosdk.generateAccount().addr
).publicKey;
/** The public key for the user in the tests that has a small DID Document */
const smallDataUserKey = algosdk.decodeAddress(
algosdk.generateAccount().addr
).publicKey;
/** The public key for the user in the tests that updates their DID Document */
const updateDataUserKey = algosdk.decodeAddress(
algosdk.generateAccount().addr
).publicKey;
/** algokit appClient for interacting with the contract */
let appClient: ApplicationClient;
/** The account that will be used to create and call the contract */
let sender: algosdk.Account;
/** The ID of the contract */
let appId: number;
beforeAll(async () => {
sender = await algokit.getDispenserAccount(algodClient, kmdClient);
appClient = new ApplicationClient(
{
resolveBy: "id",
id: 0,
sender,
app: JSON.stringify(appSpec),
},
algodClient
);
await appClient.create({
method: "createApplication",
methodArgs: [],
sendParams: { suppressLog: true },
});
await appClient.fundAppAccount({
amount: algokit.microAlgos(100_000),
sendParams: { suppressLog: true },
});
appId = Number((await appClient.getAppReference()).appId);
});
describe("uploadDIDDocument and Resolve", () => {
it("(LARGE) DIDocument upload and resolve", async () => {
// const { appId } = await appClient.getAppReference();
const addr = algosdk.encodeAddress(bigDataUserKey);
// Large upload
await uploadDIDDocument(
bigData,
Number(appId),
bigDataUserKey,
sender,
algodClient
);
// Reconstruct DID from several boxes
const resolvedData: Buffer = await resolveDID(
`did:algo:${addr}-${appId}`,
algodClient
);
expect(resolvedData.toString("hex")).toEqual(bigData.toString("hex"));
});
it("(SMALL) DIDocument upload and resolve", async () => {
// const { appId } = await appClient.getAppReference();
const addr = algosdk.encodeAddress(smallDataUserKey);
// Small upload
await uploadDIDDocument(
Buffer.from(JSON.stringify(smallJSONObject)),
Number(appId),
smallDataUserKey,
sender,
algodClient
);
// Reconstruct DID from several boxes
const resolvedData: Buffer = await resolveDID(
`did:algo:${addr}-${appId}`,
algodClient
);
expect(resolvedData.toString("hex")).toEqual(
Buffer.from(JSON.stringify(smallJSONObject)).toString("hex")
);
});
});
describe("deleteDIDDocument", () => {
const deleteDIDDocumentTest = async (userKey: Uint8Array) => {
await deleteDIDDocument(appId, userKey, sender, algodClient);
const addr = algosdk.encodeAddress(userKey);
await expect(
resolveDID(`did:algo:${addr}-${appId}`, algodClient)
).rejects.toThrow();
};
it("deletes big (multi-box) data", async () => {
await deleteDIDDocumentTest(bigDataUserKey);
});
it("deletes small (single-box) data", async () => {
await deleteDIDDocumentTest(smallDataUserKey);
});
it("returns MBR", async () => {
const { appAddress } = await appClient.getAppReference();
const appAmount = (await algodClient.accountInformation(appAddress).do())
.amount;
expect(appAmount).toBe(100_000);
});
});
describe("updateDocument", () => {
beforeAll(async () => {
// Initially upload the big data as the DID Document
await uploadDIDDocument(
bigData,
appId,
updateDataUserKey,
sender,
algodClient
);
});
it("uploads and resolves new data", async () => {
// Update the DID Document to be the small data
const data = Buffer.from(JSON.stringify(smallJSONObject));
await updateDIDDocument(
data,
appId,
updateDataUserKey,
sender,
algodClient
);
const addr = algosdk.encodeAddress(updateDataUserKey);
const resolvedData = await resolveDID(
`did:algo:${addr}-${appId}`,
algodClient
);
expect(resolvedData.toString()).toEqual(JSON.stringify(smallJSONObject));
});
});
});