ブロックチェーンは改ざんが難しいプラットフォームであることはだんだんと周知されてきました。ですが、ブロックチェーンに登録する作業やシステム構築を考えた場合に、なかなか現在のシステムを置き換えるという発想には至っていないのが現状ではないでしょうか。
NEMはノードにREST API機能が実装されており、従来のシステムに「プラス・ブロックチェーン」を簡単に実現することが可能です。
今回はGCPが提供するGoogle Cloud Storageにデータがアップロードされた場合に、自動的にNEMブロックチェーンに記録するプログラムを作成してみましょう。
Google Cloud Storage にデータが登録された場合に起動させるファンクションの作り方は公式のチュートリアルが参考になります。
Cloud Storage のチュートリアル
これにNEM Catapultのライブラリを追加していきます。
nodejs-docs-samples/functions/helloworld/
の helloGCSGeneric
を改造してhelloNEMを作成してみました。
const {
TransferTransaction,
Deadline,
Mosaic,
MosaicId,
UInt64,
NetworkType,
TransactionHttp,
Account,
PlainMessage
} = require("nem2-sdk");
exports.helloNEM = (data, context, callback) => {
const file = data;
const alice = Account.createFromPrivateKey("BCC06E6531609CABB77D33C6B59452CDB25E6485769913ACCC0E16C631DB7F36", NetworkType.TEST_NET);
const NODE = 'https://jp5.nemesis.land:3001';
const GENERATION_HASH = "CC42AAD7BD45E8C276741AB2524BC30F5529AF162AD12247EF9A98D6B54A385B";
const tx = TransferTransaction.create(
Deadline.create(),
alice.address,
[
new Mosaic(
new MosaicId('75AF035421401EF0'),
UInt64.fromUint(0)
)
],
PlainMessage.create('Hello GCP World!'),
NetworkType.TEST_NET,
UInt64.fromUint(100000)
);
const signedTx = alice.sign(tx,GENERATION_HASH);
const txHttp = new TransactionHttp(NODE);
txHttp
.announce(signedTx)
.subscribe(_ => console.log(_), err => console.error(err));
console.log(` Event: ${context.eventId}`);
console.log(` Event Type: ${context.eventType}`);
console.log(` Bucket: ${file.bucket}`);
console.log(` File: ${file.name}`);
console.log(` Metageneration: ${file.metageneration}`);
console.log(` Created: ${file.timeCreated}`);
console.log(` Updated: ${file.updated}`);
callback();
};
package.jsonにnem2-sdkの部分のみ追加します。
"dependencies": {
"@google-cloud/debug-agent": "^4.0.0",
"escape-html": "^1.0.3",
"pug": "^2.0.3",
"supertest": "^4.0.2",
"nem2-sdk": "^0.16.1"
}
デプロイします。
gcloud functions deploy helloNEM --runtime nodejs8 --trigger-resource staging.xxx.appspot.com --trigger-event google.storage.object.finalize
ファイルをアップロードしてみましょう。
gsutil cp test/test2.txt gs://staging.xxxx.appspot.com
ログが出力されました。NEMのネットワークに到達です。
TransactionAnnounceResponse {
message: 'packet 9 was pushed to the network via /transaction' }
ノードからトランザクション履歴を参照してみます。
[{
"meta":{
"height":"138477",
"hash":"C383A6ACDA343486E983B72EB49DF2797914008C6C26DE2ED54FD8051DABD629",
"merkleComponentHash":"C383A6ACDA343486E983B72EB49DF2797914008C6C26DE2ED54FD8051DABD629",
"index":0,
"id":"5E12A75C43956B2E164B4B1F"
},
"transaction":{
"signature":"2959553AC6AD908063947431A55DA9F7C6FBDFA58C6BB533FEBBC82209C29E2AE41309907BDF608C6ABD66AE7992951ADAE8D349440E2DCAF7B72FDF21EC860F",
"signerPublicKey":"62488F7A6C6F48274394C06FECD96AA2E467D9981D16E9C19066E846C9D5848A",
"version":1,
"network":152,
"type":16724,
"maxFee":"100000",
"deadline":"4857588334",
"recipientAddress":"983E665F184685E44F89C8D7D5B65FF947215A46098A0455FC",
"message":{
"type":0,"payload":"48656C6C6F2047435020576F726C6421"
},
"mosaics":[
{"id":"75AF035421401EF0","amount":"0"}
]
}
}]
無事登録成功です。
最後に
今後、ブロックチェーンと外部データとの連携は重要なテーマとなります。なぜなら、「ブロックチェーンに登録される前に改ざんされていない」という証拠があって初めて価値を持つ観測データが多くあるからです。今回はGCP上で設定されたセキュリティ基準を満たしたプラットフォームに上げさえすれば、あとはNEMブロックチェーンによって耐改ざん性が保証されるという可能性を提示しました。