GithubHelp home page GithubHelp logo

block_io-nodejs's Introduction

BlockIo

This NodeJS module is the official reference SDK for the Block.io payments API. To use this, you will need the Bitcoin, Litecoin or Dogecoin API key(s) from block.io. Go ahead, sign up :)

Installation

Install the package using npm:

npm install block_io

Supported NodeJS versions

We aim to support only the current (non-EOL) NodeJS LTS versions.

Usage

It's super easy to get started:

// load this library
const BlockIo = require('block_io');

// instantiate a client
const block_io = new BlockIo('API_KEY');

async function example() {
  try {
    // print the account balance
    let balance = await block_io.get_balance();
    console.log(JSON.stringify(balance,null,2));

    // print first page of unarchived addresses on this account
    let addresses = await block_io.get_my_addresses();
    console.log(JSON.stringify(addresses,null,2));

    // withdrawal:
    //   prepare_transaction ->
    //   summarize_prepared_transaction ->
    //   create_and_sign_transaction ->
    //   submit_transaction
    let prepared_transaction = await block_io.prepare_transaction({
      from_labels: 'label1,label2',
      to_label: 'label3',
      amount: '50.0'
    });

    // inspect the prepared data for yourself. here's a
    // summary of the transaction you will create and sign
    let summarized_transaction = await block_io.summarize_prepared_transaction({data: prepared_transaction});
    console.log(JSON.stringify(summarized_transaction,null,2));
    
    // create and sign this transaction:
    // we specify the PIN here to decrypt
    // the private key to sign the transaction
    let signed_transaction = await block_io.create_and_sign_transaction({data: prepared_transaction, pin: 'SECRET_PIN'});

    // inspect the signed transaction yourself
    // once satisfied, submit it to Block.io
    let result = await block_io.submit_transaction({transaction_data: signed_transaction});
    console.log(JSON.stringify(result,null,2)); // contains the transaction ID of the final transaction
    
  } catch (error) {
    console.log("Error:", error.message);
  }
}

example();

Promises

Since v3.0.0, all methods return promises, like so:

block_io.get_balance()
        .then(data => console.log(JSON.stringify(data,null,2)))
        .catch(error => console.log("Error:", error.message));

Callbacks

For backward compatibility, callback-style method calls are supported too. Just add a callback function/lambda as the last argument.

block_io.get_balance((error, data) => {
  if (error) return console.log("Error:", error.message);
  console.log(JSON.stringify(data,null,2));
});

For more information, see NodeJS API Docs. This client provides a mapping for all methods listed on the Block.io API site.

Configuration

To change behavior of the block_io client, attributes can be passed to the class at instantiation time, in the form of an object.

The following attributes are supported:

const config = {
  api_key: "YOUR_API_KEY",
  version: 2,              // REST API version to use. Default: 2
  options: {
    allowNoPin: false,     // Allow ommission of PIN for withdrawal.
                           // This may be useful when interfacing with
                           // hardware wallets and HSMs. Default: false.

    lowR: true,            // Sign with a low R value to save a byte and
                           // make signature size more predictable, at the
                           // cost of more CPU time needed to sign transactions.
                           // Default: true

  }
}

const block_io = new BlockIo(config);

