GithubHelp home page GithubHelp logo

isabella232 / regtest-client Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bitcoinjs/regtest-client

0.0 0.0 0.0 48 KB

A client library based off of the integration tests of bitcoinjs-lib. This requires a server component.

License: MIT License

JavaScript 52.85% TypeScript 47.15%

regtest-client's Introduction

Bitcoin Regtest Client

A library for managing regtest server. (Good for integration tests)

You must specify a server

Default URL is http://127.0.0.1:8080/1, and the recommended way to set up a server is to run a docker container locally.

You can override the URL with APIURL environment variable or by using the optional second arg new RegtestUtils(bitcoin, { APIURL: 'xxx' }) at runtime.

You can also override the API password (set on the server side) with APIPASS env variable, or new RegtestUtils(bitcoin, { APIURL: 'xxx', APIPASS: 'yyy' })

The optional second arg can have either, both, or none of the two overrides.

Docker

Check the docker folder on regtest-server to run a server locally.

Also, see bitcoinjs-lib travis config to see how to use the docker image with CI.
Check the .travis.yml here.

TypeScript support

Types are automatically generated. Develop in TypeScript, commit JS and types. Pull requests must all contain TS, JS, and types where needed.

Usage

// inside an async function to use await

// bitcoinjs-lib must be the >=5.0.6 to use.
// For bitcoinjs-lib >=4.0.3, use version v0.0.8 of regtest-client
const bitcoin = require('bitcoinjs-lib')
const { RegtestUtils } = require('regtest-client')
const regtestUtils = new RegtestUtils(bitcoin)

const network = regtestUtils.network // regtest network params

const keyPair = bitcoin.ECPair.makeRandom({ network })
const p2pkh = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey, network })

// Tell the server to send you coins (satoshis)
// Can pass address
const unspent = await regtestUtils.faucet(p2pkh.address, 2e4)

// Tell the server to send you coins (satoshis)
// Can pass Buffer of the scriptPubkey (in case address can not be parsed by bitcoinjs-lib)
// Non-standard outputs will be rejected, though.
const unspentComplex = await regtestUtils.faucetComplex(p2pkh.output, 1e4)

// Get all current unspents of the address.
const unspents = await regtestUtils.unspents(p2pkh.address)

// Get data of a certain transaction
const fetchedTx = await regtestUtils.fetch(unspent.txId)

// Mine 6 blocks, returns an Array of the block hashes
// All of the above faucet payments will confirm
const results = await regtestUtils.mine(6)

// Send our own transaction
const txb = new bitcoin.TransactionBuilder(network)
txb.addInput(unspent.txId, unspent.vout)
txb.addInput(unspentComplex.txId, unspentComplex.vout)
// regtestUtils.RANDOM_ADDRESS is created on first load.
// regtestUtils.randomAddress() will return a new random address every time.
// (You won't have the private key though.)
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 1e4)

txb.sign({
  prevOutScriptType: 'p2pkh',
  vin: 0,
  keyPair,
})
txb.sign({
  prevOutScriptType: 'p2pkh',
  vin: 1,
  keyPair,
})
const tx = txb.build()

// build and broadcast to the Bitcoin Local RegTest server
await regtestUtils.broadcast(tx.toHex())

// This verifies that the vout output of txId transaction is actually for value
// in satoshis and is locked for the address given.
// The utxo can be unconfirmed. We are just verifying it was at least placed in
// the mempool.
await regtestUtils.verify({
  txId: tx.getId(),
  address: regtestUtils.RANDOM_ADDRESS,
  vout: 0,
  value: 1e4
})

regtest-client's People

Contributors

junderw avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.