Skip to content

Examples

Hai Eng edited this page Apr 27, 2022 · 13 revisions

Type of request

Get Transactions Info

TransactionHttp allows caller to announce signed transactions to the network via method announce. This is how all transactions are submitted.

  • Get transaction information by Transaction ID or Hash
  • Get transaction information by Transaction ID or Hash and Group
  • Get transactions information by Group
  • Get transactions information by Transaction IDs or hashes and Groups
  • Get transaction status by Transaction Hash or ID
  • Get transactions statuses by Transactions Hashes or IDs
  • Get count of transactions type
  • Get cosignature transaction by parentHash, signature and signer
  • Search transactions by query params

Get Accounts Info

AccountHttp allows caller to query for

  • Get account information for an account
  • Get account information for multiple accounts
  • Get account names for multiple accounts.
  • Get account properties for an account.
  • Get account properties for multiple accounts.
  • Get confirmed transactions information.
  • Get incoming transactions information.
  • Get outgoing transactions information.
  • Get partial transactions information.
  • Get unconfirmed transactions information.
  • Get multisig account information.
  • Get multisig account graph information.

Get Blockchain Info

BlockchainHttp allows caller to query for

  • get block information by block height.
  • get block transactions by block height.
  • get block information by block height.
  • get block receipts by block height.
  • get block storage information.
  • get receipt merkle path by block height and receipt hash.
  • get transaction merkle path by block height and transaction hash.
  • get block height with limit by block height and block limit.

Get Exchange Info

ExchangeHttp allows caller to query for

  • Get account exchanges from address
  • Get exchange offers by offer type and mosaic ID
  • Get exchange offer list

Get Metadata Info

Metadata allow tagging of Accounts, Mosaics and Namespaces with a key-value pairs of data. This can be used for purposes like blacklisting.

MetadataHttp allows caller to query for metadata associated with account, mosaic or namespace

  • Get metadata information by composite hash
  • Get metadata information by list of composite hash
  • Search metadata information by query params

Get Mosaics Info

Mosaics represent tokens on the network.

MosaicHttp allows caller to query for

  • Get mosaic information by mosaic ID
  • Get mosaic information by list of mosaic ID
  • Get mosaic names defined as aliases for the mosaic
  • Get mosaic levy information by mosaic ID
  • Get mosaic rich list by mosaic ID

Get Namespaces Info

Namespaces are similar to domain names. Root namespace (similar to top-level domain) can be declared with several sub-namespaces.

NamespaceHttp allow caller to query for namespace information. Namespaces are used to create aliases to mosaics and accounts.

  • Get namespace information by namespace Id
  • Get namespaces an account owns
  • Get readable names for a set of namespaces
  • Get namespace information by list of addresses

Get Network Info

NetworkHttp allow caller to query for

  • Get network type info

Get Lock Info

Secret Lock and Proof allow exchange of secret between parties. This can be used for cross-chain swaps.

  • Get account lock hash by address or public key
  • Get account lock secret by address or public Key
  • Get composite hash by composite hash
  • Get lock hash by hash
  • Get secret hash by hash

Account

An account is a key pair (private and public key) associated with a mutable state stored on the blockchain

A private key is a key to an account. Anyone having the private key to an account can initiate any account related action.

The public key can be used to verify signatures of the account. The public key is stored in the blockchain with the first issued transaction. An account which has not issued any transaction has its public key field empty.

Each account has a unique address. You will normally share the derived address instead, as it is shorter and gathers more information.

Creating and opening an account

Generate new account

var account = Account.GenerateNewAccount(NetworkType.MIJIN_TEST);

Console.WriteLine($"{nameof(Account)} : {account}");

Create a new account if you already have a private key

var account = Account. CreateFromPrivateKey("85CFAB0E6079DAA58D7FF0990ACA64E571EC58527A16DB9391C87C436261190C ",NetworkType.MIJIN_TEST);

Console.WriteLine($"{nameof(Account)} : {account}");

You may need to take note of your account address, the public key and private key for retrieving your account information later.

Getting the account information

Your newly created account may not available on the blockchain network yet. You may need to announce your account to the network either making a new transaction from your account or receive the transaction from another account.

The quickest way to make your account available on the blockchain network is to request the XPX currency from our Testnet faucet. The Testnet faucet will make the first transaction to your account address and announce your account to the network.

