Skip to content
This repository has been archived by the owner on Jan 2, 2024. It is now read-only.

Commit

Permalink
improved Ardor Cloud Data Document retrieval
Browse files Browse the repository at this point in the history
should now also work with non archival nodes
  • Loading branch information
atz3n committed Aug 9, 2020
1 parent 80b7997 commit 64cec71
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 84 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@
"typescript": "^3.7.3"
},
"dependencies": {
"@blobaa/ardor-ts": "^2.6.4"
"@blobaa/ardor-ts": "^2.7.0"
}
}
29 changes: 16 additions & 13 deletions src/modules/lib/ArdorCloudStorage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChainId, GetTransactionParams, GetTransactionResponse, IRequest, UploadTaggedDataParams } from "@blobaa/ardor-ts";
import { ChainId, DownloadTaggedDataResponse, GetTransactionResponse, IRequest, UploadTaggedDataParams } from "@blobaa/ardor-ts";
import { DATA_CLOUD_NAME } from "../../constants";
import { ErrorCode } from "../../types";
import { IDataStorage } from "../internal-types";
Expand Down Expand Up @@ -47,21 +47,24 @@ export default class ArdorCloudStorage implements IDataStorage {


public async retrieveData(reference: string): Promise<string> {
const params: GetTransactionParams = {
const txInfo = await this.request.getTransaction(this.url, {
chain: this.chainId,
fullHash: reference
};
});

const data = await this.request.downloadTaggedData(this.url, {
chain: this.chainId,
fullHash: reference,
retrieve: true
});

const response = await this.request.getTransaction(this.url, params);
return this.extractData(response);
return this.extractData(txInfo, data);
}

private extractData(response: GetTransactionResponse): Promise<string> {
const data = response.attachment.data;
const name = response.attachment.name;
const issuer = response.senderRS;
private extractData(txInfo: GetTransactionResponse, data: DownloadTaggedDataResponse): Promise<string> {
const issuer = txInfo.senderRS;

if (!this.isMethodData(data, name)) {
if (!this.isDataAvailable(data)) {
const error = ErrorHelper.createError(ErrorCode.DIDDOC_NOT_FOUND);
return Promise.reject(error);
}
Expand All @@ -71,11 +74,11 @@ export default class ArdorCloudStorage implements IDataStorage {
return Promise.reject(error);
}

return data;
return Promise.resolve(data);
}

private isMethodData(data: string | undefined, name: string | undefined): boolean {
return (data && name && name === DATA_CLOUD_NAME) ? true : false;
private isDataAvailable(data: string | undefined): boolean {
return data ? true : false;
}

private isDataSelfSet(accounts: string[], issuer: string): boolean {
Expand Down
105 changes: 57 additions & 48 deletions src/modules/lib/RequestWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BroadcastTransactionParams, BroadcastTransactionResponse, DecodeTokenParams, DecodeTokenResponse, DeleteAccountPropertyParams, GetAccountPropertiesParams, GetAccountPropertiesResponse, GetBalanceParams, GetBalanceResponse, GetBlockchainTransactionsParams, GetBlockchainTransactionsResponse, GetBundlerRatesParams, GetBundlerRatesResponse, GetTransactionParams, IRequest, objectAny, SendMessageParams, SendMoneyParams, SetAccountPropertyParams, Transaction, UploadTaggedDataParams } from "@blobaa/ardor-ts";
import { BroadcastTransactionParams, BroadcastTransactionResponse, DecodeTokenParams, DecodeTokenResponse, DeleteAccountPropertyParams, GetAccountPropertiesParams, GetAccountPropertiesResponse, GetBalanceParams, GetBalanceResponse, GetBlockchainTransactionsParams, GetBlockchainTransactionsResponse, GetBundlerRatesParams, GetBundlerRatesResponse, GetTransactionParams, IRequest, objectAny, SendMessageParams, SendMoneyParams, SetAccountPropertyParams, Transaction, UploadTaggedDataParams, DownloadTaggedDataParams } from "@blobaa/ardor-ts";
import { Error, ErrorCode } from "../../types";


Expand All @@ -11,96 +11,105 @@ export default class RequestWrapper implements IRequest {
}


public async broadcastTransaction(url: string, params: BroadcastTransactionParams): Promise<BroadcastTransactionResponse> {
throw new Error("Method not implemented.");
private getError(error: objectAny): Error {
if (error.syscall) {
return {
code: ErrorCode.CONNECTION_ERROR,
description: "Connection error. Could not connect to node."
};
}
if (error.errorCode) {
return {
code: ErrorCode.NODE_ERROR,
description: error.errorDescription
};
}
return error as Error;
}


public async decodeToken(url: string, params: DecodeTokenParams): Promise<DecodeTokenResponse> {
throw new Error("Method not implemented.");
public async downloadTaggedData(url: string, params: DownloadTaggedDataParams): Promise<string> {
try {
return await this.request.downloadTaggedData(url, params);
} catch (error) {
return Promise.reject(this.getError(error));
}
}


public async deleteAccountProperty(url: string, params: DeleteAccountPropertyParams): Promise<BroadcastTransactionResponse> {
throw new Error("Method not implemented.");
public async getBlockchainTransactions(url: string, params: GetBlockchainTransactionsParams): Promise<GetBlockchainTransactionsResponse> {
try {
return await this.request.getBlockchainTransactions(url, params);
} catch (error) {
return Promise.reject(this.getError(error));
}
}


public async getAccountProperties(url: string, params: GetAccountPropertiesParams): Promise<GetAccountPropertiesResponse> {
throw new Error("Method not implemented.");
public async getTransaction(url: string, params: GetTransactionParams): Promise<Transaction> {
try {
return await this.request.getTransaction(url, params);
} catch (error) {
return Promise.reject(this.getError(error));
}
}


public async getBalance(url: string, params: GetBalanceParams): Promise<GetBalanceResponse> {
throw new Error("Method not implemented.");
public async setAccountProperty(url: string, params: SetAccountPropertyParams): Promise<BroadcastTransactionResponse> {
try {
return await this.request.setAccountProperty(url, params);
} catch (error) {
return Promise.reject(this.getError(error));
}
}


public async getBlockchainTransactions(url: string, params: GetBlockchainTransactionsParams): Promise<GetBlockchainTransactionsResponse> {
public async uploadTaggedData(url: string, params: UploadTaggedDataParams): Promise<BroadcastTransactionResponse> {
try {
return await this.request.getBlockchainTransactions(url, params);
return await this.request.uploadTaggedData(url, params);
} catch (error) {
return Promise.reject(this.getError(error));
}
}


public async getBundlerRates(url: string, params: GetBundlerRatesParams): Promise<GetBundlerRatesResponse> {
public async broadcastTransaction(url: string, params: BroadcastTransactionParams): Promise<BroadcastTransactionResponse> {
throw new Error("Method not implemented.");
}


public async getTransaction(url: string, params: GetTransactionParams): Promise<Transaction> {
try {
return await this.request.getTransaction(url, params);
} catch (error) {
return Promise.reject(this.getError(error));
}
public async decodeToken(url: string, params: DecodeTokenParams): Promise<DecodeTokenResponse> {
throw new Error("Method not implemented.");
}


public async sendMessage(url: string, params: SendMessageParams): Promise<BroadcastTransactionResponse> {
public async deleteAccountProperty(url: string, params: DeleteAccountPropertyParams): Promise<BroadcastTransactionResponse> {
throw new Error("Method not implemented.");
}


public async sendMoney(url: string, params: SendMoneyParams): Promise<BroadcastTransactionResponse> {
public async getAccountProperties(url: string, params: GetAccountPropertiesParams): Promise<GetAccountPropertiesResponse> {
throw new Error("Method not implemented.");
}


public async setAccountProperty(url: string, params: SetAccountPropertyParams): Promise<BroadcastTransactionResponse> {
try {
return await this.request.setAccountProperty(url, params);
} catch (error) {
return Promise.reject(this.getError(error));
}
public async getBalance(url: string, params: GetBalanceParams): Promise<GetBalanceResponse> {
throw new Error("Method not implemented.");
}


public async uploadTaggedData(url: string, params: UploadTaggedDataParams): Promise<BroadcastTransactionResponse> {
try {
return await this.request.uploadTaggedData(url, params);
} catch (error) {
return Promise.reject(this.getError(error));
}
public async getBundlerRates(url: string, params: GetBundlerRatesParams): Promise<GetBundlerRatesResponse> {
throw new Error("Method not implemented.");
}


private getError(error: objectAny): Error {
if (error.syscall) {
return {
code: ErrorCode.CONNECTION_ERROR,
description: "Connection error. Could not connect to node."
};
}
if (error.errorCode) {
return {
code: ErrorCode.NODE_ERROR,
description: error.errorDescription
};
}
return error as Error;
public async sendMessage(url: string, params: SendMessageParams): Promise<BroadcastTransactionResponse> {
throw new Error("Method not implemented.");
}


public async sendMoney(url: string, params: SendMoneyParams): Promise<BroadcastTransactionResponse> {
throw new Error("Method not implemented.");
}

}
26 changes: 21 additions & 5 deletions test/mocks/RequestMock.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
import { GetBlockchainTransactionsParams, GetTransactionParams, GetTransactionResponse, Request, SetAccountPropertyParams, SetAccountPropertyResponse, Transaction, UploadTaggedDataParams, UploadTaggedDataResponse, GetBlockchainTransactionsResponse } from "@blobaa/ardor-ts";
import { GetBlockchainTransactionsParams, GetTransactionParams, GetTransactionResponse, Request, SetAccountPropertyParams, SetAccountPropertyResponse, Transaction, UploadTaggedDataParams, UploadTaggedDataResponse, GetBlockchainTransactionsResponse, DownloadTaggedDataParams, DownloadTaggedDataResponse } from "@blobaa/ardor-ts";
import DefaultTransaction from "../modules/lib/DefaultTransaction";


export type SetAccountPropertyCallback = (params: SetAccountPropertyParams) => string;
export type UploadTaggedDataCallback = (params: UploadTaggedDataParams) => string;
export type GetTransactionCallback = (params: GetTransactionParams) => Transaction;
export type GetBlockchainTransactionCallback = (params: GetBlockchainTransactionsParams) => Transaction[];
export type DownloadTaggedDataCallback = (params: DownloadTaggedDataParams) => string;


export default class RequestMock extends Request {
private setAccPropCallback: SetAccountPropertyCallback;
private uploadDataCallback: UploadTaggedDataCallback;
private getTransactionCallback: GetTransactionCallback;
private getBcTransactionsCallback: GetBlockchainTransactionCallback;
private downloadDataCallback: DownloadTaggedDataCallback;


constructor(setAccPropCallback?: SetAccountPropertyCallback,
uploadTaggedDataCallback?: UploadTaggedDataCallback,
getTransactionCallback?: GetTransactionCallback,
getBcTransactionsCallback?: GetBlockchainTransactionCallback) {
constructor(
setAccPropCallback?: SetAccountPropertyCallback,
uploadTaggedDataCallback?: UploadTaggedDataCallback,
getTransactionCallback?: GetTransactionCallback,
getBcTransactionsCallback?: GetBlockchainTransactionCallback,
downloadDataCallback?: DownloadTaggedDataCallback
) {
super();
this.setAccPropCallback = setAccPropCallback || this.defaultSetAccPropCallback;
this.uploadDataCallback = uploadTaggedDataCallback || this.defaultUploadDataCallback;
this.getTransactionCallback = getTransactionCallback || this.defaultGetTransactionCallback;
this.getBcTransactionsCallback = getBcTransactionsCallback || this.defaultGetBcTransactionsCallback;
this.downloadDataCallback = downloadDataCallback || this.defaultDownloadDataCallback;
}

private defaultSetAccPropCallback: SetAccountPropertyCallback = (params: SetAccountPropertyParams) => {
Expand All @@ -42,6 +48,10 @@ export default class RequestMock extends Request {
return [];
}

private defaultDownloadDataCallback: DownloadTaggedDataCallback = (params: DownloadTaggedDataParams) => {
return "";
}


public setAccountProperty(url: string, params: SetAccountPropertyParams): Promise<SetAccountPropertyResponse> {
const callbackReturn = this.setAccPropCallback(params);
Expand Down Expand Up @@ -69,4 +79,10 @@ export default class RequestMock extends Request {
const callbackReturn = this.getBcTransactionsCallback(params);
return Promise.resolve({ transactions: callbackReturn, requestProcessingTime: 0 });
}


public downloadTaggedData(url: string, params: DownloadTaggedDataParams): Promise<DownloadTaggedDataResponse> {
const callbackReturn = this.downloadDataCallback(params);
return Promise.resolve(callbackReturn);
}
}
50 changes: 36 additions & 14 deletions test/modules/BBAMethodHandler/BBAMethodHandler.resolveDID.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ChainId, ChildTransactionSubtype, ChildTransactionType, GetBlockchainTransactionsParams, GetTransactionParams, Transaction } from "@blobaa/ardor-ts";
import { ChainId, ChildTransactionSubtype, ChildTransactionType, GetBlockchainTransactionsParams, GetTransactionParams, Transaction, DownloadTaggedDataParams } from "@blobaa/ardor-ts";
import { ACCOUNT_PREFIX, TRANSACTION_TIME_WINDOW } from "../../../src/constants";
import { BBAMethodHandler, bbaMethodHandler, Error, ErrorCode, ResolveDIDParams } from "../../../src/index";
import config from "../../config";
import RequestMock, { GetBlockchainTransactionCallback, GetTransactionCallback } from "../../mocks/RequestMock";
import RequestMock, { GetBlockchainTransactionCallback, GetTransactionCallback, DownloadTaggedDataCallback } from "../../mocks/RequestMock";
import DefaultTransaction from "../lib/DefaultTransaction";


Expand All @@ -28,14 +28,10 @@ if (config.test.resolveDID) {
transaction.blockTimestamp = 1000;
}

if (getTransactionCounter === 1) { // 3. get did document
if (getTransactionCounter === 1) { // 3. get did document metadata
expect(params.chain).toBe(ChainId.IGNIS);
expect(params.fullHash).toBe("1ec58d15c6fa43de48fee4702cec26c2ac96002c2a114b06e87fdef72e795340");

transaction.attachment = {
data: JSON.stringify(config.didDocument.doc1.cleaned),
name: "bba-did-document-template"
};
transaction.senderRS = config.account.alice.address;
}

Expand All @@ -58,7 +54,22 @@ if (config.test.resolveDID) {
};


const testHandler = new BBAMethodHandler(new RequestMock(undefined, undefined, getTransactionCallback, getBcTransactionsCallback));
const downloadDataCallback: DownloadTaggedDataCallback = (params: DownloadTaggedDataParams) => { // 4. get did document data
expect(params.chain).toBe(ChainId.IGNIS);
expect(params.fullHash).toBe("1ec58d15c6fa43de48fee4702cec26c2ac96002c2a114b06e87fdef72e795340");
expect(params.retrieve).toBeTruthy();

return JSON.stringify(config.didDocument.doc1.cleaned);
};


const testHandler = new BBAMethodHandler(new RequestMock(
undefined,
undefined,
getTransactionCallback,
getBcTransactionsCallback,
downloadDataCallback
));


const didParams: ResolveDIDParams = {
Expand Down Expand Up @@ -88,14 +99,10 @@ if (config.test.resolveDID) {
transaction.blockTimestamp = 1000;
}

if (getTransactionCounter === 1) { // 5. get did document
if (getTransactionCounter === 1) { // 5. get did metadata
expect(params.chain).toBe(ChainId.IGNIS);
expect(params.fullHash).toBe("1ec58d15c6fa43de48fee4702cec26c2ac96002c2a114b06e87fdef72e795340");

transaction.attachment = {
data: JSON.stringify(config.didDocument.doc1.cleaned),
name: "bba-did-document-template"
};
transaction.senderRS = config.account.alice.address;
}

Expand Down Expand Up @@ -165,7 +172,22 @@ if (config.test.resolveDID) {
};


const testHandler = new BBAMethodHandler(new RequestMock(undefined, undefined, getTransactionCallback, getBcTransactionsCallback ));
const downloadDataCallback: DownloadTaggedDataCallback = (params: DownloadTaggedDataParams) => { // 6. get did document data
expect(params.chain).toBe(ChainId.IGNIS);
expect(params.fullHash).toBe("1ec58d15c6fa43de48fee4702cec26c2ac96002c2a114b06e87fdef72e795340");
expect(params.retrieve).toBeTruthy();

return JSON.stringify(config.didDocument.doc1.cleaned);
};


const testHandler = new BBAMethodHandler(new RequestMock(
undefined,
undefined,
getTransactionCallback,
getBcTransactionsCallback,
downloadDataCallback
));


const didParams: ResolveDIDParams = {
Expand Down

0 comments on commit 64cec71

Please sign in to comment.