Block chain development Tutorial: Using JavaScript to send data to block chain __php

Source: Internet
Author: User

Block chain enthusiasts (qq:53016353)

BitPay Open source their Bitcore library package, we can use it to send information based on Node.js to the block chain. The library package allows us to produce bitcoin transactions without having to spend a few days downloading the complete block-chain ledger.

Imagine Bitcoin and block chains like pens and new page paper, so you'll need ink to write on the paper (blockchain). The "ink" in this analogy refers to the bitcoin you use to create a transaction. We're writing a deal in there. The data is called "Op_return". A op_return is an opcode, and the message you write in is like using digital ink to write information on a worldwide spreadsheet.


P_return limit 40 characters, this is Weibo size 140 words of 1/3 less than, you can store some interesting data. This allows applications to display new methods such as regulatory chains, purchase certificates, or digital notarial documents.


When you create a transaction in a block chain, it broadcasts on thousands of computers running Bitcoin software all over the world, so these transactions are distributed across the network for a few seconds op_return. Once the data is written, it is impossible to change it.


This changes the way we can create an application, because once the transaction is created, we can be assured that it will not change in any way. In addition, data can be validated by anyone who can access the block chain. What they need is the correct transaction ID.


Let's take a look at how to write a block chain information. The premise is to install good node.js


First, create a new directory:


mkdir ' HelloWorld '


Access directory, install two library packages, Bitcore and bitcore-explorers:


sudo npm install bitcore@0.13.0 && sudo npm install bitcore-explorers


To ensure that the installation is correct: type HelloWorld ls, you can see a directory called Node_modules.


Opens the text editor to start coding, creating a file Helloworld.js:


var Bitcore = require (' Bitcore ');
var explorers = require (' bitcore-explorers ');
This allows you to call the two library packages:


var insight = new Explorers. Insight ();


Create Bitcoin Wallet


We send a small amount of bitcoin from one place to another, which means we need two things: a bitcoin address, and a bit-currency address that we can control ourselves.


Bitcoin is based on public and private key, you can create a SHA-256 hash of your private key to create a public key, then how do you create a private key.


There are many ways, the simplest is to use rushwallet.com to generate the actual wallet, which is not recommended for long-term storage of Bitcoin, but suitable for this tutorial:


1. Visit


2. Create Wallet


3. Create the wallet, in "Your Bitcoin address" will see public addresses, copy stored to helloworld.js variable "publicaddress".


4. Click on the Settings button, select the export Keys, copy stored to helloworld.js variable "Privatekey".


The following is the code so far:


var Bitcore = require (' Bitcore ');
var explorers = require (' bitcore-explorers ');
var insight = new Explorers. Insight ();


var publicaddress = ' 1dufrrfejvchwptqidqmk3dw3mp9xz3uta ';
var privatekey = ' 5KG7BZHGX5JACD46CXBN1TX6NQ1ZSA4GAZ4BAKMW277RKGBH3QC '
Send Destination Address 1hlod9e4sdffpdiyfnynkblq85y51j3zb1 is Stoshi's first wallet address. Assign to bitcoinrecipient variable.


Also need a few variables, the first is a mining fee, which we send the transaction to the block chain of the basic costs, equivalent to postage stamps.


var minerfee = 667;


Here is the information sent to the block chain:


var blockchainmessage = ' Hello satoshi! ';


You can send one to the test destination address, first check the balance of our public address.


var Bitcore = require (' Bitcore ');
var explorers = require (' bitcore-explorers ');
var insight = new Explorers. Insight ();


var publicaddress = ' 18jyibktnazbs2sezsrdkgsefwljxvw9uy ';
var privatekey = ' 5hxkwx4pkze3aib4ywrdzkmyrf9hwydavp3tnkpymigbxnlulwn '


Insight.getunspentutxos (publicaddress, function (Error, Utxos) {
Console.log (' Utxos ' + ': ' + json.stringify (utxos, Undefined, 2));
});
You should see the following information:


Utxos: [
{
"Address": "18jyibktnazbs2sezsrdkgsefwljxvw9uy",
"Txid": "6c2e97cd1[author]0[/author]9[author]0[/author]6ad89fdd1[author]0[/author]c1f[author]0[/author]68fc2[ author]0[/author]ecc7[author]0[/author]9a9dcc152dab1d7a2c7118a4158 ",
"Vout": 1,
"Scriptpubkey": "76a9145[author]0[/author]1a6c[author]0[/author]e1e7[author]0[/author]3[author]0[/author]1[ Author]0[/author][author]0[/author]a9a3cdc14b9[author]0[/author]676[author]0[/author]fc45bb388ac ",
"Amount": [Author]0[/author]. [author]0[/author][author]0[/author][author]0[/author][author]0[/author]42
}
]
Here query Uxto not use the transaction output, can see how many bits in this wallet, the following code checks whether we have enough money to pay for mining costs:


if (Bitcore. UNIT.FROMBTC (Utxos[[author]0[/author]].toobject (). Amount). tosatoshis ()-minerfee > Minerfee) {
Console.log ("We ' ve got enough satoshis!");
}
If you get the wrong message, it means your wallet doesn't have enough money to pay for the mining fee.


One-way and broadcast trading


Send Bitcoin from A to point B, looking at a very high level, just like signing a digital check. It's just your "signature," which is a number of builds that use your private key and the money you used to send money uxto from a bit-currency address. This digital check can be written to any Bitcoin address, and Op_return will act like a note of this check, relating to the transaction.


Once the deal is signed, we need to tell the block chain that Bitcoin does the heaviest work here, but what happens behind the scenes is that the transaction is validated by the latest version of the spreadsheet, making sure that the money you send is actually in your control. Once verified, the miner will record your transaction and it will disclose it in blockchain.


Here is the snippet code for signing and broadcasting the transaction:


Sign the transaction with a combo of your private key, the address is want to pay, and your last uxto.


Insight.getunspentutxos (publicaddress, function (Error, Utxos) {
Console.log (' Utxos ' + ': ' + json.stringify (utxos, Undefined, 2));
if (utxos.length = = [Author]0[/author]) {
Console.log ("Not enough satoshis to cover the miner fee.");
Return
else if (bitcore. UNIT.FROMBTC (Utxos[[author]0[/author]].toobject (). Amount). tosatoshis ()-minerfee > Minerfee) {
Console.log ("We ' ve got enough satoshis!");

var transaction = new Bitcore. Transaction ()
. from (Utxos[[author]0[/author])//using the last Uxto to sign the next transaction
. to (Bitcoinrecipient, 42[author]0[/author][author]0[/author]-Minerfee)//Send 42[author]0[/author][author]0[/ Author] Satoshi ' s
. AddData (Blockchainmessage)//We message to Satoshi
. sign (Privatekey); Last step for the digital autograph

Console.log (' Transaction_hex: ' + transaction.checkedserialize ());

Insight.broadcast (Transaction, function (Error, body) {
if (Error) {
Console.log (' Error in Broadcast: ' + error);
}
else {
Console.log ("success! Here's our Transaction ID: "+ Body";
Console.log (' http://explorer.chain.com/transactions/' + body + "#!transaction-op-return")
}
});
}
});
After the above code is complete, run:


Node Helloworld.js

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.