How to query Hyperledger Fabric blockchain data outside CLI container?

匿名 (未验证) 提交于 2019-12-03 08:30:34

问题:

I am using the ./byfn.sh script from the fabric-sample to start my blockchain. I would like to invoke and query data from a PHP website from the blockchain.

I do know that I can only use the API call from CLI container or use a JavaSDK/NodeSDK, RESTapi.

How can I query data from the blockchain the easiest way? I am looking at NodeSDK and RestApi but I don't see much guides out there to help me, hence this question. Sorry for the newbie questions.

Thanks!

回答1:

The proper way to call and interact with Hyperledger Fabric peers is via SDK, here is the list of available SDK's:

  1. Java SDK
  2. GoLang SDK
  3. NodeJS SDK
  4. Python SDK


UPDATE

As suggested by @christo4ferris there is an additional project:

  1. REST SDK

(Which might be the one you are interested in)


For example you can use golang SDK and do following:

  1. Read configuration file

    var err error conf, err := config.InitConfig("config.yaml") if err != nil { fmt.Println(err) return }

  2. Initialize fabric client

    cl := fabricclient.NewClient(conf) bccspFactory.InitFactories(conf.CSPConfig()) cl.SetCryptoSuite(bccspFactory.GetDefault())

  3. Load client certificate and keys

    privKey := filepath.Join(conf.CryptoConfigPath(), "path to key") pubKey := filepath.Join(conf.CryptoConfigPath(), "path to cert")

  4. Read and setup MSP ID

    mspID, err := conf.MspID("org1") if err != nil { fmt.Println(err) return }

  5. Setup client user into the context

    user, err := fabapi.NewPreEnrolledUser(conf, privKey, pubKey, "user1", mspID, cl.GetCryptoSuite()) if err != nil { fmt.Println(err) return } cl.SetUserContext(user)

  6. Setup ordering node

    ordererConf, err := conf.OrdererConfig("orderer0") if err != nil { fmt.Println(err) return }

    o, err := orderer.NewOrderer(fmt.Sprintf("%s:%d", ordererConf.Host, ordererConf.Port), filepath.Join(conf.CryptoConfigPath(), "path to orderer cert"), "orderer.example.com", conf) if err != nil { fmt.Println(err) return }

  7. Setup and initialize channel and the endorsing peer

    peers, err := conf.PeersConfig("org1") if err != nil { fmt.Println(err) return }

    p, err := peer.NewPeer(fmt.Sprintf("%s:%d", peers[0].Host, peers[0].Port), conf) if err != nil { fmt.Println(err) }

    ch, err := cl.NewChannel("mychannel") if err != nil { fmt.Println(err) return }

    ch.AddOrderer(o) ch.AddPeer(p) ch.SetPrimaryPeer(p) cl.SaveUserToStateStore(user, true)

  8. Prepare transaction proposal request send it and submit for ordering

    txRequest := apitxn.ChaincodeInvokeRequest{ Targets: []apitxn.ProposalProcessor{p}, Fcn: "myFCN", Args: []string{"myargs"}, TransientMap: map[string][]byte{}, ChaincodeID: "helloworld", }

    proposalResponse, _, err := ch.SendTransactionProposal(txRequest) if err != nil { fmt.Println(err) return }

    fmt.Printf("%v\n", proposalResponse[0].ProposalResponse)

    tx, err := ch.CreateTransaction(proposalResponse) if err != nil { fmt.Println(err) return }

    txResponse, err := ch.SendTransaction(tx) if err != nil { fmt.Println(err) return }

    fmt.Println(txResponse[0])

Pretty same way this will work across all SDK's.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!