Open your browser and navigate to Proximax Sirius BC Testnet Faucet. Enter your account address and click send button.

alt text

Wait for 15 seconds and check your account information

// Creates an instance of SiriusClient
var client = new SiriusClient("https://bctestnet1.brimstone.xpxsirius.io/");

// Generates a new account
var account = Account.GenerateNewAccount(NetworkType.MIJIN_TEST);

// Gets the account information
var accountInfo = await client.AccountHttp.GetAccountInfo(account.Address);

Console.WriteLine($"{nameof(AccountInfo)} : {accountInfo}");

Verify the number of asset units ( mosaics ) in your account. At the moment you may only had 1 asset type which is the network XPX currency.

foreach (var asset in accountInfo.Mosaics)
{
    Console.WriteLine($"My asset : {asset.Id},No units: {asset.Amount}");
}

Getting the account filter information

Your account may configure a set of smart rules to block announcing or receiving transactions given a series of constraints.

Learn to add or remove account filter here

Address filter

An account can decide to receive transactions only from an allowed list of addresses. Similarly, an account can specify a list of addresses that don’t want to receive transactions from.

Getting the account filter information from the transaction that has the block address

var accountFilter = await client.AccountHttp.GetAccountProperty(account.Address);

// Verify the list of block account addresses that don't want
// to receive transactions from
var blockedAddressFilter =
    accountFilter.AccountProperties.Properties
    .Single(p => p.PropertyType == PropertyType.BLOCK_ADDRESS);

// Display the blocked address
foreach (var ba in blockedAddressFilter.Values)
{
  Console.WriteLine($"Blocked Address {Address.CreateFromHex(ba.ToString())}");
}

Mosaic filter

An account can configure a filter to permit incoming transactions only if all the mosaics attached are allowed. On the other hand, the account can refuse to accept transactions containing a mosaic listed as blocked

Getting the account filter information from transaction contains allowed mosaic

var accountFilter = await client.AccountHttp.GetAccountProperty(account.Address);

// Verify the list of mosaic allowed to receive from the transaction
var allowedMosaicFilter =
    accountFilter.AccountProperties.Properties.Single(ap =>
        ap.PropertyType == PropertyType.ALLOW_MOSAIC);

foreach (var am in allowedMosaicFilter.Values)
{
    // The mosaic id is an array of uint
    var arrayIds = JsonConvert.DeserializeObject<List<uint>>(am.ToString());

    // Create new mosaic id
    var mosaicId = new MosaicId(arrayIds.FromUInt8Array());

    // Display the mosaic id
    Console.WriteLine($"Mosaic Id {mosaicId}");
}

Reading an account transactions

Get the list of confirmed transactions where an account is involved.

var transactions = await client.AccountHttp.Transactions(account.PublicAccount);

foreach (var tx in transactions)
{
    Console.WriteLine($"Transaction Info {tx}");
}

By default, the Proximax.Sirius.SDK provides up to 10 transactions. However, you can increase the page size up to 100 transactions.

// set page size up to 50 transactions
const int pageSize = 100;

// create query string
var queryParams = new QueryParams(pageSize, "");

var transactions = await client.AccountHttp.Transactions(account.PublicAccount, queryParams);

foreach (var tx in transactions)
{
    Console.WriteLine($"Transaction Info {tx}");
}

// to get more than 100 transactions, you need to make further request
// with the last transaction identifier known returned by the
// previous request
queryParams = new QueryParams(pageSize, transactions.Last().TransactionInfo.Id);

transactions = await client.AccountHttp.Transactions(account.PublicAccount, queryParams);

foreach (var tx in transactions)
{
    Console.WriteLine($"Transaction Info {tx}");
}

Linking a namespace to an address

An account can link a registered name (namespace or subnamespace) with an account address. Once the link established, you can make send the transaction to the namespace or subnamespace instead of the account address.

// This is a registered namespace
var namespaceId = new NamespaceId("proximax.io");

// The account with network currency
var account = Account.CreateFromPrivateKey("85CFAB0E6079DAA58D7FF0990ACA64E571EC58527A16DB9391C87C436261190C", NetworkType.MIJIN_TEST);

