Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.

Commit 22bebcf

Browse files
committed
EIT-2337 | add new client and fix test
1 parent f97ba94 commit 22bebcf

File tree

6 files changed

+66
-49
lines changed

6 files changed

+66
-49
lines changed

src/client.ts

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@ import { AnchorService } from './anchor/service/anchor.service'
44
import { NetworkConfiguration } from './config/entity/configuration.entity'
55
import Network from './config/entity/networks.entity'
66
import { ConfigService } from './config/service/config.service'
7-
import { EncryptData } from './encryption/entity/encrypt_data'
8-
import { EncryptionService } from './encryption/service/encryption.service'
97
import { HttpClient } from './infrastructure/http.client'
108
import { Proof } from './proof/entity/proof.entity'
119
import { ProofService } from './proof/service/proof.service'
1210
import { RecordReceipt } from './record/entity/record-receipt.entity'
1311
import { Record } from './record/entity/record.entity'
1412
import { RecordService } from './record/service/record.service'
1513
import { DependencyInjection } from './shared/dependency-injection'
16-
import { TypedArray } from './shared/utils'
1714

1815
/**
1916
* Entrypoint to the Bloock SDK:
@@ -31,8 +28,6 @@ export class BloockClient {
3128

3229
private httpClient: HttpClient
3330

34-
private encryptionService: EncryptionService
35-
3631
/**
3732
* Constructor with API Key that enables accessing to Bloock's functionalities.
3833
* @param {string} apiKey Client API Key.
@@ -45,7 +40,6 @@ export class BloockClient {
4540
this.configService = container.resolve<ConfigService>('ConfigService')
4641
this.recordService = container.resolve<RecordService>('RecordService')
4742
this.proofService = container.resolve<ProofService>('ProofService')
48-
this.encryptionService = container.resolve<EncryptionService>('EncryptionService')
4943

5044
this.httpClient = container.resolve<HttpClient>('HttpClient')
5145

@@ -154,27 +148,4 @@ export class BloockClient {
154148
public async verifyRecords(records: Record[], network?: Network): Promise<number> {
155149
return this.proofService.verifyRecords(records, network)
156150
}
157-
158-
/**
159-
* Verifies if the specified integrity Proof is valid.
160-
*
161-
* @param {Proof} Proof to validate.
162-
* @return {Record} Integrity proof's root record.
163-
* @throws {ProofNotFoundException} Proof not found.
164-
*/
165-
public async verifySignatures(records: Record[]): Promise<boolean> {
166-
return this.proofService.verifySignatures(records)
167-
}
168-
169-
public async generateSecretKey(): Promise<string> {
170-
return this.encryptionService.generateSecretKey()
171-
}
172-
173-
public async encryptData(data: TypedArray, secret: string): Promise<EncryptData> {
174-
return this.encryptionService.encrypt(data, secret)
175-
}
176-
177-
public async decryptData(encrypt_data: EncryptData, secret: string): Promise<TypedArray> {
178-
return this.encryptionService.decrypt(encrypt_data, secret)
179-
}
180151
}

src/encryption_client.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { container } from 'tsyringe';
2+
import { EncryptData } from './encryption/entity/encrypt_data';
3+
import { EncryptionService } from "./encryption/service/encryption.service";
4+
import { HttpClient } from './infrastructure/http.client';
5+
import { DependencyInjection } from "./shared/dependency-injection";
6+
import { TypedArray } from './shared/utils';
7+
8+
9+
export class BloockEncryptionClient {
10+
private encryptionService: EncryptionService
11+
private httpClient: HttpClient
12+
13+
constructor(apiKey: string) {
14+
DependencyInjection.setUp()
15+
16+
this.encryptionService = container.resolve<EncryptionService>('EncryptionService')
17+
this.httpClient = container.resolve<HttpClient>('HttpClient')
18+
this.httpClient.setApiKey(apiKey)
19+
}
20+
21+
public async generateSecretKey(): Promise<string> {
22+
return this.encryptionService.generateSecretKey()
23+
}
24+
25+
public async encryptData(data: TypedArray, secret: string): Promise<EncryptData> {
26+
return this.encryptionService.encrypt(data, secret)
27+
}
28+
29+
public async decryptData(encrypt_data: EncryptData, secret: string): Promise<TypedArray> {
30+
return this.encryptionService.decrypt(encrypt_data, secret)
31+
}
32+
}

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import { BloockClient } from './client'
33
import { NetworkConfiguration } from './config/entity/configuration.entity'
44
import Network from './config/entity/networks.entity'
55
import { EncryptData } from './encryption/entity/encrypt_data'
6+
import { BloockEncryptionClient } from './encryption_client'
67
import { Proof } from './proof/entity/proof.entity'
78
import { RecordReceipt } from './record/entity/record-receipt.entity'
89
import { Record } from './record/entity/record.entity'
910

