-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAggregatorClient.ts
More file actions
68 lines (61 loc) · 2.31 KB
/
AggregatorClient.ts
File metadata and controls
68 lines (61 loc) · 2.31 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
import { CertificationData } from './CertificationData.js';
import { CertificationRequest } from './CertificationRequest.js';
import { CertificationResponse } from './CertificationResponse.js';
import { IAggregatorClient } from './IAggregatorClient.js';
import { InclusionProofResponse } from './InclusionProofResponse.js';
import { JsonRpcHttpTransport } from './json-rpc/JsonRpcHttpTransport.js';
import { StateId } from './StateId.js';
import { HexConverter } from '../serialization/HexConverter.js';
/**
* Client implementation for communicating with an aggregator via JSON-RPC.
*/
export class AggregatorClient implements IAggregatorClient {
private readonly transport: JsonRpcHttpTransport;
/**
* Create a new client pointing to the given aggregator URL.
*
* @param url Base URL of the aggregator JSON-RPC endpoint
* @param key API key for authenticating
*/
public constructor(
url: string,
private readonly key: string | null = null,
) {
this.transport = new JsonRpcHttpTransport(url);
}
public async getBlockHeight(): Promise<bigint> {
const response = await this.transport.request('get_block_height', {});
if (
response &&
typeof response === 'object' &&
'blockNumber' in response &&
(typeof response.blockNumber === 'string' ||
typeof response.blockNumber === 'number' ||
typeof response.blockNumber === 'bigint')
) {
return BigInt(response.blockNumber);
}
throw new Error('Invalid response format for block height');
}
/**
* @inheritDoc
*/
public async getInclusionProof(stateId: StateId): Promise<InclusionProofResponse> {
const data = { stateId: HexConverter.encode(stateId.data) };
return InclusionProofResponse.fromCBOR(
HexConverter.decode((await this.transport.request('get_inclusion_proof.v2', data)) as string),
);
}
/**
* @inheritDoc
*/
public async submitCertificationRequest(certificationData: CertificationData): Promise<CertificationResponse> {
const request = await CertificationRequest.create(certificationData);
const response = await this.transport.request(
'certification_request',
HexConverter.encode(request.toCBOR()),
this.key ? new Headers([['X-API-Key', this.key]]) : undefined,
);
return CertificationResponse.fromJSON(response);
}
}