// Creates the alias transaction to link the namespace
// to the account address
var namespaceAddressLinkTransaction = AliasTransaction.CreateForAddress(
    account.Address,
    namespaceId,
    AliasActionType.LINK,
    Deadline.Create(),
    NetworkType.MIJIN_TEST
);
// Get the generation hash 
var generationHash = await siriusClient.BlockHttp.GetGenerationHash();

// Signs the transaction
var signedTransaction = account.Sign(namespaceAddressLinkTransaction,generationHash);

// Announce the transaction to the network
await client.TransactionHttp.Announce(signedTransaction);

// Verifies the namespace linked to the address
var namespaceInfo = await client.NamespaceHttp.GetNamespace(namespaceId);

Console.WriteLine($"Namespace links info {namespaceInfo.Alias}");

// Send some money to the namespace instead of account address
var seedAccount = Account.CreateFromPrivateKey("85CFAB0E6079DAA58D7FF0990ACA64E571EC58527A16DB9391C87C436261190C", NetworkType.MIJIN_TEST);

var transferTransaction = TransferTransaction.Create(
    Deadline.Create(),
    namespaceInfo.Id,
    new List<Mosaic>()
    {
        NetworkCurrencyMosaic.CreateRelative(10)
    },
    PlainMessage.Create("Send to 10 xpx to namespace"),
    client.NetworkHttp.GetNetworkType().Wait());

// Signs the transaction
signedTransaction = seedAccount.Sign(transferTransaction,generationHash);

// Announce the transaction to the network
await client.TransactionHttp.Announce(signedTransaction);

// Verifies the amount sent to the address linked to the namespace
var accountInfo = await client.AccountHttp.GetAccountInfo(account.Address);

Console.WriteLine($"Check account amount info {accountInfo.Mosaics}");

Metadata

Modify Metadata

Metadata is a feature of Sirius Chain, which allows to attach some data in format of maps map[string]string. Metadata can be attached to Address, Mosaic, and Namespace.

Account Metadata

// Create account with balance
var account = await Fixture.GenerateAccountWithCurrency(10000);

//Create account metadata transaction
var metadatatrx = AccountMetadataTransaction.Create(
    Deadline.Create(), 
    Fixture.SeedAccount.PublicAccount, 
    "metadata_key_name", 
    //associated key
    "test",
    //old associated key, leave empty if it's new initial metadata
    "", 
    Fixture.NetworkType);

var aggregateTransaction = AggregateTransaction.CreateBonded(
    Deadline.Create(),
    new List<Transaction>
        {
            metadatatrx.ToAggregate(Fixture.SeedAccount.PublicAccount),
        },
        Fixture.NetworkType);

//Sign transaction(metadata)
var aggregateTransactionsigned = Fixture.SeedAccount.Sign(aggregateTransaction, Fixture.GenerationHash);

var hashLockTransaction = HashLockTransaction.Create(
    Deadline.Create(),
    NetworkCurrencyMosaic.CreateRelative(10),
    (ulong)3650,
    aggregateTransactionsigned,
    Fixture.NetworkType);

var hashLockTransactionSigned = account.Sign(hashLockTransaction, Fixture.GenerationHash);
//Announce transaction(hash lock transaction)
await Fixture.SiriusClient.TransactionHttp.Announce(hashLockTransactionSigned);
//Announce transaction(metadata)
await Fixture.SiriusClient.TransactionHttp.AnnounceAggregateBonded(aggregateTransactionsigned);

Mosaic Metadata

// Create account with balance
var account = await Fixture.GenerateAccountWithCurrency(10000);
// Generates random mosaic nonce
var nonce = MosaicNonce.CreateRandom();

var mosaicId = MosaicId.CreateFromNonce(nonce, account.PublicAccount.PublicKey);

// Defines the mosaic
var mosaicDefinitionTransaction = MosaicDefinitionTransaction.Create(
    nonce,
    mosaicId,
    Deadline.Create(),
    MosaicProperties.Create(
       supplyMutable: true,
       transferable: true,
       divisibility: 6,
       duration: 0
    ),
    Fixture.NetworkType);

// Defines the mosaic supply
var mosaicSupplyChangeTransaction = MosaicSupplyChangeTransaction.Create(
    Deadline.Create(),
    mosaicDefinitionTransaction.MosaicId,
    MosaicSupplyType.INCREASE,
    1000,
    Fixture.NetworkType);