10-
export { BloockClient, Record, RecordReceipt, Proof, Network, NetworkConfiguration, EncryptData }
11+
export { BloockClient, BloockEncryptionClient, Record, RecordReceipt, Proof, Network, NetworkConfiguration, EncryptData }

src/record/service/record-impl.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ export class RecordServiceImpl implements RecordService {
5454
if (response == null) {
5555
return []
5656
}
57-
57+
5858
return response.map(
59-
(record) =>
59+
(record) =>
6060
new RecordReceipt(
6161
record.anchor || 0,
6262
record.client || '',
6363
record.message || '',
6464
record.status || ''
65-
)
65+
)
6666
)
6767
}
6868
}

test/acceptance.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ describe('Acceptance Tests', () => {
188188

189189
await expect(sdk.getProof(records)).rejects.toEqual(
190190
new HttpRequestException(
191-
"Record '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef' not found."
191+
"Internal Server Error"
192192
)
193193
)
194194
})
@@ -302,7 +302,7 @@ describe('Acceptance Tests', () => {
302302

303303
await expect(sdk.verifyRecords(records, Network.BLOOCK_CHAIN)).rejects.toEqual(
304304
new HttpRequestException(
305-
"Record '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef' not found."
305+
"Internal Server Error"
306306
)
307307
)
308308
})

test/functional.test.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fs from 'fs'
2-
import { BloockClient, Network, Record, RecordReceipt } from '../src'
2+
import { BloockClient, BloockEncryptionClient, Network, Record, RecordReceipt } from '../src'
33
import { Anchor } from '../src/anchor/entity/anchor.entity'
44
import { PDFDocument } from '../src/record/entity/document/pdf'
55

@@ -11,7 +11,14 @@ function getSdk(): BloockClient {
1111
return client
1212
}
1313

14+
function getEncryptionSDK(): BloockEncryptionClient {
15+
const apiKey = process.env['API_KEY'] || ''
16+
let client = new BloockEncryptionClient(apiKey)
17+
return client
18+
}
19+
1420
describe('Functional Tests', () => {
21+
1522
test('testSendRecord', async () => {
1623
jest.setTimeout(120000)
1724

@@ -38,9 +45,9 @@ describe('Functional Tests', () => {
3845
const sdk = getSdk()
3946

4047
const records = [
41-
Record.fromString('Example Data 1'),
42-
Record.fromString('Example Data 2'),
43-
Record.fromString('Example Data 3')
48+
Record.fromString('Example Data 4'),
49+
Record.fromString('Example Data 5'),
50+
Record.fromString('Example Data 6')
4451
]
4552

4653
const sendReceipt = await sdk.sendRecords(records)
@@ -64,9 +71,9 @@ describe('Functional Tests', () => {
6471
const sdk = getSdk()
6572

6673
const records = [
67-
Record.fromString('Example Data 1'),
68-
Record.fromString('Example Data 2'),
69-
Record.fromString('Example Data 3')
74+
Record.fromString('Example Data 7'),
75+
Record.fromString('Example Data 8'),
76+
Record.fromString('Example Data 9')
7077
]
7178

7279
const sendReceipt = await sdk.sendRecords(records)
@@ -90,9 +97,9 @@ describe('Functional Tests', () => {
9097
const sdk = getSdk()
9198

9299
const records = [
93-
Record.fromString('Example Data 1'),
94-
Record.fromString('Example Data 2'),
95-
Record.fromString('Example Data 3')
100+
Record.fromString('Example Data 4'),
101+
Record.fromString('Example Data 5'),
102+
Record.fromString('Example Data 6')
96103
]
97104

98105
let proof = await sdk.getProof(records)
@@ -105,9 +112,9 @@ describe('Functional Tests', () => {
105112
const sdk = getSdk()
106113

107114
const records = [
108-
Record.fromString('Example Data 1'),
109-
Record.fromString('Example Data 2'),
110-
Record.fromString('Example Data 3')
115+
Record.fromString('Example Data 4'),
116+
Record.fromString('Example Data 5'),
117+
Record.fromString('Example Data 6')
111118
]
112119

113120
let proof = await sdk.getProof(records)
@@ -123,7 +130,7 @@ describe('Functional Tests', () => {
123130
test('testEncryptDocument', async () => {
124131
jest.setTimeout(120000)
125132

126-
const sdk = getSdk()
133+
const sdk = getEncryptionSDK()
127134

128135
const bytes = fs.readFileSync('./test/assets/dummy.pdf')
129136
let file = new PDFDocument(bytes)
@@ -141,3 +148,9 @@ describe('Functional Tests', () => {
141148
expect(decryptedData).toEqual(file.getDataBytes())
142149
})
143150
})
151+
152+
function sleep(ms: number | undefined) {
153+
return new Promise((resolve) => {
154+
setTimeout(resolve, ms);
155+
});
156+
}

0 commit comments

Comments
 (0)