stacks-archive / stacks-transactions-js

The JavaScript library for generating Stacks 2.0 transactions
19 stars 17 forks source link

fix: invalid private key length generated from `StacksPrivateKey.makeRandom` #41

Closed zone117x closed 4 years ago

zone117x commented 4 years ago

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:

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

Checklist