Contributing

  1. Fork it ( https://github.com/BlockIo/block_io-nodejs/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Testing

We use tape for unit and integration tests. To run the unit tests simply run npm test.

To run the integration tests you need to specify BLOCK_IO_API_KEY and BLOCK_IO_PIN environment variables.

DO NOT USE PRODUCTION CREDENTIALS FOR INTEGRATION TESTING!

Integration test syntax:

BLOCK_IO_API_KEY="API_KEY" BLOCK_IO_PIN="SECRET_PIN" node test/integration/api.js

block_io-nodejs's People

Contributors

dependabot[bot] avatar doersf avatar kuzzmi avatar patricklodder avatar ppitonak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

block_io-nodejs's Issues

Web sockets and testnets?

I came across this #8 and I'm a little further for it, but how do I get transaction information for testnets from the web sockets? is there a different url, or some way to indicate to the socket which network I want info for?

API Limit response error

During testing, the API limit was exceeded - responses from block.io were not handled gracefully by block_io.

/node_modules/block_io/lib/block_io.js:245
    if (typeof(data) !== 'object' || data.status !== 'success') {
                                         ^

TypeError: Cannot read property 'status' of null
    at Request._callback (/node_modules/block_io/lib/block_io.js:245:42)
    at Request.self.callback (/node_modules/block_io/node_modules/request/request.js:368:22)
    at emitTwo (events.js:106:13)
    at Request.emit (events.js:191:7)
    at Request.<anonymous> (/node_modules/block_io/node_modules/request/request.js:1219:14)
    at emitOne (events.js:101:20)
    at Request.emit (events.js:188:7)
    at IncomingMessage.<anonymous> (/node_modules/block_io/node_modules/request/request.js:1167:12)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:926:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

AFAIK the response was in relation to API limit being exceeded, the purpose for exceeding the limit was developing response handlers.

Cannot read property 'status' of null

/usr/src/app/node_modules/block_io/lib/block_io.js:245
    if (typeof(data) !== 'object' || data.status !== 'success') {
                                         ^

TypeError: Cannot read property 'status' of null
    at Request._callback (/usr/src/app/node_modules/block_io/lib/block_io.js:245:42)
    at Request.self.callback (/usr/src/app/node_modules/request/request.js:368:22)
    at emitTwo (events.js:106:13)
    at Request.emit (events.js:192:7)
    at Request.<anonymous> (/usr/src/app/node_modules/request/request.js:1219:14)
    at emitOne (events.js:101:20)
    at Request.emit (events.js:189:7)
    at IncomingMessage.<anonymous> (/usr/src/app/node_modules/request/request.js:1167:12)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:186:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

Problem With Withdrawals

I am member in Block.io from autumn 2014. I asked a Litecoin withdrawal before a few minutes. Your system says that: Unexpected character in line 1 column 1. I can't withdraw my Dogecoins too. I am waiting for the solution. Thank you.

what will be type is i want to create notification with new transactions

block_io.create_notification({'type': 'new-transactions', 'url': 'YOUR URL'}, console.log);

when is hit this module its showing ERROR:
==>Error: Must specify a valid type of events to watch.
................................................................................................
what will be type is i want to create notification with new transactions,i used type=new-transactions
but i got =>Error: Must specify a valid type of events to watch.
...block_io.create_notification({'type': 'new-transactions', 'url': 'http://13.12.118.65:3000/getReq'}, console.log);

archive_addresses is not defined

You guys have made typo either in documentation on your website or in module. In module the method is called archive_address, but on the website it says archive_addresses.

localhost connection error

installed "block_io": "^3.0.5" in node 12.0v and 10.9v.

and I used this code.

const BlockIo = require('block_io');
const block_io = new BlockIo(process.env.MULTISEND_BTC_BLOCKIO_KEY);

block_io.get_balance()
.then(data => {
console.log(JSON.stringify(data));
res.status(200).send(JSON.stringify(data));
}
)
.catch(error => {
console.log("Error:", error.message);
res.status(200).send(error.message);
}
);

block_io.get_balance((error, data) => {
  if (error) return console.log("Error:", error.message);
  console.log(JSON.stringify(data));
});

btw, there is such error in my local machine.{"errno":"ECONNREFUSED","code":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1","port":443}

and in my test server(https).
Error: URL is not defined
Error: URL is not defined

When I use [email protected]
let balance = await block_io.get_balance();
console.log(balance);

it returns "undefined"

Create transaction issue

We have used the code for sending LTC. But we are getting below error always in block_io.create_and_sign_transaction({data: prepared_transaction, pin: 'SECRET_PIN'})

"Error: 3MR2oZx9xxxxxxxxxxxxsyX9CNuYCHr has no matching Script".

Please guide us to fix these issues.

Thank you.

Create and sign transaction function references this.keys which is undefined

When I take the response data from prepare_transaction and use it as the input for create_and_sign_transaction, I get an error when the block_io library attempts to find the existence of a key in this.keys. This occurs as this.key has not yet been defined.

A workaround is to initialize block_io with a config object, rather than a string as this explicitly sets this.keys equal to an empty object.

Connecting via WebSockets

Hi BlockIO team,

I just signed up and want to test out the Websockets API to check on an address balance. Following the API docs, I was trying to see if I could get a proof of concept working, but I can't seem to get past the "success" message. Can someone take a look at this code and let me know what I'm doing wrong?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Websocket Test</title>
</head>
<body>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.5/socket.io.min.js"></script>
    <script>
        var connection = new WebSocket('wss://n.block.io/:443');
        var message = {
            "type": "account",
            "api_key": ".....",
            "network": "BTC",
                "type": "address",
                "address": "13qUEUgSZRBqrXUyDghm1JhXMzJyrhA69h"
        };
        connection.onmessage = function(e){
           var server_message = e.data;
           console.log(server_message);
        }
        connection.onopen = function(){
           connection.send(message);
        }
    </script>
</body>
</html>

one to many, many to one payments

Hello there,

My case is that each user is assigned a wallet address, and every month I want to take subscription fee. To save on fess I would like to group transactions so I could specify sending addresses and amounts.

For e.g:

sending_addresses:user_1_wallet,user_2_wallet,user_3_wallet
amounts: 0.1,0.2,0.5
receiving_addresses: main_service_wallet

problem is that you can't have multiple amounts specified and only one unique destination, but at the same point, I can't have 3 identical destinations.

This means that I have to make 3 separate transactions. Can someone suggest solution for this using block.io.

Thank you

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.