Skip to content

Commit 9048b27

Browse files
committed
course 3
1 parent 21ef45c commit 9048b27

File tree

3 files changed

+548
-1
lines changed

3 files changed

+548
-1
lines changed

README3-zhchs.md

+271
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
# 基于Mixin Network的 Nodejs 比特币开发教程: 创建比特币钱包
2+
![cover](https://github.com/wenewzhang/mixin_network-nodejs-bot2/raw/master/Bitcoin_node.jpg)
3+
我们已经创建过一个[回复消息](https://github.com/wenewzhang/mixin_network-nodejs-bot2/blob/master/README-zhchs.md)的机器人和一个能自动[支付比特币](https://github.com/wenewzhang/mixin_network-nodejs-bot2/blob/master/README2-zhchs.md)的机器人.
4+
5+
### 通过本教程的学习,你可以学到如下内容
6+
1. 如何创建一个比特币钱包.
7+
2. 如何读取比特币钱包的余额.
8+
3. 如何支付比特币并即时确认.
9+
4. 如何将Mixin Network的比特币提现到你的冷钱包或第三方交易所.
10+
11+
12+
前期准备:你要有一个Mixin Network账户。下面的代码创建一个帐号,并写到csv文件里。
13+
```js
14+
console.log("create wallet ....");
15+
const { generateKeyPairSync } = require('crypto');
16+
const { publicKey, privateKey } = generateKeyPairSync('rsa',
17+
{ modulusLength: 1024, // the length of your key in bits
18+
publicKeyEncoding: {
19+
type: 'spki', // recommended to be 'spki' by the Node.js docs
20+
format: 'pem'
21+
},
22+
privateKeyEncoding: {
23+
type: 'pkcs1', // recommended to be 'pkcs8' by the Node.js docs
24+
format: 'pem',
25+
//cipher: 'aes-256-cbc', // *optional*
26+
//passphrase: 'top secret' // *optional*
27+
}
28+
});
29+
publicKey1 = publicKey.replace("-----BEGIN PUBLIC KEY-----","");
30+
publicKey2 = publicKey1.replace("-----END PUBLIC KEY-----","");
31+
publicKey3 = publicKey2.replace(/\r?\n|\r/g, "");
32+
33+
console.log(publicKey);
34+
console.log(publicKey3);
35+
(async () => {
36+
const info = await clientBot.createUser({full_name : "nodejs bitcoin wallet",
37+
session_secret: publicKey3,
38+
});
39+
40+
```
41+
上面的语句会在本地创建一个RSA密钥对,然后调用Mixin Network来创建帐号,最后保存帐号信息到csv文件.
42+
```js
43+
let aesKey = '';
44+
const privateKeyBytes = pem.decode(Buffer.from(privateKey));
45+
const aesKeyBuffer = await oaepDecrypt(
46+
Buffer.from(info.pin_token, 'base64'),
47+
privateKeyBytes,
48+
'SHA-256',
49+
Buffer.from(info.session_id)
50+
);
51+
aesKey = Buffer.from(aesKeyBuffer).toString('base64');
52+
console.log(aesKey);
53+
54+
var csvStream = csv.createWriteStream({headers: false, ignoreEmpty: true}),
55+
writableStream = fs.createWriteStream(WalletName, {flags: 'a'});
56+
57+
writableStream.on("finish", function(){
58+
console.log("Bitcoin wallet DONE!");
59+
});
60+
csvStream.pipe(writableStream);
61+
csvStream.write({a: privateKey, b: info.pin_token,
62+
c: info.session_id, d: info.user_id,
63+
e: "123456"}
64+
);
65+
csvStream.end();
66+
fs.appendFile(WalletName, require("os").EOL, function(){});
67+
68+
const newUserConfig = {clientId: info.user_id, aesKey: aesKey,
69+
privateKey: privateKey, sessionId: info.session_id,
70+
clientSecret: "do not need", assetPin: "123456"};
71+
console.log(newUserConfig);
72+
const newUserClient = new HttpClient(newUserConfig);
73+
var info2 = await newUserClient.updatePin({oldPin : "",
74+
newPin: "123456",
75+
});
76+
console.log(info2);
77+
78+
const verifyPin = await newUserClient.verifyPin("123456");
79+
console.log({ verifyPin });
80+
81+
```
82+
83+
现在你需要小心保管好你的帐号信息,在读取该账户的比特币资产余额或者进行其他操作时,将需要用到这些信息.
84+
### 给新建的帐号创建一个比特币钱包
85+
新账号并不默认内置比特币钱包, 现在读一下比特币余额就可以创建一个比特币钱包。
86+
```js
87+
const assetInfo = await newUserClient.getUserAsset(BTC_ASSET_ID);
88+
console.log("Bitcoin address is ", assetInfo.public_key);
89+
console.log("Bitcoin balance is ", assetInfo.balance);
90+
console.log("Bitcoin price is (USD) ", assetInfo.price_usd);
91+
```
92+
创建的帐号的比特币资产详细信息如下,其中public key就是比特币的存币地址:
93+
```bash
94+
Make your choose(select the uuid for open the specified wallet): 0b10471b-1aed-3944-9eda-5ab947562761
95+
You select the : 0b10471b-1aed-3944-9eda-5ab947562761
96+
You select the wallet 0b10471b-1aed-3944-9eda-5ab947562761
97+
?
98+
Make your choose 1: Read Bitcoin Balance & Address
99+
You choice to : { type: '1: Read Bitcoin Balance & Address' }
100+
You wallet is : 0b10471b-1aed-3944-9eda-5ab947562761
101+
Bitcoin address is 15MySY7UnA827TRMQWuCKGiogCYXUmt21M
102+
Bitcoin balance is 0
103+
Bitcoin price is (USD) 5029.59915767
104+
You select the : 0b10471b-1aed-3944-9eda-5ab947562761
105+
You select the wallet 0b10471b-1aed-3944-9eda-5ab947562761
106+
```
107+
108+
这个API能够提供若干与比特币有关的信息:
109+
* 存币地址:[public_key]
110+
* Logo: [icon_url]
111+
* 资产名字:[name]
112+
* 资产在Mixin Network的uuid: [asset_key]
113+
* 对美元的价格(Coinmarketcap.com提供): [price_usd]
114+
* 存币时确认的区块数量:[confirmations]
115+
116+
117+
### 比特币私钥呢?
118+
比特币的私钥呢?这个私钥被Mixin Network通过多重签名保护,所以对用户来说是不可见的,比特币资产的提现和转账都需要用户提供正确的的RSA签名,PIN代码与会话密钥才能完成.
119+
120+
### 不只是比特币,还有以太坊,EOS等
121+
这个帐号不只支持比特币,还支持以太坊,EOS等, 完整的区块链支持[列表](https://mixin.one/network/chains). 这个账户同时也支持所有的 ERC20 代币与 EOS 代币.
122+
123+
创建其它的币的钱包与创建比特币钱包过程一样,读对应的资产余额就可以.
124+
125+
#### Mixin Network 当前支持的加密货币 (2019-02-19)
126+
127+
|crypto |uuid in Mixin Network
128+
|---|---
129+
|EOS|6cfe566e-4aad-470b-8c9a-2fd35b49c68d
130+
|CNB|965e5c6e-434c-3fa9-b780-c50f43cd955c
131+
|BTC|c6d0c728-2624-429b-8e0d-d9d19b6592fa
132+
|ETC|2204c1ee-0ea2-4add-bb9a-b3719cfff93a
133+
|XRP|23dfb5a5-5d7b-48b6-905f-3970e3176e27
134+
|XEM|27921032-f73e-434e-955f-43d55672ee31
135+
|ETH|43d61dcd-e413-450d-80b8-101d5e903357
136+
|DASH|6472e7e3-75fd-48b6-b1dc-28d294ee1476
137+
|DOGE|6770a1e5-6086-44d5-b60f-545f9d9e8ffd
138+
|LTC|76c802a2-7c88-447f-a93e-c29c9e5dd9c8
139+
|SC|990c4c29-57e9-48f6-9819-7d986ea44985
140+
|ZEN|a2c5d22b-62a2-4c13-b3f0-013290dbac60
141+
|ZEC|c996abc9-d94e-4494-b1cf-2a3fd3ac5714
142+
|BCH|fd11b6e3-0b87-41f1-a41f-f0e9b49e5bf0
143+
144+
EOS的存币地址与其它的币有些不同,它由两部分组成: account_name and account tag, 如果你向Mixin Network存入EOS,你需要填两项数据: account name 是**eoswithmixin**,备注里输入你的account_tag,比如**0aa2b00fad2c69059ca1b50de2b45569**.
145+
146+
EOS的资产余额返回结果如下:
147+
```bash
148+
Make your choose 3: Read EOS Balance & Address
149+
You choice to : { type: '3: Read EOS Balance & Address' }
150+
You wallet is : 0b10471b-1aed-3944-9eda-5ab947562761
151+
EOS account name is eoswithmixin tag is 30f0c36057b9b22151173b309bd0d79c
152+
EOS balance is 0
153+
EOS price is (USD) 5.26225922
154+
You select the : 0b10471b-1aed-3944-9eda-5ab947562761
155+
You select the wallet 0b10471b-1aed-3944-9eda-5ab947562761
156+
```
157+
158+
### 存入比特币与读取比特币余额
159+
现在,你可以向比特币的钱包存币了。
160+
161+
当然,在比特币网络里转币,手续费是相当贵的,费用的中位数在0.001BTC,按当前4000美元的价格,在4美元左右,有一个方便的办法,如果你有[Mixin Messenger](https://mixin.one/messenger)帐号,里面并且有比特币的话,可以直接提现比特币到新创建的帐号的比特币充值地址,它们在同一个Mixin Network网络内,手续费为0,而且1秒到账。
162+
163+
下面的代码,可以读取比特币钱包余额.
164+
```js
165+
const assetInfo = await newUserClient.getUserAsset(BTC_ASSET_ID);
166+
console.log("Bitcoin address is ", assetInfo.public_key);
167+
console.log("Bitcoin balance is ", assetInfo.balance);
168+
console.log("Bitcoin price is (USD) ", assetInfo.price_usd);
169+
```
170+
### Mixin Network网内免手续费的,并且即时确认
171+
任何币在Mixin Network内部的交易,都是无手续费的,并且立刻到账。
172+
前期准备: 账户设置了PIN
173+
174+
对于新创建的帐号,我们通过updatePin来设置新PIN码, 代码如下:
175+
```js
176+
var info2 = await newUserClient.updatePin({oldPin : "",
177+
newPin: "123456",
178+
});
179+
console.log(info2);
180+
181+
const verifyPin = await newUserClient.verifyPin("123456");
182+
console.log({ verifyPin });
183+
```
184+
#### Mixin Network帐号之间的比特币支付
185+
通过Mixin Messenger,我们可以先转比特币给机器人,然后让机器人转币给新用户。
186+
```js
187+
const assetInfo = await newUserClient.getUserAsset(EOS_ASSET_ID);
188+
console.log("The Wallet 's EOS balance is ", assetInfo.balance);
189+
if ( assetInfo.balance > 0 ) {
190+
const Obj = {
191+
assetId: BTC_ASSET_ID,
192+
recipientId: MASTER_UUID,
193+
traceId: newUserClient.getUUID(),
194+
amount: assetInfo.balance,
195+
memo: '',
196+
};
197+
console.log(Obj);
198+
newUserClient.transferFromBot(Obj);
199+
}
200+
```
201+
202+
读取Bitcoin的余额,来确认比特币是不是转成功了! 注意**newUserClient**是新用户的。
203+
```js
204+
const assetInfo = await newUserClient.getUserAsset(BTC_ASSET_ID);
205+
console.log("Bitcoin address is ", assetInfo.public_key);
206+
console.log("Bitcoin balance is ", assetInfo.balance);
207+
```
208+
### 如何将比特币存入你的冷钱包或者第三方交易所
209+
如果你希望将币存入你的冷钱包或者第三方交易所, 先要得到冷钱包或者你在第三方交易所的钱包地址,然后将钱包地址提交到Mixin Network.
210+
211+
- **要点提示**: 提现是需要支付收续费的,准备好比特币包地址!
212+
213+
#### 增加目的钱包地址到Mixin Network
214+
调用createAddress API, 将会返回一个address_id,下一步的提现操作会用到这个id。
215+
```js
216+
const withdrawAddress = await newUserClient.createWithdrawAddress({
217+
assetId: BTC_ASSET_ID,
218+
label: 'BTC withdraw',
219+
publicKey: BTC_WALLET_ADDR,
220+
});
221+
```
222+
这里的 **14T129GTbXXPGXXvZzVaNLRFPeHXD1C25C** 就是一个比特币钱包地址, 如下所示,提现费用是0.0025738 BTC, address_id 是"345855b5-56a5-4f3b-ba9e-d99601ef86c1".
223+
```bash
224+
Make your choose 9: BTC withdraw
225+
You choice to : { type: '9: BTC withdraw' }
226+
You wallet is : 0b10471b-1aed-3944-9eda-5ab947562761
227+
{ type: 'address',
228+
address_id: 'a513da38-a18a-4536-abe4-d1c29ca3a1a8',
229+
asset_id: 'c6d0c728-2624-429b-8e0d-d9d19b6592fa',
230+
public_key: '14T129GTbXXPGXXvZzVaNLRFPeHXD1C25C',
231+
label: 'BTC withdraw',
232+
account_name: '',
233+
account_tag: '',
234+
fee: '0.00212232',
235+
reserve: '0',
236+
dust: '0.0001',
237+
updated_at: '2019-04-04T02:20:42.552274992Z' }
238+
? Input you BTC amount:
239+
```
240+
241+
242+
#### 创建提现地址成功后,你可以用readAddress读取最新的提现费。
243+
```js
244+
const addressList = await newUserClient.getWithdrawAddress(BTC_ASSET_ID);
245+
console.log(addressList);
246+
```
247+
248+
#### 提交提现请求,Mixin Network会即时处理提现请求.
249+
提交提现请求到 Mixin Network, withdrawAddress.address_id 就是createAddress创建的。
250+
```js
251+
console.log(withdrawAddress);
252+
const prompts = [
253+
{
254+
name: 'amount',
255+
type: 'input',
256+
message: "Input you BTC amount: ",
257+
},
258+
];
259+
const answers = await inquirer.prompt(prompts);
260+
console.log(answers);
261+
const withdrawResult = await newUserClient.withdraw({
262+
addressId: withdrawAddress.address_id,
263+
assetId: BTC_ASSET_ID,
264+
amount: answers.amount,
265+
memo: 'withdraw by nodejs',
266+
});
267+
console.log(withdrawResult);
268+
```
269+
#### 可以通过blockchain explore来查看进度.
270+
271+
[完整的代码在这儿](https://github.com/wenewzhang/mixin_network-nodejs-bot2/blob/master/bitcoin-wallet-nodejs/bitcoin-wallet-nodejs.js)

0 commit comments

Comments
 (0)