Gom3rye

Fabcar smart contract 분석 본문

졸업 프로젝트

Fabcar smart contract 분석

Gom3rye 2022. 5. 7. 19:33
package main
// Importing the Fabric SDK Go packages.
import (
        "errors"
        "fmt"
        "io/ioutil"
        "os"
        "path/filepath"

        "github.com/hyperledger/fabric-sdk-go/pkg/core/config"
        "github.com/hyperledger/fabric-sdk-go/pkg/gateway"
)

func main() {
// Set the environment variable DISCOVERY_AS_LOCALHOST to true.
        os.Setenv("DISCOVERY_AS_LOCALHOST", "true")
// Create a new FileSystemWallet named wallet.
// A wallet existing in the 'wallet' folder
        wallet, err := gateway.NewFileSystemWallet("wallet")
// If the wallet creation failed, print an error message and exit.
        if err != nil {
                fmt.Printf("Failed to create wallet: %s\n", err)
                os.Exit(1)
        }
// First, it checks if the wallet already exists. If it doesn’t, it creates it.
        if !wallet.Exists("appUser") {
                // Next, it checks if the wallet is empty. If it is, it populates it with the contents of the appUser.json file.
err = populateWallet(wallet)
                if err != nil {
                        fmt.Printf("Failed to populate wallet contents: %s\n", err)
                        os.Exit(1)
                }
        }
// Path to the network config (CCP) file
        ccpPath := filepath.Join(
                "..",
                "..",
                "test-network",
                "organizations",
                "peerOrganizations",
                "org1.example.com",
                "connection-org1.yaml",
        )
// Connect to the gateway peer(s) using the network config and identity in the wallet
gw, err := gateway.Connect(
                gateway.WithConfig(config.FromFile(filepath.Clean(ccpPath))),
                gateway.WithIdentity(wallet, "appUser"),
        )
        if err != nil {
                fmt.Printf("Failed to connect to gateway: %s\n", err)
                os.Exit(1)
        }
        defer gw.Close()

// Get the network channel 'mychannel'
        network, err := gw.GetNetwork("mychannel")
        if err != nil {
                fmt.Printf("Failed to get network: %s\n", err)
                os.Exit(1)
        }
// Get the smart contract 'fabcar'
		contract := network.GetContract("fabcar")

        result, err := contract.EvaluateTransaction("queryAllCars")
        if err != nil {
                fmt.Printf("Failed to evaluate transaction: %s\n", err)
                os.Exit(1)
        }
// Print the result
        fmt.Println(string(result))
// Submit a transaction in that contract to the ledger
        result, err = contract.SubmitTransaction("createCar", "CAR10", "VW", "Polo", "Grey", "Mary")
        if err != nil {
                fmt.Printf("Failed to submit transaction: %s\n", err)
                os.Exit(1)
        }
        fmt.Println(string(result))

        result, err = contract.EvaluateTransaction("queryCar", "CAR10")
        if err != nil {
                fmt.Printf("Failed to evaluate transaction: %s\n", err)
                os.Exit(1)
        }
        fmt.Println(string(result))

        _, err = contract.SubmitTransaction("changeCarOwner", "CAR10", "Archie")
        if err != nil {
                fmt.Printf("Failed to submit transaction: %s\n", err)
                os.Exit(1)
        }

        result, err = contract.EvaluateTransaction("queryCar", "CAR10")
        if err != nil {
                fmt.Printf("Failed to evaluate transaction: %s\n", err)
os.Exit(1)
        }
        fmt.Println(string(result))
}

func populateWallet(wallet *gateway.Wallet) error {
        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 errors.New("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))

        err = wallet.Put("appUser", identity)
        if err != nil {
                return err
        }
        return nil
}
728x90
반응형