Skip to content

Commit c0ed7e6

Browse files
removed account field from RecordScanner
1 parent 6c2d52a commit c0ed7e6

File tree

3 files changed

+42
-93
lines changed

3 files changed

+42
-93
lines changed

sdk/src/record-scanner.ts

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
import { Account } from "./account";
21
import { EncryptedRecord } from "./models/record-provider/encryptedRecord";
32
import { OwnedFilter } from "./models/record-scanner/ownedFilter";
43
import { OwnedRecord } from "./models/record-provider/ownedRecord";
54
import { RecordProvider } from "./record-provider";
65
import { Field, Poseidon4, RecordPlaintext, ViewKey } from "./wasm";
7-
import { RecordSearchParams } from "./models/record-provider/recordSearchParams";
86
import { RecordsFilter } from "./models/record-scanner/recordsFilter";
9-
import { RecordsResponseFilter } from "./models/record-scanner/recordsResponseFilter";
107
import { RegistrationRequest } from "./models/record-scanner/registrationRequest";
118
import { RegistrationResponse } from "./models/record-scanner/registrationResponse";
129
import { StatusResponse } from "./models/record-scanner/statusResponse";
1310
import { RECORD_DOMAIN } from "./constants";
1411

1512
type RecordScannerOptions = {
1613
url: string;
17-
account?: Account;
1814
apiKey?: string | { header: string, value: string };
1915
}
2016

