传送门
copay钱包(1.windows环境编译运行)
copay钱包(2.RestfulAPI初步分析)
copay钱包(3.转账功能报文分析)
copay钱包(4.bitcore-lib与bitcore-wallet-client类库修改)
copay钱包(5.助记词导出导入代码阅读)
经过几天的代码阅读,发现copay本身就是一个标准的移动钱包App,主要负责页面的展示和跳转,具体bitcoin部分都封装在bitcore-wallet-client里面,它又依赖bitcore-lib,所以整个客户端是这样的依赖
copay -> bitcore-wallet-client -> bitcore-lib
-
bitcore-lib:js的bitcoin类库
-
bitcore-wallet-client在lib的基础上再次封装,增加了和server端交互的功能.(与bitpay-wallet-service深度绑定)
-
copay:移动钱包App
bitcore-lib构建
阅读package,可见构建命令为
gulp browser
直接运行会出现如下错误:
bitcore-lib-make.png
windows下,可以把以下脚本增加到package.json里面:
构建bitcore-lib的js,需要在package.json的script里面加上
"publish": "sh ./node_modules/.bin/browserify --require ./index.js:bitcore-lib -o bitcore-lib.js"
这样运行
npm run publish
即可编译出bitcore-lib.js
bitcore-wallet-client构建
阅读package,可见构建命令为
browserify index.js -o bitcore-wallet-client.js
源码修改
构建了这两个js,但是实际的构建出来也没什么用.
bitcore-lib的index.js内容为
'use strict';
var bitcore = module.exports;
// module information
bitcore.version = 'v' + require('./package.json').version;
bitcore.versionGuard = function(version) {
if (version !== undefined) {
var message = 'More than one instance of bitcore-lib found. ' +
'Please make sure to require bitcore-lib and check that submodules do' +
' not also include their own bitcore-lib dependency.';
throw new Error(message);
}
};
bitcore.versionGuard(global._bitcore);
global._bitcore = bitcore.version;
// crypto
bitcore.crypto = {};
bitcore.crypto.BN = require('./lib/crypto/bn');
bitcore.crypto.ECDSA = require('./lib/crypto/ecdsa');
bitcore.crypto.Hash = require('./lib/crypto/hash');
bitcore.crypto.Random = require('./lib/crypto/random');
bitcore.crypto.Point = require('./lib/crypto/point');
bitcore.crypto.Signature = require('./lib/crypto/signature');
// encoding
bitcore.encoding = {};
bitcore.encoding.Base58 = require('./lib/encoding/base58');
bitcore.encoding.Base58Check = require('./lib/encoding/base58check');
bitcore.encoding.BufferReader = require('./lib/encoding/bufferreader');
bitcore.encoding.BufferWriter = require('./lib/encoding/bufferwriter');
bitcore.encoding.Varint = require('./lib/encoding/varint');
// utilities
bitcore.util = {};
bitcore.util.buffer = require('./lib/util/buffer');
bitcore.util.js = require('./lib/util/js');
bitcore.util.preconditions = require('./lib/util/preconditions');
// errors thrown by the library
bitcore.errors = require('./lib/errors');
// main bitcoin library
bitcore.Address = require('./lib/address');
bitcore.Block = require('./lib/block');
bitcore.MerkleBlock = require('./lib/block/merkleblock');
bitcore.BlockHeader = require('./lib/block/blockheader');
bitcore.HDPrivateKey = require('./lib/hdprivatekey.js');
bitcore.HDPublicKey = require('./lib/hdpublickey.js');
bitcore.Networks = require('./lib/networks');
bitcore.Opcode = require('./lib/opcode');
bitcore.PrivateKey = require('./lib/privatekey');
bitcore.PublicKey = require('./lib/publickey');
bitcore.Script = require('./lib/script');
bitcore.Transaction = require('./lib/transaction');
bitcore.URI = require('./lib/uri');
bitcore.Unit = require('./lib/unit');
// dependencies, subject to change
bitcore.deps = {};
bitcore.deps.bnjs = require('bn.js');
bitcore.deps.bs58 = require('bs58');
bitcore.deps.Buffer = Buffer;
bitcore.deps.elliptic = require('elliptic');
bitcore.deps._ = require('lodash');
// Internal usage, exposed for testing/advanced tweaking
bitcore.Transaction.sighash = require('./lib/transaction/sighash');
可见,bitcore是直接从lib目录构造出来的,并不用生成的bitcore-lib.js
同理:
bitcore-wallet-client的index.js内容为
var Client = require('./lib');
module.exports = Client;
// Errors thrown by the library
Client.errors = require('./lib/errors');
Client 对象也是从lib目录构造出来的,并不用生成的bitcore-wallet-client.js
因此,生成出目标js并没什么用处,直接修改lib内容既可影响到copay
测试源码修改
1.修改node_modules\bitcore-wallet-client\lib\api.js
API.prototype.createWallet = function (walletName, copayerName, m, n, opts, cb) {
// 增加调试日志
console.log("wallet-client custom change");
var self = this;
...
}
- 修改 node_modules\bitcore-lib\lib\privatekey.js
PrivateKey.fromString = PrivateKey.fromWIF = function (str) {
// 增加调试日志
console.log("bitcore lib custom change");
$.checkArgument(_.isString(str), 'First argument is expected to be a string.');
return new PrivateKey(str);
};
3.运行npm run start,启动copay
点击创建新的BTC钱包
create-testnet-wallet.png可看见两个lib修改的部分已经生效,打印出了我们修改的地方
custom-logs.png但是这两个lib位于node_modules,ionic就无法watch到文件的变化,修改后自动编译加载,目前只能每次都重启ionic,需要考虑一下怎么高效的修改和调试这两个lib库.
网友评论