mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-25 11:05:10 +00:00
Refactor Go files
1. replace deprecated ioutil functions (ioutil is deprecated since Go 1.16) 2. fix variable names that collide with imported package name 3. fix typos Signed-off-by: Tommy TIAN <xtianae@connect.ust.hk>
This commit is contained in:
parent
9f9cec7195
commit
16acf7b6dd
7 changed files with 31 additions and 35 deletions
|
|
@ -13,8 +13,8 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -52,7 +52,7 @@ func main() {
|
||||||
sign := newSign()
|
sign := newSign()
|
||||||
|
|
||||||
// Create a Gateway connection for a specific client identity
|
// Create a Gateway connection for a specific client identity
|
||||||
gateway, err := client.Connect(
|
gw, err := client.Connect(
|
||||||
id,
|
id,
|
||||||
client.WithSign(sign),
|
client.WithSign(sign),
|
||||||
client.WithClientConnection(clientConnection),
|
client.WithClientConnection(clientConnection),
|
||||||
|
|
@ -65,9 +65,9 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
defer gateway.Close()
|
defer gw.Close()
|
||||||
|
|
||||||
network := gateway.GetNetwork(channelName)
|
network := gw.GetNetwork(channelName)
|
||||||
contract := network.GetContract(chaincodeName)
|
contract := network.GetContract(chaincodeName)
|
||||||
|
|
||||||
fmt.Println("initLedger:")
|
fmt.Println("initLedger:")
|
||||||
|
|
@ -126,7 +126,7 @@ func newIdentity() *identity.X509Identity {
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadCertificate(filename string) (*x509.Certificate, error) {
|
func loadCertificate(filename string) (*x509.Certificate, error) {
|
||||||
certificatePEM, err := ioutil.ReadFile(filename)
|
certificatePEM, err := os.ReadFile(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to read certificate file: %w", err)
|
return nil, fmt.Errorf("failed to read certificate file: %w", err)
|
||||||
}
|
}
|
||||||
|
|
@ -135,11 +135,11 @@ func loadCertificate(filename string) (*x509.Certificate, error) {
|
||||||
|
|
||||||
// newSign creates a function that generates a digital signature from a message digest using a private key.
|
// newSign creates a function that generates a digital signature from a message digest using a private key.
|
||||||
func newSign() identity.Sign {
|
func newSign() identity.Sign {
|
||||||
files, err := ioutil.ReadDir(keyPath)
|
files, err := os.ReadDir(keyPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Errorf("failed to read private key directory: %w", err))
|
panic(fmt.Errorf("failed to read private key directory: %w", err))
|
||||||
}
|
}
|
||||||
privateKeyPEM, err := ioutil.ReadFile(path.Join(keyPath, files[0].Name()))
|
privateKeyPEM, err := os.ReadFile(path.Join(keyPath, files[0].Name()))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Errorf("failed to read private key file: %w", err))
|
panic(fmt.Errorf("failed to read private key file: %w", err))
|
||||||
|
|
@ -222,10 +222,10 @@ func transferAssetAsync(contract *client.Contract) {
|
||||||
fmt.Printf("Successfully submitted transaction to transfer ownership from %s to Mark. \n", string(submitResult))
|
fmt.Printf("Successfully submitted transaction to transfer ownership from %s to Mark. \n", string(submitResult))
|
||||||
fmt.Println("Waiting for transaction commit.")
|
fmt.Println("Waiting for transaction commit.")
|
||||||
|
|
||||||
if status, err := commit.Status(); err != nil {
|
if commitStatus, err := commit.Status(); err != nil {
|
||||||
panic(fmt.Errorf("failed to get commit status: %w", err))
|
panic(fmt.Errorf("failed to get commit status: %w", err))
|
||||||
} else if !status.Successful {
|
} else if !commitStatus.Successful {
|
||||||
panic(fmt.Errorf("transaction %s failed to commit with status: %d", status.TransactionID, int32(status.Code)))
|
panic(fmt.Errorf("transaction %s failed to commit with status: %d", commitStatus.TransactionID, int32(commitStatus.Code)))
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("*** Transaction committed successfully\n")
|
fmt.Printf("*** Transaction committed successfully\n")
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
@ -129,14 +128,14 @@ func populateWallet(wallet *gateway.Wallet) error {
|
||||||
|
|
||||||
certPath := filepath.Join(credPath, "signcerts", "cert.pem")
|
certPath := filepath.Join(credPath, "signcerts", "cert.pem")
|
||||||
// read the certificate pem
|
// read the certificate pem
|
||||||
cert, err := ioutil.ReadFile(filepath.Clean(certPath))
|
cert, err := os.ReadFile(filepath.Clean(certPath))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
keyDir := filepath.Join(credPath, "keystore")
|
keyDir := filepath.Join(credPath, "keystore")
|
||||||
// there's a single file in this dir containing the private key
|
// there's a single file in this dir containing the private key
|
||||||
files, err := ioutil.ReadDir(keyDir)
|
files, err := os.ReadDir(keyDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -144,7 +143,7 @@ func populateWallet(wallet *gateway.Wallet) error {
|
||||||
return fmt.Errorf("keystore folder should have contain one file")
|
return fmt.Errorf("keystore folder should have contain one file")
|
||||||
}
|
}
|
||||||
keyPath := filepath.Join(keyDir, files[0].Name())
|
keyPath := filepath.Join(keyDir, files[0].Name())
|
||||||
key, err := ioutil.ReadFile(filepath.Clean(keyPath))
|
key, err := os.ReadFile(filepath.Clean(keyPath))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@ package main
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
@ -121,7 +120,7 @@ func (s *SmartContract) UpdateAsset(ctx contractapi.TransactionContextInterface,
|
||||||
return fmt.Errorf("the asset %s does not exist", id)
|
return fmt.Errorf("the asset %s does not exist", id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// overwritting original asset with new asset
|
// overwriting original asset with new asset
|
||||||
asset := Asset{
|
asset := Asset{
|
||||||
ID: id,
|
ID: id,
|
||||||
Color: color,
|
Color: color,
|
||||||
|
|
@ -138,7 +137,7 @@ func (s *SmartContract) UpdateAsset(ctx contractapi.TransactionContextInterface,
|
||||||
return ctx.GetStub().PutState(id, assetJSON)
|
return ctx.GetStub().PutState(id, assetJSON)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteAsset deletes an given asset from the world state.
|
// DeleteAsset deletes a given asset from the world state.
|
||||||
func (s *SmartContract) DeleteAsset(ctx contractapi.TransactionContextInterface, id string) error {
|
func (s *SmartContract) DeleteAsset(ctx contractapi.TransactionContextInterface, id string) error {
|
||||||
exists, err := s.AssetExists(ctx, id)
|
exists, err := s.AssetExists(ctx, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -254,18 +253,18 @@ func getTLSProperties() shim.TLSProperties {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if !tlsDisabled {
|
if !tlsDisabled {
|
||||||
keyBytes, err = ioutil.ReadFile(key)
|
keyBytes, err = os.ReadFile(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("error while reading the crypto file: %s", err)
|
log.Panicf("error while reading the crypto file: %s", err)
|
||||||
}
|
}
|
||||||
certBytes, err = ioutil.ReadFile(cert)
|
certBytes, err = os.ReadFile(cert)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("error while reading the crypto file: %s", err)
|
log.Panicf("error while reading the crypto file: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Did not request for the peer cert verification
|
// Did not request for the peer cert verification
|
||||||
if clientCACert != "" {
|
if clientCACert != "" {
|
||||||
clientCACertBytes, err = ioutil.ReadFile(clientCACert)
|
clientCACertBytes, err = os.ReadFile(clientCACert)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("error while reading the crypto file: %s", err)
|
log.Panicf("error while reading the crypto file: %s", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
"github.com/hyperledger/fabric-gateway/pkg/identity"
|
"github.com/hyperledger/fabric-gateway/pkg/identity"
|
||||||
|
|
@ -62,7 +62,7 @@ func newIdentity() *identity.X509Identity {
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadCertificate(filename string) (*x509.Certificate, error) {
|
func loadCertificate(filename string) (*x509.Certificate, error) {
|
||||||
certificatePEM, err := ioutil.ReadFile(filename)
|
certificatePEM, err := os.ReadFile(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to read certificate file: %w", err)
|
return nil, fmt.Errorf("failed to read certificate file: %w", err)
|
||||||
}
|
}
|
||||||
|
|
@ -71,11 +71,11 @@ func loadCertificate(filename string) (*x509.Certificate, error) {
|
||||||
|
|
||||||
// newSign creates a function that generates a digital signature from a message digest using a private key.
|
// newSign creates a function that generates a digital signature from a message digest using a private key.
|
||||||
func newSign() identity.Sign {
|
func newSign() identity.Sign {
|
||||||
files, err := ioutil.ReadDir(keyPath)
|
files, err := os.ReadDir(keyPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Errorf("failed to read private key directory: %w", err))
|
panic(fmt.Errorf("failed to read private key directory: %w", err))
|
||||||
}
|
}
|
||||||
privateKeyPEM, err := ioutil.ReadFile(path.Join(keyPath, files[0].Name()))
|
privateKeyPEM, err := os.ReadFile(path.Join(keyPath, files[0].Name()))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Errorf("failed to read private key file: %w", err))
|
panic(fmt.Errorf("failed to read private key file: %w", err))
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,6 @@ package main
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
|
@ -111,14 +110,14 @@ func populateWallet(wallet *gateway.Wallet) error {
|
||||||
|
|
||||||
certPath := filepath.Join(credPath, "signcerts", "cert.pem")
|
certPath := filepath.Join(credPath, "signcerts", "cert.pem")
|
||||||
// read the certificate pem
|
// read the certificate pem
|
||||||
cert, err := ioutil.ReadFile(filepath.Clean(certPath))
|
cert, err := os.ReadFile(filepath.Clean(certPath))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
keyDir := filepath.Join(credPath, "keystore")
|
keyDir := filepath.Join(credPath, "keystore")
|
||||||
// there's a single file in this dir containing the private key
|
// there's a single file in this dir containing the private key
|
||||||
files, err := ioutil.ReadDir(keyDir)
|
files, err := os.ReadDir(keyDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -126,7 +125,7 @@ func populateWallet(wallet *gateway.Wallet) error {
|
||||||
return errors.New("keystore folder should have contain one file")
|
return errors.New("keystore folder should have contain one file")
|
||||||
}
|
}
|
||||||
keyPath := filepath.Join(keyDir, files[0].Name())
|
keyPath := filepath.Join(keyDir, files[0].Name())
|
||||||
key, err := ioutil.ReadFile(filepath.Clean(keyPath))
|
key, err := os.ReadFile(filepath.Clean(keyPath))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,6 @@ import (
|
||||||
|
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hyperledger/fabric-gateway/pkg/client"
|
"github.com/hyperledger/fabric-gateway/pkg/client"
|
||||||
|
|
@ -53,7 +52,7 @@ func main() {
|
||||||
}
|
}
|
||||||
defer hsmSignerFactory.Dispose()
|
defer hsmSignerFactory.Dispose()
|
||||||
|
|
||||||
certificatePEM, err := ioutil.ReadFile(certPath)
|
certificatePEM, err := os.ReadFile(certPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
@ -150,7 +149,7 @@ func newHSMSign(h *identity.HSMSignerFactory, certPEM []byte) (identity.Sign, id
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadCertificate(filename string) (*x509.Certificate, error) {
|
func loadCertificate(filename string) (*x509.Certificate, error) {
|
||||||
certificatePEM, err := ioutil.ReadFile(filename) //#nosec G304
|
certificatePEM, err := os.ReadFile(filename) //#nosec G304
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ package functions
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/hyperledger/fabric-sdk-go/pkg/gateway"
|
"github.com/hyperledger/fabric-sdk-go/pkg/gateway"
|
||||||
|
|
@ -29,14 +29,14 @@ func populateWallet(wallet *gateway.Wallet) error {
|
||||||
|
|
||||||
certPath := filepath.Join(credPath, "signcerts", "cert.pem")
|
certPath := filepath.Join(credPath, "signcerts", "cert.pem")
|
||||||
// read the certificate pem
|
// read the certificate pem
|
||||||
cert, err := ioutil.ReadFile(filepath.Clean(certPath))
|
cert, err := os.ReadFile(filepath.Clean(certPath))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
keyDir := filepath.Join(credPath, "keystore")
|
keyDir := filepath.Join(credPath, "keystore")
|
||||||
// there's a single file in this dir containing the private key
|
// there's a single file in this dir containing the private key
|
||||||
files, err := ioutil.ReadDir(keyDir)
|
files, err := os.ReadDir(keyDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -44,7 +44,7 @@ func populateWallet(wallet *gateway.Wallet) error {
|
||||||
return fmt.Errorf("keystore folder should have contain one file")
|
return fmt.Errorf("keystore folder should have contain one file")
|
||||||
}
|
}
|
||||||
keyPath := filepath.Join(keyDir, files[0].Name())
|
keyPath := filepath.Join(keyDir, files[0].Name())
|
||||||
key, err := ioutil.ReadFile(filepath.Clean(keyPath))
|
key, err := os.ReadFile(filepath.Clean(keyPath))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue