@abcpros/bitcore-wallet-client 中文文档教程
bitcore-wallet-client
Description
此软件包使用 REST API 与 BWS Bitcore 钱包服务 通信。 所有 REST 端点都被包装为简单的异步方法。 来自 BWS 的所有相关响应均由对等方独立检查,因此在与第三方 BWS 实例对话时使用此库非常重要。
请参阅 Bitcore-wallet,了解在 BWS 上中继并使用 bitcore-wallet-client 的简单 CLI 钱包实现。
Get Started
您可以通过 NPM 开始使用 bitcore-wallet-client:通过运行 npm install bitcore-wallet -client
来自您的控制台。
Example
启动您自己的本地 Bitcore 钱包服务 实例。 在此示例中,我们假设您在 localhost:3232
上运行了 bitcore-wallet-service
。
然后创建两个文件 irene.js
和 tomas.js
内容如下:
irene.js
var Client = require('bitcore-wallet-client');
var fs = require('fs');
var BWS_INSTANCE_URL = 'https://bws.bitpay.com/bws/api'
var client = new Client({
baseUrl: BWS_INSTANCE_URL,
verbose: false,
});
client.createWallet("My Wallet", "Irene", 2, 2, {network: 'testnet'}, function(err, secret) {
if (err) {
console.log('error: ',err);
return
};
// Handle err
console.log('Wallet Created. Share this secret with your copayers: ' + secret);
fs.writeFileSync('irene.dat', client.export());
});
tomas.js
var Client = require('bitcore-wallet-client');
var fs = require('fs');
var BWS_INSTANCE_URL = 'https://bws.bitpay.com/bws/api'
var secret = process.argv[2];
if (!secret) {
console.log('./tomas.js <Secret>')
process.exit(0);
}
var client = new Client({
baseUrl: BWS_INSTANCE_URL,
verbose: false,
});
client.joinWallet(secret, "Tomas", {}, function(err, wallet) {
if (err) {
console.log('error: ', err);
return
};
console.log('Joined ' + wallet.name + '!');
fs.writeFileSync('tomas.dat', client.export());
client.openWallet(function(err, ret) {
if (err) {
console.log('error: ', err);
return
};
console.log('\n\n** Wallet Info', ret); //TODO
console.log('\n\nCreating first address:', ret); //TODO
if (ret.wallet.status == 'complete') {
client.createAddress({}, function(err,addr){
if (err) {
console.log('error: ', err);
return;
};
console.log('\nReturn:', addr)
});
}
});
});
Install bitcore-wallet-client
before start:
npm i bitcore-wallet-client
Create a new wallet使用第一个脚本:
$ node irene.js
info Generating new keys
Wallet Created. Share this secret with your copayers: JbTDjtUkvWS4c3mgAtJf4zKyRGzdQzZacfx2S7gRqPLcbeAWaSDEnazFJF6mKbzBvY1ZRwZCbvT
使用生成的秘密加入此钱包:
$ node tomas.js JbTDjtUkvWS4c3mgAtJf4zKyRGzdQzZacfx2S7gRqPLcbeAWaSDEnazFJF6mKbzBvY1ZRwZCbvT
Joined My Wallet!
Wallet Info: [...]
Creating first address:
Return: [...]
请注意,脚本创建了两个名为 irene.dat
和 tomas.dat
的文件。 使用这些文件,您可以获取状态、生成地址、创建提案、签署交易等。
bitcore-wallet-client
The official client library for bitcore-wallet-service.
Description
This package communicates with BWS Bitcore wallet service using the REST API. All REST endpoints are wrapped as simple async methods. All relevant responses from BWS are checked independently by the peers, thus the importance of using this library when talking to a third party BWS instance.
See Bitcore-wallet for a simple CLI wallet implementation that relays on BWS and uses bitcore-wallet-client.
Get Started
You can start using bitcore-wallet-client via NPM: by running npm install bitcore-wallet-client
from your console.
Example
Start your own local Bitcore wallet service instance. In this example we assume you have bitcore-wallet-service
running on your localhost:3232
.
Then create two files irene.js
and tomas.js
with the content below:
irene.js
var Client = require('bitcore-wallet-client');
var fs = require('fs');
var BWS_INSTANCE_URL = 'https://bws.bitpay.com/bws/api'
var client = new Client({
baseUrl: BWS_INSTANCE_URL,
verbose: false,
});
client.createWallet("My Wallet", "Irene", 2, 2, {network: 'testnet'}, function(err, secret) {
if (err) {
console.log('error: ',err);
return
};
// Handle err
console.log('Wallet Created. Share this secret with your copayers: ' + secret);
fs.writeFileSync('irene.dat', client.export());
});
tomas.js
var Client = require('bitcore-wallet-client');
var fs = require('fs');
var BWS_INSTANCE_URL = 'https://bws.bitpay.com/bws/api'
var secret = process.argv[2];
if (!secret) {
console.log('./tomas.js <Secret>')
process.exit(0);
}
var client = new Client({
baseUrl: BWS_INSTANCE_URL,
verbose: false,
});
client.joinWallet(secret, "Tomas", {}, function(err, wallet) {
if (err) {
console.log('error: ', err);
return
};
console.log('Joined ' + wallet.name + '!');
fs.writeFileSync('tomas.dat', client.export());
client.openWallet(function(err, ret) {
if (err) {
console.log('error: ', err);
return
};
console.log('\n\n** Wallet Info', ret); //TODO
console.log('\n\nCreating first address:', ret); //TODO
if (ret.wallet.status == 'complete') {
client.createAddress({}, function(err,addr){
if (err) {
console.log('error: ', err);
return;
};
console.log('\nReturn:', addr)
});
}
});
});
Install bitcore-wallet-client
before start:
npm i bitcore-wallet-client
Create a new wallet with the first script:
$ node irene.js
info Generating new keys
Wallet Created. Share this secret with your copayers: JbTDjtUkvWS4c3mgAtJf4zKyRGzdQzZacfx2S7gRqPLcbeAWaSDEnazFJF6mKbzBvY1ZRwZCbvT
Join to this wallet with generated secret:
$ node tomas.js JbTDjtUkvWS4c3mgAtJf4zKyRGzdQzZacfx2S7gRqPLcbeAWaSDEnazFJF6mKbzBvY1ZRwZCbvT
Joined My Wallet!
Wallet Info: [...]
Creating first address:
Return: [...]
Note that the scripts created two files named irene.dat
and tomas.dat
. With these files you can get status, generate addresses, create proposals, sign transactions, etc.