var aggregateTransactiontx = AggregateTransaction.CreateComplete(
    Deadline.Create(),
    new List<Transaction>
        {
          mosaicDefinitionTransaction.ToAggregate(account.PublicAccount),
          mosaicSupplyChangeTransaction.ToAggregate(account.PublicAccount)
        },
     Fixture.NetworkType);

//Sign aggregate transaction (mosaic)
var aggregateTransactionsigned = account.Sign(aggregateTransactiontx, Fixture.GenerationHash);

//Create mosaic metadata transaction
var metadatatrx = MosaicMetadataTransaction.Create(
    Deadline.Create(),
    account.PublicAccount,
    mosaicDefinitionTransaction.MosaicId,
    // metadata key
    "test_mosaic",
    // metadata value
    "1",
    "",
    Fixture.NetworkType);

//Create aggregate bonded transaction
var aggregateTransaction = AggregateTransaction.CreateBonded(
    Deadline.Create(),
     new List<Transaction>
         {
            metadatatrx.ToAggregate(account.PublicAccount),
         },
         Fixture.NetworkType);

//Sign aggregate transaction (metadata)
var aggregateTrxsigned = account.Sign(aggregateTransaction, Fixture.GenerationHash);
//Create hash lock transaction
var hashLockTransaction = HashLockTransaction.Create(
    Deadline.Create(),
    NetworkCurrencyMosaic.CreateRelative(10),
    (ulong)3650,
    aggregateTrxsigned,
    Fixture.NetworkType);

var hashLockTransactionSigned = account.Sign(hashLockTransaction, Fixture.GenerationHash);

// Announce transaction
await Fixture.SiriusClient.TransactionHttp.Announce(hashLockTransactionSigned);

// Announce aggregate transaction
await Fixture.SiriusClient.TransactionHttp.AnnounceAggregateBonded(aggregateTransactionsigned);

Namespace Metadata

// Create account with balance
var account = await Fixture.GenerateAccountWithCurrency(10000);

var rootNs = "namespace_metadata_test";

// Creates a new namespace
var rootId = new NamespaceId(rootNs);

// create a namespace
var registerRootTransaction = RegisterNamespaceTransaction.CreateRootNamespace(
    Deadline.Create(),
    rootNs,
    3650,
    Fixture.NetworkType);

var aggregateTransactiontx = AggregateTransaction.CreateComplete(
    Deadline.Create(),
    new List<Transaction>
        {
           registerRootTransaction.ToAggregate(account.PublicAccount),
        },
    Fixture.NetworkType);

//Sign aggregate transaction (create namespace)
var aggregateTransactionsigned = account.Sign(aggregateTransactiontx, Fixture.GenerationHash);
await Fixture.SiriusClient.TransactionHttp.Announce(aggregateTransactionsigned);

//Create namespace metadata transaction
var metadatatrx = NamespaceMetadataTransaction.Create(
    Deadline.Create(),
    account.PublicAccount,
    rootId,
    "test_Namespace",
    "1",
    "",
    Fixture.NetworkType);

var aggregateTransaction = AggregateTransaction.CreateBonded(
    Deadline.Create(),
    new List<Transaction>
        {
             metadatatrx.ToAggregate(account.PublicAccount),
        },
    Fixture.NetworkType);

//Sign aggreate transaction(metadata)
var aggregateTrxsigned = account.Sign(aggregateTransaction, Fixture.GenerationHash);

//Initial hash lock transaction
var hashLockTransaction = HashLockTransaction.Create(
    Deadline.Create(),
    NetworkCurrencyMosaic.CreateRelative(10),
    (ulong)3650,
    aggregateTrxsigned,
    Fixture.NetworkType);

//Sign hash lock transaction
var hashLockTransactionSigned = account.Sign(hashLockTransaction, Fixture.GenerationHash);

//announce hash lock transaction
await Fixture.SiriusClient.TransactionHttp.Announce(hashLockTransactionSigned);

//announce aggreagte bonded transaction
await Fixture.SiriusClient.TransactionHttp.AnnounceAggregateBonded(aggregateTrxsigned);

Mosaic

Mosaics are part of what makes the Smart Asset System unique and flexible. They are fixed assets on the ProximaX Sirius blockchain that can represent a set of multiple identical things that do not change.

