|
1 | 1 | ---
|
2 | 2 | sidebar_position: 1
|
3 | 3 | ---
|
| 4 | + |
4 | 5 | # Set up a Project
|
5 | 6 |
|
| 7 | +Create a TypeScript project and introduce the `meta-contract` library. |
| 8 | + |
| 9 | +> Note: All tests below use Node.js version 18.20.1. The keys and addresses used in this tutorial are for testing |
| 10 | +> purposes only and should not be used in a production environment. The author is not responsible for any losses due to |
| 11 | +> key leakage. |
| 12 | +
|
| 13 | +## Create a Node.js Project |
| 14 | + |
| 15 | +First, initialize a Node.js project, |
| 16 | + |
| 17 | +```bash |
| 18 | +mkdir mvc-test-project |
| 19 | +cd mvc-test-project |
| 20 | +npm init -y |
| 21 | +``` |
| 22 | + |
| 23 | +## Install TypeScript |
| 24 | + |
| 25 | +This tutorial uses TypeScript to write the program, and it's recommended to use TypeScript to write contracts. |
| 26 | + |
| 27 | +The following command installs the TypeScript dependencies. |
| 28 | + |
| 29 | +```bash |
| 30 | +npm install typescript ts-node @types/node fs-extra --save-dev |
| 31 | +``` |
| 32 | + |
| 33 | +Initialize the TypeScript configuration file |
| 34 | + |
| 35 | +```bash |
| 36 | +npx tsc --init |
| 37 | +``` |
| 38 | + |
| 39 | +## Install Meta-Contract |
| 40 | + |
| 41 | +```bash |
| 42 | +npm install meta-contract --save |
| 43 | +``` |
| 44 | + |
| 45 | +## Start Writing the Program |
| 46 | + |
| 47 | +Create an `index.ts` file to execute the logic. |
| 48 | + |
| 49 | +```bash |
| 50 | +mkdir src |
| 51 | +touch src/index.ts |
| 52 | +``` |
| 53 | + |
| 54 | +Write the following code in the `src/index.ts` file: |
| 55 | + |
| 56 | +```typescript |
| 57 | +import {Mnemonic} from "meta-contract/dist/mvc"; |
| 58 | +import {promises as fs} from 'fs'; |
| 59 | +import {API_NET, Wallet} from "meta-contract"; |
| 60 | + |
| 61 | +const FILE_PATH = 'mnemonic-seed.txt' |
| 62 | +const WALLET_PATH = "m/44'/10001'/0'/0/0" |
| 63 | +let seed = ''; |
| 64 | +const generateMnemonic = async (): Promise<string> => { |
| 65 | + console.log("Generating random mnemonic seed: <%s>, use your own if you already have it", Mnemonic.fromRandom().toString()) |
| 66 | + const mnemonic = Mnemonic.fromRandom().toString(); |
| 67 | + await fs.writeFile(FILE_PATH, mnemonic, 'utf8'); |
| 68 | + return mnemonic; |
| 69 | +}; |
| 70 | + |
| 71 | +const readMnemonic = async (): Promise<string> => { |
| 72 | + return await fs.readFile(FILE_PATH, 'utf8'); |
| 73 | +}; |
| 74 | + |
| 75 | +// Detect if the mnemonic seed file exists, if not, generate a new one |
| 76 | +// Then create a wallet with the seed with path WALLET_PATH |
| 77 | +const setupMyWallet = async (): Promise<void> => { |
| 78 | + try { |
| 79 | + await fs.access(FILE_PATH); |
| 80 | + const mnemonic = await readMnemonic(); |
| 81 | + console.log('Mnemonic seed exists: <%s>, will use this one.', mnemonic); |
| 82 | + seed = mnemonic; |
| 83 | + } catch (error) { |
| 84 | + const mnemonic = await generateMnemonic(); |
| 85 | + console.log('Mnemonic seed not detected, generating new one. <%s> , please keep it safe for future use.', mnemonic); |
| 86 | + seed = mnemonic; |
| 87 | + } |
| 88 | + console.log('Creating wallet with seed: <%s> and path <%s>', seed, WALLET_PATH); |
| 89 | + let mnemonicParsed = Mnemonic.fromString(seed); |
| 90 | + let privateKey = mnemonicParsed.toHDPrivateKey("", API_NET.TEST).deriveChild(WALLET_PATH); |
| 91 | + let wallet = new Wallet(privateKey.privateKey.toWIF(), API_NET.TEST, 1); |
| 92 | + console.log("Your private key %s", privateKey.privateKey.toWIF()); |
| 93 | + console.log("Your address %s", privateKey.privateKey.toAddress(API_NET.TEST).toString()); |
| 94 | + console.log("Your balance %s satoshis", await wallet.getBalance()); |
| 95 | +}; |
| 96 | + |
| 97 | +setupMyWallet().catch(console.error); |
| 98 | +``` |
| 99 | + |
| 100 | +### Functionality of This Code: |
| 101 | + |
| 102 | +1. Check if the `mnemonic-seed.txt` file exists in the project directory. If not, generate a new mnemonic and write it |
| 103 | + to the file. Note that this mnemonic is randomly generated and should not be used in a production environment. Also, |
| 104 | + keep this mnemonic safe as subsequent operations will use it. |
| 105 | +2. Read the mnemonic from the file if it exists. |
| 106 | +3. Generate a wallet using the mnemonic, and print the private key, address, and balance. |
| 107 | + |
| 108 | +## Run the Program |
| 109 | + |
| 110 | +Execute the following commands to run the program: |
| 111 | + |
| 112 | +```bash |
| 113 | +npx tsc |
| 114 | +node src/index.js |
| 115 | +``` |
| 116 | + |
| 117 | +If everything is correct, you will see an output similar to the following: |
| 118 | + |
| 119 | +```text |
| 120 | +Mnemonic seed exists: <* * * * * * * * * * * *>, will use this one. |
| 121 | +Creating wallet with seed: <* * * * * * * * * * * *> and path <m/44'/10001'/0'/0/0> |
| 122 | +Your private key * |
| 123 | +Your address mi51xGS45itNchtDRdRP3Fbh3vjEQWxh38 |
| 124 | +Your balance 0 satoshis |
| 125 | +``` |
| 126 | + |
| 127 | +The wallet creation is complete. You can use this wallet address for subsequent operations. |
0 commit comments