-
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
5785a15
commit f47d130
Showing
11 changed files
with
184 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ArchiveController } from '../../controllers/ArchiveController' | ||
import { IncorrectToken } from '../../errors/errors' | ||
import { Token } from '../Tokens' | ||
|
||
export const PostArchive = (archiveController: ArchiveController) => async (ctx: any, next: any): Promise<any> => { | ||
const { req } = ctx.request | ||
const { user, tokenData } = ctx.state | ||
|
||
const allowedTokens = [Token.ApiKey.meta.name, Token.TestApiKey.meta.name] | ||
|
||
if (!allowedTokens.includes(tokenData.data.meta.name)) | ||
throw new IncorrectToken(tokenData.data.meta.name, allowedTokens) | ||
|
||
const response = await archiveController.postArchive(user, req, tokenData.data.meta.network) | ||
|
||
ctx.status = 200 | ||
ctx.body = response | ||
} |
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import * as Pino from 'pino' | ||
|
||
import { PoetNode } from '../daos/PoetNodeDao' | ||
import { PoeAddressNotVerified, PoeBalanceInsufficient } from '../errors/errors' | ||
import { fetchBalance } from '../helpers/ethereum' | ||
import { Network } from '../interfaces/Network' | ||
import { Account } from '../models/Account' | ||
|
||
export interface ArchiveController { | ||
readonly postArchive: ( | ||
account: Account, | ||
archive: ReadableStream, | ||
network: Network, | ||
) => Promise<any> | ||
} | ||
|
||
interface Arguments { | ||
readonly dependencies: Dependencies | ||
readonly configuration: Configuration | ||
} | ||
|
||
interface Dependencies { | ||
readonly logger: Pino.Logger | ||
readonly mainnetNode: PoetNode | ||
readonly testnetNode: PoetNode | ||
} | ||
|
||
interface Configuration { | ||
readonly poeContractAddress: string | ||
readonly poeBalanceMinimum: number | ||
} | ||
|
||
export const ArchiveController = ({ | ||
dependencies: { | ||
logger, | ||
mainnetNode, | ||
testnetNode, | ||
}, | ||
configuration: { | ||
poeContractAddress, | ||
poeBalanceMinimum, | ||
}, | ||
}: Arguments): ArchiveController => { | ||
logger.info({ poeContractAddress, poeBalanceMinimum }, 'ArchiveController Instantiated') | ||
|
||
const fetchPoeBalance = fetchBalance(poeContractAddress) | ||
|
||
const networkToNode = (network: Network) => network === Network.LIVE ? mainnetNode : testnetNode | ||
|
||
const postArchive = async (account: Account, archive: ReadableStream, network: Network) => { | ||
const node = networkToNode(network) | ||
|
||
const { id: userId, email, poeAddress, poeAddressVerified } = account | ||
|
||
logger.debug({ userId, email, poeAddress, poeAddressVerified, network }) | ||
|
||
if (!poeAddressVerified) | ||
throw new PoeAddressNotVerified() | ||
|
||
const poeBalance = await fetchPoeBalance(poeAddress) | ||
|
||
if (poeBalance < poeBalanceMinimum) | ||
throw new PoeBalanceInsufficient(poeBalanceMinimum, poeBalance) | ||
|
||
return node.postArchive(archive) | ||
} | ||
|
||
return { | ||
postArchive, | ||
} | ||
} |
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,27 @@ | ||
import * as FormData from 'form-data' | ||
import fetch from 'node-fetch' | ||
|
||
export interface PoetNode { | ||
readonly postArchive: (archive: any) => Promise<ReadonlyArray<{ readonly hash: string, readonly archiveUrl: string }>> | ||
} | ||
|
||
export const PoetNode = (url: string): PoetNode => { | ||
const postArchive = async (archive: any) => { | ||
const formData = new FormData() | ||
formData.append('content', archive, { filepath: 'content' }) | ||
|
||
const response = await fetch(`${url}/files`, { | ||
method: 'post', | ||
body: formData, | ||
}) | ||
|
||
if (!response.ok) | ||
throw new Error(await response.text()) | ||
|
||
return response.json() | ||
} | ||
|
||
return { | ||
postArchive, | ||
} | ||
} |
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