Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/bitpay/bitcore into named…
Browse files Browse the repository at this point in the history
…Testnets
  • Loading branch information
leolambo committed May 17, 2024
2 parents 8c859cc + 141a14a commit 53119ce
Show file tree
Hide file tree
Showing 23 changed files with 206 additions and 69 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"lerna": "2.9.1",
"version": "10.0.33",
"version": "10.0.35",
"packages": [
"packages/[^insight]*"
]
Expand Down
4 changes: 2 additions & 2 deletions packages/bitcore-client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/bitcore-client/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "bitcore-client",
"description": "Wallet client for Bitcore node",
"version": "10.0.33",
"version": "10.0.34",
"author": "Justin Langston <[email protected]>",
"main": "./ts_build/src/index.js",
"types": "./ts_build/src/index.d.ts",
Expand All @@ -25,7 +25,7 @@
"bcrypt": "5.1.0",
"bitcore-mnemonic": "^10.0.28",
"commander": "11.1.0",
"crypto-wallet-core": "^10.0.33",
"crypto-wallet-core": "^10.0.34",
"elliptic": "^6.5.3",
"ethers": "5.7.1",
"level-js": "4.0.2",
Expand Down
32 changes: 20 additions & 12 deletions packages/bitcore-client/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import requestStream from 'request';
import request from 'request-promise-native';
import * as secp256k1 from 'secp256k1';
import * as stream from 'stream';
import { URL } from 'url';
import * as utils from './utils';
let usingBrowser = (global as any).window;
const URLClass = usingBrowser ? usingBrowser.URL : URL;
const bitcoreLib = require('crypto-wallet-core').BitcoreLib;
Expand All @@ -19,13 +19,12 @@ export class Client {
return await request(params);
} catch (err) {
if (err.statusCode) {
throw new Error(`${err.statusCode} - ${params.url} - "${JSON.stringify(err.error)}"`);
throw new Error(`${err.statusCode} - ${params.method}: ${params.url} - "${JSON.stringify(err.error)}"`);
} else if (err.error instanceof Error) {
throw err.error;
}
throw err;
}

}

_buildQueryString( params: any) {
Expand Down Expand Up @@ -170,18 +169,27 @@ export class Client {
return result;
}

async importAddresses(params) {
async importAddresses(params: { pubKey: string; payload: Array<{ address: string }> }): Promise<void> {
const { payload, pubKey } = params;
const url = `${this.apiUrl}/wallet/${pubKey}`;
const signature = this.sign({ method: 'POST', url, payload });

return this._request({
method: 'POST',
url,
headers: { 'x-signature': signature },
body: payload,
json: true
});
for (let i = 0; i < payload.length; i += 100) {
const body = payload.slice(i, i + 100);
console.log(`Importing addresses ${i + 1}-${i + body.length} of ${payload.length}`);
if (i >= 100) {
await utils.sleep(1000); // cooldown
}

const signature = this.sign({ method: 'POST', url, payload: body });

await this._request({
method: 'POST',
url,
headers: { 'x-signature': signature },
body,
json: true
});
}
}

async broadcast(params) {
Expand Down
1 change: 1 addition & 0 deletions packages/bitcore-client/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const sleep = (ms) => new Promise(r => setTimeout(r, ms));
79 changes: 79 additions & 0 deletions packages/bitcore-client/test/unit/wallet.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import sinon from 'sinon';
import chai from 'chai';
import crypto from 'crypto';
import * as CWC from 'crypto-wallet-core';
import { Wallet, AddressTypes } from '../../src/wallet';
import { Client } from '../../src/client';
import * as utils from '../../src/utils';


const should = chai.should();
Expand Down Expand Up @@ -247,5 +249,82 @@ describe('Wallet', function() {
});
}
});

describe('importKeys', function() {
walletName = 'BitcoreClientTestImportKeys';
let requestStub;
let sleepStub;

beforeEach(async function() {
wallet = await Wallet.create({
name: walletName,
chain: 'BTC',
network: 'testnet',
phrase: 'snap impact summer because must pipe weasel gorilla actor acid web whip',
password: 'abc123',
storageType,
});
await wallet.unlock('abc123');
requestStub = sandbox.stub(wallet.client, '_request').resolves();
sleepStub = sandbox.stub(utils, 'sleep').resolves();
});

it('should import 1 key', async function() {
const keys = [];
for (let i = 0; i < 1; i++) {
const pk = crypto.randomBytes(32).toString('hex');
keys.push({
privKey: pk,
address: libMap.BTC.PrivateKey(pk).toAddress().toString()
});
}
await wallet.importKeys({
keys,
rederiveAddys: false
});

requestStub.callCount.should.equal(1);
sleepStub.callCount.should.equal(0);
requestStub.args.flatMap(arg => arg[0].body).should.deep.equal(keys.map(k => ({ address: k.address })));
});

it('should import <100 keys', async function() {
const keys = [];
for (let i = 0; i < 100; i++) {
const pk = crypto.randomBytes(32).toString('hex');
keys.push({
privKey: pk,
address: libMap.BTC.PrivateKey(pk).toAddress().toString()
});
}
await wallet.importKeys({
keys,
rederiveAddys: false
});

requestStub.callCount.should.equal(1);
sleepStub.callCount.should.equal(0);
requestStub.args.flatMap(arg => arg[0].body).should.deep.equal(keys.map(k => ({ address: k.address })));
});

it('should import >100 keys', async function() {
const keys = [];
for (let i = 0; i < 101; i++) {
const pk = crypto.randomBytes(32).toString('hex');
keys.push({
privKey: pk,
address: libMap.BTC.PrivateKey(pk).toAddress().toString()
});
}
await wallet.importKeys({
keys,
rederiveAddys: false
});

requestStub.callCount.should.equal(2);
sleepStub.callCount.should.equal(1);
requestStub.args.flatMap(arg => arg[0].body).should.deep.equal(keys.map(k => ({ address: k.address })));
});
});
});

