8
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

新しいSymbol SDKへのフィードバック募集

Last updated at Posted at 2024-05-09

本記事はSymbol SDKのv3改善のためのフィードバックを募集する記事です、Discordに書くと長くなるので記事にしました。

お時間を取らせてしまいますが、一度下記に従ってトランザクションのアナウンスまで使ってみていただければ幸いです。そのうえで、Discordへコメントをください。特に意見が無い場合でも使ってみた感触(悪くない!良い!使いやすい!使いにくい!)やイイネ等の絵文字だけでもありがたいです。

本アップデートは、既存のコードには変更を加えていないため、これまでの使用方法でもなんら問題ありません。

インストール

まずレジストリを設定します

npm config set registry https://registry.symbolsyndicate.us/repository/npm-group

続いてsdkのインストール

npm install symbol-sdk@3.2.1

使い方

まず、インポートやアナウンス用の関数、アカウントの設定などをしておきます。

本アップデートよりV2のようにSymbolAccount(V2ではAccount)、SymbolPublicAccount(PublicAccount)クラスが用意されました。

import {
  SymbolFacade,
  descriptors,
  Address,
  models,
  generateMosaicId,
  SymbolTransactionFactory,
  SymbolAccount,
} from 'symbol-sdk/symbol'
import { PrivateKey, PublicKey } from 'symbol-sdk'

const signAndAnnounce = (account: SymbolAccount, transaction: models.Transaction) => {
  const signature = account.signTransaction(transaction)
  let payload = facade.transactionFactory.static.attachSignature(transaction, signature)
  // or let payload = SymbolTransactionFactory.attachSignature(transaction, signature)
  console.log('hash: ', facade.hashTransaction(transaction).toString())

  const options = {
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json',
    },
    body: payload,
  }
  fetch('https://NODE_URL:3001/transactions', options).then(async (res) => {
    console.log(await res.json())
  })
}

const facade = new SymbolFacade('testnet')
const aliceAccount = facade.createAccount(
  new PrivateKey('PRIVATE_KEY')
)
const bobPublicAccount = facade.createPublicAccount(
  new PublicKey('PUBLIC_KEY')
)

TransferTransaction

const transferTransactionDescripter = new descriptors.TransferTransactionV1Descriptor(
  bobPublicAccount.address, // or new Address('TA5LGYEWS6L2WYBQ75J2DGK7IOZHYVWFWRLOFWI'),
  [
    new descriptors.UnresolvedMosaicDescriptor(
      new models.UnresolvedMosaicId(0x72c0212e67a08bcen),
      new models.Amount(1n)
    ),
  ],
  `test`
)

const transferTransaction = facade.createTransactionFromTypedDescriptor(
  transferTransactionDescripter,
  aliceAccount.publicKey,
  100,
  3600
)
signAndAnnounce(aliceAccount, transferTransaction)

一番シンプルなトランザクションです。
エディタ補完が効くのでコピペではなく手打ちで試していただきたいです。
元々v3では型が分からないために使いにくいという意見を見ました。それが改善されています。

TransferTransactionにおけるメッセージ領域にはNEMからの慣習で平文メッセージであれば頭に1byteの0を付けるようにしてエクスプローラーやデスクトップウォレットで判別できるようにしていました。しかし、ジャガーさん個人的にはできれば廃止したいようです。もし、TransferTransactionV1Descriptorの引数で0を付けたい場合は

`\0test`

のようにするとOKです。

AggregateTransaction 署名者は一人

const mosaicId = generateMosaicId(aliceAccount.address, 1)
const mosaicDefinitionDescriptor = new descriptors.MosaicDefinitionTransactionV1Descriptor(
  new models.MosaicId(mosaicId),
  new models.BlockDuration(0n),
  new models.MosaicNonce(1),
  new models.MosaicFlags(models.MosaicFlags.TRANSFERABLE.value + models.MosaicFlags.SUPPLY_MUTABLE.value),
  0
)
const mosaicSupplyChangeDescriptor = new descriptors.MosaicSupplyChangeTransactionV1Descriptor(
  new models.UnresolvedMosaicId(mosaicId),
  new models.Amount(1000n),
  models.MosaicSupplyChangeAction.INCREASE
)

const embeddedTransactions = [
  facade.createEmbeddedTransactionFromTypedDescriptor(mosaicDefinitionDescriptor, aliceAccount.publicKey),
  facade.createEmbeddedTransactionFromTypedDescriptor(mosaicSupplyChangeDescriptor, aliceAccount.publicKey),
]

const aggregateDescriptor = new descriptors.AggregateCompleteTransactionV2Descriptor(
  facade.static.hashEmbeddedTransactions(embeddedTransactions),
  embeddedTransactions
)

const mosaicTransaction = facade.createTransactionFromTypedDescriptor(aggregateDescriptor, aliceAccount.publicKey, 100, 3600)
signAndAnnounce(aliceAccount, mosaicTransaction)

サンプルでは署名者が一人でアグリゲートを使用するケースが多い、モザイク定義にしました。

さいごに

以上ですが、何か思いつくことなど気軽に書いていただければありがたいです。v2は非推奨になるため今後はこのv3へのフィードバックが重要になります。

8
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?