GithubHelp home page GithubHelp logo

howardwu / abstract-common-blockchain Goto Github PK

View Code? Open in Web Editor NEW

This project forked from blockai-unofficial/abstract-common-blockchain

2.0 2.0 0.0 113 KB

A test suite and interface you can use to implement standard Bitcoin blockchain API calls for various backends and platforms.

JavaScript 100.00%

abstract-common-blockchain's Introduction

abstract-common-blockchain

A test suite and interface you can use to implement standard Bitcoin blockchain API calls for various backends and platforms.

Combines the common-blockchain REST API with bitcoind JSON-RPC.

The goal of this module is to define a de-facto standard blockchain API. Inspired by the abstract-blob-store and abstract-leveldown modules, which have test suites that are usable as modules.

Publishing a test suite as a module lets multiple modules all ensure compatibility since they use the same test suite. For example, s3-blob-store uses abstract-blob-store, and so does torrent-blob-store, bitstore-blob-store and many others.

Using this module will help to create easy to consume APIs for building custom bitcoin transactions.

How to use

To use the test suite from this module you can require('abstract-common-blockchain/tests/testnet') or require('abstract-common-blockchain/tests/mainnet')

An example of this can be found in the chain-unofficial test suite. There is also an example in tests/run.js in this repo.

You have to implement a setup and teardown function:

var common = {
  setup: function(t, cb) {
    // setup takes a tap/tape compatible test instance in and a callback
    // this method should construct a new commonBlockchain instance and pass it to the callback:
    var commonBlockchain = biteasyAPI({ network: 'testnet' })
    cb(null, commonBlockchain)
  },
  teardown: function(t, commonBlockchain, cb) {
    // teardown takes in the test instance, as well as the commonBlockchain instance
    else cb()
    // be sure to call cb() when you are done with teardown
  }
}

To run the tests simply pass your test module (tap or tape or any other compatible modules are supported) and your common methods in:

var abstractCommonBlockchainTests = require('abstract-common-blockchain/tests/testnet')
abstractCommonBlockchainTests(test, common)

How to build blockchain software

Create an SPV wallet with BitcoinJS, CryptoCoinJS or Bitcore to sign transactions and messages.

var bitcoin = require("bitcoinjs-lib")

var address = "n3PDRtKoHXHNt8FU17Uu9Te81AnKLa7oyU"
var privateKeyWIF = "KyjhazeX7mXpHedQsKMuGh56o3rh8hm8FGhU3H6HPqfP9pA4YeoS"

var signFromPrivateKeyWIF = function(privateKeyWIF) {
  return function(txHex, callback) {
    var tx = bitcoin.Transaction.fromHex(txHex);
    var key = bitcoin.ECKey.fromWIF(privateKeyWIF)
    tx.sign(0, key)
    callback(false, tx)
  }
};

var signRawTransaction = signFromPrivateKeyWIF(privateKeyWIF)

var signMessage = function (message, callback) {
  var key = bitcoin.ECKey.fromWIF(privateKeyWIF)
  var network = bitcoin.networks.testnet
  callback(false, bitcoin.Message.sign(key, message, network).toString('base64'))
}

var commonWallet = {
  signRawTransaction: signRawTransaction,
  signMessage: signMessage
}

Read and write data from the blockchain with a provider like Blocktrail, Blockcypher, Biteasy or various others.

Example (Biteasy)

var biteasyAPI = require('biteasy-unofficial')

var commonBlockchain = biteasyAPI({ network: 'testnet' })

commonBlockchain.Addresses.Transactions([
  "n3PDRtKoHXHNt8FU17Uu9Te81AnKLa7oyU"
], function (err, resp) {
  console.log(resp)
});

Create custom bitcoin transactions and data formats to embed in the blockchain with Blocksign, Blockcast, and Open Publish.

var openpublish = require('openpublish')

var sha1 = "dc724af18fbdd4e59189f5fe768a5f8311527050"

openpublish.register({
  sha1: sha1,
  address: address,
  commonBlockchain: commonBlockchain,
  commonWallet: commonWallet
}, function(err, receipt) {

})

API

A valid common blockchain interface should implement the following APIs. There is a public-access CORS-enabled testnet implementation hosted by Blockai available at index.js in this repo.

Be sure to check out types.json in this repo for information about inputs and ouputs of common blockchain functions.

