Created
August 30, 2020 12:24
-
-
Save YasunoriMATSUOKA/7b4f396c342cd9d95c3d38c661b8e326 to your computer and use it in GitHub Desktop.
サンプルコード: nem-libraryを用いてメッセージ無しでXEMを送る
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Node.js + ts-nodeの環境で動作すると思います | |
import { | |
NEMLibrary, | |
NetworkTypes, | |
Address, | |
TransferTransaction, | |
TimeWindow, | |
EmptyMessage, | |
TransactionHttp, | |
XEM, | |
Account, | |
ServerConfig, | |
} from "nem-library"; | |
// メインネットの設定でライブラリを初期化 | |
NEMLibrary.bootstrap(NetworkTypes.MAIN_NET); | |
// 未署名のトランザクションデータの生成 | |
// (送信先アドレス, 送金XEM数量, 空のメッセージを指定) | |
const transferTransction = TransferTransaction.create( | |
TimeWindow.createWithDeadline(), | |
new Address("NCJZ2B4GU3XHL4PQ63CPOASKEDSHYQ7MZ5FTLAA2"), | |
new XEM(1), | |
EmptyMessage | |
); | |
console.log( | |
"transferTransaction", | |
JSON.stringify(transferTransction, null, " ") | |
); | |
// 送信元アカウントの秘密鍵から署名用アカウントを生成 | |
const privateKey = "PUT_YOUR_PRIVATE_KEY_HERE"; // 秘密鍵をソースコードに書くことは非推奨. 誤ってリモートレポジトリに公開したりしないように細心の注意を払いましょう! | |
const account = Account.createWithPrivateKey(privateKey); | |
console.log("account", JSON.stringify(account, null, " ")); | |
// 署名用アカウントで未署名のトランザクションに署名を付与して署名済トランザクションを生成 | |
const signedTransferTransaction = account.signTransaction(transferTransction); | |
console.log( | |
"signedTransferTransaction", | |
JSON.stringify(signedTransferTransaction, null, " ") | |
); | |
// ノードリストの通信プロトコル, ドメイン, ポート番号から通信用モジュールを生成 | |
const serverConfigs: ServerConfig[] = [ | |
{ | |
protocol: "https", | |
domain: "aqualife1.supernode.me", | |
port: 7891, | |
}, | |
{ | |
protocol: "https", | |
domain: "aqualife2.supernode.me", | |
port: 7891, | |
}, | |
{ | |
protocol: "https", | |
domain: "aqualife3.supernode.me", | |
port: 7891, | |
}, | |
]; | |
const transactionHttp = new TransactionHttp(serverConfigs); | |
console.log("transactionHttp", JSON.stringify(transactionHttp, null, " ")); | |
// 署名済トランザクションをノードに送付し、レスポンスを待ち受け(RxJSのsubscribe)してコンソールに表示 | |
// (あくまでも署名済トランザクションがノードに送付されるだけで、秘密鍵がこの操作でネットワークに流れるわけではありません) | |
const transactionAnnounceSubscription = transactionHttp | |
.announceTransaction(signedTransferTransaction) | |
.subscribe( | |
(response) => { | |
console.log("トランザクション送付に成功しました"); | |
console.log(JSON.stringify(response, null, " ")); | |
}, | |
(error) => { | |
console.log("エラー: トランザクション送付に失敗しました!"); | |
console.log(error); | |
}, | |
() => { | |
console.log("処理は全て終了しました"); | |
} | |
); | |
// タイムアウト時にレスポンスの待ち受け(RxJSの待ち受け)を解除 | |
// タイムアウトは20秒に設定 | |
setTimeout(() => { | |
console.log("タイムアウトしました!"); | |
transactionAnnounceSubscription.unsubscribe(); | |
}, 20000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
コメントありがとうございます。ちょうどこういった開発をしていたところで、ほぼコピペだったので大丈夫です。
最初はNEM-sdkがわかりやすくて便利だと思いますが、本格的にJavaScriptの開発を進めていくと、nem-libraryの方が便利な局面が出てくるかな...という印象です。(次期バージョンのSymbolのSDKも、どちらかというと、nem-libraryの発展形的な形で作られているようです。)
色々試して頂いて、ぜひ素敵なサービスの開発に繋げて頂ければ...と思います。