2

我正在尝试使用 bitcore-lib 生成比特币地址并使用 bitcore-explorer 获取未使用的交易.. 生成地址这里是代码:

var bitcore = require('bitcore-lib');
var rand_buffer = bitcore.crypto.Random.getRandomBuffer(32);
var rand_number = bitcore.crypto.BN.fromBuffer(rand_buffer);
var privateKay = new bitcore.PrivateKey(rand_number);
var privateKeyWif = privateKay.toWIF();
var address = privateKay.toAddress('testnet');
console.log({
  rand_buffer:rand_buffer,
  rand_number_hex:rand_number,
  rand_number_dec:rand_number.toString(),
  privateKey:privateKay,
  privateKeyWif: privateKeyWif,
  address:address,
});

哪个工作正常......输出是:

{ rand_buffer: <Buffer 55 8b 27 c4 51 87 97 17 9a 7d 1d 72 48 26 e5 83 95 74 5b 3b b1 b4 b5 b6 a7 1c df 9f 18 e6 97 2e>,
  rand_number_hex: <BN: 558b27c4518797179a7d1d724826e58395745b3bb1b4b5b6a71cdf9f18e6972e>,
  rand_number_dec: '38692458332424984226826540178179935156087120588336482991409403810055901845294',
  privateKey: <PrivateKey: 558b27c4518797179a7d1d724826e58395745b3bb1b4b5b6a71cdf9f18e6972e, network: livenet>,
  privateKeyWif: 'Kz5zkBwfiYNkyswsKjot4wWmxHWUZdVMmxf65Z5wLk29ufhxnnQT',
  address: <Address: msTDjA4PmyePSWx2VcaQWoWoQ7gWzU2Kqx, type: pubkeyhash, network: testnet> }

在生成的地址上进行任何交易后,我需要使用 bitcore-explorers 所以我需要 bitcore-explorers 这里是代码:

var Insight = require('bitcore-explorers').Insight;
var insight = new Insight('testnet');
insight.getUnspentUtxos(address1,(error,utxos)=>{
  if(error) return console.log(error);
  console.log(utxos)
});

问题是当我需要 bitcore-explorers 时,它给了我以下错误:

D:\RAHEEL\Projects\gateway\node_modules\bitcore-explorers\node_modules\bitcore-lib\index.js:12
    throw new Error(message);
    ^

Error: 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.
    at Object.bitcore.versionGuard (D:\RAHEEL\Projects\gateway\node_modules\bitcore-explorers\node_modules\bitcore-lib\index.
js:12:11)
    at Object.<anonymous> (D:\RAHEEL\Projects\gateway\node_modules\bitcore-explorers\node_modules\bitcore-lib\index.js:15:9)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (D:\RAHEEL\Projects\gateway\node_modules\bitcore-explorers\lib\models\addressinfo.js:3:15)
4

3 回答 3

3

这里有一个临时解决方案。

~/bitcore-explorers/node_modules/bitcore-lib/index.js

第 7 行:

bitcore.versionGuard = function(version) {

将其更改为:

bitcore.versionGuard = function(version) { return;

但是,这个问题应该针对当前尚未修复的主分支进行修复。你可以在这里查看

于 2017-12-06T17:04:04.887 回答
2

是的,目前,这似乎仍在进行中(并且有争议)。它已在Github中多次提出

尽管要求略有不同,但我遇到了同样的问题:我使用的是bitocore-p2pnpm 包;当前版本为 1.1.2,需要bitcore-lib版本 0.14.0 作为依赖项。

我宁愿不打补丁bitcore-p2p/node_modules/bitcore-lib/index.js(根据此处和github bitcore issues中的另一个答案)。相反,在我的项目中,package.json我维护一个bitocore-p2p依赖项,然后引用它的(唯一的)v0.14.0bitcore-lib依赖项:

var p2p     = require('bitcore-p2p');                           //p2p exports
var p2pMod  = require.cache[require.resolve('bitcore-p2p')];    //p2p module
var bitcore = p2pMod.require('bitcore-lib');                    //p2p/bitcore-lib exports

或者可以使用一种更脆弱的方法:

var p2p     = require('bitcore-p2p');                           //p2p exports    
var bitcore = require('bitcore-p2p/node_modules/bitcore-lib');  //p2p/bitcore-lib exports

就我而言,这不是问题。但很明显,如果我需要说 0.16.0 版本,bitcore-lib我通常希望将其作为我项目的直接依赖项,然后遇到麻烦。

于 2018-12-17T11:15:34.187 回答
-1

我通过使用解决了这个问题

var bitcore = require('bitcore-explorers/node_modules/bitcore-lib');

因此,它现在不会产生任何错误。

于 2019-07-26T17:16:13.613 回答