fabric-samples/asset-transfer-basic/application-go/assetTransfer.go
Brett Logan 11c05fa612 Add missing apps and chaincodes to CI
Adds the apps and chaincodes to linting and testing
CI that weren't added before.

Linting issues were corrected where necessary to make CI pass.

The Basic-Go application and Private-Javascript application
are currently disabled pending fixes currently being worked on.

Signed-off-by: Brett Logan <brett.t.logan@ibm.com>
2020-08-16 23:11:24 -04:00

142 lines
3.3 KiB
Go

/*
Copyright 2020 IBM All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config"
"github.com/hyperledger/fabric-sdk-go/pkg/gateway"
)
func main() {
log.Println("============ application-golang starts ============")
err := os.Setenv("DISCOVERY_AS_LOCALHOST", "true")
if err != nil {
log.Fatalf("Error setting DISCOVERY_AS_LOCALHOST environemnt variable: %v", err)
}
wallet, err := gateway.NewFileSystemWallet("wallet")
if err != nil {
log.Fatalf("Failed to create wallet: %v", err)
}
if !wallet.Exists("appUser") {
err = populateWallet(wallet)
if err != nil {
log.Fatalf("Failed to populate wallet contents: %v", err)
}
}
ccpPath := filepath.Join(
"..",
"..",
"test-network",
"organizations",
"peerOrganizations",
"org1.example.com",
"connection-org1.yaml",
)
gw, err := gateway.Connect(
gateway.WithConfig(config.FromFile(filepath.Clean(ccpPath))),
gateway.WithIdentity(wallet, "appUser"),
)
if err != nil {
log.Fatalf("Failed to connect to gateway: %v", err)
}
defer gw.Close()
network, err := gw.GetNetwork("mychannel")
if err != nil {
log.Fatalf("Failed to get network: %v", err)
}
contract := network.GetContract("basic")
result, err := contract.SubmitTransaction("InitLedger")
if err != nil {
log.Fatalf("failed to evaluate transaction: %v", err)
}
log.Println(string(result))
result, err = contract.EvaluateTransaction("GetAllAssets")
if err != nil {
log.Fatalf("Failed to evaluate transaction: %v", err)
}
log.Println(string(result))
result, err = contract.SubmitTransaction("CreateAsset", "asset13", "yellow", "5", "Tom", "1300")
if err != nil {
log.Fatalf("Failed to submit transaction: %v", err)
}
log.Println(string(result))
result, err = contract.EvaluateTransaction("ReadAsset", "asset4")
if err != nil {
log.Fatalf("Failed to evaluate transaction: %v", err)
}
log.Println(string(result))
_, err = contract.SubmitTransaction("TransferAsset", "asset1", "Tom")
if err != nil {
log.Fatalf("Failed to submit transaction: %v", err)
}
result, err = contract.EvaluateTransaction("ReadAsset", "asset1")
if err != nil {
log.Fatalf("Failed to evaluate transaction: %v", err)
}
log.Println(string(result))
log.Println("============ application-golang ends ============")
}
func populateWallet(wallet *gateway.Wallet) error {
log.Println("============ Populating wallet ============")
credPath := filepath.Join(
"..",
"..",
"test-network",
"organizations",
"peerOrganizations",
"org1.example.com",
"users",
"User1@org1.example.com",
"msp",
)
certPath := filepath.Join(credPath, "signcerts", "cert.pem")
// read the certificate pem
cert, err := ioutil.ReadFile(filepath.Clean(certPath))
if err != nil {
return err
}
keyDir := filepath.Join(credPath, "keystore")
// there's a single file in this dir containing the private key
files, err := ioutil.ReadDir(keyDir)
if err != nil {
return err
}
if len(files) != 1 {
return fmt.Errorf("keystore folder should have contain one file")
}
keyPath := filepath.Join(keyDir, files[0].Name())
key, err := ioutil.ReadFile(filepath.Clean(keyPath))
if err != nil {
return err
}
identity := gateway.NewX509Identity("Org1MSP", string(cert), string(key))
return wallet.Put("appUser", identity)
}