npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bitpacket

v0.0.18

Published

> Bitcoin transaction output builder

Downloads

8

Readme

bitpacket

Bitcoin transaction output builder

bitpacket

bitpacket is a minimal JavaScript library for declaratively creating:

  1. Unsigned bitcoin transactions
  2. bitcoin output scripts

code

Because it only deals with output scripts, bitpacket has zero dependency on any API services.

You can use bitpacket to build bitcoin output scripts or unsigned transactions, and then feed into a wallet to build fully signed transactions.


Design Principles

comparison

Bitpacket only deals with output scripts, which means you can use it to build outputs or unsigned transactions, and then incorporate them into existing wallet workflow.

You can think of Bitpacket as a library laser-focused on the "output" side of what Datapay used to do.

  1. Minimal: Because it focuses only on outputs, it has gotten rid of the input-side API dependencies.
  2. Pluggable: Because it's powered by the new bsv.js 2.0 and generates the bsv transaction and script objects, you can use it to construct an object, and pass it to another function which uses the bsv library directly. bitpacket even exposes the bitpacket.bsv object as one of its attributes so you can just use bitpacket instead of including both bitpacket and bsv. cies as well as provide much more powerful focused features just for building outputs.
  3. Wallet Compatible: bitpacket fits into existing workflow easily. Most bitcoin app use cases delegate the transaction input building to 3rd party Bitcoin wallets anyway. You can simply use Bitpacket to build an unsigned transaction or bitcoin output scripts, and pass to wallet libraries to build fully signed transactions.

Features

1. bsv.js 2.0

Uses the new and lean bsv.js 2.0 instead of the old deprecated version.

2. only output

You can build either output scripts or unsigned transactions. Once you have either of the two, you can feed them into your existing wallet workflow.


Install

1. Node.js

npm install --save bitpacket

2. Browser

<script src="https://unpkg.com/bitpacket"></script>

Syntax

const bitpacket = require('bitpacket')

// 1. build a single output script
let script = await bitpacket.script(<SCRIPT_DSL>)

// 2. build multiple output scripts
let scripts = await bitpacket.script([<SCRIPT_DSL>, <SCRIPT_DSL>, ...])

// 3. build an unsigned, output-only transaction
let tx = await bitpacket.tx([<SCRIPT_DSL>, <SCRIPT_DSL>, ...])

Where <SCRIPT_DSL> is a JavaScript object where each key follows a convention:

  • s[0-9]+: UTF8 chunk (example: s0, s1, s2, ...) where the suffix integer represents the chunk position within a script
  • h[0-9]+: HEX chunk (example: h0, h1, h2, ...) where the suffix integer represents the chunk position within a script
  • b[0-9]+: Buffer (node.js) or ArrayBuffer (browser) type chunk or BASE64 string (example: b0, b1, b2, ...) where the suffix integer represents the chunk position within a script
    • if the value is Buffer type, then it will be treated as Buffer
    • if the value is ArrayBuffer type, then it will be treated as ArrayBuffer
    • if the value is string type, then it will be treated as a BASE64 string
  • o[0-9]+: Opcode string (example: o0, o1, o2, ...) where the suffix integer represents the chunk position within a script
// SCRIPT_DSL example

{
  o0: "OP_0",
  o1: "OP_RETURN",
  s2: "Hello world",
  h3: "bdf63990d6dc33d705b756e13dd135466c06b3b5",
  b4: <Buffer|ArrayBuffer>
}

Examples

A. Script

1. Single output script

const bitpacket = require('bitpacket')
let script = bitpacket.script({
  o0: "OP_0", o1: "OP_RETURN", s2: "A", s3: "is for", s4: "Alice"
})

2. Multiple output scripts

const bitpacket = require('bitpacket')
let script = bitpacket.script([
  { o0: "OP_0", o1: "OP_RETURN", s2: "A", s3: "is for", s4: "Alice" },
  { o0: "OP_0", o1: "OP_RETURN", s2: "B", s3: "is for", s4: "Bob" },
  { o0: "OP_0", o1: "OP_RETURN", s2: "C", s3: "is for", s4: "Carol" }
])

3. Arbitrary output script

const bitpacket = require('bitpacket')
let script = bitpacket.script({
  o0: "OP_DUP",
  o1: "OP_HASH160",
  h2: "bdf63990d6dc33d705b756e13dd135466c06b3b5",
  o3: "OP_EQUALVERIFY",
  o4: "OP_CHECKSIG",
  val: 1000
})

4. Buffer

const bitpacket = require('bitpacket')
let script = bitpacket.script({
  o0: "OP_0",
  o1: "OP_RETURN",
  b2: Buffer.from("Hello world", "utf8")
})

5. ArrayBuffer (browser)

var reader = new FileReader();
var input = document.getElementById("files").files;
var data = new Blob([input[0]]);
reader.onload = function() {
  let script = await bitpacket.script({
    o0: "OP_0",
    o1: "OP_RETURN",
    b2: reader.result
  })
}
reader.readAsArrayBuffer(data);

6. Pay-to-pubkey-hash

Bitpacket includes a convenience method for pay-to-pubkey-hash. The examble right above can be re-written as following (pkh stands for "pubkey hash":

const bitpacket = require('bitpacket')
let script = bitpacket.script({
  pkh: "bdf63990d6dc33d705b756e13dd135466c06b3b5",
  val: 1000
})

7. Address

Bitpacket also ships with another convenience method, where you can use address instead of pkh from the previous example:

const bitpacket = require('bitpacket')
let script = bitpacket.script({
  address: "1JKRgG4F7k1b7PbAhQ7heEuV5aTJDpK9TS",
  val: 1000
})

B. Transaction

Everything you can do with script() you can do with tx().

1. Single transaction

let tx = bitpacket.tx([
  { o0: "OP_0", o1: "OP_RETURN", s2: "A", s3: "is for", s4: "Alice" },
  { o0: "OP_0", o1: "OP_RETURN", s2: "B", s3: "is for", s4: "Bob" },
  { o0: "OP_0", o1: "OP_RETURN", s2: "C", s3: "is for", s4: "Carol" }
])

2. Unsigned transaction with arbitrary outputs

 let tx = bitpacket.tx([
   { o0: "OP_0", o1: "OP_RETURN", s2: "hello" },
   {
     o0: "OP_DUP",
     o1: "OP_HASH160",
     h2: "bdf63990d6dc33d705b756e13dd135466c06b3b5",
     o3: "OP_EQUALVERIFY",
     o4: "OP_CHECKSIG",
     val: 1000
   }
 ])

3. From transaction hex

 let tx = bitpacket.tx(
   "010000000001e8030000000000001976a91451e928263591749c1a8cf7271ef261a74e2aeaf388ac00000000"
 )

4. Including raw script hex

let tx = bitpacket.tx([
	"006a0b48656c6c6f20776f726c64",
	"006a0b48656c6c6f20776f726c64"
])