Skip to content

Commit

Permalink
feat(assets): get asset owner
Browse files Browse the repository at this point in the history
  • Loading branch information
JGiter committed Apr 20, 2022
1 parent d3d1a67 commit 1eb4832
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
31 changes: 25 additions & 6 deletions e2e/assets.service.e2e.ts
@@ -1,7 +1,12 @@
import { Wallet } from 'ethers';
import { ethAddrPattern } from '@ew-did-registry/did';
import { replenish, rpcUrl, setupENS } from './utils/setup-contracts';
import { AssetsService, CacheClient, fromPrivateKey } from '../src';
import {
AssetsService,
CacheClient,
fromPrivateKey,
AssetNotExist,
} from '../src';

const mockGetAssetById = jest.fn();
const mockGetOwnedAssets = jest.fn();
Expand Down Expand Up @@ -42,14 +47,28 @@ describe('Assets tests', () => {
expect(assetAddress).toEqual(expect.stringMatching(ethAddrPattern));
});

// As for now assets service just forwards calls to cache service, tests will make sense when asssets service will be able to communicate with IdentityManager
test('should throw AssetNotExist when getting owner of not existing asset', async () => {
mockGetAssetById.mockResolvedValueOnce(undefined);
const id = '<ASSET DID>';
expect(assetsService.getAssetOwner(id)).rejects.toEqual(
new AssetNotExist(id)
);
});

/**
* @todo when ssi hub is included in test environment
*/
test.todo('should be able to get asset owner');

test.todo('owner should be able to offer asset');
test.todo('owner should not be able to offer asset to himself');
test.todo('owner should not be able to offer asset to asset');

test.todo('asset should be able to cancel offer');
test.todo('owner should be able to cancel offer');

test.todo('asset should be able to accept offer');
test.todo('receiver should be able to accept offer');

test.todo('asset should be able to reject offer');
test.todo('receiver should be able to reject offer');

test.todo('update did document for asset');
test.todo('owner should be able to update asset document');
});
5 changes: 5 additions & 0 deletions src/errors/asset-not-exist.ts
@@ -0,0 +1,5 @@
export class AssetNotExist extends Error {
constructor(assetId: string) {
super(`Asset ${assetId} doesn not exist`);
}
}
1 change: 1 addition & 0 deletions src/errors/index.ts
Expand Up @@ -6,3 +6,4 @@ export { ENSOwnerNotValidAddressError } from './ens-owner-not-valid-address.erro
export { ERROR_MESSAGES } from './error-messages';
export { MalformedDIDError } from './malformed-did.error';
export { NotAuthorizedIssuer } from './not-authorized-issuer';
export { AssetNotExist } from './asset-not-exist';
17 changes: 15 additions & 2 deletions src/modules/assets/assets.service.ts
Expand Up @@ -8,6 +8,7 @@ import { CacheClient } from '../cache-client/cache-client.service';
import { Order } from '../cache-client/cache-client.types';
import { SignerService } from '../signer/signer.service';
import { AssetHistoryEventType } from './assets.types';
import { AssetNotExist } from '../../errors/asset-not-exist';

export class AssetsService {
private _owner: string;
Expand Down Expand Up @@ -41,7 +42,7 @@ export class AssetsService {

/**
* @description Registers a new Asset to the User
* @returns Asset DID
* @returns Asset address
*/
async registerAsset(): Promise<string> {
const data = this._assetManagerInterface.encodeFunctionData(
Expand Down Expand Up @@ -148,13 +149,25 @@ export class AssetsService {

/**
* @description Get Asset by Id
* @param id Asset Id
* @param id Asset decentralized identifier
* @returns Asset
*/
async getAssetById({ id }: { id: string }) {
return this._cacheClient.getAssetById(id);
}

/**
* Returns owner of the given asset
* @param id asset decentralized identifier
*/
async getAssetOwner(id: string) {
const asset = await this.getAssetById({ id });
if (!asset) {
throw new AssetNotExist(id);
}
return asset.owner;
}

/**
* @description Get previously owned asset for a given DID
* @param params.owner User DID
Expand Down

0 comments on commit 1eb4832

Please sign in to comment.