I've been running into intermittent invalid private key generation. The problem was a BN.js toString('hex') call not performing any padding.
The following test could trigger the error in the previous code:
test('Stacks key generation', () => {
for (let i = 0; i < 1000; i++) {
StacksPrivateKey.makeRandom();
}
});
This would often result in 63-char length keys like:
9aa98e0581c44ca257519215f3a8246fbe6f659dede388c147bacc8f2960986 vs the correct result:
09aa98e0581c44ca257519215f3a8246fbe6f659dede388c147bacc8f2960986
Also added error handling to several conditionals that would silently fail or result in hard to track down errors later on.
Type of Change
[ ] New feature
[x] Bug fix
[ ] API reference/documentation update
[ ] Other
Checklist
[x] Code is commented where needed
[x] Unit test coverage for new or modified code paths
[x] npm run test passes
[ ] Changelog is updated
[x] Tag 1 of @yknl, @zone117x, @reedrosenbluth for review
Description
I've been running into intermittent invalid private key generation. The problem was a BN.js
toString('hex')
call not performing any padding.The following test could trigger the error in the previous code:
This would often result in 63-char length keys like:
9aa98e0581c44ca257519215f3a8246fbe6f659dede388c147bacc8f2960986
vs the correct result:09aa98e0581c44ca257519215f3a8246fbe6f659dede388c147bacc8f2960986
Also added error handling to several conditionals that would silently fail or result in hard to track down errors later on.
Type of Change
Checklist
npm run test
passes