Skip to content

I'm encountering an error when trying to create a DID using @hashgraph/did-sdk-js in a React application.  #209

@24sanskar

Description

@24sanskar

Description

Error message:
App.js:66 Error: TypeError: sdk_1.Hbar is not a constructor
at new HcsIdentityNetworkBuilder (hcs-identity-network-builder.js:18:1)
at createDID (App.js:31:1)

Environment:

  • React version: ^19.0.0
  • @hashgraph/sdk version: 2.16.2
  • @hashgraph/did-sdk-js version: ^0.1.1
  • Node.js version: v23.3.0

Code snippet:
const createDID = async () => {
try {
const accId = AccountId.fromString("0.0.52******");

  const privateKey = PrivateKey.fromString(
    "3030020100300706052b8104000a042204200441846fdb***************"
  );

  // Set up Hedera client
  const client = Client.forTestnet();
  client.setOperator(accId, privateKey);

  console.log(" ===== CLIENT =====");
  console.log(client);
  console.log(" ===== CLIENT =====");

  // Create an identity network
  const network = new HcsIdentityNetworkBuilder()
    .setNetwork("testnet")
    .setAppnetName("exampleAppnet")
    .execute(client);

  console.log(" ===== NETWORK =====");
  console.log(network);
  console.log(" ===== NETWORK =====");

  // Generate a new DID
  const didRootKey = PrivateKey.generate();
  const did = HcsDid.fromDidRootKey(didRootKey, network);

  // Create a DID document
  const didDocument = new DidDocumentBase(did.toString());
  didDocument.setDidRootKey(new HcsDidRootKey(didRootKey));

  // Create a DID transaction
  const didTransaction = new HcsDidTransaction()
    .setDidDocument(didDocument)
    .setOperation(DidMethodOperation.CREATE)
    .signWithDidRootKey(didRootKey);

  // Submit the transaction to the Hedera network
  const response = await didTransaction.execute(client);

  console.log(" ===== DID =====");
  console.log(did.toString());
  console.log(" ===== DID =====");

  console.log("======= DID CREATED =======");
  console.log(response.transactionId, "txId", response);
  console.log("======= DID CREATED =======");
} catch (error) {
  console.error("Error:", error);
}

};

Great, it's a good idea to raise this as a GitHub issue if you've exhausted other troubleshooting options. When reporting the bug, it's important to provide as much relevant information as possible. Here's a template you can use or adapt for your GitHub issue:

Title: "TypeError: sdk_1.Hbar is not a constructor in React app using @hashgraph/did-sdk-js"

Description:

I'm encountering an error when trying to create a DID using @hashgraph/did-sdk-js in a React application.

Error message:
App.js:66 Error: TypeError: sdk_1.Hbar is not a constructor
at new HcsIdentityNetworkBuilder (hcs-identity-network-builder.js:18:1)
at createDID (App.js:31:1)

Environment:

  • React version: [Your React version]
  • @hashgraph/sdk version: [Your SDK version]
  • @hashgraph/did-sdk-js version: [Your DID SDK version]
  • Node.js version: [Your Node.js version]

Code snippet:
[Include your createDID function here]

I've tried the following solutions:

  1. Updating @hashgraph/sdk and @hashgraph/did-sdk-js to the latest versions
  2. Modifying import statements
  3. Using different methods to create Hbar instances

None of these solutions resolved the issue.

Additional information:
I'm using Create React App and have added the following webpack configuration to handle polyfills:
const webpack = require("webpack");

module.exports = {
webpack: {
configure: (webpackConfig) => {
webpackConfig.resolve.fallback = {
crypto: require.resolve("crypto-browserify"),
stream: require.resolve("stream-browserify"),
vm: require.resolve("vm-browserify"),
process: require.resolve("process/browser.js"),
buffer: require.resolve("buffer"),
};

  webpackConfig.plugins = (webpackConfig.plugins || []).concat([
    new webpack.ProvidePlugin({
      process: "process/browser.js",
      buffer: ["buffer", "Buffer"],
    }),
  ]);
  return webpackConfig;
},

},
};

Steps to reproduce

const createDID = async () => {
try {
const accId = AccountId.fromString("0.0.5****");

  const privateKey = PrivateKey.fromString(
    "3030020100300706052b8104000a********************"
  );

  // Set up Hedera client
  const client = Client.forTestnet();
  client.setOperator(accId, privateKey);

  console.log(" ===== CLIENT =====");
  console.log(client);
  console.log(" ===== CLIENT =====");

  // Create an identity network
  const network = new HcsIdentityNetworkBuilder()
    .setNetwork("testnet")
    .setAppnetName("exampleAppnet")
    .execute(client);

  console.log(" ===== NETWORK =====");
  console.log(network);
  console.log(" ===== NETWORK =====");

  // Generate a new DID
  const didRootKey = PrivateKey.generate();
  const did = HcsDid.fromDidRootKey(didRootKey, network);

  // Create a DID document
  const didDocument = new DidDocumentBase(did.toString());
  didDocument.setDidRootKey(new HcsDidRootKey(didRootKey));

  // Create a DID transaction
  const didTransaction = new HcsDidTransaction()
    .setDidDocument(didDocument)
    .setOperation(DidMethodOperation.CREATE)
    .signWithDidRootKey(didRootKey);

  // Submit the transaction to the Hedera network
  const response = await didTransaction.execute(client);

  console.log(" ===== DID =====");
  console.log(did.toString());
  console.log(" ===== DID =====");

  console.log("======= DID CREATED =======");
  console.log(response.transactionId, "txId", response);
  console.log("======= DID CREATED =======");
} catch (error) {
  console.error("Error:", error);
}

};

Additional context

image

Hedera network

testnet

Version

0.1.1

Operating system

Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions