Bitpay开源了它们的Bitcore库包,咱们能够运用它来基于Node.js发送信息到区块链。该库包能让咱们生产比特币买卖,却不用花费几天时刻下载完好的区块链账本。

幻想比特币和区块链如同笔和新的页面纸张,为了在“纸张”(Blockchain)上写字你需求墨水。在这个比方中的“墨水”是指你用来创立买卖的比特币。咱们在里边写一个买卖数据被称为“op_return”。一个op_return是操作码,你写在里边的消息就像运用数字墨水在世界范围的电子表格上写信息。

P_Return限制40字符,这是微博大小140字的1/3不到,你能够存储一些有趣的数据。这样应用程序就能够显示监管链,购买证明,或数字公证文件等新方法。

当你在区块链创立买卖,它会在世界各地的数千台运转比特币软件的电脑上播送,所以这些买卖op_return几秒钟也散布在整个网络。一旦数据被写入,它是不可能改变它。

这改变了咱们能够创立应用程序的方法,由于一旦事务被创立后,咱们能得到确保不变,由于不会以任何方法改变。另外,数据能够被能拜访区块链的任何人验证。他们所需求的是正确的买卖ID。

下面看看怎么写一个区块链信息。条件是装置好Node.js

首要,创立一个新目录:

mkdir 'HelloWorld'

进入目录,装置两个库包,bitcore和bitcore-explorers:

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

为确保装置正确:键入HelloWorld ls,可看到一个叫node_modules目录。

打开文本编辑器开始编码,创立一个文件HelloWorld.js:

var bitcore = require('bitcore');
var explorers = require(‘bitcore-explorers');

这样就能调用这两个库包:比方:

var insight = new explorers.Insight();

创立比特币钱包

咱们从一个地方发少数比特币到另外一个地方,那就意味着需求两件事:比特币承受地址,和一个咱们自己能操控的自己的比特币地址。

比特币是基于公有和私有key,你能创立你私有key的SHA-256 哈希创立公有key,那么你怎么创立私有key?

有许多方法,最简单的是运用RushWallet.com产生实际的钱包,这关于长时间存储比特币是不引荐的方法,却适合这儿教程:

1.拜访 RushWallet.com

2.创立钱包

3.创立完钱包,在"Your Bitcoin Address"会看到公有地址,仿制存储到HelloWorld.js的变量“publicAddress”.

4.点击设置按钮,挑选Export Keys,仿制存储到HelloWorld.js的变量“privateKey”.

下面是到目前为止的代码:

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

var publicAddress = '1DuFRRFEJvchWpTQiDqMk3DW3mP9XZ3UTa';
var privateKey = '5KG7bZhGX5jaCD46cxbN1tX6nq1zSa4gAZ4baKmw277RKGbH3qc'

发送方针地址1HLoD9E4SDFFPDiYfNYnkBLQ85Y51J3Zb1是Stoshi的第一个钱包地址。赋值给bitcoinRecipient变量。

还需求几个变量,第一个是挖矿费,这是咱们发送买卖给区块链的基础费用,相当于邮票邮费。

var minerFee = 667;

下面是发送给区块链的信息:

var blockchainMessage = 'Hello Satoshi!';

你能够发送一就是到测试方针地址,首要查看咱们共用地址的余额。

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) https://bicoin8.com/wp-content/uploads/2023/04/202304211cHpE0.jpg
console.log('utxos' + ' :' + JSON.stringify(utxos, undefined, 2));
});

你应该看到如下信息:

utxos :[
https://bicoin8.com/wp-content/uploads/2023/04/202304211cHpE0.jpg
"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
}
]

这儿查询UXTO未用买卖输出,能看到这个钱包中有多少比特币,下面代码查看咱们钱包是否有足够钱付出挖矿费:

if (bitcore.Unit.fromBTC(utxos[[author]0[/author]].toObject().amount).toSatoshis() - minerFee > minerFee)     https://bicoin8.com/wp-content/uploads/2023/04/202304211cHpE0.jpg
console.log("We've got enough Satoshis!");
}

如果得到错误信息,意味着你的钱包没有足够钱付出挖矿费。

单向和播送买卖

发送比特币从A点到B点,从一个非常高的水平看,就像是签署一个数字查看。也就是只要你的“签名”,这是一个用你的私钥和曩昔可从一个比特币地址寄钱UXTO的生成数。这一数字查看能够写到任何比特币地址,op_return会像扮演一份这种查看备注,与买卖有关的备注。

一旦买卖被签署,咱们需求告诉区块链,比特币做的最繁重的作业在这儿,但在暗地发生的是:买卖被最新版别的电子表格验证,确保你发送的钱是你实际上操控的。一经查实,矿工会记录你的买卖,它将在blockchain公开它。

这儿是签署和播送买卖的片段代码:

// Sign the transaction with a combo of your private key, the address you want to pay, and your last UXTO.

insight.getUnspentUtxos(publicAddress, function (error, utxos) https://bicoin8.com/wp-content/uploads/2023/04/202304211cHpE0.jpg
console.log('utxos' + ' :' + JSON.stringify(utxos, undefined, 2));
if(utxos.length == [author]0[/author]) https://bicoin8.com/wp-content/uploads/2023/04/202304211cHpE0.jpg
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) https://bicoin8.com/wp-content/uploads/2023/04/202304211cHpE0.jpg
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) // Our message to Satoshi
.sign(privateKey); // Last step for the digital autograph

console.log('transaction_hex: ' + transaction.checkedSerialize());

insight.broadcast(transaction, function (error, body) https://bicoin8.com/wp-content/uploads/2023/04/202304211cHpE0.jpg
if (error) https://bicoin8.com/wp-content/uploads/2023/04/202304211cHpE0.jpg
console.log('Error in broadcast: ' + error);
}
else https://bicoin8.com/wp-content/uploads/2023/04/202304211cHpE0.jpg
console.log("Success! Here's our Transaction ID: " + body);
console.log('http://explorer.chain.com/transactions/' + body + "#!transaction-op-return")
}
});
}
});

上述代码完成后,运转:

node HelloWorld.js

总结:自动驾驶汽车,数字民主和运送食品等等都能够从区块链中得到优点,从这个网络咱们能读取 信赖更重要是对改变做出反应。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注