傳送門
copay錢包(1.windows環(huán)境編譯運行)
copay錢包(2.RestfulAPI初步分析)
copay錢包(3.轉賬功能報文分析)
copay錢包(4.bitcore-lib與bitcore-wallet-client類庫修改)
copay錢包(5.助記詞導出導入代碼閱讀)
經(jīng)過幾天的代碼閱讀,發(fā)現(xiàn)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
直接運行會出現(xiàn)如下錯誤:
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
點擊創(chuàng)建新的BTC錢包
可看見兩個lib修改的部分已經(jīng)生效,打印出了我們修改的地方
但是這兩個lib位于node_modules,ionic就無法watch到文件的變化,修改后自動編譯加載,目前只能每次都重啟ionic,需要考慮一下怎么高效的修改和調試這兩個lib庫.