diff --git a/src/server.ts b/src/server.ts index d9ca60b..0a867a1 100644 --- a/src/server.ts +++ b/src/server.ts @@ -790,6 +790,27 @@ export async function buildServer(): Server { } }) + server.route({ + method: 'POST', + path: '/api/v1/posts', + handler: handlers.content.create, + options: { + description: 'Submit Bitcoin Transactions Containing Posts To Index', + notes: 'Receives post transactions in hex form and indexes them if they are valid bitcoin transactions', + tags: ['api', 'content'], + response: { + failAction: 'log' + }, + validate: { + payload: Joi.object({ + transactions: Joi.array().items(Joi.object({ + tx: Joi.string.required + })).required() + }) + } + } + }) + const swaggerOptions = { info: { title: 'Powco API Docs', diff --git a/src/server/handlers/content.ts b/src/server/handlers/content.ts index a77ba70..367abd2 100644 --- a/src/server/handlers/content.ts +++ b/src/server/handlers/content.ts @@ -1,11 +1,16 @@ import { cacheContent } from '../../content' +import { flatten } from "lodash" + import models from '../../models' import { badRequest } from 'boom' import { log } from '../../log' +import { ingestBmapTransaction } from '../../bmap' + +const { TransformTx, bobFromRawTx } = require('bmapjs') export async function show(req) { @@ -48,3 +53,46 @@ export async function show(req) { } } + +export async function create(request, hapi) { + + // parse content transaction + // look up on by txid blockchain + // if not on chain, broadcast then import + + try { + + const { transactions } = request.payload + + let records; + + Promise.all(transactions.map(async tx => { + + log.info('post.content.tx.import', tx) + + let bob = await bobFromRawTx(tx) + let bmap = await TransformTx(bob) + + log.info('getBmapFromTxHex.result', { bob, bmap }) + + let record = await ingestBmapTransaction({ bob, bmap }) + + log.info("content.tx.import.response", record) + + records.push(record) + + })) + + let json = flatten(records).map(r => r.toJSON()) + + return hapi.response({ posts: json }).code(200) + + } catch (error) { + + log.error('posts.tx.import.error', error) + + return { posts: []} + + } + +} diff --git a/src/test/server/post_test.ts b/src/test/server/post_test.ts new file mode 100644 index 0000000..d481cff --- /dev/null +++ b/src/test/server/post_test.ts @@ -0,0 +1,38 @@ +import { expect, server } from '../utils' + +import { run } from '../../run' + +describe("API - Posts", () => { + + it('POST /api/v1/posts should import posts', async () => { + expect( + server.inject({ + url: 'api/v1/posts', + method: 'POST', + payload: { + + } + }) + ) + .to.be.eventually.rejected + }) + + it('POST api/v1/posts should import a post txhex', async () => { + + const txid = 'c4053a1bf2a4f0599646dfb8c29d3708487a1787ccb36ae4743c4733fc2fc988' + + const hex = await run.blockchain.fetch(txid) + + const response = await server.inject({ + url: '/api/v1/posts', + method: 'POST', + payload: { + transactions: [{ + tx: hex + }] + } + }) + + expect(response.statusCode).to.be.equal(200) + }) +}) \ No newline at end of file