-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f3c15d5
commit c8458aa
Showing
10 changed files
with
212 additions
and
250 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,14 @@ | ||
import * as Joi from 'joi' | ||
import { errors } from '../../errors/errors' | ||
import { isLiveNetwork } from '../../helpers/token' | ||
import { WorksController } from '../../modules/Works/Works.controller' | ||
import { WorkController } from '../../controllers/WorkController' | ||
|
||
export const GetWorkSchema = () => ({ | ||
workId: Joi.string().required(), | ||
}) | ||
|
||
export const GetWork = (poetUrl: string, testPoetUrl: string) => async (ctx: any, next: any): Promise<any> => { | ||
const logger = ctx.logger(__dirname) | ||
export const GetWork = (workController: WorkController) => async (ctx: any, next: any): Promise<any> => { | ||
const { workId } = ctx.params | ||
const { tokenData } = ctx.state | ||
|
||
try { | ||
const { workId } = ctx.params | ||
const { tokenData } = ctx.state | ||
|
||
const nodeNetwork = isLiveNetwork(tokenData.data.meta.network) ? poetUrl : testPoetUrl | ||
|
||
const worksController = new WorksController(ctx.logger, nodeNetwork) | ||
|
||
try { | ||
const response = await worksController.get(workId) | ||
ctx.body = response.claim | ||
return | ||
} catch (e) { | ||
const { WorkNotFound } = errors | ||
ctx.status = WorkNotFound.code | ||
ctx.body = WorkNotFound.message | ||
} | ||
} catch (exception) { | ||
logger.error({ exception }, 'api.GetWork') | ||
ctx.status = 500 | ||
} | ||
const response = await workController.getById(workId, tokenData.data.meta.network) | ||
ctx.body = response.claim | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,10 @@ | ||
import { SignedVerifiableClaim } from '@po.et/poet-js' | ||
import { errors } from '../../errors/errors' | ||
import { isLiveNetwork } from '../../helpers/token' | ||
import { WorksController } from '../../modules/Works/Works.controller' | ||
import { WorkController } from '../../controllers/WorkController' | ||
|
||
export const GetWorks = (poetUrl: string, testPoetUrl: string) => async (ctx: any, next: any): Promise<any> => { | ||
const logger = ctx.logger(__dirname) | ||
export const GetWorks = (workController: WorkController) => async (ctx: any, next: any): Promise<any> => { | ||
const { user, tokenData } = ctx.state | ||
const { issuer } = user | ||
|
||
try { | ||
const { user, tokenData } = ctx.state | ||
const { issuer } = user | ||
const response = await workController.searchWorks({ issuer }, tokenData.data.meta.network) | ||
|
||
const nodeNetwork = isLiveNetwork(tokenData.data.meta.network) ? poetUrl : testPoetUrl | ||
|
||
const worksController = new WorksController(ctx.logger, nodeNetwork) | ||
|
||
try { | ||
const response = await worksController.getWorksByIssuer(issuer) | ||
|
||
ctx.body = response.map((work: SignedVerifiableClaim) => work.claim) | ||
return | ||
} catch (e) { | ||
const { WorkNotFound } = errors | ||
ctx.status = WorkNotFound.code | ||
ctx.body = WorkNotFound.message | ||
} | ||
} catch (exception) { | ||
logger.error({ exception }, 'api.GetWorks') | ||
ctx.status = 500 | ||
} | ||
ctx.body = response.map(work => work.claim) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { configureCreateVerifiableClaim, getVerifiableClaimSigner, SignedVerifiableClaim } from '@po.et/poet-js' | ||
import * as Pino from 'pino' | ||
import { pipeP } from 'ramda' | ||
|
||
import { PoetNode, WorkSearchFilters } from '../daos/PoetNodeDao' | ||
import { Network } from '../interfaces/Network' | ||
import { Vault } from '../utils/Vault/Vault' | ||
|
||
export interface WorkController { | ||
readonly getById: (id: string, network: Network) => Promise<SignedVerifiableClaim> | ||
readonly searchWorks: (filters: WorkSearchFilters, network: Network) => Promise<ReadonlyArray<SignedVerifiableClaim>> | ||
readonly create: ( | ||
claim: any, | ||
context: any, | ||
issuer: string, | ||
privateKey: string, | ||
network: Network, | ||
) => Promise<SignedVerifiableClaim> | ||
} | ||
|
||
interface Arguments { | ||
readonly dependencies: Dependencies | ||
} | ||
|
||
interface Dependencies { | ||
readonly logger: Pino.Logger | ||
readonly mainnetNode: PoetNode | ||
readonly testnetNode: PoetNode | ||
} | ||
|
||
export const WorkController = ({ | ||
dependencies: { | ||
logger, | ||
mainnetNode, | ||
testnetNode, | ||
}, | ||
}: Arguments): WorkController => { | ||
const networkToNode = (network: Network) => network === Network.LIVE ? mainnetNode : testnetNode | ||
|
||
const getById = async (id: string, network: Network) => { | ||
const node = networkToNode(network) | ||
return node.getWorkById(id) | ||
} | ||
|
||
const searchWorks = async (filters: WorkSearchFilters, network: Network) => { | ||
const node = networkToNode(network) | ||
return node.searchWorks(filters) | ||
} | ||
|
||
const create = async (claim: any, context: any, issuer: string, encryptedPrivateKey: string, network: Network) => { | ||
const node = networkToNode(network) | ||
|
||
const legacyContext = { | ||
content: 'schema:text', | ||
} | ||
|
||
const aboutContext = { | ||
about: { | ||
'@id': 'schema:url', | ||
'@container': '@list', | ||
}, | ||
} | ||
|
||
const privateKey = await Vault.decrypt(encryptedPrivateKey) | ||
|
||
const createAndSignClaim = pipeP( | ||
configureCreateVerifiableClaim({ issuer, context: { ...legacyContext, ...context, ...aboutContext} }), | ||
getVerifiableClaimSigner().configureSignVerifiableClaim({ privateKey }), | ||
) | ||
|
||
const { content, ...newWork } = claim | ||
|
||
const [{ archiveUrl = '', hash = '' } = {}] = content ? await node.postArchive(content) : [] | ||
|
||
const signedVerifiableClaim = await createAndSignClaim({ about: [ archiveUrl ], hash, ...newWork }) | ||
|
||
logger.info({ signedVerifiableClaim }) | ||
|
||
await node.postWork(signedVerifiableClaim) | ||
|
||
return signedVerifiableClaim | ||
} | ||
|
||
return { | ||
getById, | ||
searchWorks, | ||
create, | ||
} | ||
} |
Oops, something went wrong.