Addresses

commonBlockchain.Addresses.Summary

Summary returns a JSON of information regarding provided Bitcoin addresses.

commonBlockchain.Addresses.Summary([
  "1HUTmSsFp9Rg4FYRftp85GGyZFEndZSoeq",
  "1DmUeGjuQWLHxq5jhyn3uPCD9N16Ar9xGw"
], function (err, resp) {
  console.log(resp);
});

commonBlockchain.Addresses.Transactions

Transactions returns a JSON with a list of transactions associated with the provided Bitcoin addresses.

commonBlockchain.Addresses.Transactions([
  "1HUTmSsFp9Rg4FYRftp85GGyZFEndZSoeq",
  "1DmUeGjuQWLHxq5jhyn3uPCD9N16Ar9xGw"
], function (err, resp) {
  console.log(resp);
});

commonBlockchain.Addresses.Unspents

Unspents returns a JSON with a list of unspent outputs for the provided Bitcoin addresses.

commonBlockchain.Addresses.Unspents([
  "1HUTmSsFp9Rg4FYRftp85GGyZFEndZSoeq",
  "1DmUeGjuQWLHxq5jhyn3uPCD9N16Ar9xGw"
], function (err, resp) {
  console.log(resp);
});

Blocks

commonBlockchain.Blocks.Get

Get returns a JSON of information for the provided block IDs.

commonBlockchain.Blocks.Get([
  "00000000000000000216a936ebc1962e319a51bab8d3eae69335ac940284491d",
  "00000000000000001034f207d3ce18f03054ddfb0e4dba712f5b76cb1cda9499"
], function (err, resp) {
  console.log(resp);
});

commonBlockchain.Blocks.Latest

Latest returns a JSON of the latest blocks to hit a commonBlockChainAPI's endpoint.

commonBlockchain.Blocks.Latest(function (err, resp) {
  console.log(resp);
});

commonBlockchain.Blocks.Propogate

Propogate takes in raw block hex and will propogate it to the common blockchain API's network (NOTE: not supported by most providers)

commonBlockchain.Blocks.Propogate(blockHex, function (err, resp) {
  console.log(resp);
});

commonBlockchain.Blocks.Transactions

Transactions returns a JSON of transactions for the provided block IDs.

commonBlockchain.Blocks.Transactions([
  "00000000000000000216a936ebc1962e319a51bab8d3eae69335ac940284491d",
  "00000000000000001034f207d3ce18f03054ddfb0e4dba712f5b76cb1cda9499"
], function (err, resp) {
  console.log(resp);
});

Transactions

commonBlockchain.Transactions.Get

Get returns a JSON with transaction data for provided transaction IDs.

commonBlockchain.Transactions.Get([
  "186efd8689fc403e5cc6faeef9497fcf177750b52afe55f407244d0c95625836",
  "9375818c85a6712416dac6edd403498180ee9ee0e604bd11ec35beaea384da51"
], function (err, resp) {
  console.log(resp);
});

commonBlockchain.Transactions.Latest

Latest returns a JSON of the latest transactions to hit a common blockchain API's endpoint.

commonBlockchain.Transactions.Latest(function (err, resp) {
  console.log(resp);
});

commonBlockchain.Transactions.Outputs

Outputs returns a JSON of output information for provided transaction IDs. NOTE: "txid" and "txId" can be used interchangebly in this context to support the union of both bitcoind and common blockchain's standards.

commonBlockchain.Transactions.Outputs([
  {
    vout: 0,
    txId: "186efd8689fc403e5cc6faeef9497fcf177750b52afe55f407244d0c95625836"
  }
], function (err, resp) {
  console.log(resp);
});

commonBlockchain.Transactions.Propogate

Propogates the transaction supplied via raw hex.

commonBlockchain.Transactions.Propogate(hex, function (err, resp) {
  console.log(resp);
});

commonBlockchain.Transactions.Status

Transactions returns a JSON of transactions for the provided transaction IDs.

commonBlockchain.Transactions.Status([
  "186efd8689fc403e5cc6faeef9497fcf177750b52afe55f407244d0c95625836",
  "9375818c85a6712416dac6edd403498180ee9ee0e604bd11ec35beaea384da51"
], function (err, resp) {
  console.log(resp);
});

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.