Some checks are pending
Build Release and Upload to TestFlight (iOS) / build (push) Waiting to run
Build Release and Upload to TestFlight (iOS) / testflight-upload (push) Blocked by required conditions
BuildReleaseApk / buildReleaseApk (push) Waiting to run
BuildReleaseApk / browserstack (push) Blocked by required conditions
57 lines
2.1 KiB
JavaScript
57 lines
2.1 KiB
JavaScript
import assert from 'assert';
|
|
import * as bitcoin from 'bitcoinjs-lib';
|
|
import ElectrumClient from 'electrum-client';
|
|
import { sha256 as _sha256 } from '@noble/hashes/sha256';
|
|
|
|
const net = require('net');
|
|
const tls = require('tls');
|
|
|
|
jest.setTimeout(150 * 1000);
|
|
|
|
const hardcodedPeers = [
|
|
{ host: 'electrum1.bluewallet.io', ssl: '443' },
|
|
{ host: 'electrum2.bluewallet.io', ssl: '443' },
|
|
{ host: 'electrum3.bluewallet.io', ssl: '443' },
|
|
];
|
|
|
|
function bitcoinjs_crypto_sha256(buffer /*: Buffer */) /*: Buffer */ {
|
|
return Buffer.from(_sha256(Uint8Array.from(buffer)));
|
|
}
|
|
|
|
describe('ElectrumClient', () => {
|
|
it('can connect and query', async () => {
|
|
for (const peer of hardcodedPeers) {
|
|
const mainClient = new ElectrumClient(net, tls, peer.ssl || peer.tcp, peer.host, peer.ssl ? 'tls' : 'tcp');
|
|
|
|
try {
|
|
await mainClient.connect();
|
|
await mainClient.server_version('2.7.11', '1.4');
|
|
} catch (e) {
|
|
mainClient.reconnect = mainClient.keepAlive = () => {}; // dirty hack to make it stop reconnecting
|
|
mainClient.close();
|
|
throw new Error('bad connection: ' + JSON.stringify(peer) + ' ' + e.message);
|
|
}
|
|
|
|
let addr4elect = 'bc1qwqdg6squsna38e46795at95yu9atm8azzmyvckulcc7kytlcckxswvvzej';
|
|
let script = bitcoin.address.toOutputScript(addr4elect);
|
|
let hash = bitcoinjs_crypto_sha256(script);
|
|
let reversedHash = Buffer.from(hash.reverse());
|
|
const start = +new Date();
|
|
let balance = await mainClient.blockchainScripthash_getBalance(reversedHash.toString('hex'));
|
|
const end = +new Date();
|
|
end - start > 1000 && console.warn(peer.host, 'took', (end - start) / 1000, 'seconds to fetch balance');
|
|
assert.ok(balance.confirmed > 0);
|
|
|
|
addr4elect = '3GCvDBAktgQQtsbN6x5DYiQCMmgZ9Yk8BK';
|
|
script = bitcoin.address.toOutputScript(addr4elect);
|
|
hash = bitcoinjs_crypto_sha256(script);
|
|
reversedHash = Buffer.from(hash.reverse());
|
|
balance = await mainClient.blockchainScripthash_getBalance(reversedHash.toString('hex'));
|
|
|
|
// let peers = await mainClient.serverPeers_subscribe();
|
|
// console.log(peers);
|
|
mainClient.close();
|
|
}
|
|
});
|
|
});
|