Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
48 changes: 48 additions & 0 deletions src/server/handlers/content.ts
Original file line number Diff line number Diff line change
@@ -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) {

Expand Down Expand Up @@ -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: []}

}

}
38 changes: 38 additions & 0 deletions src/test/server/post_test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})