The proper way to call and interact with Hyperledger Fabric peers is via SDK, here is the list of available SDK's:
Read configuration file
var err error conf, err := config.InitConfig("config.yaml") if err != nil { fmt.Println(err) return }
Initialize fabric client
cl := fabricclient.NewClient(conf) bccspFactory.InitFactories(conf.CSPConfig()) cl.SetCryptoSuite(bccspFactory.GetDefault())
Load client certificate and keys
privKey := filepath.Join(conf.CryptoConfigPath(), "path to key") pubKey := filepath.Join(conf.CryptoConfigPath(), "path to cert")
Read and setup MSP ID
mspID, err := conf.MspID("org1") if err != nil { fmt.Println(err) return }
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)
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 }
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)
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.