vaporized
v0.0.27
Published
> Vapor Daemon
Downloads
26
Readme
Vaporized
Vapor Daemon
Vaporized is the reference implementation of a Vapor server.
It is powered by express.js and should be familiar to any web developer with express.js experience.
Quickstart
- Clone the vapor-starfish project to start a Vapor server
- Visit http://localhost:3006 to get started.
- If this is your first time, you will be directed to install Starfish, which is a Bitcoin wallet implementation that supports Vapor.
Install
npm install --save vaporized
Usage
A. Server
1. Initialize
Initialize a Vapor object:
const Vapor = require('vaporized')
const vapor = new Vapor({
store: <THE ROOT PATH WHERE VAPOR STORES TRANSACTIONS AND KEYS>,
port: <HTTP PORT TO LISTEN TO>,
parse: <A PARSE FUNCTION FOR TRANSACTIONS>
})
The vapor
object has the following attributes:
- app: The express app object
- express: The express object
- bitcoin: The Vapor request handler
2. Building the Web Service
Using the built-in express.js engine, you can build your web UI. Example:
const Vapor = require('vaporized')
const { app, bitcoin } = new Vapor()
app.set('view engine', 'ejs')
app.engine('ejs', require('ejs').__express)
app.set('views', path.resolve(process.cwd(), './views'));
app.use(express.static(path.join(process.cwd(), './public')))
app.get("/", (req, res) => {
res.render("index")
})
app.get("/files/:txid", (req, res) => {
fs.createReadStream(store + "/files/" + req.params.txid).pipe(res)
})
3. Building the Bitcoin Service
Vapor works by wrapping HTTP requests in a Bitcoin transaction following a protocol.
The system provides an effortless way to handle Bitcoinized HTTP requests
It provides a set of request handlers that make it trivial thandle the Bitcoinized HTTP requests in a familiar manner:
const Vapor = require('vaporized')
const { app, bitcoin } = new Vapor()
bitcoin.post("/posts", (req, res, data, tx) => {
/***************************************************************************
*
* req := <The express.js req object>
*
* res := <The express.js res object>
*
* data := {
* id: '4f3cb2b85d59a7360f6228cb968c43346b4178a778689986e1d4eaed194412e6', // Wrapper transaction id
* header: {
* address: '1qDymcCUGDNwWYzVfoJwh91p7wLLiLgon', // User identity
* ts: 1604484364603
* },
* timestamp: 1604484364614, // Server-side timestamp
* request: {
* version: '1', // Protocol version
* host: '1BJnZBCm4qLTNjg6Z5hrU3SjB9FWetN8CA', // Server identity
* method: 'post', // Request method
* resource: '/message', // Request path
* body: <Buffer 7b 22 6d 65 73 73 61 67 65 22 68 65 72 65 22 7d> // Request payload in Buffer
* }
* }
*
* tx := <The Bitcoin transaction object that wraps the Vapor request>
*
****************************************************************************/
let body = data.request.body.toString()
res.json({ id: data.id, body: body })
})
4. Read API
Using the app
obejct you can implement as many "Read" APIs as you want. But Vapor also ships with 2 default APIs:
chain.query()
: query the chainchain.tx()
: get raw transaction hex
query()
const Vapor = require('vaporized')
const { chain } = new Vapor()
chain.query({
find: {
ts: { $gt: 1604514885559 }
},
sort: {
ts: -1
},
limit: 10
}).then((transactions) => {
console.log("transactions = ", transactions)
})
tx()
const Vapor = require('vaporized')
const { chain } = new Vapor()
chain.tx("e7ba555961fdb793165f2c9daac5f5885536aaf07d7cebb73f74617b059d3eb8")
.pipe(process.stdout)
The tx()
API is also available as a web endpoint at /rawtx/:txid
(example: http://localhost:3006/rawtx/e7ba555961fdb793165f2c9daac5f5885536aaf07d7cebb73f74617b059d3eb8)
B. Client
There are 2 options to posting data to a Vapor endpoint:
1. Post JSON
Initialize a vapor object with a Bitcoin wallet (Default wallet: Starfish) and post to it.
<html>
<body>
<script src="https://unpkg.com/vaporize"></script>
<script>
const vapor = new Vapor()
vapor.post("/posts", { text: e.target.value }).then((response) => {
console.log("response = ", response)
})
</script>
</body>
</html>
2. Post ArrayBuffer
<html>
<body>
<script src="https://unpkg.com/vaporize"></script>
<script>
const vapor = new Vapor()
const arrayBuffer = new TextEncoder().encode("Hello World").buffer
vapor.post("/files", arrayBuffer).then((response) => {
console.log("response = ", response)
})
</script>
</body>
</html>
This can be used to upload binary:
<html>
<body>
<script src="https://unpkg.com/vaporize"></script>
<script>
const vapor = new Vapor()
document.querySelector("#upload").addEventListener("change", async (e) => {
let reader = new FileReader();
reader.onloadend = async () => {
const response = await vapor.post("/files", reader.result)
console.log("response = ", response)
};
reader.readAsArrayBuffer(e.target.files[0]);
})
</script>
</body>
</html>