Getting the asset identifier behind a namespace with receipts

Get the resolution for a given alias and transaction using receipts.

Background

Catapult accounts can link registered namespaces to other accounts or mosaics by announcing an AliasTransaction. This feature allows you to replace long and complex identifiers with short and familiar names for your accounts and mosaics.

Imagine a ticket vendor sending tickets to their customers on Catapult’s public chain. The company needs to send 1 0dc67fbe1cad29e3 to SCVG35-ZSPMYP-L2POZQ-JGSVEG-RYOJ3V-BNIU3U-N2E6. With aliases, the ticket vendor can define the same transaction as sending 1 ticketsales.event1.ticket to @alice instead.

../../_images/namespace-tickets.png

Recognizable mosaics and addresses

To ensure the transactions are being sent to the correct place with the correct mosaic, you can directly query the network about the current mosaic identifier behind a namespace by running the following snippet:

// replace with namespace name
const namespaceId = new NamespaceId('symbol.xym');

// replace with node endpoint
const nodeUrl = 'http://api-xym-harvest-3-01.us-west-2.nemtech.network:3000';
const repositoryFactory = new RepositoryFactoryHttp(nodeUrl);
const namespaceHttp = repositoryFactory.createNamespaceRepository();

namespaceHttp
    .getLinkedMosaicId(namespaceId)
    .subscribe((mosaicId) => console.log(mosaicId!.toHex()),
            (err) => console.log(err));
// replace with namespace name
const namespaceId = new nem2_sdk_1.NamespaceId('symbol.xym');
// replace with node endpoint
const nodeUrl = 'http://api-xym-harvest-3-01.us-west-2.nemtech.network:3000';
const repositoryFactory = new nem2_sdk_1.RepositoryFactoryHttp(nodeUrl);
const namespaceHttp = repositoryFactory.createNamespaceRepository();
namespaceHttp
    .getLinkedMosaicId(namespaceId)
    .subscribe((mosaicId) => console.log(mosaicId.toHex()), (err) => console.log(err));
        // replace with node endpoint
        try (final RepositoryFactory repositoryFactory = new RepositoryFactoryVertxImpl(
                "http://api-xym-harvest-3-01.us-west-2.nemtech.network:3000")) {
            final NamespaceRepository namespaceRepository = repositoryFactory.createNamespaceRepository();
            final NamespaceId namespaceId = NamespaceId.createFromName("symbol.xym");
            final MosaicId mosaicId = namespaceRepository.getLinkedMosaicId(namespaceId)
                    .toFuture().get();
            System.out.print(mosaicId.getIdAsHex());
        }

However, the same method cannot be used to verify transactions of the past. This is due to the facts that:

  • Transactions using aliased mosaics or accounts are stored on the blockchain using the namespace identifier, not the real address or mosaic id behind it.
  • Links are editable. The namespace creator can link its namespace to another asset.
  • Namespaces expire. The namespace link could be deleted.

At this point, you might be wondering: how then can we get the accurate relation between a namespace and its real identifier for a past transaction? The answer lies with receipts. For each block, Catapult nodes store receipts that contain every invisible state change that cannot be retrieved directly from the transaction or block header.

Prerequisites

Getting into some code

In this example, we are going to announce a TransferTransaction using symbol.xym instead of the native currency mosaic id. Once the network confirms the transaction, we will get the block height where the transaction has been recorded. With this information, we will then get the namespace-mosaic relation by looking into the block receipts’.

  1. Define the mosaic you want to send. Use a linked namespace identifier (e.g. symbol.xym) instead of the mosaic identifier.
const aliasedMosaic = new Mosaic(
    new NamespaceId('symbol.xym'),
    UInt64.fromUint(1000000),
);
const aliasedMosaic = new nem2_sdk_1.Mosaic(new nem2_sdk_1.NamespaceId('symbol.xym'), nem2_sdk_1.UInt64.fromUint(1000000));
  1. Attach the mosaic to a TransferTransaction.
