hyperledger-fabrichyperledger-fabric-sdk-java

Running hyperledger fabric java gateway example code throws The gateway identity must be set


I am trying to write a fabric gateway app on basic-network, my code looks like this.


     public static void main(String[] args) throws Exception {
       // Load a file system based wallet for managing identities.
       Path walletPath = Paths.get("wallet");
       Wallet wallet = Wallet.createFileSystemWallet(walletPath);

       // load a CCP
       Path networkConfigPath = Paths.get("..", "..", "basic-network", "connection.yaml");

       Gateway.Builder builder = Gateway.createBuilder();
       builder.identity(wallet, "user1");
       builder.networkConfig(networkConfigPath).discovery(true);

       // create a gateway connection
       try (Gateway gateway = builder.connect()) {
          System.out.println("Hello");
       }
       catch(Exception e) {
          e.printStackTrace();
     }
   }

when i run the code, it throws the error

Exception in thread "main" java.lang.IllegalStateException: The gateway identity must be set

Please to help me to find how to set identity of this network?

my connection.yaml is

name: basic-network
version: 1.0.0
client:
    organization: Org1
    connection:
        timeout:
            peer:
                endorser: '300'
            orderer: '300'
channels:
    mychannel:
        orderers:
        - orderer.example.com
        peers:
            peer0.org1.example.com: {}
organizations:
    Org1:
        mspid: Org1MSP
        peers:
        - peer0.org1.example.com
        certificateAuthorities:
        - ca.example.com
orderers:
    orderer.example.com:
        url: grpc://localhost:7050
peers:
    peer0.org1.example.com:
        url: grpc://localhost:7051
certificateAuthorities:
    ca.example.com:
        url: http://localhost:7054
        caName: ca.example.com

there is a folder called User1@org1.example.com located in basic-network/crypto-config/peerOrganizations/org1.example.com/users/


Solution

  • Ok, I understood your problem. You are uncorrectly running the fabcar example from fabric samples.

    Inside the Java folder you have 2 directories: wallet and src. Inside src you will find your code, while in wallet you will store your user certificates.

    Before running the application, you should create certificates and you don't have certificates at the moment. Inside the folder where you run ClientApp.java, you should find 2 other files: EnrollAdmin.java and RegisterUser.java.

    First of all, run EnrollAdmin.java. This will contact your CA and obtain certificates for an admin user. Next, run RegisterUser.java to create user certificates.

    Your ClientApp is currently looking for user1 certificates, but you have no user1 in your wallet folder. After doing so, run again ClientApp.java and you should be fine. In your comment you mentioned about the User1@org1.example.com folder inside basic-network/crypto-config/peerOrganizations/org1.example.com/users/ but you should not use it for this example since you need X509 certificates released from your SDK contacting the CA.