Skip to main content

Import ULedger SDK

In this example, we'll be connecting our program to a ULedger blockchain and blockchain services. This is important because, to successfully use the SDK, we need to create a session that will connect us with the network. Once this is set up, we'll be able to push and pull transactions.

info

To complete this example, please install the ULedger SDK. For additional information, please visit Getting Started

Create a Blockchain session

To create a blockchain session, we will need a few things, specifically from the ULedger portal. Assuming you have a blockchain with ULedger, please head over to the portal and retrieve your Node URL, Node Id, and the Atomic Clock URL. If you are unable to find this information, please feel free to reach out to the ULedger Support team.

Once you have your Node ID, Node URL, and Atomic Clock URL, we should be able to create a session with the blockchain using the following code. Make sure to replace the placeholder values with the information you retrieved for the Node Id, Node URL, and Atomic Clock URL.

// Required imports
import (
"uledger.io/uledger-sdk/models"
"uledger.io/uledger-sdk/session"
)
// Create a session
optionsV2 := models.SessionOptions{
AtomicClockUrl: "https://acs.uledger.io",
NodeUrl: "http://my.node1.uledger.io",
NodeId: "MyNodeId",
}
transactionSession := session.NewULedgerTransactionSession(optionsV2)

The constant we created, "session," will be used to make calls to the ULedger blockchain. This is important because it allows us to create transactions and submit data to the blockchain. For more information about creating transactions, please visit this tutorial.

Create a BMS session

What exactly is the BMS, and why do we need it? The BMS, or Blockchain Management System, is a centralized service that helps maintain the status of each blockchain. We need the BMS because it processes all of our queries. Whether we need to pull blocks, search for a transaction, or count the transactions we've created on-chain, we can send a request to the BMS.

To create the BMS session, we only need a URL to the service. However, if your blockchain is private, we will need Authorization. First, you will need to create a session for a public blockchain. After that, you will need to review the additional steps needed to create a session with a private blockchain.

info

If you are unable to find the BMS URL or need help retrieving Authorization for your private chain, please feel free to reach out to the ULedger Support team.

Creating a BMS Session with a public blockchain

In the code below, we are creating a session with the blockchain management service. Please replace the placeholder values with your BMS URL.

// Required imports
import (
"uledger.io/uledger-sdk/models"
"uledger.io/uledger-sdk/session"
)
// Create a BMS Session
options := models.BMSSessionOptions{
URL: "http://127.0.0.1:8080/api/v1/bms",
}
sessionBms := session.NewULedgerBMSSession(options)

Creating a BMS Session with a private blockchain

In the code below, we are creating a session with the blockchain management service. This is for connecting to a private blockchain specifically. Please replace the placeholder values with your BMS URL and Authorization token.

// Required imports
import (
"uledger.io/uledger-sdk/authorizer"
"uledger.io/uledger-sdk/models"
"uledger.io/uledger-sdk/session"
)
// Create Authorization
auth := authorizer.NewBearerAuthorizer("{{AUTHORIZATION TOKEN}}")
// Create a BMS Session
options := models.BMSSessionOptions{
URL: "http://127.0.0.1:8080/api/v1/bms",
Authorizer: auth,
}
sessionBms := session.NewULedgerBMSSession(options)
© 2023 ULedger Inc. All rights reserved