README
nanostore-publisher
Publish UHRP Content with NanoStore
Overview
This package allows you to create Universal Hash Resolution Protocol (UHRP) content publications and hosting contracts for files and data. Since UHRP URLs are content-addressed, they are self-authenticating. Since any UHRP host can advertise the availability of content, discovery is no longer controlled by a trusted third party as is the case with HTTP.
Usage
Check out NanoStore UI to see a file upload example with React.
The below code relies on the Babbage SDK to pay for a file to be hosted:
const Babbage = require('@babbage/sdk')
(async () => {
// Get a reference to a File element somehow, or create one if using Node
const file = document.getElementById('file_upload_form_input').files[0]
// Send an invoice to the server to get transaction outputs
const inv = await invoice({
fileSize: file.size,
retentionPeriod: 525600, // Host for one year
serverURL
})
// Create an Action with Babbage SDK to pay the invoice
const tx = await Babbage.createAction({
outputs: inv.outputs.map(x => ({
satoshis: x.amount,
script: x.outputScript
})),
keyName: 'primarySigning',
keyPath: 'm/1033/1',
description: 'Upload with NanoStore',
labels: ['nanostore']
})
// Upload the file and submit the payment of the invoice
const response = await upload({
referenceNumber: inv.referenceNumber,
transactionHex: tx.rawTransaction,
mapiResponses: tx.mapiResponses,
inputs: tx.inputs,
file,
serverURL
})
// The file is now published
console.log(response)
})()
API
Table of Contents
invoice
Creates an invoice for a NanoStore file hosting contract.
Parameters
obj
Object All parameters are given in an object.obj.fileSize
Number The size of the file you want to host in bytes.obj.retentionPeriod
Number The whole number of minutes you want the file to be hosted.obj.serverURL
String The URL of the NanoStore server to contract with. By default, the Babbage NanoStore server is used. (optional, defaulthttps://nanostore.babbage.systems
)
Returns Promise<Object> The invoice object, containing referenceNumber
and outputs
, which is an array of BSV transaction output scripts to use when creating the transaction that you will provide to the upload
function. Each element in the outputs array contains outputScript
and amount
(satoshis). The object also contains publicURL
, which is the HTTP URL where the file will become available for the duration of the contract once uploaded.
upload
Uploads a file to NanoStore and pays an invoice, thereby starting the file hosting contract.
Parameters
obj
Object All parameters are given in an object.obj.referenceNumber
String The reference number that was given to you when you called theinvoice
function.obj.transactionHex
String A Bitcoin SV transaction, in hex string format, which includes the outputs specified by theinvoice
function. It must be signed, and if not already broadcasted, it will be sent to miners by the NanoStore server.obj.file
File The file to upload. This is usually obtained by querying for your HTML form's file upload<input />
tag and referencingtagElement.files[0]
.obj.serverURL
String The URL of the NanoStore server to contract with. By default, the Babbage NanoStore server is used. (optional, defaulthttps://nanostore.babbage.systems
)obj.onUploadProgress
Function? A function called with periodic progress updates as the file uploads (optional, default()=>{}
)obj.inputs
obj.mapiResponses
obj.proof
Returns Promise<Object> The publication object. Fields are published=true
, hash
(the UHRP URL of the new file), and publicURL
, the HTTP URL where the file is published.
License
The license for the code in this repository is the Open BSV License.