From 40e58e70d0eb341e85c7e4dd87fceec4b3b9a5ba Mon Sep 17 00:00:00 2001 From: Guillaume De Martino Date: Fri, 26 May 2023 16:35:40 +0200 Subject: [PATCH 1/2] POST api/v1/posts --- src/server.ts | 21 +++++++++++++++ src/server/handlers/content.ts | 48 ++++++++++++++++++++++++++++++++++ src/test/server/post_test.ts | 38 +++++++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 src/test/server/post_test.ts 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..e7ce58a 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; + + transactions.forEach(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 From 2086af4706fe614da17ff1a723939d048b446b35 Mon Sep 17 00:00:00 2001 From: Guillaume De Martino Date: Sun, 28 May 2023 09:44:30 +0200 Subject: [PATCH 2/2] correct async loop for post content handler --- src/server/handlers/content.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/server/handlers/content.ts b/src/server/handlers/content.ts index e7ce58a..367abd2 100644 --- a/src/server/handlers/content.ts +++ b/src/server/handlers/content.ts @@ -66,7 +66,7 @@ export async function create(request, hapi) { let records; - transactions.forEach(async tx => { + Promise.all(transactions.map(async tx => { log.info('post.content.tx.import', tx) @@ -81,7 +81,7 @@ export async function create(request, hapi) { records.push(record) - }); + })) let json = flatten(records).map(r => r.toJSON())