@@ -60,28 +56,13 @@ type RecordScannerOptions = {
6056
* const records = await recordScanner.findRecords(filter);
6157
*/
6258
class RecordScanner implements RecordProvider {
63-
account?: Account;
6459
readonly url: string;
6560
private apiKey?: { header: string, value: string };
66-
private uuid?: string;
61+
private uuid?: Field;
6762

6863
constructor(options: RecordScannerOptions) {
6964
this.url = options.url;
70-
this.account = options.account;
7165
this.apiKey = typeof options.apiKey === "string" ? { header: "X-Provable-API-Key", value: options.apiKey } : options.apiKey;
72-
if (this.account) {
73-
this.uuid = this.computeUUID(this.account.viewKey());
74-
}
75-
}
76-
77-
/**
78-
* Set the account to use for the record scanner.
79-
*
80-
* @param {Account} account The account to use for the record scanner.
81-
*/
82-
async setAccount(account: Account): Promise<void> {
83-
this.account = account;
84-
this.uuid = this.computeUUID(account.viewKey());
8566
}
8667

8768
/**
@@ -93,24 +74,28 @@ class RecordScanner implements RecordProvider {
9374
this.apiKey = typeof apiKey === "string" ? { header: "X-Provable-API-Key", value: apiKey } : apiKey;
9475
}
9576

77+
/**
78+
* Set the UUID to use for the record scanner.
79+
*
80+
* @param {Field} uuid The UUID to use for the record scanner.
81+
*/
82+
async setUuid(uuidOrViewKey: Field | ViewKey): Promise<void> {
83+
this.uuid = uuidOrViewKey instanceof ViewKey ? this.computeUUID(uuidOrViewKey) : uuidOrViewKey;
84+
}
85+
9686
/**
9787
* Register the account with the record scanner service.
9888
*
9989
* @param {number} startBlock The block height to start scanning from.
10090
* @returns {Promise<RegistrationResponse>} The response from the record scanner service.
10191
*/
102-
async register(startBlock: number): Promise<RegistrationResponse> {
103-
let request: RegistrationRequest;
104-
if (!this.account) {
105-
throw new Error("Account not set");
106-
} else {
107-
request = {
108-
view_key: this.account.viewKey().to_string(),
92+
async register(viewKey: ViewKey, startBlock: number): Promise<RegistrationResponse> {
93+
try {
94+
let request: RegistrationRequest = {
95+
view_key: viewKey.to_string(),
10996
start: startBlock,
11097
};
111-
}
11298

113-
try {
11499
const response = await this.request(
115100
new Request(`${this.url}/register`, {
116101
method: "POST",
@@ -252,7 +237,7 @@ class RecordScanner implements RecordProvider {
252237
throw new Error("You are using the RecordScanner implementation of the RecordProvider. No account has been registered with the RecordScanner which is required to use the findRecords method. Please set an with the setAccount method before calling the findRecords method again.");
253238
}
254239

255-
filter.uuid = this.uuid;
240+
filter.uuid = this.uuid?.toString();
256241

257242
try {
258243
const response = await this.request(
@@ -287,7 +272,7 @@ class RecordScanner implements RecordProvider {
287272
program: "credits.aleo",
288273
record: "credits",
289274
},
290-
uuid: this.uuid,
275+
uuid: this.uuid?.toString(),
291276
});
292277

293278
const record = records.find(record => {
@@ -325,7 +310,7 @@ class RecordScanner implements RecordProvider {
325310
program: "credits.aleo",
326311
record: "credits",
327312
},
328-
uuid: this.uuid,
313+
uuid: this.uuid?.toString(),
329314
});
330315
return records.filter(record => {
331316
const plaintext = RecordPlaintext.fromString(record.record_plaintext ?? '');
@@ -364,7 +349,7 @@ class RecordScanner implements RecordProvider {
364349

365350
private computeUUID(vk: ViewKey): Field {
366351
// Construct the material needed for the Poseidon oracle.
367-
const inputs = [Field.newDomainSeparator(RECORD_DOMAIN), vk().toField(), Field.one()]
352+
const inputs = [Field.newDomainSeparator(RECORD_DOMAIN), vk.toField(), Field.one()]
368353
// Calculate the uuid.
369354
const hasher = new Poseidon4();
370355
return hasher.hash(inputs);

sdk/tests/record-scanner.test.ts

Lines changed: 23 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,8 @@ describe("RecordScanner", () => {
2626
expect(recordScanner.url).equal("https://record-scanner.aleo.org");
2727
});
2828

29-
it("should intialize with the correct account", async () => {
30-
recordScanner = new RecordScanner({ url: "https://record-scanner.aleo.org", account: defaultAccount });
31-
expect(recordScanner.account).equal(defaultAccount);
32-
});
33-
3429
it("should intialize with the correct api key as a string", async () => {
35-
recordScanner = new RecordScanner({ url: "https://record-scanner.aleo.org", account: defaultAccount, apiKey: "1234567890" });
30+
recordScanner = new RecordScanner({ url: "https://record-scanner.aleo.org", apiKey: "1234567890" });
3631

3732
const mockResponse = {
3833
ok: true,
@@ -42,14 +37,14 @@ describe("RecordScanner", () => {
4237
};
4338

4439
fetchStub.resolves(mockResponse);
45-
await recordScanner.register(0);
40+
await recordScanner.register(defaultAccount.viewKey(), 0);
4641

4742
const request = fetchStub.firstCall.args[0] as Request;
4843
expect(request.headers.get("X-Provable-API-Key")).to.equal("1234567890");
4944
});
5045

5146
it("should intialize with the correct api key as an object", async () => {
52-
recordScanner = new RecordScanner({ url: "https://record-scanner.aleo.org", account: defaultAccount, apiKey: { header: "Some-API-Key", value: "1234567890" } });
47+
recordScanner = new RecordScanner({ url: "https://record-scanner.aleo.org", apiKey: { header: "Some-API-Key", value: "1234567890" } });
5348

5449
const mockResponse = {
5550
ok: true,
@@ -59,14 +54,14 @@ describe("RecordScanner", () => {
5954
};
6055

6156
fetchStub.resolves(mockResponse);
62-
await recordScanner.register(0);
57+
await recordScanner.register(defaultAccount.viewKey(), 0);
6358

6459
const request = fetchStub.firstCall.args[0] as Request;
6560
expect(request.headers.get("Some-API-Key")).to.equal("1234567890");
6661
});
6762

6863
it("should return RegistrationResponse after successfully registering the account", async () => {
69-
recordScanner = new RecordScanner({ url: "https://record-scanner.aleo.org", account: defaultAccount });
64+
recordScanner = new RecordScanner({ url: "https://record-scanner.aleo.org" });
7065

7166
const mockResponse = {
7267
ok: true,
@@ -76,7 +71,7 @@ describe("RecordScanner", () => {
7671
};
7772

7873
fetchStub.resolves(mockResponse);
79-
const registrationResponse = await recordScanner.register(0);
74+
const registrationResponse = await recordScanner.register(defaultAccount.viewKey(), 0);
8075

8176
expect(fetchStub.calledOnce).to.be.true;
8277
const request = fetchStub.firstCall.args[0] as Request;
@@ -92,7 +87,7 @@ describe("RecordScanner", () => {
9287
});
9388

9489
it("should return the optional fields of RegistrationResponse if present after successfully registering the account", async () => {
95-
recordScanner = new RecordScanner({ url: "https://record-scanner.aleo.org", account: defaultAccount });
90+
recordScanner = new RecordScanner({ url: "https://record-scanner.aleo.org" });
9691

9792
const mockResponse = {
9893
ok: true,
@@ -102,7 +97,7 @@ describe("RecordScanner", () => {
10297
};
10398

10499
fetchStub.resolves(mockResponse);
105-
const registrationResponse = await recordScanner.register(0);
100+
const registrationResponse = await recordScanner.register(defaultAccount.viewKey(), 0);
106101

107102
expect(fetchStub.calledOnce).to.be.true;
108103
const request = fetchStub.firstCall.args[0] as Request;
@@ -119,22 +114,9 @@ describe("RecordScanner", () => {
119114
expect(registrationResponse.status).equal("pending");
120115
});
121116

122-
it("should throw an error if the account is not set", async () => {
123-
recordScanner = new RecordScanner({ url: "https://record-scanner.aleo.org" });
124-
let failed = false;
125-
try {
126-
await recordScanner.register(0);
127-
} catch (err: any) {
128-
expect(err).to.be.instanceOf(Error);
129-
expect(err.message).to.equal("Account not set");
130-
failed = true;
131-
}
132-
expect(failed).to.be.true;
133-
});
134-
135117

136118
it("should return EncryptedRecord[] after successfully getting encrypted records", async () => {
137-
recordScanner = new RecordScanner({ url: "https://record-scanner.aleo.org", account: defaultAccount });
119+
recordScanner = new RecordScanner({ url: "https://record-scanner.aleo.org" });
138120
const mockResponse = {
139121
ok: true,
140122
status: 200,
@@ -178,17 +160,8 @@ describe("RecordScanner", () => {
178160
});
179161

180162
it("should return OwnedRecord[] after successfully getting owned records", async () => {
181-
recordScanner = new RecordScanner({ url: "https://record-scanner.aleo.org", account: defaultAccount });
182-
const mockRegisterResponse = {
183-
ok: true,
184-
status: 201,
185-
text: () => Promise.resolve('{"uuid": "test-uuid"}'),
186-
json: () => Promise.resolve({ uuid: "test-uuid" })
187-
};
188-
fetchStub.resolves(mockRegisterResponse);
189-
await recordScanner.register(0);
190-
191-
fetchStub.resetHistory();
163+
recordScanner = new RecordScanner({ url: "https://record-scanner.aleo.org" });
164+
recordScanner.setUuid(defaultAccount.viewKey());
192165

193166
const mockResponse = {
194167
ok: true,
@@ -235,17 +208,8 @@ describe("RecordScanner", () => {
235208
});
236209

237210
it("should return OwnedRecord after successfully getting owned record", async () => {
238-
recordScanner = new RecordScanner({ url: "https://record-scanner.aleo.org", account: defaultAccount });
239-
const mockRegisterResponse = {
240-
ok: true,
241-
status: 201,
242-
text: () => Promise.resolve('{"uuid": "test-uuid"}'),
243-
json: () => Promise.resolve({ uuid: "test-uuid" })
244-
};
245-
fetchStub.resolves(mockRegisterResponse);
246-
await recordScanner.register(0);
247-
248-
fetchStub.resetHistory();
211+
recordScanner = new RecordScanner({ url: "https://record-scanner.aleo.org" });
212+
recordScanner.setUuid(defaultAccount.viewKey());
249213

250214
const mockResponse = {
251215
ok: true,
@@ -262,7 +226,7 @@ describe("RecordScanner", () => {
262226
const request = fetchStub.firstCall.args[0] as Request;
263227
const body = await request.text();
264228
const expectedBody = JSON.stringify({
265-
uuid: "test-uuid",
229+
uuid: "7884164224800444110633570141944665301008802280502652120359195870264061098703field",
266230
});
267231
expect(body).to.equal(expectedBody);
268232
expect(request.url).to.equal("https://record-scanner.aleo.org/records/owned");
@@ -271,7 +235,7 @@ describe("RecordScanner", () => {
271235
});
272236

273237
it("should throw an error if the uuid is not registered", async () => {
274-
recordScanner = new RecordScanner({ url: "https://record-scanner.aleo.org", account: defaultAccount });
238+
recordScanner = new RecordScanner({ url: "https://record-scanner.aleo.org" });
275239
let failed = false;
276240
try {
277241
await recordScanner.findRecords({
@@ -290,14 +254,14 @@ describe("RecordScanner", () => {
290254
});
291255
} catch (err: any) {
292256
expect(err).to.be.instanceOf(Error);
293-
expect(err.message).to.equal("Not registered");
257+
expect(err.message).to.equal("You are using the RecordScanner implementation of the RecordProvider. No account has been registered with the RecordScanner which is required to use the findRecords method. Please set an with the setAccount method before calling the findRecords method again.");
294258
failed = true;
295259
}
296260
expect(failed).to.be.true;
297261
});
298262

299263
it("should return record of string->boolean after successfully checking serial numbers", async () => {
300-
recordScanner = new RecordScanner({ url: "https://record-scanner.aleo.org", account: defaultAccount });
264+
recordScanner = new RecordScanner({ url: "https://record-scanner.aleo.org" });
301265
const mockResponse = {
302266
ok: true,
303267
status: 200,
@@ -324,7 +288,7 @@ describe("RecordScanner", () => {
324288
});
325289

326290
it("should return record of string->boolean after successfully checking tags", async () => {
327-
recordScanner = new RecordScanner({ url: "https://record-scanner.aleo.org", account: defaultAccount });
291+
recordScanner = new RecordScanner({ url: "https://record-scanner.aleo.org" });
328292
const mockResponse = {
329293
ok: true,
330294
status: 200,
@@ -353,7 +317,7 @@ describe("RecordScanner", () => {
353317
});
354318

355319
it("should return StatusResponse after successfully checking status", async () => {
356-
recordScanner = new RecordScanner({ url: "https://record-scanner.aleo.org", account: defaultAccount });
320+
recordScanner = new RecordScanner({ url: "https://record-scanner.aleo.org" });
357321
const mockResponse = {
358322
ok: true,
359323
status: 200,
@@ -374,7 +338,7 @@ describe("RecordScanner", () => {
374338
});
375339

376340
it("should handle HTTP errors", async () => {
377-
recordScanner = new RecordScanner({ url: "https://record-scanner.aleo.org", account: defaultAccount });
341+
recordScanner = new RecordScanner({ url: "https://record-scanner.aleo.org" });
378342
let mockResponse = {
379343
ok: false,
380344
status: 500,
@@ -384,7 +348,7 @@ describe("RecordScanner", () => {
384348
fetchStub.resolves(mockResponse);
385349
let failed = false;
386350
try {
387-
await recordScanner.register(0);
351+
await recordScanner.register(defaultAccount.viewKey(), 0);
388352
} catch (err: any) {
389353
expect(err).to.be.instanceOf(Error);
390354
expect(err.message).to.equal('{"error": "Internal server error"}');
@@ -401,7 +365,7 @@ describe("RecordScanner", () => {
401365
fetchStub.resolves(mockResponse);
402366
failed = false;
403367
try {
404-
await recordScanner.register(0);
368+
await recordScanner.register(defaultAccount.viewKey(), 0);
405369
} catch (err: any) {
406370
expect(err).to.be.instanceOf(Error);
407371
expect(err.message).to.equal('{"error": "Invalid view key"}');
@@ -412,7 +376,7 @@ describe("RecordScanner", () => {
412376
fetchStub.rejects(new Error("Unknown error"));
413377
failed = false;
414378
try {
415-
await recordScanner.register(0);
379+
await recordScanner.register(defaultAccount.viewKey(), 0);
416380
} catch (err: any) {
417381
expect(err).to.be.instanceOf(Error);
418382
expect(err.message).to.equal("Unknown error");

wasm/src/types/field.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl Field {
117117

118118
/// Initializes a new field as a domain separator.
119119
#[wasm_bindgen(js_name = "newDomainSeparator")]
120-
pub fn new_domain_separator(&self, domain: &str) -> Field {
120+
pub fn new_domain_separator(domain: &str) -> Field {
121121
let domain_native =
122122
FieldNative::new(<CurrentNetwork as Environment>::Field::from_bytes_le_mod_order(domain.as_bytes()));
123123
Field::from(domain_native)

0 commit comments

Comments
 (0)