10 changes: 5 additions & 5 deletions packages/bitcore-node/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions packages/bitcore-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"engines": {
"node": ">=18.0.0"
},
"version": "10.0.33",
"version": "10.0.35",
"author": "Justin Langston <[email protected]>",
"contributors": [
"Justin Langston <[email protected]>",
Expand Down Expand Up @@ -91,19 +91,19 @@
},
"dependencies": {
"abi-decoder": "2.4.0",
"bitcore-client": "^10.0.33",
"bitcore-client": "^10.0.34",
"bitcore-lib": "^10.0.28",
"bitcore-lib-cash": "^10.0.28",
"bitcore-lib-doge": "^10.0.32",
"bitcore-lib-ltc": "^10.0.28",
"bitcore-p2p": "^10.0.28",
"bitcore-p2p-cash": "^10.0.28",
"bitcore-p2p-doge": "^10.0.32",
"bitcore-wallet-client": "^10.0.33",
"bitcore-wallet-client": "^10.0.35",
"body-parser": "1.18.3",
"cors": "2.8.4",
"crypto-rpc": "https://github.com/bitpay/crypto-rpc.git#2c94b2a671021ff8c76d08233147fe197f0794de",
"crypto-wallet-core": "^10.0.33",
"crypto-wallet-core": "^10.0.34",
"heapdump": "0.3.15",
"http": "0.0.0",
"lodash": "4.17.11",
Expand Down
18 changes: 14 additions & 4 deletions packages/bitcore-node/src/providers/chain-state/evm/api/ecsp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,10 @@ export class BaseEVMExternalStateProvider extends InternalStateProvider implemen
}).bind(this);
const txStream = new NodeQueryStream(block?.transactions || [], getTransaction, args);
// stream results into response
await NodeQueryStream.onStream(txStream, req!, res!);
const result = await NodeQueryStream.onStream(txStream, req!, res!);
if (result?.success === false) {
logger.error('Error mid-stream (streamTransactions): %o', result.error);
}
} catch (err) {
logger.error('Error streaming transactions from historical node %o', err);
throw err;
Expand All @@ -220,14 +223,18 @@ export class BaseEVMExternalStateProvider extends InternalStateProvider implemen
const { tokenAddress } = args;
try {
// Calculate confirmations with tip height
let result;
const tip = await this.getLocalTip(params);
args.tipHeight = tip ? tip.height : 0;
if (!args.tokenAddress) {
const txStream = MoralisAPI.streamTransactionsByAddress({ chain, network, address, args });
await ExternalApiStream.onStream(txStream, req!, res!);
result = await ExternalApiStream.onStream(txStream, req!, res!);
} else {
const tokenTransfers = MoralisAPI.streamERC20TransactionsByAddress({ chain, network, address, tokenAddress, args });
await ExternalApiStream.onStream(tokenTransfers, req!, res!);
result = await ExternalApiStream.onStream(tokenTransfers, req!, res!);
}
if (result?.success === false) {
logger.error('Error mid-stream (streamAddressTransactions): %o', result.error);
}
} catch (err) {
logger.error('Error streaming address transactions from external provider: %o', err);
Expand Down Expand Up @@ -258,7 +265,10 @@ export class BaseEVMExternalStateProvider extends InternalStateProvider implemen
// Pipe all txStreams to the mergedStream
ExternalApiStream.mergeStreams(txStreams, mergedStream);
// Ensure mergeStream resolves
await _mergedStream;
const result = await _mergedStream;
if (result?.success === false) {
logger.error('Error mid-stream (streamWalletTransactions): %o', result.error);
}
} catch (err) {
logger.error('Error streaming wallet transactions from external provider: %o', err);
throw err;
Expand Down
Loading

0 comments on commit 53119ce

Please sign in to comment.