BlockIO

This Lib make integration with block.io in easy way.

With Block.io you can

  • create new wallets: Bitcoin, Dogecoin, Litecoin

  • look transactions

  • accept payments

  • make withdraws

  • etc

Demo bot

See DemoBlockIOBot. It also available in the Store.

See docs about this bot.

Initial setup

Need to set Api key and secret pin in first. You can create command /init with such BJS:

// set secret PIN
Libs.BlockIO.setSecretPin("YOURS SECRET PIN");
Libs.BlockIO.Bitcoin.setApiKey("YOURS_API_KEY for Bitcoin");
Libs.BlockIO.Dogecoin.setApiKey("YOURS_API_KEY for Dogecoin");
Libs.BlockIO.Litecoin.setApiKey("YOURS_API_KEY for Litecoin");
// Testnet
Libs.BlockIO.testNet.Bitcoin.setApiKey("YOURS_API_KEY for Bitcoin");
Libs.BlockIO.testNet.Dogecoin.setApiKey("YOURS_API_KEY for Dogecoin");
Libs.BlockIO.testNet.Litecoin.setApiKey("YOURS_API_KEY for Litecoin");

Just run bot and execute this command /init. Then you can remove it.

You get Secret PIN from Block.io by e-mail

You get API keys in Block.io dashboard by link "Show API Keys"

You can also provide pin and api key in commands in options

Methods

All methods from https://block.io/api available in the Lib.

You can run it via command:

Libs.BlockIO.XXXcoin.methodYYY(
{ onSuccess: "/onNewAddress",
onError: "/onerror",
// ....
// OTHER POSSIBLE PARAMS - see are later
} );

XXXCoin - it is Bitcoin, Litecoin or Dogecoin

You can also use Testnet: Libs.BlockIO.testNet.XXXcoin

All methods

Method name for Libs.BlockIO.XXXcoin

Description

getNewAddress

Returns a newly generated address, and its unique(!) label generated by Block.io. You can optionally specify a custom label.

getBalance

Returns the balance of your entire Bitcoin, Litecoin, or Dogecoin account (i.e., the sum of balances of all addresses/users within it) as numbers to 8 decimal points, as strings.

getAddressBalance

Returns the balance of the specified addresses, or labels. Upto 2500 addresses/labels can be specified per request.

Can be used to query balances for external (non-account) addresses. If an external address' balance is returned, its user_id and label fields will be null.

getMyAddresses

Returns the (unarchived) addresses, their labels, user ids, and balances on your account. Upto 2500 addresses per page. Page parameter is optional.

getAddressByLabel

Returns the address specified by a label.

isValidAddress

Returns whether a single specified address is valid for the network, or not.

archiveAddresses

Archives upto 100 addresses in a single API call. Addresses can be specified by their labels.

unarchiveAddresses

Unarchives upto 100 addresses in a single API call. Addresses can be specified by their labels.

getMyArchivedAddresses

Returns all the archived addresses, their labels, and user ids on your account.

getTransactions

Returns various data for the last 25 transactions spent or received. You can optionally specify a before_tx parameter to get earlier transactions.

You can use this method to query for addresses that are not on your account.

getRawTransaction

Returns the raw data, including transaction hex, for a given transaction ID.

getNetworkFeeEstimate

Estimates the Network Fee you will need to pay when you make a withdrawal request. The Network Fee is required by the Bitcoin/Dogecoin/etc. networks, not Block.io.

isGreenTransaction

Returns an array of transactions that were sent by Block.io Green Addresses. Funds sent from Green Addresses are guaranteed by Block.io, and can be used immediately on receipt with zero network confirmations.

getCurrentPrice

Returns the prices from the largest exchanges for Bitcoin, Dogecoin, or Litecoin, specified by the API Key. Specifying the base currency is optional.

withdraw

Withdraws amount of coins from any addresses in your account to up to 2500 destination addresses.

withdrawFromAddresses

Withdraws AMOUNT coins from upto 2500 addresses at a time, and deposits it to up to 2500 destination addresses.

withdrawFromLabels

Withdraws AMOUNT coins from upto 2500 labels at a time, and deposits it to upto 2500 destination addresses, or labels.

Full methods docs here

So if you need, for example, getTransactions method for Litecoin change this code:

Libs.BlockIO.XXXcoin.methodYYY(
{ onSuccess: "/onNewAddress",
onError: "/onerror",
// ....
// OTHER POSSIBLE PARAMS
} );

to:

Libs.BlockIO.Litecoin.getTransactions(
{ onSuccess: "/onGetTransaction",
onError: "/onerror",
// ....
// OTHER POSSIBLE PARAMS
} );

What it is - "OTHER POSSIBLE PARAMS" ?

It can be:

  • api_key and pin. If you do not set here (for all methods)

  • label, address (for getNewAddress methods and etc)

  • labels, addresses

  • to_addresses, from_addresses, from_labels (for withdraw )

  • page

  • transaction_ids (for isGreenTransaction method)

  • type (for getTransactions method)

See available params in https://block.io/api page

Example for getNewAddress

For example: "Actions for Handling Addresses"

From block.io help:

from Block.io help - https://block.io/api

So we have API method "Get New Address":

Libs.BlockIO.Bitcoin.getNewAddress(
{ onSuccess: "/onNewAddress",
onError: "/onerror"
} );

This function getNewAddress have 2 callbacks: onSuccess and onError.

onSuccess callback command /onNewAddress will be runned:

let wallet = options; // we have Block.io response in options
// uncomment this line for inspecting all options
// Bot.sendMessage(inspect(options));
Bot.sendMessage(
"*New wallet* was created. \n #⃣Wallet: `" +
wallet.address + "`\n 🏷Label: `" +
wallet.label + "`"
);

Example for making Withdraw

from Block.io help - https://block.io/api

We know about params: to_addresses and from_labels from block.io help. See "Withdraw From Labels" method there

Transfer coin from Block.io wallet with label "default" to any wallet:

Libs.BlockIO.Bitcoin.withdrawFromLabels(
{ to_addresses:"3KyRZdjJCpHjHfnjaELvrDpNAxUE3D1NK4",
from_labels:"default",
amounts:0.002,
onSuccess: "/onwithdraw", onError: "/onerror"
});

onWithdraw command:

Bot.sendMessage(inspect(options))

Error handing

You can show error message from Block.io

Bot.sendMessage("Error");
if(options&&options.data){
Bot.sendMessage(options.data.error_message);
}

How to...

Open new BTC address for each user (chat)?

possible BJS for command /newAddress:

Libs.BlockIO.Bitcoin.getNewAddress(
{
label: "chat" + chat.chatid,
onSuccess: "/onNewAddress",
onError: "/onError",
} );

command /onNewAddress:

Bot.sendMessage(inspect(options));
Bot.sendMessage("Created: " + options.address);

command /onError:

Bot.sendMessage("Error happens on wallet creation");

How can I check any payment by user on block.io if I know him bitcoin address and payment will be 0,0001 Bitcoin?

Looks like you need to receive payments on one own wallet from several users.

It is bad practice:

  1. User must say his wallet before payment. Because after payment anybody can see any addresses in blockchain

  2. User can do mistake with his address wallet

  3. User can do mistake with amount

Thus, users will have incorrect operations. You should check them in manual mode. It will be very difficult for you to verify incorrect transactions in the blockchain.

More good practice it is generate new income address for each payment. It is more secure.