LoginSignup
11
5

More than 1 year has passed since last update.

フルオンチェーンNFTもどきをSymbolで作成してみる。

Last updated at Posted at 2021-12-30

SymbolのテストネットでフルオンチェーンNFTを作る方法を解説します。

この記事で解説しないこと

  • トークンの作成方法

    • ウォレットで作成してください
  • NFTデータのエンコード方法

    • Base64でエンコードしてください

テストネット

アカウント準備

alice = sym.Account.generateNewAccount(sym.NetworkType.TEST_NET);

"https://testnet.symbol.tools/?recipient=" + alice.address.plain() +"&amount=10"

データ分割

Base64でエンコードされたNFTデータを分割してmessagesという配列に格納します。

nftdata = "UGGUGUUAUUAAUGGUUU";
let messages = [];
for (let i = 0; i < nftdata.length / 3; i++) {
    messages.push(nftdata.substr(i * 3, 3));
}
console.log(messages);

今回はサンプルとして"UGGUGUUAUUAAUGGUUU"という文字列を3文字ずつに分割しています。
直接RawDataを扱いたい場合はこちらの記事を参考にしてください。

トランザクション作成

配列の数だけトランザクションを作成して、アグリゲートトランザクションに集約させます。

createTx = function(publicAccount,message){
  return sym.TransferTransaction.create(
    sym.Deadline.create(epochAdjustment),
    publicAccount.address,
    [],
    sym.PlainMessage.create(message),
    networkType
  );
}

aggregateArray = Array();
for(message of messages){
   tx = createTx(alice,message);
  aggregateArray.push(tx.toAggregate(alice.publicAccount));

}
console.log(aggregateArray);

aggregateTx = sym.AggregateTransaction.createComplete(
  sym.Deadline.create(epochAdjustment),
  aggregateArray,
  networkType,[]
).setMaxFeeForAggregate(100,0);

今回はaliceがalice自身にNFTデータを送信するトランザクションにしています。
一般的にNFTはメタデータにNFT本体のハッシュ値を記録しますが、今回は改ざんできないトランザクションメッセージ内にNFTデータを記録します。

署名&アナウンス

トランザクションに署名して、ネットワークに通知します。

signedTx = aggregateTx.signTransactionWithCosignatories(
alice,[],generationHash);

txRepo.announce(signedTx);

"https://testnet.symbol.fyi/transactions/" + signedTx.hash;


結果

11
5
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
11
5