@uledger/uledger-sdk
v1.0.1
Published
ULedger TypeScript SDK
Readme
ULedger TypeScript SDK
TypeScript implementation for Public ULedger SDK.
Docs
npm run generate:docsThe definition of the API is available from ./docs/index.html
Installation
To install the package run:
npm i @uledger/uledger-sdkQuickstart
To get started, please walk through the instalation steps above. Once this is done and you have installed the uledger-sdk using NPM, you may proceed with the following steps. Note: This tutorial assumes that you have an existing blockchain set up with uledger.
The code snippet below is typescript implementation of creating a transaction and submitting it to the blockchain using the uledger SDK. To execute this successfully, you will need to replace the placeholder values with real values. This includes your node url, the node id, the url to the atomic clock service, blockchain id, and have user accounts set up with uledger.
import { ULedgerTransactionInputV2, ULedgerTransactionSessionV2 } from '@uledger/uledger-sdk';
async function main() {
try {
(async () => {
const txnSession = new ULedgerTransactionSessionV2(
{
nodeUrl: "{{ URL TO A ULEDGER NODE }}",
// url is a parameter coming from ULedger Team, make sure you specify the version.
atomicClockUrl: "{{ ACS URL }}",
nodeId: "{{ ULEDGER NODE ID }}"
}
);
// Customize the payload to be sent
const txnInputData: ULedgerTransactionInputV2 = {
blockchainId: "{{ your blockchain id }}",
to: "{{ the address of the user you would like to send the transaction to }}",
from: "{{ the address of the account you are sending the transaction from }}",
payload: {
myPayload: "Hello World!"
},
payloadType: "DATA",
senderSignature: "{{ This is where you would put the cryptographic signature of the transaction }}"
}
// Send the request to the node
const txn = await txnSession.createTransaction(txnInputData);
// Show the result
console.log(txn);
})();
} catch (error) {
console.log("Fail ", error);
}
}
main();In the next example, we will perform the same operation as above but this time we will take the response from the blockchain node and use that to query the transaction in the bms and the block that minted it.
import { ULedgerTransactionInputV2, ULedgerTransactionSessionV2, ULedgerBMSSession } from '@uledger/uledger-sdk';
async function main() {
try {
(async () => {
const txnSession = new ULedgerTransactionSessionV2(
{
nodeUrl: "{{ URL TO A ULEDGER NODE }}",
// url is a parameter coming from ULedger Team, make sure you specify the version.
atomicClockUrl: "{{ ACS URL }}/api/v1/acs",
nodeId: "{{ ULEDGER NODE ID }}"
}
);
// Customize the payload to be sent
const txnInputData: ULedgerTransactionInputV2 = {
blockchainId: "{{ your blockchain id }}",
to: "{{ the address of the user you would like to send the transaction to }}",
from: "{{ the address of the account you are sending the transaction from }}",
payload: {
myPayload: "Hello World!"
},
payloadType: "DATA",
senderSignature: "{{ This is where you would put the cryptographic signature of the transaction }}"
}
// Send the request to the node
const txn = await txnSession.createTransaction(txnInputData);
// Show the result
console.log(txn);
// Create a new BMS session
const session = new ULedgerBMSSession({
// url is a parameter coming from ULedger Team, make sure you specify the version.
url: "{{ MY_BMS_URL }}/api/v1/bms"
});
// "trim" = exclude the inner payload of the transaction or block to be queried
const trim = true;
// Extract the transaction Id from the createTransaction response
const transactionId = txn.transactionId;
// --- Search for your transaction in the BMS
const bmsTxn = await session.searchTransactionById(transactionId, trim);
console.log("Retrieved transaction by ID:\n", bmsTxn);
// Extract the block Id from the createTransaction response
const blockId = txn.blockId;
// --- Search for your transaction in the BMS
const bmsBlock = await session.searchBlockById(blockId, trim);
console.log("Retrieved block by ID:\n", bmsBlock);
})();
} catch (error) {
console.log("Fail ", error);
}
}
main();
