Skip to content

Commit 0624052

Browse files
Weston SiegenthalerWeston Siegenthaler
Weston Siegenthaler
authored and
Weston Siegenthaler
committed
Simple validation for intermediate codes
1 parent 9c10900 commit 0624052

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

src/bip38.js

+30-9
Original file line numberDiff line numberDiff line change
@@ -219,17 +219,13 @@ Bitcoin.BIP38 = (function () {
219219
* Creates new private key using an intermediate EC point.
220220
*/
221221
BIP38.newAddressFromIntermediate = function(intermediate, compressed) {
222-
// decode IPS
223-
var intermediateBytes = Bitcoin.Base58.decode(intermediate);
224-
var expectedChecksum = intermediateBytes.slice(49,53)
225-
var checksum = Bitcoin.Util.dsha256(intermediateBytes.slice(0, 49)).slice(0, 4)
226-
if (expectedChecksum[0] != checksum[0] ||
227-
expectedChecksum[1] != checksum[1] ||
228-
expectedChecksum[2] != checksum[2] ||
229-
expectedChecksum[3] != checksum[3]) {
230-
throw new Error("Invalid intermediate passphrase string");
222+
// validate intermediate code
223+
if (!BIP38.verifyIntermediate(intermediate)) {
224+
throw new Error("Invalid intermediate passphrase string");
231225
}
232226

227+
// decode IPS
228+
var intermediateBytes = Bitcoin.Base58.decode(intermediate);
233229
var noNumbers = (intermediateBytes[7] === 0x53);
234230
var ownerEntropy = intermediateBytes.slice(8, 8+8);
235231
var passpoint = intermediateBytes.slice(16, 16+33);
@@ -397,6 +393,31 @@ Bitcoin.BIP38 = (function () {
397393

398394
return { valid: valid, address: generatedAddress };
399395
}
396+
397+
/**
398+
* Checks the validity of an intermediate code.
399+
*/
400+
BIP38.verifyIntermediate = function (intermediate) {
401+
// Simple regex check
402+
var regexValid = (/^passphrase[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]+$/.test(intermediate));
403+
if (!regexValid) return false;
404+
405+
// Correct bytelen
406+
var intermediateBytes = Bitcoin.Base58.decode(intermediate);
407+
if (intermediateBytes.length != 53) return false;
408+
409+
// Checksum check
410+
var expectedChecksum = intermediateBytes.slice(49,53);
411+
var checksum = Bitcoin.Util.dsha256(intermediateBytes.slice(0, 49)).slice(0, 4);
412+
if (expectedChecksum[0] != checksum[0] ||
413+
expectedChecksum[1] != checksum[1] ||
414+
expectedChecksum[2] != checksum[2] ||
415+
expectedChecksum[3] != checksum[3]) {
416+
return false;
417+
}
418+
419+
return true;
420+
}
400421

401422
/**
402423
* Detects keys encrypted according to BIP-38 (58 base58 characters starting with 6P)

0 commit comments

Comments
 (0)