区块链c端应用小程序
by Niharika Singh
由Niharika Singh
如何使用Hyperledger在区块链上创建应用程序 (How to create an application on blockchain using Hyperledger)
We are going to build a digital bank using Hyperledger Composer. It will have customers and accounts. At the end of it, you’ll be able to transfer funds and record all transactions on blockchain. We’ll expose a RESTful API for the same, so that even a person who has no clue what blockchain is can make a beautiful user interface (UI) around it. We’ll also create this application’s UI in Angular.
我们将使用Hyperledger Composer构建数字银行。 它将有客户和帐户。 最后,您将能够转移资金并在区块链上记录所有交易。 我们将公开相同的RESTful API,以便即使不知道区块链是什么的人也可以围绕它创建漂亮的用户界面(UI)。 我们还将在Angular中创建此应用程序的UI。
I’m super excited to share this step-by-step guide with you. So let’s get started right away!
很高兴与您分享此分步指南。 因此,让我们立即开始吧!
When I was first coding this out, I ran into errors. Lots and lots of them. But I think that’s good, because it made me learn a lot of things. Errors are essential. I got to a point where I felt switching it on and off would make things better. It almost made me lose my mind, but it’s an integral part in every hacker’s life.
当我第一次进行编码时,我遇到了错误。 很多很多。 但是我认为那很好,因为它使我学到了很多东西。 错误是必不可少的。 我觉得开和关它会使事情变得更好。 这几乎使我失去了理智,但这是每个黑客生活中不可或缺的一部分。
Before getting started, you need to ensure that the machine you’re using is equipped with the required configurations. You may need to download certain prerequisites and set up a basic dev environment. Below are the links to do that. Follow those steps before starting to develop an application, otherwise you’ll definitely run into stupid errors.
开始之前,您需要确保所使用的机器配备了必需的配置。 您可能需要下载某些先决条件并设置基本的开发环境。 以下是执行此操作的链接。 在开始开发应用程序之前,请执行以下步骤,否则,您肯定会遇到愚蠢的错误。
First install the Hyperledger composer. Then install the development environment.
首先安装Hyperledger composer 。 然后安装开发环境 。
There’s no need to start Playground while you’re installing the environment.
在安装环境时,无需启动Playground。
Make sure docker is running, and when you run ./startFabric.sh it’s going to take a couple of minutes. So be patient.
确保docker正在运行,并且在运行./startFabric.sh时将花费几分钟。 所以要耐心点。
Now that your machine is all set, we can start coding!
现在您的机器已经准备就绪,我们可以开始编码了!
步骤1:概述您的业务网络 (Step 1: Outline your Business Network)
Our Business Network Definition (BND) consists of the data model, transaction logic, and access control rules. The data model and access control rules are coded in domain specific language (which is very simple to catch up with). The transaction logic will be coded in JavaScript.
我们的业务网络定义(BND)由数据模型,事务逻辑和访问控制规则组成。 数据模型和访问控制规则以特定于域的语言进行编码(非常容易掌握)。 事务逻辑将用JavaScript编码。
To create a BND, we need to create a suitable project structure on disk. We will create a skeleton business network using Yeoman. To create a project structure, open your terminal and run the following command:
要创建BND,我们需要在磁盘上创建合适的项目结构。 我们将使用Yeoman创建一个基本的业务网络。 要创建项目结构,请打开您的终端并运行以下命令:
$ yo hyperledger-composer
This will shoot out a series of questions as follows. You’ll be required to use your arrow keys to navigate through the answers.
这将引发以下一系列问题。 您将需要使用箭头键浏览答案。
Open this project in your favorite text editor. I’m using Visual Code. This is what the file structure will look like:
在您喜欢的文本编辑器中打开该项目。 我正在使用Visual Code。 文件结构如下所示:
Delete the contents of test/logic.js. We won’t be using it at the moment.
删除test / logic.js的内容。 我们暂时不会使用它。
步骤2.1:编制我们的业务网络(models / test.cto) (Step 2.1: Coding out our Business Network (models/test.cto))
First, we’ll define models/test.cto. It contains the class definitions for all assets, participants, and transactions in the business network. This file is written in Hyperledger Composer Modelling Language.
首先,我们将定义models / test.cto 。 它包含业务网络中所有资产,参与者和交易的类定义。 该文件是用Hyperledger Composer建模语言编写的。
namespace test
asset Account identified by accountId {o String accountId--> Customer ownero Double balance}
participant Customer identified by customerId {o String customerIdo String firstNameo String lastName}
transaction AccountTransfer {--> Account from--> Account too Double amount}
Account is an asset which is uniquely identified with accountId. Each account is linked with Customer who is the owner of the account. Account has a property of balance which indicates how much money the account holds at any moment.
帐户是一种由accountId唯一标识的资产。 每个帐户都与该帐户所有者的客户相关联。 帐户具有余额属性,可指示帐户随时拥有多少资金。
Customer is a participant which is uniquely identified with customerId. Each Customer has firstName and lastName.
客户是一个由customerId唯一标识的参与者。 每个客户都有firstName和lastName 。
AccountTransfer is a transaction that can occur to and from an Account. And how much money is to be transferred is stored in amount.
AccountTransfer是可能发生的,并从一个账号的交易。 多少钱将被转存。
步骤2.2:编制业务网络(lib / logic.js) (Step 2.2: Coding out the Business Network (lib/logic.js))
In this file, we’ll add transaction logic in JavaScript.
在此文件中,我们将在JavaScript中添加事务逻辑。
/*** Sample transaction* @param {test.AccountTransfer} accountTransfer* @transaction*/
function accountTransfer(accountTransfer) {if (accountTransfer.from.balance < accountTransfer.to.balance) {throw new Error ("Insufficient funds");}
accountTransfer.from.balance -= accountTransfer.amount;accountTransfer.to.balance += accountTransfer.amount;
return getAssetRegistry('test.Account').then (function (assetRegistry) {return assetRegistry.update(accountTransfer.from);}).then (function () {return getAssetRegistry('test.Account');}).then(function (assetRegistry) {return assetRegistry.update(accountTransfer.to);});
}
@param {test.AccountTransfer} accountTransfer is the decorator we put at the top of the file to link the transaction with our JavaScript function. Then we validate if the account where funds are has enough money. Otherwise, an error will be thrown. Then we perform basic addition and subtraction on the account’s balance.
@param {test.AccountTransfer} accountTransfer是我们放置在文件顶部的装饰器,用于将交易与我们JavaScript函数链接在一起。 然后,我们验证资金所在的帐户中是否有足够的资金。 否则,将引发错误。 然后,我们对帐户余额执行基本的加法和减法。
At this point, the most important step is to update this on the blockchain. To do this we call getAssetRegistry API of our assets which is Account. Then we update the retrieved assetRegistry for both the account doling out the funds and the account receiving the funds.
此时,最重要的步骤是在区块链上进行更新。 为此,我们调用资产的Account的getAssetRegistry API。 然后,我们为分配资金的帐户和接收资金的帐户更新检索到的assetRegistry 。
步骤3:生成业务网络存档(BNA) (Step 3: Generate the Business Network Archive (BNA))
Now that the business network has been defined, it must be packaged into a deployable business network archive (.bna
) file.
既然已经定义了业务网络,则必须将其打包到可部署的业务网络档案( .bna
)文件中。
Step 3.1: Navigate into the test-bank app in your terminal.
步骤3.1:进入终端中的测试银行应用。
Step 3.2: Run the following command:
步骤3.2:运行以下命令:
$ composer archive create -t dir -n .
This creates a .bna file in the test-bank folder.
这将在测试银行文件夹中创建一个.bna文件。
步骤4:在结构上部署业务网络存档文件 (Step 4: Deploy the Business Network Archive file on the Fabric)
Step 4.1: Install composer runtime
步骤4.1:安装Composer运行时
$ composer runtime install --card PeerAdmin@hlfv1 --businessNetworkName test-bank
Step 4.2: Deploy the business network
步骤4.2:部署业务网络
$ composer network start --card PeerAdmin@hlfv1 --networkAdmin admin --networkAdminEnrollSecret adminpw --archiveFile test-bank@0.0.1.bna --file networkadmin.card
(Make sure you’re in the test-bank folder).
(确保您位于测试银行文件夹中)。
Step 4.3: Import the network administrator identity as a usable business network card
步骤4.3:将网络管理员身份导入为可用的商务网卡
$ composer card import --file networkadmin.card
Step 4.4: To check that the business network has been deployed successfully, run the following command to ping the network:
步骤4.4:要检查业务网络是否已成功部署,请运行以下命令ping网络:
$ composer network ping --card admin@test-bank
步骤5:公开RESTful API (STEP 5: Expose a RESTful API)
To create a RESTful API from your command line, run the following command:
要从命令行创建RESTful API,请运行以下命令:
$ composer-rest-server
This will shoot a lot of questions.
这将引发很多问题。
Now point your browser to http://localhost:3000/explorer.
现在,将浏览器指向http:// localhost:3000 / explorer。
You’ll see your beautiful blockchain API.
您将看到美丽的区块链API。
Now let’s add two Customers.
现在让我们添加两个客户。
Fist, let’s add a customer named Niharika Singh:
拳头,让我们添加一个名为Niharika Singh的客户:
We get a 200 response code.
我们得到一个200的响应码。
Now we’ll add customer named Tvesha Singh in a similar way.
现在,我们以类似的方式添加名为Tvesha Singh的客户。
To check if you’ve added them correctly, GET them.
要检查是否正确添加了它们,请获取它们。
You’ll see two customers in the response body.
您将在响应正文中看到两个客户。
Now let’s add 2 accounts linked to these two customers.
现在,我们添加链接到这两个客户的2个帐户。
Add accounts this way. Now, GET them to check if you’ve added them correctly.
这样添加帐户。 现在,获取它们以检查是否正确添加了它们。
Now let’s transfer 75 from Niharika to Tvesha.
现在让我们将75从Niharika转移到Tvesha。
Let’s check if the balance is updated by getting the account information.
让我们通过获取帐户信息来检查余额是否已更新。
Viola! It works. Niharika has 25 now, and Tvesha has 125.
中提琴! 有用。 Niharika现在有25,而Tvesha有125。
第6步:有角前端 (Step 6: Angular Front End)
To create Angular scaffolding automatically, run the following command in the test-bank folder:
要自动创建Angular脚手架,请在test-bank文件夹中运行以下命令:
$ yo
This will ask multiple questions.
这将问多个问题。
And it will take a couple of minutes.
这将需要几分钟。
Navigate into the bank-app.
导航到银行应用程序。
$ npm start
This starts the Angular server.
这将启动Angular服务器。
The Angular file structure is created as follows:
Angular文件结构创建如下:
Point your browser to http://localhost:4200. That’s where the magic is happening! You’ll see this screen:
将浏览器指向http:// localhost:4200。 那就是魔术发生的地方! 您会看到以下屏幕:
Now go to Assets in the top right corner and click on Account.
现在,转到右上角的“ 资产” ,然后点击“ 帐户” 。
These are the exact accounts we created.
这些是我们创建的确切帐户。
So now you can play around with this.
因此,现在您可以尝试一下。
You have your front end and your backend ready!
您已经准备好前端和后端!
All transactions that happen on localhost:3000 are reflected on localhost:4200 and vice versa. And this is all on blockchain.
在localhost:3000上发生的所有事务都反映在localhost:4200上,反之亦然。 而这一切都在区块链上。
Recently I wrote an article on blockchain’s use cases. I listed and explained about 20 ideas. They can be found here:
最近,我写了一篇有关区块链用例的文章。 我列出并解释了大约20个想法。 它们可以在这里找到:
How can India get blockchained?The blockchain epoch has just begun and like any other technology, blockchain will also hit couple of roadblocks…medium.com
印度如何获得区块链? 区块链时代才刚刚开始,就像其他任何技术一样,区块链也将遇到很多障碍…… medium.com
If you have a business idea and want to concretise it with technology and architectural details, feel free to reach me at niharika.3297@gmail.com
如果您有经营理念,并想通过技术和建筑细节来具体化,请随时通过niharika.3297@gmail.com与我联系。
区块链c端应用小程序