Skip to content

Commit

Permalink
Merge pull request #7 from cryptocoinjs/verify
Browse files Browse the repository at this point in the history
bip38: add verify and tests
  • Loading branch information
jprichardson committed Dec 23, 2014
2 parents 5986f7e + c51d7bb commit ce22e33
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 3 deletions.
30 changes: 30 additions & 0 deletions lib/bip38.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,34 @@ Bip38.prototype.decryptECMult = function(encData, passphrase) {
}
}

Bip38.prototype.verify = function(encryptedBase58) {
var decoded
try {
decoded = cs.decode(encryptedBase58)
} catch (e) {
return false
}

if (decoded.length !== 39) return false
if (decoded.readUInt8(0) !== 0x01) return false

var type = decoded.readUInt8(1)
var flag = decoded.readUInt8(2)

// encrypted WIF
if (type === 0x42) {
if (flag !== 0xc0 && flag !== 0xe0) return false

// EC mult
} else if (type === 0x43) {
if ((flag & ~0x24)) return false

} else {
return false
}


return true
}

module.exports = Bip38
24 changes: 23 additions & 1 deletion test/bip38.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,21 @@ describe('bip38', function() {
})
})

fixtures.invalid.forEach(function(f) {
fixtures.invalid.decrypt.forEach(function(f) {
it('should throw ' + f.description, function() {
assert.throws(function() {
bip38.decrypt(f.bip38, f.passphrase)
}, new RegExp(f.description, 'i'))
})
})

fixtures.invalid.verify.forEach(function(f) {
it('should throw because ' + f.description, function() {
assert.throws(function() {
bip38.decrypt(f.base58, 'foobar')
}, new RegExp(f.exception))
})
})
})

describe('encrypt', function() {
Expand All @@ -36,4 +44,18 @@ describe('bip38', function() {
})
})
})

describe('verify', function() {
fixtures.valid.forEach(function(f) {
it('should return true for ' + f.bip38, function() {
assert(bip38.verify(f.bip38))
})
})

fixtures.invalid.verify.forEach(function(f) {
it('should return false for ' + f.description, function() {
assert(!bip38.verify(f.base58))
})
})
})
})
49 changes: 47 additions & 2 deletions test/fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,51 @@
"sequence": 1
}
],
"invalid": [
]
"invalid": {
"decrypt": [],
"encrypt": [],
"verify": [
{
"description": "Invalid base58",
"exception": "Invalid checksum",
"base58": "6PgGWtx25kUg8QWvwuJAgorN6k9FbE25rv5dMRwu5SKMnfpfVe5marXXXX"
},
{
"description": "Length > 39",
"exception": "Invalid BIP38 data length",
"hex": "0142c000000000000000000000000000000000000000000000000000000000000000000000000000",
"base58": "QmxDezFMDL7ExfYmsETsQXAtBbw5YE1CDyA8pm1AGpMpVVUpsVy1yXv4VTL"
},
{
"description": "Length < 39",
"exception": "Invalid BIP38 data length",
"hex": "0142c00000000000000000000000000000000000000000000000000000000000000000000000",
"base58": "2DnNxWcx4Prn8wmjbkvtYGDALsq8BMWxQ33KnXkeH8vrxE41psDLXRmK3"
},
{
"description": "prefix !== 0x01",
"exception": "Invalid BIP38 prefix",
"hex": "0242c0000000000000000000000000000000000000000000000000000000000000000000000000",
"base58": "AfE1YY4Wr2FLAENaH9PVaLRdyk714V4rhwiJMSGyQCGFB3rhGDCs2R7c4s"
},
{
"description": "flag !== 0xc0 && flag !== 0xe0",
"exception": "Invalid BIP38 type",
"hex": "0101ff000000000000000000000000000000000000000000000000000000000000000000000000",
"base58": "5JjnYkbFBmUnhGeDMVhR7aSitLToe1odEfXDBeg4RMK6JmAm9g7rkm7qY3"
},
{
"description": "EC Mult: ~(flag & 0x24)",
"exception": "Invalid BIP38 type",
"hex": "0101db000000000000000000000000000000000000000000000000000000000000000000000000",
"base58": "5JbtdQFKSemRTqMuWrJgSfzE8AX2jdz1KiZuMmuUcv9iXha1s6UarQTciW"
},
{
"description": "EC Mult: ~(flag & 0x24)",
"exception": "Invalid BIP38 type",
"hex": "010135000000000000000000000000000000000000000000000000000000000000000000000000",
"base58": "5HyV7HSYdHUgLf7w36mxMHDPH9muTgUYHEj6cEogKMuV7ae8VRM3VEg56w"
}
]
}
}

0 comments on commit ce22e33

Please sign in to comment.