A mosaic could be a token, but it could also be a collection of more specialized assets such as reward points, shares of stock, signatures, status flags, votes or even other currencies.

Getting mosaic information

var client = new SiriusClient("https://bctestnet1.brimstone.xpxsirius.io");

var mosaicHex = "85BBEA6CC462B244";

// Creates a new mosaic
var mosaicId = new MosaicId(mosaicHex);

// Get the mosaic info
var mosaicInfo = await client.MosaicHttp.GetMosaic(mosaicId);

Console.WriteLine($"Mosaic info {mosaicInfo}");

Get mosaic names

var client = new SiriusClient("https://bctestnet1.brimstone.xpxsirius.io");

var list = new List<string>
{ "0DC67FBE1CAD29E3" };

// Get the mosaic info
var mosaicNames = await client.MosaicHttp.GetMosaicNames(list);

foreach(var name in mosaicNames) {
   Console.WriteLine($"Mosaic name {name}");
}

Creating a mosaic

We are going to create 1.000.000 mosaic units.

var client = new SiriusClient("https://bctestnet1.brimstone.xpxsirius.io");

var networkType = client.NetworkHttp.GetNetworkType().Wait();

var account = Account.CreateFromPrivateKey("85CFAB0E6079DAA58D7FF0990ACA64E571EC58527A16DB9391C87C436261190C", networkType);

// Generates random mosaic nonce
var nonce = MosaicNonce.CreateRandom();

var mosaicId =  MosaicId.CreateFromNonce(nonce, account.PublicAccount.PublicKey);
// Defines the mosaic
var mosaicDefinitionTransaction = MosaicDefinitionTransaction.Create(
    nonce,
    mosaicId,
    Deadline.Create(),
    MosaicProperties.Create(
        supplyMutable: true,
        transferable: true,
        divisibility: 0,
        duration: 1000
    ),
    networkType);

// Defines the mosaic supply
var mosaicSupplyChangeTransaction = MosaicSupplyChangeTransaction.Create(
    Deadline.Create(),
    mosaicDefinitionTransaction.MosaicId,
    MosaicSupplyType.INCREASE,
    1000000,
    networkType);

var aggregateTransaction = AggregateTransaction.CreateComplete(
    Deadline.Create(),
    new List<Transaction>
    {
        mosaicDefinitionTransaction.ToAggregate(account.PublicAccount),
        mosaicSupplyChangeTransaction.ToAggregate(account.PublicAccount)
    },
    networkType);

// Get the generation hash 
var generationHash = await siriusClient.BlockHttp.GetGenerationHash();

var signedTransaction = account.Sign(aggregateTransaction,generationHash);

// Announces the transaction to create mosaic
await client.TransactionHttp.Announce(signedTransaction);

// Get the mosaic info
var mosaicInfo = await client.MosaicHttp.GetMosaic(mosaicId);

Console.WriteLine($"Mosaic info {mosaicInfo}");

Modifying mosaic supply

Let's increase the initial supply of the created mosaic to 2.000.000

var client = new SiriusClient("https://bctestnet1.brimstone.xpxsirius.io");

var networkType = client.NetworkHttp.GetNetworkType().Wait();

var account = Account.CreateFromPrivateKey("85CFAB0E6079DAA58D7FF0990ACA64E571EC58527A16DB9391C87C436261190C", networkType);

// Defines the mosaic supply
var mosaicSupplyChangeTransaction = MosaicSupplyChangeTransaction.Create(
    Deadline.Create(),
    mosaicDefinitionTransaction.MosaicId,
    MosaicSupplyType.INCREASE,
    2000000,
    networkType);

// Get the generation hash 
var generationHash = await siriusClient.BlockHttp.GetGenerationHash();

var signedTransaction = account.Sign(aggregateTransaction,generationHash);

// Announces the transaction to create mosaic
await client.TransactionHttp.Announce(signedTransaction);

// Get the mosaic info
var mosaicInfo = await client.MosaicHttp.GetMosaic(mosaicId);

Console.WriteLine($"Mosaic info {mosaicInfo}");

Decrease your mosaic supply by changing MosaicSupplyType.INCREASE to MosaicSupplyType.DECREASE

Linking a namespace to a mosaic

An account can link a registered name (namespace or subnamespace) with a mosaic

var client = new SiriusClient("https://bctestnet1.brimstone.xpxsirius.io");

var networkType = client.NetworkHttp.GetNetworkType().Wait();

// The registered account
var account = Account.CreateFromPrivateKey("85CFAB0E6079DAA58D7FF0990ACA64E571EC58527A16DB9391C87C436261190C", networkType);

// The registered namespace
var namespaceId = new NamespaceId("proximax.xpx");

// The registered mosaic
var mosaicId = new MosaicId(8414911888806253036);

// Creates the alias transaction
var mosaicAliasTransaction = AliasTransaction.CreateForMosaic(
    mosaicId,
    namespaceId,
    AliasActionType.LINK,
    Deadline.Create(),
    networkType
);

// Get the generation hash 
var generationHash = await siriusClient.BlockHttp.GetGenerationHash();

// Signs the transaction
var aliasSignedTransaction = account.Sign(mosaicAliasTransaction, generationHash);

// Announces to the network
await client.TransactionHttp.Announce(aliasSignedTransaction);

Once the namespace linked to the mosaic transaction confirmed. You may send mosaic to another account using the namespace info instead of defining the complete mosaicId.

// Generates new account
var newAccount = Account.GenerateNewAccount(networkType);

// Creates transfer transaction, send 10 mosaic units using
// the alias namespace proximax.xpx instead of mosaic id 8414911888806253036
// to the new account address
var transferTransaction = TransferTransaction.Create(
    Deadline.Create(),
    newAccount.Address,
    new List<Mosaic>()
    {
        new Mosaic(new NamespaceId("proximax.xpx"), 10)
    },
    PlainMessage.Create("Send some mosaic to new address"),
    networkType
);

// Get the generation hash 
var generationHash = await siriusClient.BlockHttp.GetGenerationHash();

// Signs the transaction using the registered account
// that owned the registered namespace and mosaic
var signedTransaction = account.Sign(transferTransaction,generationHash);

// Announces to the network
await client.TransactionHttp.Announce(signedTransaction);

// Verifies whether the new account had 10 mosaic units
var newAccountInfo = await client.AccountHttp.GetAccountInfo(newAccount.Address);

Console.WriteLine($"{nameof(newAccountInfo)} : {newAccountInfo}");

Modify Mosaic Levy

var account = Fixture.SeedAccount;
var recipient = Account.GenerateNewAccount(Fixture.NetworkType);

var nonce = MosaicNonce.CreateRandom();
var mosaicId = MosaicId.CreateFromNonce(nonce, account.PublicAccount.PublicKey);
var mosaicDefinitionTransaction = MosaicDefinitionTransaction.Create(
    nonce,
    mosaicId,
    Deadline.Create(),
    MosaicProperties.Create(
        supplyMutable: true,
        transferable: true,
        divisibility: 0,
        duration: 0),
    Fixture.NetworkType);

var mosaicSupplyChangeTransaction = MosaicSupplyChangeTransaction.Create(
    Deadline.Create(),
    mosaicDefinitionTransaction.MosaicId,
    MosaicSupplyType.INCREASE,
    1000000,
    Fixture.NetworkType);

var aggregateTransaction = AggregateTransaction.CreateComplete(
    Deadline.Create(),
    new List<Transaction>
    {
        mosaicDefinitionTransaction.ToAggregate(account.PublicAccount),
        mosaicSupplyChangeTransaction.ToAggregate(account.PublicAccount)
    },
    Fixture.NetworkType);

var signedTransaction = account.Sign(aggregateTransaction, Fixture.GenerationHash);

Fixture.WatchForFailure(signedTransaction);

await Fixture.SiriusClient.TransactionHttp.Announce(signedTransaction);

var mosaic_Id = new MosaicId(mosaicId.HexId);

var mosaicLevy = ModifyMosaicLevyTransaction.Create(
    Deadline.Create(),
    mosaic_Id,
    MosaicLevy.CreateWithAbsoluteFee(
    Recipient.From(recipient.Address),
    mosaic_Id, 10),
    Fixture.NetworkType);

var signedTransaction_mosaicLevy = account.Sign(mosaicLevy, Fixture.GenerationHash);

Namespace

Namespaces allow you to create an on-chain unique place for your business and your assets on the ProximaX Sirius blockchain.