// replace with network type
const networkType = NetworkType.TEST_NET;
const transferTransaction = TransferTransaction.create(
    Deadline.create(),
    Address.createFromRawAddress('TBULEA-UG2CZQ-ISUR44-2HWA6U-AKGWIX-HDABJV-IPS4'),
    [aliasedMosaic],
    PlainMessage.create('Test aliased mosaic'),
    networkType,
    UInt64.fromUint(2000000));

// replace with sender private key
const privateKey = '1111111111111111111111111111111111111111111111111111111111111111';
const account = Account.createFromPrivateKey(privateKey, networkType);
// replace with meta.generationHash (nodeUrl + '/block/1')
const networkGenerationHash = '45870419226A7E51D61D94AD728231EDC6C9B3086EF9255A8421A4F26870456A';
const signedTransaction = account.sign(transferTransaction, networkGenerationHash);
console.log(signedTransaction.hash);
// replace with network type
const networkType = nem2_sdk_1.NetworkType.TEST_NET;
const transferTransaction = nem2_sdk_1.TransferTransaction.create(nem2_sdk_1.Deadline.create(), nem2_sdk_1.Address.createFromRawAddress('TBULEA-UG2CZQ-ISUR44-2HWA6U-AKGWIX-HDABJV-IPS4'), [aliasedMosaic], nem2_sdk_1.PlainMessage.create('Test aliased mosaic'), networkType, nem2_sdk_1.UInt64.fromUint(2000000));
// replace with sender private key
const privateKey = '1111111111111111111111111111111111111111111111111111111111111111';
const account = nem2_sdk_1.Account.createFromPrivateKey(privateKey, networkType);
// replace with meta.generationHash (nodeUrl + '/block/1')
const networkGenerationHash = '45870419226A7E51D61D94AD728231EDC6C9B3086EF9255A8421A4F26870456A';
const signedTransaction = account.sign(transferTransaction, networkGenerationHash);
console.log(signedTransaction.hash);

3. Announce the TransferTransaction. Once the transaction is confirmed, retrieve the receipts attached to the block and find for the namespace resolution.

// replace with node endpoint
const nodeUrl = 'http://api-xym-harvest-3-01.us-west-2.nemtech.network:3000';
const repositoryFactory = new RepositoryFactoryHttp(nodeUrl);
const receiptHttp = repositoryFactory.createReceiptRepository();
const transactionHttp = repositoryFactory.createTransactionRepository();
const listener = repositoryFactory.createListener();
const transactionService = new TransactionService(transactionHttp, receiptHttp);

listener.open().then(() => {
    transactionService
        .announce(signedTransaction, listener)
        .pipe(
            mergeMap((transaction) =>
                transactionService.resolveAliases([transaction.transactionInfo!.hash!])),
            map((transactions) => transactions[0] as TransferTransaction))
        .subscribe((transaction) => {
            console.log('Resolved MosaicId: ', transaction.mosaics[0].id.toHex());
            listener.close();
        }, (err) => console.log(err));
});
// replace with node endpoint
const nodeUrl = 'http://api-xym-harvest-3-01.us-west-2.nemtech.network:3000';
const repositoryFactory = new nem2_sdk_1.RepositoryFactoryHttp(nodeUrl);
const receiptHttp = repositoryFactory.createReceiptRepository();
const transactionHttp = repositoryFactory.createTransactionRepository();
const listener = repositoryFactory.createListener();
const transactionService = new nem2_sdk_1.TransactionService(transactionHttp, receiptHttp);
listener.open().then(() => {
    transactionService
        .announce(signedTransaction, listener)
        .pipe(operators_1.mergeMap((transaction) => transactionService.resolveAliases([transaction.transactionInfo.hash])), operators_1.map((transactions) => transactions[0]))
        .subscribe((transaction) => {
        console.log('Resolved MosaicId: ', transaction.mosaics[0].id.toHex());
        listener.close();
    }, (err) => console.log(err));
});

What is next?

Receipts do not only store resolutions for aliases, but also every invisible state change that is not directly retrievable from transactions or the block header. You can check under the receipts documentation the complete list of changes logged.