Node.js+TypeScriptではじめるSymbol-SDK
Node.js まわりのバージョン
- Yarn v4.6.0
- Node.js v22.13.0
プロジェクトフォルダの作成と初期化
適当な場所に作成します。
mkdir test-project
cd test-project
yarn が node_modules
フォルダを作成するようファイルを作成します。
echo "nodeLinker: node-modules" > .yarnrc.yml
プロジェクトフォルダを初期化します。
yarn init
念のため Node.js などのバージョンのピン留めもしておきます。
volta pin node
volta pin npm
volta pin yarn
git の変更箇所が大量に出るので、出ないように .gitignore
末尾に以下を追記します。
node_modules
dist
開発用パッケージのインストールと設定
開発時に使用するパッケージをインストールします。
yarn add -D @eslint/js @types/node eslint eslint-config-prettier globals prettier prettier-eslint tsx typescript typescript-eslint
ESLint の設定
eslint.config.js
を作成&編集。
import pluginJs from "@eslint/js";
import eslintConfigPrettier from "eslint-config-prettier";
import globals from "globals";
import tseslint from "typescript-eslint";
/** @type {import('eslint').Linter.Config[]} */
export default [
{ files: ["**/*.{js,mjs,cjs,ts}"] },
{ languageOptions: { globals: globals.browser } },
pluginJs.configs.recommended,
...tseslint.configs.recommended,
eslintConfigPrettier,
];
Prettier の設定
.prettierrc.json
を作成&編集。
{
"tabWidth": 2,
"printWidth": 80,
"trailingComma": "all",
"semi": false,
"singleQuote": true
}
tsconfig.json の作成
tsconfig.json
を作成&編集。
TypeScript ソースがなくて include
と exclude
付近でエラー出るかもしれませんが、無視してください。
{
"compilerOptions": {
"target": "ES2022",
"module": "Node16",
"moduleResolution": "node16",
"outDir": "dist",
"rootDir": "src",
"declaration": true,
"strict": true,
"composite": true
},
"include": [
"./src/**/*"
],
"exclude": [
"node_modules",
"dist"
]
}
package.json の編集
package.json
に "type": "module"
を追加します。これにより、ES モジュールとして扱われるようになります。ついでに使いそうな script
も追加しておくと良いと思います。
{
"name": "test-project",
"packageManager": "yarn@4.6.0",
"type": "module",
"scripts": {
"build": "tsc",
"start": "yarn tsx src/index.ts",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"format": "prettier --write \"src/**/*.ts\""
},
...
ESLint がうまく設定ファイルが読み込めない場合があるので、この辺りで「ウィンドウの再読み込み」をしておくと良いと思います。
メニューのコマンドパレットにあります。
表示 → コマンドパレット >reload window
パッケージのインストール
symbol-sdk
をインストールします。
yarn add symbol-sdk
動作確認
確認のため簡単なコードを動かしてみます。
src
ディレクトリを作成して、その中に index.ts
ファイルを作成します。
import { Bip32 } from 'symbol-sdk'
import { Address, Network, SymbolFacade } from 'symbol-sdk/symbol'
// ニーモニック生成
const bip32 = new Bip32()
const mnemonic = bip32.random()
console.log(`mnemonic: ${mnemonic}`)
// faced生成
const facade = new SymbolFacade(Network.TESTNET)
// ニーモニックから10件アカウントを生成する
const bip32Node = bip32.fromMnemonic(mnemonic, '')
for (let i = 0; i < 10; i++) {
// Bip32Path生成
const bip32Path = facade.bip32Path(i)
// Bip32Pathから子孫Bip32Path生成
const childBip32Node = bip32Node.derivePath(bip32Path)
// 子孫Bip32Pathからキーペア生成
const keyPair = SymbolFacade.bip32NodeToKeyPair(childBip32Node)
// 公開鍵からアドレス生成
const address = new Address(
facade.network.publicKeyToAddress(keyPair.publicKey),
)
// 表示
console.log(
`[${i}]`,
`Addr:${address.toString()}`,
`PubK:${keyPair.publicKey.toString()}`,
`PriK:${keyPair.privateKey.toString()}`,
)
}
実行します。
yarn start
ニーモニックと 10 件のアカウントが表示されれば OK です。
さいごに
これでサーバーサイドで動く Symbol アプリ開発ができるようになりました。
Electron や React、Next.js で Symbol-SDK を使用する場合は、別途パッケージをインストールして Wasm を読み込ませる必要があります。方法はまた今度…きっと……
Discussion