Getting a namespace information

var client = new SiriusClient("https://bctestnet1.brimstone.xpxsirius.io");

var namespaceName = "proximax";

// Creates a new namespace
var namespaceId = new NamespaceId(namespaceName);

// Check namespace info
namespaceInfo = await client.NamespaceHttp.GetNamespace(namespaceId);

Console.WriteLine($"Namespace info {namespaceInfo}");

Registering a namespace

In order to register a namespace, you must have an account with the network currency.

Create an account and obtain the network currency here

var client = new SiriusClient("https://bctestnet1.brimstone.xpxsirius.io");

var namespaceName = "proximax";

// Creates a new namespace
var namespaceId = new NamespaceId(namespaceName);

// Verifies the namespace is available
var namespaceInfo = await client.NamespaceHttp.GetNamespace(namespaceId);

if (namespaceInfo != null)
{
    throw new ArgumentNullException($"Namespace already exits");
}

var registerNamespaceTransaction = RegisterNamespaceTransaction.CreateRootNamespace(
        Deadline.Create(),
        namespaceName,
        (ulong)100,
        NetworkType.MIJIN_TEST);

// Account with at least 100 XPX
var account = Account.CreateFromPrivateKey("85CFAB0E6079DAA58D7FF0990ACA64E571EC58527A16DB9391C87C436261190C"
    , NetworkType.MIJIN_TEST);

// Get the generation hash 
var generationHash = await siriusClient.BlockHttp.GetGenerationHash();

var signedTransaction = account.Sign(registerNamespaceTransaction,generationHash );

// Announce transaction
await client.TransactionHttp.Announce(signedTransaction);

// Check namespace info
namespaceInfo = await client.NamespaceHttp.GetNamespace(namespaceId);

Console.WriteLine($"Namespace info {namespaceInfo}");

Creating a subnamespace

Once you have a registered root namespace, you can create up to 3 levels of subnamespaces.

Subnamespaces do not have a renting duration. They have the same one as their parent namespace.

var namespaceName = "xpx";
var namespaceId  = new NamespaceId("xpx");
var parentNamespaceId = new NamespaceId("proximax");

// create a subnamespace
var registerSubNamespaceTransaction = RegisterNamespaceTransaction.CreateSubNamespace(
    Deadline.Create(),
    namespaceName,
    parentNamespaceId,
    NetworkType.MIJIN_TEST
);

// Announce transaction
await client.TransactionHttp.Announce(signedTransaction);

// Check namespace info
namespaceInfo = await client.NamespaceHttp.GetNamespace(namespaceId);

Console.WriteLine($"Namespace info {namespaceInfo}");

Transaction

A transaction generally represents a unit of work within a database system. In the case of blockchain, that is when an action signed by an account changes its state.

Transactions accepted by the network are stored permanently on blocks.

Sending a transfer transaction

Transfer transactions are used to send mosaics between two accounts. They can hold a messages of length 1023 characters.

// Creates instance of SiriusClient
var client = new SiriusClient("https://bctestnet1.xpxsirius.io");

// Gets the network type 
var networkType = client.NetworkHttp.GetNetworkType().Wait();

// The registered account with some XPX currency
var account = Account.CreateFromPrivateKey("85CFAB0E6079DAA58D7FF0990ACA64E571EC58527A16DB9391C87C436261190C", networkType);

// Generates new account
var newAccount = Account.GenerateNewAccount(networkType);

// mosaic to be transferred
var mosaicToTransfer = NetworkCurrencyMosaic.CreateRelative(10);

// Creates transfer transaction, send 10 mosaic units using
// to the new account address
var transferTransaction = TransferTransaction.Create(
    Deadline.Create(),
    newAccount.Address,
    new List<Mosaic>()
    {
       mosaicToTransfer
    },
    PlainMessage.Create("Send some mosaic to new address"),
    networkType
);

// Get the generation hash 
var generationHash = await siriusClient.BlockHttp.GetGenerationHash();

// Signs the transaction using the registered account
var signedTransaction = account.Sign(transferTransaction,generationHash);

// Announces to the network
await client.TransactionHttp.Announce(signedTransaction);

// Verifies whether the new account had 10 mosaic units
var newAccountInfo = await client.AccountHttp.GetAccountInfo(newAccount.Address);

