About Hyperledger fabric - Node.js connection

0

Hi, I'm trying to connect hyperledger in my node.js application. I worked locally on test-network and it worked properly for me. Then I created the network via Aws and deployed the chaincodes. I then try to connect to this network via nodejs ( https://docs.aws.amazon.com/ja_jp/managed-blockchain/latest/hyperledger-fabric-dev/managed-blockchain-get-started-tutorial.html ) follow this document I have successfully established the network, but on the nodejs side, I got an invalid ca end point error at the first stage. Is there any document or help for aws hyperledger fabric and nodejs connection?

here is my connection.json file

{
    "name": "${NETWORKNAME}-${MEMBERNAME}",
    "version": "1.0.0",
    "client": {
        "organization": "${MEMBERNAME}",
        "connection": {
            "timeout": {
                "peer": {
                    "endorser": "300"
                }
            }
        }
    },
    "organizations": {
        "${MEMBERNAME}": {
            "mspid": "${MEMBERID}",
            "peers": ["peer0.org.${MEMBERNAME}.aws.com"],
            "certificateAuthorities": ["ca.org.${MEMBERNAME}.aws.com"]
        }
    },
    "peers": {
        "peer0.org.${MEMBERNAME}.aws.com": {
            "url": "grpcs://${PEERSERVICEENDPOINT}",
            "tlsCACerts": {
                "pem": "${PEERPEM}"
            },
            "grpcOptions": {
                "ssl-target-name-override": "peer0.org.${MEMBERNAME}.aws.com",
                "hostnameOverride": "peer0.org.${MEMBERNAME}.aws.com"
            }
        }
    },
    "certificateAuthorities": {
        "ca.org.${MEMBERNAME}.aws.com": {
            "url": "https://${CASERVICEENDPOINT}",
            "caName": "${MEMBERID}",
            "tlsCACerts": {
                "pem": ["${CAPEM}"]
            },
            "httpOptions": {
                "verify": false
            }
        }
    }
}

here is my api endpoint :

import { NextApiRequest, NextApiResponse } from 'next'
import { ok, badRequest } from '@helpers/response'
import FabricCAServices from 'fabric-ca-client'
import { Gateway, GatewayOptions, Wallets, Wallet } from 'fabric-network'
import * as path from 'path'

const deneme = async (req: NextApiRequest, res: NextApiResponse) => {
  if (req.method === 'POST') {
    const createWallet = async (): Promise<Wallet> => {
      const walletPath = path.resolve(process.cwd(), 'wallet')
      const wallet = await buildWallet(walletPath)
      return wallet
    }

    const buildWallet = async (walletPath: string): Promise<Wallet> => {
      let wallet: Wallet
      if (walletPath) {
        wallet = await Wallets.newFileSystemWallet(walletPath)
      } else {
        wallet = await Wallets.newInMemoryWallet()
      }
      return wallet
    }

    const buildCAClient = (
      ccp: Record<string, any>,
      caHostName: string
    ): FabricCAServices => {
      // Create a new CA client for interacting with the CA.
      const caInfo = ccp.certificateAuthorities[caHostName] // lookup CA details from config
      const caTLSCACerts = caInfo.tlsCACerts.pem
      const caClient = new FabricCAServices(
        caInfo.url,
        { trustedRoots: caTLSCACerts, verify: false },
        caInfo.caName
      )

      return caClient
    }

    const enrollAdmin = async (
      caClient: FabricCAServices,
      wallet: Wallet,
      orgMspId: string
    ): Promise<void> => {
      try {
        // Check to see if we've already enrolled the admin user.
        const identity = await wallet.get('********')
        if (identity) {
          return
        }

        console.log('Enrolling Admin...')

        // Enroll the admin user, and import the new identity into the wallet.
        const enrollment = await caClient.enroll({
          enrollmentID: 'admin',
          enrollmentSecret: 'adminpw',
        })
        console.log('Enrollment: ', enrollment)
        const x509Identity = {
          credentials: {
            certificate: enrollment.certificate,
            privateKey: enrollment.key.toBytes(),
          },
          mspId: orgMspId,
          type: 'X.509',
        }
        await wallet.put(('********'), x509Identity)
        console.log(
          'Successfully enrolled admin user and imported it into the wallet'
        )
      } catch (error) {
        console.error(`Failed to enroll admin user : ${error}`)
      }
    }

    const createGateway = async (
      connectionProfile: Record<string, any>,
      identity: string,
      wallet: Wallet
    ): Promise<Gateway> => {
      const gateway = new Gateway()

      const options: GatewayOptions = {
        wallet,
        identity,
        discovery: { enabled: false, asLocalhost: true },
      }

      console.log('Connecting to Fabric gateway...')
      console.log('Connection Profile: ', options.wallet)

      const a = await gateway.connect(connectionProfile, options)
      console.log(a)

      return gateway
    }

    const wallet = await createWallet()
    const ccp = require('ccp path')
    const caClient = buildCAClient(ccp, 'ca.org.****.aws.com')
    await enrollAdmin(caClient, wallet, '********id*****')

    const gatewayOrg1 = await createGateway(ccp, '********', wallet)

    return ok(res, 'Register Admin')
  } else {
    return badRequest(res)
  }
}

export default deneme

: Failed to connect before the deadline on Endorser- name: {PEER ENDPOINT} connected:false, connectAttempted:true

waitForReady - Failed to connect to remote gRPC server peer0.org.stratance-prod.aws.com url:grpcs://{PEERN ENDPOINT} 2023-07-03T13:07:47.347Z - info: [NetworkConfig]: buildPeer - Unable to connect to the endorser peer0.org.stratance-prod.aws.com due to Error: Failed to connect before the deadline on Endorser- name: peer0.org.stratance-prod.aws.com, url:grpcs://{PEER ENDPOINT}, connected:false, connectAttempted:true at checkState (/Users/ersinaydogmus/Desktop/Cruxia Projeler/mvp/node_modules/@grpc/grpc-js/build/src/client.js:77:26) at Timeout._onTimeout (/Users/ersinaydogmus/Desktop/Cruxia Projeler/mvp/node_modules/@grpc/grpc-js/build/src/channel.js:525:17) at listOnTimeout (node:internal/timers:569:17) at process.processTimers (node:internal/timers:512:7) { connectFailed: true }

Ersin
asked 10 months ago817 views
No Answers

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions