Create a TypeScript project and introduce the meta-contract library.
Note: All tests below use Node.js version 18.20.1. The keys and addresses used in this tutorial are for testing purposes only and should not be used in a production environment. The author is not responsible for any losses due to key leakage.
Create a Node.js Project
First, initialize a Node.js project,
mkdirmvc-test-projectcdmvc-test-projectnpminit-y
Install TypeScript
This tutorial uses TypeScript to write the program, and it's recommended to use TypeScript to write contracts.
The following command installs the TypeScript dependencies.
Write the following code in the src/index.ts file:
import {Mnemonic} from"meta-contract/dist/mvc";import {promises as fs} from'fs';import {API_NET, Wallet} from"meta-contract";constFILE_PATH='mnemonic-seed.txt'constWALLET_PATH="m/44'/10001'/0'/0/0"let seed ='';constgenerateMnemonic=async ():Promise<string> => { console.log("Generating random mnemonic seed: <%s>, use your own if you already have it", Mnemonic.fromRandom().toString())
constmnemonic=Mnemonic.fromRandom().toString();awaitfs.writeFile(FILE_PATH, mnemonic,'utf8');return mnemonic;};constreadMnemonic=async ():Promise<string> => {returnawaitfs.readFile(FILE_PATH,'utf8');};// Detect if the mnemonic seed file exists, if not, generate a new one// Then create a wallet with the seed with path WALLET_PATHconstsetupMyWallet=async ():Promise<void> => {try {awaitfs.access(FILE_PATH);constmnemonic=awaitreadMnemonic();console.log('Mnemonic seed exists: <%s>, will use this one.', mnemonic); seed = mnemonic; } catch (error) {constmnemonic=awaitgenerateMnemonic(); console.log('Mnemonic seed not detected, generating new one. <%s> , please keep it safe for future use.', mnemonic);
seed = mnemonic; }console.log('Creating wallet with seed: <%s> and path <%s>', seed,WALLET_PATH);let mnemonicParsed =Mnemonic.fromString(seed);let privateKey =mnemonicParsed.toHDPrivateKey("",API_NET.TEST).deriveChild(WALLET_PATH);let wallet =newWallet(privateKey.privateKey.toWIF(),API_NET.TEST,1);console.log("Your private key %s",privateKey.privateKey.toWIF());console.log("Your address %s",privateKey.privateKey.toAddress(API_NET.TEST).toString());console.log("Your balance %s satoshis",awaitwallet.getBalance());};setupMyWallet().catch(console.error);
Functionality of This Code:
Check if the mnemonic-seed.txt file exists in the project directory. If not, generate a new mnemonic and write it to the file. Note that this mnemonic is randomly generated and should not be used in a production environment. Also, keep this mnemonic safe as subsequent operations will use it.
Read the mnemonic from the file if it exists.
Generate a wallet using the mnemonic, and print the private key, address, and balance.
Run the Program
Execute the following commands to run the program:
npxtscnodesrc/index.js
If everything is correct, you will see an output similar to the following:
Mnemonic seed exists: <* * * * * * * * * * * *>, will use this one.
Creating wallet with seed: <* * * * * * * * * * * *> and path <m/44'/10001'/0'/0/0>
Your private key *
Your address mi51xGS45itNchtDRdRP3Fbh3vjEQWxh38
Your balance 0 satoshis
The wallet creation is complete. You can use this wallet address for subsequent operations.