From 14a72211fbc2305b30b2dd30446be8ac030759e9 Mon Sep 17 00:00:00 2001 From: bip32JP Date: Fri, 2 Jan 2015 03:04:25 +0900 Subject: [PATCH] Pad privkey bytes with 0s I had a WIF privkey come up with a 5 instead of a L or K. The problem was the BigInteger created a 32 byte privkey with a "0x00" byte as msb. BigInteger's toByteArrayUnsigned() function does not account for that, so we must check the length and pad with 0 bytes to make 32 bytes. --- js/brainwallet.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/js/brainwallet.js b/js/brainwallet.js index 8ed3d7c..6fc671e 100644 --- a/js/brainwallet.js +++ b/js/brainwallet.js @@ -391,7 +391,11 @@ if( result.has_private_key ) { $("#derived_private_key").val(result.extended_private_key_string("base58")); - var bytes = [key_coin.private_prefix].concat(result.eckey.priv.toByteArrayUnsigned()).concat([1]); + var privkeyBytes = result.eckey.priv.toByteArrayUnsigned(); + while (privkeyBytes.length < 32) { + privkeyBytes.unshift(0); + }; + var bytes = [key_coin.private_prefix].concat(privkeyBytes).concat([1]); var checksum = Crypto.SHA256(Crypto.SHA256(bytes, {asBytes: true}), {asBytes: true}).slice(0, 4); $("#derived_private_key_wif").val(Bitcoin.Base58.encode(bytes.concat(checksum))) } else {