Skip to content

Commit

Permalink
v1.1.2:
Browse files Browse the repository at this point in the history
* use bip32 v3.0.1
* use tiny-secp256k1 v2.1.1 cjs
* add retry for backend requests
  • Loading branch information
doersf committed Nov 19, 2021
1 parent 6e0f1bf commit 30ea763
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 14 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "blockio-basic-multisig-sweep",
"version": "1.1.1",
"version": "1.1.2",
"description": "",
"main": "example.js",
"scripts": {
Expand All @@ -19,7 +19,8 @@
"dependencies": {
"bitcoinjs-lib": "^6.0.0",
"ecpair": "^1.0.1",
"bip32": "^2.0.6",
"bip32": "^3.0.1",
"tiny-secp256k1": "^2.1.1",
"chai": "^4.3.4",
"mocha": "^8.4.0",
"node-fetch": "^2.6.6"
Expand Down
1 change: 0 additions & 1 deletion src/services/AddressService.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const bitcoin = require('bitcoinjs-lib')
const ExtendedKeyService = require('./ExtendedKeyService')
const fetch = require('node-fetch')

const AddressService = function () { }

Expand Down
15 changes: 8 additions & 7 deletions src/services/ExtendedKeyService.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,27 @@
const bitcoin = require('bitcoinjs-lib')
const bip32 = require('bip32');
const ecpair = require('ecpair');
const ecc = require('tiny-secp256k1');
const ExtendedKeyService = function () { }

ExtendedKeyService.prototype.getKeyAtPath = (bip32PrivKey, network, i, derivationPath, chainCodeType) => {
// returns the key object at a given path

let pathParts = derivationPath.split('/')

// m
const extendedKey = bip32.fromBase58(bip32PrivKey, network)

// m
const extendedKey = bip32.default(ecc).fromBase58(bip32PrivKey, network)

if (chainCodeType === "nonstandard") { extendedKey.chainCode = Buffer.from(extendedKey.chainCode.toString('hex').replace(/^(00)+/,''), 'hex') }

// m/i
let child = extendedKey.derive(pathParts[1] === 'i' ? i : parseInt(pathParts[1]))

if (chainCodeType === "nonstandard") { child.chainCode = Buffer.from(child.chainCode.toString('hex').replace(/^(00)+/,''), 'hex') }

// m/i/j
let leaf = child.derive(pathParts[2] === 'i' ? i : parseInt(pathParts[2]))

return ecpair.ECPair.fromPrivateKey(leaf.privateKey, { network: network })

}
Expand Down
10 changes: 9 additions & 1 deletion src/services/ProviderService.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const constants = require('../constants')
const fetch = require('node-fetch')
const delay = ms => new Promise(resolve => setTimeout(resolve, ms))

const ProviderService = function (provider, network) {
const providerIndex = Object.values(constants.PROVIDERS).indexOf(provider)
Expand Down Expand Up @@ -105,7 +106,14 @@ module.exports = ProviderService

async function fetchUrl (url) {
try {
return await fetch(url)
let response = await fetch(url)
if (response.ok) {
return response;
} else {
console.log(" -- retrying in 10 seconds due to status = " + response.status);
await delay(10000);
return await fetchUrl(url);
}
} catch (err) {
throw new Error(err)
}
Expand Down
4 changes: 2 additions & 2 deletions src/sweeper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ const constants = require('./constants')
const networks = require('./networks')
const AddressService = require('./services/AddressService')
const ProviderService = require('./services/ProviderService')
const fetch = require('node-fetch')
const bitcoin = require('bitcoinjs-lib')
const ecpair = require('ecpair');
const bip32 = require('bip32');
const ecc = require('tiny-secp256k1');

function BlockIoSweep (network, bip32_private_key_1, private_key_2, destination_address, n, derivation_path, options) {
// TODO perform error checking on all these inputs
Expand Down Expand Up @@ -80,7 +80,7 @@ BlockIoSweep.prototype.begin = async function () {

let psbt = new bitcoin.Psbt({ network: this.networkObj })

const root = bip32.fromBase58(this.bip32PrivKey, this.networkObj)
const root = bip32.default(ecc).fromBase58(this.bip32PrivKey, this.networkObj)
let ecKeys = {}

let balToSweep = 0
Expand Down
3 changes: 2 additions & 1 deletion test/signature.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const bip32 = require('bip32')
const ecpair = require('ecpair')
const networks = require('../src/networks')
const constants = require('../src/constants')
const ecc = require('tiny-secp256k1')
const AddressService = require('../src/services/AddressService')

describe('Signatures', () => {
Expand All @@ -15,7 +16,7 @@ describe('Signatures', () => {
const derivationPath = 'm/i/0'
const path = 'm/0/0'
const payment = AddressService.generateAddresses(constants.P2SH, bip32Priv, ecpair.ECPair.fromWIF(privKey2, network).publicKey, network, 0, derivationPath)[0].payment
const hdRoot = bip32.fromBase58(bip32Priv, network)
const hdRoot = bip32.default(ecc).fromBase58(bip32Priv, network)
const masterFingerprint = hdRoot.fingerprint

const childNode = hdRoot.derivePath(path)
Expand Down

0 comments on commit 30ea763

Please sign in to comment.