Fabric 实践 (一):用户验证Chaincode

本文介绍了基于Fabric V1.1.0构建的记账DAPP的第一步,详细阐述了一个简单的Chaincode实现,用于用户登录验证。Chaincode通过用户名作为Key,密码作为Value存储数据,确保登陆时的密码校验。在测试环节,演示了如何添加、验证、修改和删除用户,同时展示了如何在Couchdb中查看和管理这些信息,确保密码的安全存储。
摘要由CSDN通过智能技术生成

说明

我计划构建一个基于Fabric的记帐DAPP,同时也是一个学习的过程,在此分享。
以下内容全部基于Fabric V1.1.0

摘要

下面实现第一个Chaincode 用于用户登陆验证。该Chaincode非常简单,以用户名作为Key 以其密码作为Value,这样当用户登陆时就可以检查其密码是否正确。

Chaincode

package main
/**
* file:authentication_chaincode.go
* 用于用户登陆验证
**/
import (
    "encoding/json"
    "fmt"
    "strconv"
    "strings"

    "github.com/hyperledger/fabric/core/chaincode/shim"
    pb "github.com/hyperledger/fabric/protos/peer"
)

type AuthenticationChaincode struct{}


type ResInfo struct {
    Status bool   `json:"status"`
    Msg    string `json:"msg"`
}

func (t *ResInfo) error(msg string) {
    t.Status = false
    t.Msg = msg
}
func (t *ResInfo) ok(msg string) {
    t.Status = true
    t.Msg = msg
}

func (t *ResInfo) response() pb.Response {
    resJson, err := json.Marshal(t)
    if err != nil {
        return shim.Error("Failed to generate json result " + err.Error())
    }
    return shim.Success(resJson)
}

func main() {
    err := shim.Start(new(AuthenticationChaincode))
    if err != nil {
        fmt.Printf("Error starting Simple chaincode: %s", err)
    }
}

// Init initializes chaincode
// ===========================
func (t *AuthenticationChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
    return shim.Success(nil)
}

func (t *AuthenticationChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
    function, args := stub.GetFunctionAndParameters()
    fmt.Println("invoke is running " + function)

    // Handle different functions
        if function == "add" { //create a new marble
        return t.add(stub, args)
    } else if function == "del" { //change owner of a specific marble
        return t.del(stub, args)
    } else if function == "update" {
        return t.update(stub, args)
    } else if function == "authenticate" { //transfer all marbles of a certain color
        return t.authenticate
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

iblks

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值