tests/ECPair: test fromPublic/fromPrivate in isolation
This commit is contained in:
parent
d8b66641b3
commit
85b1b92b6d
3 changed files with 52 additions and 34 deletions
|
@ -5,9 +5,6 @@ const types = require('./types')
|
||||||
const wif = require('wif')
|
const wif = require('wif')
|
||||||
|
|
||||||
const NETWORKS = require('./networks')
|
const NETWORKS = require('./networks')
|
||||||
|
|
||||||
// TODO: why is the function name toJSON weird?
|
|
||||||
function isPoint (x) { return ecc.isPoint(x) }
|
|
||||||
const isOptions = typeforce.maybe(typeforce.compile({
|
const isOptions = typeforce.maybe(typeforce.compile({
|
||||||
compressed: types.maybe(types.Boolean),
|
compressed: types.maybe(types.Boolean),
|
||||||
network: types.maybe(types.Network)
|
network: types.maybe(types.Network)
|
||||||
|
@ -57,7 +54,7 @@ function fromPrivateKey (buffer, options) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function fromPublicKey (buffer, options) {
|
function fromPublicKey (buffer, options) {
|
||||||
typeforce(isPoint, buffer)
|
typeforce(ecc.isPoint, buffer)
|
||||||
typeforce(isOptions, options)
|
typeforce(isOptions, options)
|
||||||
return new ECPair(null, buffer, options)
|
return new ECPair(null, buffer, options)
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,23 @@ const GROUP_ORDER = Buffer.from('fffffffffffffffffffffffffffffffebaaedce6af48a03
|
||||||
const GROUP_ORDER_LESS_1 = Buffer.from('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140', 'hex')
|
const GROUP_ORDER_LESS_1 = Buffer.from('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140', 'hex')
|
||||||
|
|
||||||
describe('ECPair', function () {
|
describe('ECPair', function () {
|
||||||
describe('constructor', function () {
|
describe('getPublicKey', function () {
|
||||||
|
let keyPair
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
keyPair = ECPair.fromPrivateKey(ONE)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('calls pointFromScalar lazily', hoodwink(function () {
|
||||||
|
assert.strictEqual(keyPair.__Q, null)
|
||||||
|
|
||||||
|
// .publicKey forces the memoization
|
||||||
|
assert.strictEqual(keyPair.publicKey.toString('hex'), '0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798')
|
||||||
|
assert.strictEqual(keyPair.__Q.toString('hex'), '0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798')
|
||||||
|
}))
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('fromPrivateKey', function () {
|
||||||
it('defaults to compressed', function () {
|
it('defaults to compressed', function () {
|
||||||
const keyPair = ECPair.fromPrivateKey(ONE)
|
const keyPair = ECPair.fromPrivateKey(ONE)
|
||||||
|
|
||||||
|
@ -49,8 +65,6 @@ describe('ECPair', function () {
|
||||||
fixtures.valid.forEach(function (f) {
|
fixtures.valid.forEach(function (f) {
|
||||||
it('derives public key for ' + f.WIF, function () {
|
it('derives public key for ' + f.WIF, function () {
|
||||||
const d = Buffer.from(f.d, 'hex')
|
const d = Buffer.from(f.d, 'hex')
|
||||||
console.log(d)
|
|
||||||
|
|
||||||
const keyPair = ECPair.fromPrivateKey(d, {
|
const keyPair = ECPair.fromPrivateKey(d, {
|
||||||
compressed: f.compressed
|
compressed: f.compressed
|
||||||
})
|
})
|
||||||
|
@ -59,37 +73,25 @@ describe('ECPair', function () {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
fixtures.invalid.constructor.forEach(function (f) {
|
fixtures.invalid.fromPrivateKey.forEach(function (f) {
|
||||||
it('throws ' + f.exception, function () {
|
it('throws ' + f.exception, function () {
|
||||||
if (f.d) {
|
const d = Buffer.from(f.d, 'hex')
|
||||||
const d = Buffer.from(f.d, 'hex')
|
assert.throws(function () {
|
||||||
assert.throws(function () {
|
ECPair.fromPrivateKey(d, f.options)
|
||||||
ECPair.fromPrivateKey(d, f.options)
|
}, new RegExp(f.exception))
|
||||||
}, new RegExp(f.exception))
|
|
||||||
} else {
|
|
||||||
const Q = Buffer.from(f.Q, 'hex')
|
|
||||||
assert.throws(function () {
|
|
||||||
ECPair.fromPublicKey(Q, f.options)
|
|
||||||
}, new RegExp(f.exception))
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('getPublicKey', function () {
|
describe('fromPublicKey', function () {
|
||||||
let keyPair
|
fixtures.invalid.fromPublicKey.forEach(function (f) {
|
||||||
|
it('throws ' + f.exception, function () {
|
||||||
beforeEach(function () {
|
const Q = Buffer.from(f.Q, 'hex')
|
||||||
keyPair = ECPair.fromPrivateKey(ONE)
|
assert.throws(function () {
|
||||||
|
ECPair.fromPublicKey(Q, f.options)
|
||||||
|
}, new RegExp(f.exception))
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('calls pointFromScalar lazily', hoodwink(function () {
|
|
||||||
assert.strictEqual(keyPair.__Q, null)
|
|
||||||
|
|
||||||
// .publicKey forces the memoization
|
|
||||||
assert.strictEqual(keyPair.publicKey.toString('hex'), '0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798')
|
|
||||||
assert.strictEqual(keyPair.__Q.toString('hex'), '0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798')
|
|
||||||
}))
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('fromWIF', function () {
|
describe('fromWIF', function () {
|
||||||
|
|
23
test/fixtures/ecpair.json
vendored
23
test/fixtures/ecpair.json
vendored
|
@ -66,7 +66,7 @@
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"invalid": {
|
"invalid": {
|
||||||
"constructor": [
|
"fromPrivateKey": [
|
||||||
{
|
{
|
||||||
"exception": "Private key not in range \\[1, n\\)",
|
"exception": "Private key not in range \\[1, n\\)",
|
||||||
"d": "0000000000000000000000000000000000000000000000000000000000000000"
|
"d": "0000000000000000000000000000000000000000000000000000000000000000"
|
||||||
|
@ -93,7 +93,26 @@
|
||||||
"network": {}
|
"network": {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
],
|
||||||
|
"fromPublicKey": [
|
||||||
|
{
|
||||||
|
"exception": "Expected isPoint, got Buffer",
|
||||||
|
"Q": "",
|
||||||
|
"options": {}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"exception": "Expected property \"network.messagePrefix\" of type Buffer|String, got undefined",
|
||||||
|
"Q": "044289801366bcee6172b771cf5a7f13aaecd237a0b9a1ff9d769cabc2e6b70a34cec320a0565fb7caf11b1ca2f445f9b7b012dda5718b3cface369ee3a034ded6",
|
||||||
|
"options": {
|
||||||
|
"network": {}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"description": "Bad X coordinate (== P)",
|
||||||
|
"exception": "Expected isPoint, got Buffer",
|
||||||
|
"Q": "040000000000000000000000000000000000000000000000000000000000000001fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f",
|
||||||
|
"options": {}
|
||||||
|
}
|
||||||
],
|
],
|
||||||
"fromWIF": [
|
"fromWIF": [
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Reference in a new issue