Skip to content

Instantly share code, notes, and snippets.

@daoka
Last active October 5, 2021 00:55
Show Gist options
  • Save daoka/e9d343f37db4f7a6d27ded7b2d84ddd0 to your computer and use it in GitHub Desktop.
Save daoka/e9d343f37db4f7a6d27ded7b2d84ddd0 to your computer and use it in GitHub Desktop.
Generate and read mnemonic QR for Symbol
import { ExtendedKey, MnemonicPassPhrase, Network, Wallet } from "symbol-hd-wallets";
import { MnemonicQR } from "symbol-qr-library";
import { Account, NetworkType } from "symbol-sdk";
const words = 'INPUT YOUR MNEMONIC WORDS';
const mnemonic = new MnemonicPassPhrase(words);
const testnetGenerationHash = '3B5E1FA6445653C971A50687E75E6D09FB30481055E3990C84B25E9222DC1155';
const mainnetGenerationHash = '57F7DA205008026C776CB6AED843393F04CD458E0AA2D9F1D5F31A402072B2D6';
const generationhash = mainnetGenerationHash;
/**
* Symbolデスクトップウォレットのtestnetのpathは以下の様に続いていく
* 0: 'm/44\'/1\'/0\'/0\'/0\''
* 1: 'm/44\'/1\'/1\'/0\'/0\''
* 2: 'm/44\'/1\'/2\'/0\'/0\''
* ...
*/
const testnetDefalutPass = 'm/44\'/1\'/0\'/0\'/0\'';
/**
* Symbolデスクトップウォレットのmainnetのpathは以下の様に続いていく
* 0: 'm/44\'/4343\'/0\'/0\'/0\''
* 1: 'm/44\'/4343\'/1\'/0\'/0\''
* 2: 'm/44\'/4343\'/2\'/0\'/0\''
* ...
*/
const mainnetDefaultPass = 'm/44\'/4343\'/0\'/0\'/0\''
const defalutPass = mainnetDefaultPass;
const testnetNetWorkType = NetworkType.TEST_NET;
const mainnetNetworkType = NetworkType.MAIN_NET;
const networkType = mainnetNetworkType;
const pass = 'INPUT YOUR PASSWORD';
// ニーモニック用のQRコードを生成する
const outputQr = new MnemonicQR(mnemonic.plain, NetworkType.TEST_NET, generationhash, pass);
const qrJson = outputQr.toJSON();
console.log(qrJson);
// QRコードを読み込んだ際に読み取られるJSONからニーモニックを読み込む
const inputQr = MnemonicQR.fromJSON(qrJson, pass);
console.log(inputQr);
const mnemonic2 = new MnemonicPassPhrase(inputQr.mnemonicPlainText);
// ニーモニックからHDウォレット生成する
const bip32seed = mnemonic2.toSeed();
const xkey = ExtendedKey.createFromSeed(bip32seed.toString('hex'), Network.SYMBOL);
const wallet = new Wallet(xkey);
// HDウォレットから子アカウントを生成する
const childAccountPrivateKey = wallet.getChildAccountPrivateKey(defalutPass);
const account = Account.createFromPrivateKey(childAccountPrivateKey, networkType);
console.log(account.address.pretty());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment