Skip to content

Commit c25620e

Browse files
committed
finish sdk ordinary transaction
1 parent 2850063 commit c25620e

File tree

5 files changed

+85
-19
lines changed

5 files changed

+85
-19
lines changed

docs/tools/wallets/cli-wallet.md

Lines changed: 0 additions & 4 deletions
This file was deleted.

i18n/zh-CN/docusaurus-plugin-content-docs/current/introduction/getting-started/transaction/ft_transaction_cli.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

i18n/zh-CN/docusaurus-plugin-content-docs/current/introduction/getting-started/transaction/nft_transaction_cli.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

i18n/zh-CN/docusaurus-plugin-content-docs/current/introduction/getting-started/transaction/orinary_transaction_cli.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

i18n/zh-CN/docusaurus-plugin-content-docs/current/introduction/getting-started/transaction/orinary_transaction_sdk.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,88 @@ sidebar_position: 2
33
---
44

55
# 使用SDK构建普通交易
6+
7+
本文介绍使用MetaContract SDK构建普通交易(P2PKH)的方法。
8+
9+
> 使用之前请确保已经完成环境配置和测试币领取,请参考[搭建一个MVC项目](../enviroment/setup-project)
10+
11+
本文基于上述已经配置好的环境和密钥进行操作。
12+
13+
## 修改源码
14+
15+
`src/index.ts`文件替换为以下内容:
16+
17+
```typescript
18+
import {Mnemonic} from "meta-contract/dist/mvc";
19+
import {promises as fs} from 'fs';
20+
import {API_NET, Wallet} from "meta-contract";
21+
22+
const FILE_PATH = 'mnemonic-seed.txt'
23+
const WALLET_PATH = "m/44'/10001'/0'/0/0"
24+
let seed = '';
25+
const generateMnemonic = async (): Promise<string> => {
26+
console.log("Generating random mnemonic seed: <%s>, use your own if you already have it", Mnemonic.fromRandom().toString())
27+
const mnemonic = Mnemonic.fromRandom().toString();
28+
await fs.writeFile(FILE_PATH, mnemonic, 'utf8');
29+
return mnemonic;
30+
};
31+
32+
const readMnemonic = async (): Promise<string> => {
33+
return await fs.readFile(FILE_PATH, 'utf8');
34+
};
35+
36+
// Detect if the mnomonic seed file exists, if not, generate a new one
37+
// Then create a wallet with the seed with path WALLET_PATH
38+
const setupMyWalletAndSend = async (): Promise<void> => {
39+
try {
40+
await fs.access(FILE_PATH);
41+
const mnemonic = await readMnemonic();
42+
console.log('Mnemonic seed exists: <%s>, will use this one.', mnemonic);
43+
seed = mnemonic;
44+
} catch (error) {
45+
const mnemonic = await generateMnemonic();
46+
console.log('Mnemonic seed not detected, generating new one. <%s> , please keep it safe for future use.', mnemonic);
47+
seed = mnemonic;
48+
}
49+
console.log('Creating wallet with seed: <%s> and path <%s>', seed, WALLET_PATH);
50+
let mnemonicParsed = Mnemonic.fromString(seed);
51+
let privateKey = mnemonicParsed.toHDPrivateKey("", API_NET.TEST).deriveChild(WALLET_PATH);
52+
let wallet = new Wallet(privateKey.privateKey.toWIF(), API_NET.TEST, 1);
53+
console.log("Your private key %s", privateKey.privateKey.toWIF());
54+
console.log("Your address %s", privateKey.privateKey.toAddress(API_NET.TEST).toString());
55+
console.log("Your balance %s satoshis", await wallet.getBalance());
56+
57+
// Send 10000 satoshi back to the faucet
58+
console.log("Sending 10000 satoshis back to the faucet");
59+
wallet
60+
.send("mktGt8zJo5qYmXc97SVvCLPvtLExymgBKF", 10000)
61+
.then((tx) => {
62+
console.log("Transaction sent, txid %s, rawHex %s", tx.txId, tx.txHex);
63+
})
64+
.then(() => wallet.getBalance())
65+
.then((balance) => {
66+
console.log("Your balance now is %s satoshis", balance);
67+
});
68+
};
69+
70+
71+
setupMyWalletAndSend().catch(console.error);
72+
73+
```
74+
75+
在初始化钱包的基础上增加了发送Space交易的逻辑。
76+
77+
这段代码的功能介绍:
78+
1. 初始化钱包并打印出地址和余额。
79+
2. 向水龙头发送10000 satoshi。
80+
3. 打印出交易ID和发送交易后的余额。
81+
82+
如果一切正常,你会看到类似以下输出:
83+
84+
```text
85+
Your address mmGruHTY1ivexPzmvjz8AxrhhWWE8BbgN9
86+
Your balance 9928215 satoshis
87+
Sending 10000 satoshis back to the faucet
88+
Transaction sent, txid 602e6d6216dfa059d815c373868f696bf63efee7d798e2f8607213a73148e744, rawHex 0a000000019ed5a08a5dceb21c04faa56529702bd821bb7df9f6ef8a05f4558c34896ca62a010000006a47304402202c202844366d0ea54f0efc297982e19c6e47fd0ac8e94bf9343d304f8bb0da5202203fc50d96855acd27c18c26789362b3a970f7c83dfe2a85e581aaaeb201424648412102b9302409e6a04b5cb8470ef1471e36a894e2676373e51cbcff54e6fa516d12f5ffffffff0210270000000000001976a9143ae0e15c763122be84e4d2d75a9e92b0d8703b2c88ac08569700000000001976a9143f266e0384df8e171b60895832f58aaf4d5b58ab88ac00000000
89+
Your balance now is 9917960 satoshis
90+
```

0 commit comments

Comments
 (0)