Console.WriteLine($"{nameof(newAccountInfo)} : {newAccountInfo}");

Message Encryption and Decryption and Plain Message

Receiver private key and sender public key required for the encryption and decryption of the message.

//Plain Message
var message = PlainMessage.Create("c#__ SDK plain message test");

//Encrypt Message
var message = SecureMessage.Create("Test secure message", receiverPrivateKey, senderPublicKey);

//Decrypt Message to get the payload
var payload = securedMessage.DecryptPayload(receiverPrivateKey, senderPublicKey);

Monitoring a transaction

To make sure a transaction gets included in the blockchain after being announced, you must track the transaction using SiriusWebSocketClient listeners.

The listeners make receiving notifications possible when a transaction or event occurs in the blockchain.

// Creates instance of SiriusClient
var client = new SiriusClient("https://bctestnet1.xpxsirius.io");

// Gets the network type 
var networkType = client.NetworkHttp.GetNetworkType().Wait();

// The registered account with some XPX currency
var account = Account.CreateFromPrivateKey("85CFAB0E6079DAA58D7FF0990ACA64E571EC58527A16DB9391C87C436261190C", networkType);

// Generates new account
var newAccount = Account.GenerateNewAccount(networkType);

// mosaic to be transferred
var mosaicToTransfer = NetworkCurrencyMosaic.CreateRelative(10);

// Creates transfer transaction, send 10 mosaic units using
// to the new account address
var transferTransaction = TransferTransaction.Create(
    Deadline.Create(),
    newAccount.Address,
    new List<Mosaic>()
    {
       mosaicToTransfer
    },
    PlainMessage.Create("Send some mosaic to new address"),
    networkType
);

// Get the generation hash 
var generationHash = await siriusClient.BlockHttp.GetGenerationHash();

// Signs the transaction using the registered account
var signedTransaction = account.Sign(transferTransaction,generationHash);

// Creates instance of SiriusWebSocketClient to monitor transactions
// If you need to enable the secure protocol use
// new SiriusWebSocketClient("bctestnet1.xpxsirius.io", 3000,useSSL:true);
var ws = new SiriusWebSocketClient("bctestnet1.xpxsirius.io", 3000);

// Opens the listener
await ws.Listener.Open();

// Monitors if the websocker listener is alive by subscribing to NewBlock channel.
// Blocks are generated every 15 seconds in average, so a timeout can be raised if
// there is no response after 30 seconds.
ws.Listener.NewBlock()
  .Timeout(TimeSpan.FromSeconds(30))  
  .Subscribe(
    block => {
      Console.WriteLine($"New block is created {block.Height}");
    },
    err => {
      Console.WriteLine($"Unexpected error {err}");
      ws.Listener.Close();
    }
  );

// Monitors if there is any validation error with the issued transaction
var signerAddress = Address.CreateFromPublicKey(signedTransaction.Signer,networkType);

ws.Listener.TransactionStatus(signerAddress)
  .Timeout(TimeSpan.FromSeconds(30))  
  .Subscribe(
    // transaction info
    tx =>
    {
        Console.WriteLine($"Transaction id {tx.Hash} - status {tx.Status}");
    },
    // handle if any error occured
    txErr =>
    {
        Console.WriteLine($"Transaction error - {txErr}");
        ws.Listener.Close();
    });
  )

// Monitors if the transaction arrives the network but not yet include in the block
var unconfirmedTx = await ws.Listener.UnconfirmedTransactionsAdded(newAccount.Address)
                                     .Take(1)
                                     .Timeout(TimeSpan.FromSeconds(30);

// Monitors if the transaction get included in the block
var confirmedTx = await ws.Listener.ConfirmedTransactionsGiven(newAccount.Address)
                                   .Take(1)
                                   .Timeout(TimeSpan.FromSeconds(30);

// Announces to the network
await client.TransactionHttp.Announce(signedTransaction);

// Gets the results
var unconfirmedResult = await confirmedTx;

Console.WriteLine($"Request transaction {unconfirmedResult.TransactionInfo.Hash} reached network");

var confirmedResult = await confirmedTx;

Console.WriteLine($"Request confirmed with transaction {confirmedResult.TransactionInfo.Hash}");

Copyright (c) 2022 ProximaX Limited