BSN one day learn to use the block chain development -python articles

"Using BSN one day learn to block chain development" series can help developers have some development experience, without the need for learning block chain development language, complete the application on the block chain block chain services network (BSN) development.

Before paper describes how a user interacts carried out using the Python language and the data link through the chain of business systems under the relevant operating procedures described, in the description of business data exchange system and chain at the chain, first of all to the user how to block chain service portal registration, application publishing services, application services and application management involved in setting them.

Registration and login

Open the block chain service network URL: www.bsnbase.com, click [beta] application login page button to enter the closed beta application page:

3e33cfa88df849829266957b6be69391

According to fill a page of tips beta closed beta application and submit the application, the user closed beta application is divided into individual users and business users the latter two, the applicant should be a progressive application according to their user type optional, closed beta applications submitted wait for the system to be reviewed, you can log in until the system through the audit will send the activation email application, applicant user can activate the link to activate the account activation email based on [], account activation system.

Application Services released

Application service is one that has been released and the block chain applications running in the block chain service network, users can publish their own block chain application service through the service portal, application services are divided into two kinds of public and private (closed beta period release of non-public service by default. For public service required to submit published applications, back-office operations personnel for review, only through the audit service is available for viewing in-service portal application store) , where not one screenshot, to outline the overall process application service release:

· After logging block chain service portal into the application I published -> I published service page;

· Click Create a new service button, enter the page to create a new service, enter the appropriate information as prompted;

· Click on the Next button to start uploading chain code package (package code can be used to upload chain chain code package developed their own chain code or direct the use of prefabricated package service provided by the network) , and then define the function and role of service (system call indicates chain business chain authorization code corresponding to) , select a city node issued (representing the current release of the service code corresponding to the chain nodes in the deployment of which city) , the information payment period and whether to use cloud services;

· Continue to click Next button, go to Settings access information page, enter the website address, mobile terminal access and API access and other services (information access methods are under chain access) , if no need to set access, you can skip this page continue to click Next button to enter the service page bill be determined (measured during service network, all the resources are free to use) ;

· Click OK after the application of my post -> I posted the service list page you can see services under the newly created, and the status of the service is "pending trial" status, operators and publishers need to wait for the operation and maintenance personnel review and publication, the publisher released after the service by default does not participate in the service.

Application services involved

During the closed beta service network, all published services are non-public, if they want to participate or other users of the service has been published, can be obtained by the use of my release -> service I posted the list of invited participants , will be invited participants link address to the user needs to participate in services, application services involved in the overall process is as follows:

· According to the invitation link address, enter the service information page, click on the application to participate in the application to enter service role and urban node selection page;

