From 05ca2c2c55a73d3beb15282923f69da66ede1cfc Mon Sep 17 00:00:00 2001 From: Matthew Zipkin Date: Mon, 14 Mar 2022 13:17:50 -0400 Subject: [PATCH] Fix: addresses being derived from change branch instead of receive --- build/hip2-server.js | 15 +++++++++++++-- src/derive.js | 15 +++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/build/hip2-server.js b/build/hip2-server.js index 5209dc3..4c0cd07 100644 --- a/build/hip2-server.js +++ b/build/hip2-server.js @@ -33,18 +33,29 @@ if (xpub === 'xpub6DBMpym6PM3qe7Ug7BwG6zo7dinMMjpk8nmb73czsjkzPTzfQ1d5ZvqDea4uNm throw new Error('Example xpub must not be used! Repalce with your own account xpub.'); const acct = HDPublicKey.fromBase58(xpub); + +// For some reason when bpkg'ed with -browser modules, we need to call +// this once as a throwaway before actually using it. +// The first function call FAILS because entropy can not be found to +// "pregenerate a random blinding value" as part of the ECDSA precomputation. +// For whatever reason, the function call succeeds from here on. +// This is either a bug in bcrypto or bpkg, exposing an inconguity +// with the -browser module. +// See https://github.com/handshake-org/hsd/issues/700 +acct.derive(0); + const recv = acct.derive(0); function addr() { const indexFile = path.join(__dirname, '..', 'log', 'hip2-index'); let index = 0; try { - index = parseInt(fs.readFileSync(indexFile)); + index = parseInt(fs.readFileSync(indexFile, 'utf-8')); } catch (e) { ; } - fs.writeFileSync(indexFile, index + 1); + fs.writeFileSync(indexFile, String(index + 1), 'utf-8'); // Wow, used all our non-hardened addresses! // Don't roll over the saved index (that way the user knows this has happened) diff --git a/src/derive.js b/src/derive.js index 820541d..071acc9 100644 --- a/src/derive.js +++ b/src/derive.js @@ -25,18 +25,29 @@ if (xpub === 'xpub6DBMpym6PM3qe7Ug7BwG6zo7dinMMjpk8nmb73czsjkzPTzfQ1d5ZvqDea4uNm throw new Error('Example xpub must not be used! Repalce with your own account xpub.'); const acct = HDPublicKey.fromBase58(xpub); + +// For some reason when bpkg'ed with -browser modules, we need to call +// this once as a throwaway before actually using it. +// The first function call FAILS because entropy can not be found to +// "pregenerate a random blinding value" as part of the ECDSA precomputation. +// For whatever reason, the function call succeeds from here on. +// This is either a bug in bcrypto or bpkg, exposing an inconguity +// with the -browser module. +// See https://github.com/handshake-org/hsd/issues/700 +acct.derive(0); + const recv = acct.derive(0); function addr() { const indexFile = path.join(__dirname, '..', 'log', 'hip2-index'); let index = 0; try { - index = parseInt(fs.readFileSync(indexFile)); + index = parseInt(fs.readFileSync(indexFile, 'utf-8')); } catch (e) { ; } - fs.writeFileSync(indexFile, index + 1); + fs.writeFileSync(indexFile, String(index + 1), 'utf-8'); // Wow, used all our non-hardened addresses! // Don't roll over the saved index (that way the user knows this has happened)