6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Google Apps Script で Blockchain 入門

Posted at

どうも。営業職だったのに色々触れているうちにがっつりエンジニア方面を歩み出した faunsu19000 です。今回は比較的入門し易い Google Apps Script を用いて Blockchain の情報を読み出したり書き込んだりする方法を解説しようと思います。

今回やること

  • Google Apps Script で指定したブロックチェーンアカウントの残高情報を取得する
  • Google Apps Script でブロックチェーンにデータを書き込む

ソースコード

以下の URL に格納しておきました。少し長めのファイルもある為、この Qiita には全てのソースコードは張りません。事前にダウンロードしてからお読み下さい。

用意したコード

今回は Blockchain のノードとやりとりをする処理を記述した tool.js と 処理したい内容を記述した main.js、最後に QR コードを作成する為のライブラリである _module.js の3つを用意します。

main.js

function main() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
  const address = sheet.getRange(1, 2).getDisplayValue();

  // トランザクションの作成
  const transactionQr = getTransactionUri(address, 104, 10, "test");

  const qr = qrcode(0, "L");
  qr.addData(JSON.stringify(transactionQr));
  qr.make();

  const base64str = qr.createDataURL(5).replace("data:image/gif;base64,", "");
  const imageBlob = Utilities.newBlob(Utilities.base64Decode(base64str), "image/gif", "qr");

  sheet.insertImage(imageBlob, 2, 3);
}

function getAmount(args) {
  const node = "https://symbolnode.blockchain-authn.app:3001";
  return getAccountAmount(args, node, 104);
}

tool.js

/**
 * @param {string} recipientPlainAddress
 * @param {104|152} networkType
 * @param {number} amount
 * @param {string} message
 */
function getTransactionUri(recipientPlainAddress, networkType, amount, message) {
  // get transaction qr json
  const res = UrlFetchApp.fetch("http://symbolnode.blockchain-authn.app:8080/invoke/01H253JJT8NG9000GZJ000000B", {
    method: "post",
    contentType: "application/json",
    payload: JSON.stringify({
      recipientAddress: recipientPlainAddress,
      networkType: networkType,
      amount: amount,
      message: message,
    }),
  });

  return JSON.parse(res.getContentText());
}

/**
 *
 * @param {string} plainAddress
 * @param {string} nodeUrl
 * @param {104|152} networkType
 * @returns
 */
function getAccountAmount(plainAddress, nodeUrl, networkType) {
  const res = UrlFetchApp.fetch(`${nodeUrl}/accounts/${plainAddress}`, {
    method: "get",
    contentType: "application/json",
  });

  const currencyId = networkType === 104 ? "6BED913FA20223F8" : "72C0212E67A08BCE";
  const mosaic = JSON.parse(res.getContentText()).account.mosaics.find((e) => e.id === currencyId);

  if (!mosaic) {
    throw new Error("Mosaic is not find");
  }

  return Number(mosaic.amount) / 1000000;
}

_module.js

// コード量が長い為、ここには張りません。 Github からダウンロードした zip ファイルを参照下さい

Spreadsheet 側の用意

今回ブロックチェーンアカウントの指定に Spreadsheet を利用しています。 Spreadsheet 側は以下のイメージで用意して下さい。重要な点は B1 セルにブロックチェーンアカウントを指定する場所を用意することです。アカウントの用意が面倒は方は以下をご利用下さい。

NARXCCAQZI6IZKCSVVIYGEYQ4PJYBNGVJETLQTQ

スクリーンショット 2023-06-05 22.50.00.png

アカウント残高読み取り用関数の指定

Google Apps Script の main.jsgetAmount という関数を用意しています。この関数を用意している事で Spreadsheet 側から呼び出すことができます。 C1 セルに =getAmount(B1) と入力しましょう。スクリプトの準備に問題がなければ残高情報が表示されます。

データ書き込み用 QR コードの作成用関数の指定

Google Apps Script の main.jsmain という関数を用意しています。この関数を実行すると、ブロックチェーンにデータを書き込む為のトランザクションの QRコードを用意することができます。

Google Apps Script の画面に移動し、 main 関数を実行しましょう。以下のように QR コードが表示されると思います。

スクリーンショット 2023-06-05 22.56.41.png

QR コードを読み込んでみよう

この QR コードは Symbol と呼ばれる Blockchain の Wallet アプリから読み取る事ができます。幾つか公開されているアプリケーションを以下の通り紹介します。是非読み込んでみて ブロックチェーンにデータを書き込んでみたり、ブロックチェーンのデータを読み出したりしてみましょう。

https://apps.apple.com/jp/app/arcana-wallet/id1603599435
https://play.google.com/store/apps/details?id=com.shu.software.symbol_arcana&hl=ja&gl=US&pli=1

最後に

もしブロックチェーン関連で疑問点がありましたらお気軽に twitter の方へリプライ下さい。答えれそうであれば答えますし、答えられそうになければ RT しまして分かりそうな人に転送致します。

また、ブロックチェーン Symbol に関する情報は以下にまとまっています

6
3
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
6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?