• Select service role and urban nodes require the use of (A role is user participation rights on the business system chain under the service after the chain corresponding; urban node refers to the user's Chain business systems to exchange data through the city node chain ). After the user selects a service role, the system will display the corresponding royalties role, select a node when the city apply for a new certificate or select an existing certificate.

· Click on the OK button to prompt access to urban nodes, access and cost information:

• After confirming participation in urban nodes access services, access and cost information (closed beta period, all resources are free) in my application to participate - > I participated in the list of pages you can view the application services are involved, the status of the application services to publishers pending , waiting for the application service publisher of participants for review.

· During the closed beta service network as long as the publisher of the services involved in the application review by the state, participation in the information service updates have been confirmed . So far, successful participation application services.

· After a successful application to participate in the service, under chain business systems can be called a gateway city node node corresponding to the data on the chain and push the chain from data acquisition and chain block information, call the gateway node corresponding parameters can be entered me participating services -> I was involved in -> View -> access configuration parameters and service access node city section view, access node city for urban node certificate access and download the city calls node gateway address on display, application service access configuration parameters for the system call parameters urban traffic at the gateway node chain display, details of this section are described with reference to " data exchange " section ;

Application Management

After application service release, to enter the application I published -> I published service management has released the list of services for unified management, list management specifically includes the following features:

Invite participants: mainly used to invite link address will be applied to other users to participate, for the other to participate in this service by this invitation link address;

· Original: mainly closed beta period, all published services are closed by default, if you need to open the service, the public can submit applications through this function;

· Service Upgrade: mainly used for application service chain code package to upgrade;

· Configuration upgrade: mainly used for the allocation of resources to upgrade application services, such as urban node deployment and configuration information for node resources;

· Historical Versions: mainly used for future service upgrades to query the historical version;

· Run info: mainly used for the deployment of nodes and node resource information and links on data application services (derived from the chain under the operational system by calling the data node gateway interface will push the chain) monitoring;

· Set the access mode: the access link is mainly used for business applications and services system configuration;

· View: mainly used for basic information application services, and the deployment of chain code information, service information role, as well as approval of recording information such as access to view.

Application development chain code

Chain code (ChainCode), also known as intelligent contracts, contract terms describing computer language, terms of the transaction, the business logic of the transaction, etc., and automatically perform operations on data books transactions by calling intelligent contracts. A plurality of block chaining applications can be deployed chain code, each comprising a plurality of chain code method.

Chain code supports multiple languages, including golang, java, node.js. Each program chain code must implement Chaincode interface chain code comprising: three basic operations Init, Invoke, Query:

· The Init: chain code initialization method, called once the chain code instantiation or upgrade, so that the chain code can perform any necessary initialization, including initialization state of the application.

· The Invoke: receiving and processing chains under the operational system call transaction proposal, the name of the program chain code in which the function call parameters comprising a specific business process and data parameters. I.e. other services call the Invoke branch processing response parameters according to different methods. Invoke inlet can be simply understood as a chain code method.

· Query: providing chain code data query method, the method is only used as a query does not provide data on the operation of the chain of operations. Query operation may be called upon, as the branch is also invokes methods in the Invoke method. This method may not be implemented.

Below an example common data link packet code elaborate.

Common Data Link code package that we provide to the business chain code data basic CRUD operations for application developers (Golang written language). Application developers can based on this chain code on the package further expand the chain code features based on application business requirements. This supports the chain code type of data stored character string, integer, float, set (map, list) and the like.

Chain code package Download: www.bsnbase.com/static/base/BaseChainCode.zip

1. The chain code package function as follows:

1.1 Increasing data (set)

Input Parameter Description

Unique primary key identifying the need to save, baseValue:: baseKey stored data

例:{"baseKey":"str","baseValue":"this is string"}

NOTE: wherein baseKey string is not empty, baseValue may be any type of data. If baseKey already exists, the process directly returns already exists, can not be added; if not, the data is added.

1.2. Modified data (Update)

Input Parameter Description

baseKey: the need to modify the primary key of a unique, baseValue: stored data

例:{"baseKey":"str","baseValue":"this is string"}

NOTE: wherein baseKey string is not empty, baseValue may be any type of data. If baseKey does not exist, it can not be updated if it already exists, modify the data.

1.3 Deleting data (delete)

Input Parameter Description

baseKey: the primary key value only need to remove identified

例:"str"

Note: baseKey where the value can not be empty, and must exist, otherwise it will not be deleted.

1.4. Acquiring data (get)

Input Parameter Description

baseKey: unique primary key value of the identified need to obtain

例:"str"

Note: baseKey where the value can not be empty, and must exist, otherwise it will not be able to get the appropriate information.

2. Introduction chain code

2.1.Init method

653a2dec2be84bc79c3c263221894fba

This is the most simple chain code initialization function, write the log, initialize a data saved to the database, response.

Recommended chain code to initialize (Init), do not have too many business operations.

2.2. Invoke

2597930de192469e89691683c377d9cb

stub.GetFunctionAndParameters (): method name (string) and the parameter information acquisition request ([] String)

return t.set (stub, args) // call the save method, specific business processes

Node gateway

节点网关是部署在各个城市节点,接收应用系统的请求,使用托管的用户身份信息,向相应的应用链码发起访问并返回链码的执行结果。节点网关的调用是通过向区块链服务的各个城市节点的网关服务发送HTTP请求来实现。节点网关负责验证用户身份信息、应用信息,通过用户身份信息和应用信息以及需要访问的链码、链码方法来传递链码参数、获取链码执行结果的服务通道。

业务系统需要按照接口说明在请求中加入相应的请求参数,调用节点网关以后,节点网关会返回链码的执行结果。

接口地址:https://节点网关地址/api/node/reqChainCode

注:用户参与服务成功后可以在服务详情页面查看并下载应用链下业务系统开发所需要的应用服务配置参数、节点网关地址和应用身份证书,如下图:

c05b7c157de6487b8bc7edee583e85be

通讯方式:POST

签名算法:

1、将userCode+ appCode+ chainCode+ funcName的值以及args中每一项数据拼接成字符串A;

2、对字符串A使用用户证书的私钥进行SHA256WITHECDSA签名。

请求参数

082f14f415d04731916540a9d19a46fe

响应参数

f57c1c35e5674cd2b2d9936d84115436

数据交互

应用服务参与审核通过之后,链下业务系统就可以通过节点网关与链上数据进行数据交互,调用节点网关需要节点网关接入地址、节点网关请求参数以及证书等信息,此部分数据可以通过我参与的应用->我参与的->查看明细页面进行获取,下面对这三方面的参数进行一一说明;

· 节点网关接入地址:为链下业务系统调用链上所对应的城市节点入口,所有与链上的数据交互都是通过此地址进行访问,如下图所示:

bfe278642d67456dabf4a9131b2581c0

· 节点网关请求参数:如果需要通过节点网关接入地址与链上进行数据交互,肯定需要按照节点网关接口调用规范,节点网关接口调用需要userCode、appCode、tid、chainCode、funcName、agrs等参数信息,下面对相关参数进行概要说明。

· userCode:应用发布者或者参与者的登录名(也就是登录门户的用户名)。

· appCode:参与应用的唯一标识。应用创建时,系统自动生成的唯一标识。

· tid:用户与参与的应用之间关联的唯一标识。

· chainCode:区块链服务网络中,运维部署的链码的名称。需要注意的是,这里的链码名称不是服务发布时的链码名称。

· funcName:调用链码的方法名称。

· agrs:调用链码方法的参数集合。字符串类型的数组,参与者需要与应用发布者联系,获取方法对应的参数。

参数获取页面如下图所示:

e92114411b4749e18f164584182e0215

· 节点用户证书:在请求节点网关过程中需要https证书、请求报文签名证书(即用户身份证书)和响应结果验签证书。

· https请求证书:为保障数据传输层的安全,需要使用https请求。

· 请求报文签名证书:用户私钥证书。

· 响应结果验签证书:网关公钥证书。

用户身份证书的下载可以通过我参与的应用->我参与的->查看->接入的城市节点部分进行下载,也可以通过进入我的身份证书-证书查看列表中找到对应的应用信息,进行证书下载。下载的证书文件包含https证书、用户证书(私钥证书、公钥证书(网关对报文的验签))、网关证书(网关公钥证书)。证书下载页面如下图所示

6102c9bc46c24a7e9701c358ccd1fd5a

34ecc79a45e442b1bd08a3ea2f28322a

业务系统开发

业务系统就是链下业务系统,需要与链上进行数据交互的系统,下面根据预置链码包的Python开发实例(可从门户下载)着重说一下与网关交互的说明。

项目环境准备:

1、Pycharm/VS Code(可以使用您习惯的IDE)

2、Python 3.6.6+

3、Django 2.2.5

4、Django-bootstrap3 11.1.0

5、requests 2.22.0

6、cryptography 2.7

7、fabric-sdk-py 0.8.1

注:可根据requirements.txt文件自动安装依赖

项目介绍:

9e542828699d426eb0d210b495a91a59

上图为项目的目录结构:

该项目使用Python-Django框架,直接调用服务网关api接口,实现数据交互:

1、 common 文件夹下文件说明:

· myecdsa256.py(椭圆曲线 SHA256WITHECDSA 签名方法和验签方法)

· loggers.py(日志方法)

2、 certificate 文件夹下文件说明(下载用户证书zip包获取):

· bsn_https.pem(https请求的公钥证书)

· gateway_public_cert.pem(网关公钥证书)

· private_key.pem(用户私钥证书)

· public_cert.pem(用户公钥证书)

3、 packages 文件夹下文件说明 :

· fabric-sdk-py-master.zip (fabric官方py库包,需解压后手动安装到python第三方库中 )

4、logs 文件夹下存放日志文件。

流程说明:

1、 用户在客户端(web页面)填写上链信息

2、 进入相应的方法,获取用户填写的上链信息,并且判断输入信息不可为空

3、 拼接待签名的字符串,对字符串使用用户私钥证书进行 SHA256WITHECDSA 签名加密(调用myecdsa256下的ecdsa_sign方法生成base64格式的mac值)

4、 发起post请求,并且附加HTTPS证书

5, get mac Returns the value in the packet, the value returned for mac message, the public key certificate signed using a gateway to carry out inspection, verify signatures when signing the same content pass argument string

6, if the inspection after signing the return message processing is successful, the chains display the web page

Detailed description

4fb0bd31b73a404f9343012587bbf7ac

dbc166f49e984e91bfe3852cfe8252a6

Initiate a request (for example to add data save)

e99bfa48b8a946cb88c745e806cdc01c

7c383b23b9c84d2c9871335308e06bf2

b757b4a9e2ca4b8586952e50351718e8



Guess you like

Origin blog.51cto.com/14580794/2454166