2019-09-07 06:42:03 +02:00
|
|
|
import * as assert from 'assert';
|
|
|
|
import { beforeEach, describe, it } from 'mocha';
|
|
|
|
import * as proxyquire from 'proxyquire';
|
|
|
|
import { ECPair, ECPairInterface, networks as NETWORKS } from '..';
|
|
|
|
import * as fixtures from './fixtures/ecpair.json';
|
|
|
|
const hoodwink = require('hoodwink');
|
|
|
|
const tinysecp = require('tiny-secp256k1');
|
|
|
|
|
|
|
|
const NETWORKS_LIST = Object.values(NETWORKS);
|
|
|
|
const ZERO = Buffer.alloc(32, 0);
|
|
|
|
const ONE = Buffer.from(
|
|
|
|
'0000000000000000000000000000000000000000000000000000000000000001',
|
|
|
|
'hex',
|
|
|
|
);
|
|
|
|
const GROUP_ORDER = Buffer.from(
|
|
|
|
'fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141',
|
|
|
|
'hex',
|
|
|
|
);
|
|
|
|
const GROUP_ORDER_LESS_1 = Buffer.from(
|
|
|
|
'fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140',
|
|
|
|
'hex',
|
|
|
|
);
|
2018-05-22 08:33:43 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('ECPair', () => {
|
|
|
|
describe('getPublicKey', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
let keyPair: ECPairInterface;
|
2018-07-26 09:35:31 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
beforeEach(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
keyPair = ECPair.fromPrivateKey(ONE);
|
|
|
|
});
|
|
|
|
|
|
|
|
it(
|
|
|
|
'calls pointFromScalar lazily',
|
|
|
|
hoodwink(() => {
|
|
|
|
assert.strictEqual((keyPair as any).__Q, undefined);
|
|
|
|
|
|
|
|
// .publicKey forces the memoization
|
|
|
|
assert.strictEqual(
|
|
|
|
keyPair.publicKey.toString('hex'),
|
|
|
|
'0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
|
|
|
|
);
|
|
|
|
assert.strictEqual(
|
|
|
|
(keyPair as any).__Q.toString('hex'),
|
|
|
|
'0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
2018-07-26 09:35:31 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('fromPrivateKey', () => {
|
|
|
|
it('defaults to compressed', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const keyPair = ECPair.fromPrivateKey(ONE);
|
2014-10-17 04:31:01 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(keyPair.compressed, true);
|
|
|
|
});
|
2014-10-17 04:31:01 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('supports the uncompressed option', () => {
|
2018-06-25 08:37:45 +02:00
|
|
|
const keyPair = ECPair.fromPrivateKey(ONE, {
|
2019-09-07 06:42:03 +02:00
|
|
|
compressed: false,
|
|
|
|
});
|
2014-10-17 04:31:01 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(keyPair.compressed, false);
|
|
|
|
});
|
2014-10-17 04:31:01 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('supports the network option', () => {
|
2018-06-25 08:37:45 +02:00
|
|
|
const keyPair = ECPair.fromPrivateKey(ONE, {
|
2014-10-17 04:31:01 +02:00
|
|
|
compressed: false,
|
2019-09-07 06:42:03 +02:00
|
|
|
network: NETWORKS.testnet,
|
|
|
|
});
|
2014-10-17 04:31:01 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(keyPair.network, NETWORKS.testnet);
|
|
|
|
});
|
2014-10-17 04:31:01 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
fixtures.valid.forEach(f => {
|
|
|
|
it('derives public key for ' + f.WIF, () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const d = Buffer.from(f.d, 'hex');
|
2018-06-25 08:37:45 +02:00
|
|
|
const keyPair = ECPair.fromPrivateKey(d, {
|
2019-09-07 06:42:03 +02:00
|
|
|
compressed: f.compressed,
|
|
|
|
});
|
2014-10-17 04:31:01 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(keyPair.publicKey.toString('hex'), f.Q);
|
|
|
|
});
|
|
|
|
});
|
2014-10-17 04:31:01 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
fixtures.invalid.fromPrivateKey.forEach(f => {
|
|
|
|
it('throws ' + f.exception, () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const d = Buffer.from(f.d, 'hex');
|
2019-04-09 08:09:50 +02:00
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
ECPair.fromPrivateKey(d, (f as any).options);
|
|
|
|
}, new RegExp(f.exception));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-10-17 04:31:01 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('fromPublicKey', () => {
|
|
|
|
fixtures.invalid.fromPublicKey.forEach(f => {
|
|
|
|
it('throws ' + f.exception, () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const Q = Buffer.from(f.Q, 'hex');
|
2019-04-09 08:09:50 +02:00
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
ECPair.fromPublicKey(Q, (f as any).options);
|
|
|
|
}, new RegExp(f.exception));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-10-17 04:31:01 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('fromWIF', () => {
|
|
|
|
fixtures.valid.forEach(f => {
|
|
|
|
it('imports ' + f.WIF + ' (' + f.network + ')', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const network = (NETWORKS as any)[f.network];
|
|
|
|
const keyPair = ECPair.fromWIF(f.WIF, network);
|
2015-07-28 08:42:57 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(keyPair.privateKey!.toString('hex'), f.d);
|
|
|
|
assert.strictEqual(keyPair.compressed, f.compressed);
|
|
|
|
assert.strictEqual(keyPair.network, network);
|
|
|
|
});
|
|
|
|
});
|
2015-07-28 08:42:57 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
fixtures.valid.forEach(f => {
|
|
|
|
it('imports ' + f.WIF + ' (via list of networks)', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const keyPair = ECPair.fromWIF(f.WIF, NETWORKS_LIST);
|
2014-10-17 04:31:01 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(keyPair.privateKey!.toString('hex'), f.d);
|
|
|
|
assert.strictEqual(keyPair.compressed, f.compressed);
|
|
|
|
assert.strictEqual(keyPair.network, (NETWORKS as any)[f.network]);
|
|
|
|
});
|
|
|
|
});
|
2014-10-17 04:31:01 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
fixtures.invalid.fromWIF.forEach(f => {
|
|
|
|
it('throws on ' + f.WIF, () => {
|
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const networks = f.network
|
|
|
|
? (NETWORKS as any)[f.network]
|
|
|
|
: NETWORKS_LIST;
|
2016-02-25 03:26:05 +01:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
ECPair.fromWIF(f.WIF, networks);
|
|
|
|
}, new RegExp(f.exception));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-10-17 04:31:01 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('toWIF', () => {
|
|
|
|
fixtures.valid.forEach(f => {
|
|
|
|
it('exports ' + f.WIF, () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const keyPair = ECPair.fromWIF(f.WIF, NETWORKS_LIST);
|
|
|
|
const result = keyPair.toWIF();
|
|
|
|
assert.strictEqual(result, f.WIF);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-10-17 04:31:01 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('makeRandom', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const d = Buffer.alloc(32, 4);
|
|
|
|
const exWIF = 'KwMWvwRJeFqxYyhZgNwYuYjbQENDAPAudQx5VEmKJrUZcq6aL2pv';
|
2014-10-17 04:31:01 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('uses randombytes RNG', () => {
|
|
|
|
it('generates a ECPair', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const stub = {
|
|
|
|
randombytes: (): Buffer => {
|
|
|
|
return d;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const ProxiedECPair = proxyquire('../src/ecpair', stub);
|
|
|
|
|
|
|
|
const keyPair = ProxiedECPair.makeRandom();
|
|
|
|
assert.strictEqual(keyPair.toWIF(), exWIF);
|
|
|
|
});
|
|
|
|
});
|
2014-10-17 04:31:01 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('allows a custom RNG to be used', () => {
|
2018-06-25 08:37:45 +02:00
|
|
|
const keyPair = ECPair.makeRandom({
|
2019-09-07 06:42:03 +02:00
|
|
|
rng: (size): Buffer => {
|
|
|
|
return d.slice(0, size);
|
|
|
|
},
|
|
|
|
});
|
2014-10-17 04:31:01 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(keyPair.toWIF(), exWIF);
|
|
|
|
});
|
2015-09-08 15:39:31 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('retains the same defaults as ECPair constructor', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const keyPair = ECPair.makeRandom();
|
2016-10-10 04:01:51 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(keyPair.compressed, true);
|
|
|
|
assert.strictEqual(keyPair.network, NETWORKS.bitcoin);
|
|
|
|
});
|
2016-10-10 04:01:51 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('supports the options parameter', () => {
|
2018-06-25 08:37:45 +02:00
|
|
|
const keyPair = ECPair.makeRandom({
|
2016-10-10 04:01:51 +02:00
|
|
|
compressed: false,
|
2019-09-07 06:42:03 +02:00
|
|
|
network: NETWORKS.testnet,
|
|
|
|
});
|
2016-10-10 04:01:51 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(keyPair.compressed, false);
|
|
|
|
assert.strictEqual(keyPair.network, NETWORKS.testnet);
|
|
|
|
});
|
2016-10-10 04:01:51 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('throws if d is bad length', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
function rng(): Buffer {
|
|
|
|
return Buffer.alloc(28);
|
2018-03-20 03:19:39 +01:00
|
|
|
}
|
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
ECPair.makeRandom({ rng });
|
|
|
|
}, /Expected Buffer\(Length: 32\), got Buffer\(Length: 28\)/);
|
|
|
|
});
|
|
|
|
|
|
|
|
it(
|
|
|
|
'loops until d is within interval [1, n) : 1',
|
|
|
|
hoodwink(function(this: any): void {
|
|
|
|
const rng = this.stub(() => {
|
|
|
|
if (rng.calls === 0) return ZERO; // 0
|
|
|
|
return ONE; // >0
|
|
|
|
}, 2);
|
|
|
|
|
|
|
|
ECPair.makeRandom({ rng });
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
it(
|
|
|
|
'loops until d is within interval [1, n) : n - 1',
|
|
|
|
hoodwink(function(this: any): void {
|
|
|
|
const rng = this.stub(() => {
|
|
|
|
if (rng.calls === 0) return ZERO; // <1
|
|
|
|
if (rng.calls === 1) return GROUP_ORDER; // >n-1
|
|
|
|
return GROUP_ORDER_LESS_1; // n-1
|
|
|
|
}, 3);
|
|
|
|
|
|
|
|
ECPair.makeRandom({ rng });
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
2014-10-17 04:31:01 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('.network', () => {
|
|
|
|
fixtures.valid.forEach(f => {
|
|
|
|
it('returns ' + f.network + ' for ' + f.WIF, () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const network = (NETWORKS as any)[f.network];
|
|
|
|
const keyPair = ECPair.fromWIF(f.WIF, NETWORKS_LIST);
|
2015-09-21 09:37:21 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(keyPair.network, network);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-09-21 09:37:21 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('tinysecp wrappers', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
let keyPair: ECPairInterface;
|
|
|
|
let hash: Buffer;
|
|
|
|
let signature: Buffer;
|
2014-10-17 04:31:01 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
beforeEach(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
keyPair = ECPair.makeRandom();
|
|
|
|
hash = ZERO;
|
|
|
|
signature = Buffer.alloc(64, 1);
|
|
|
|
});
|
2014-10-17 04:31:01 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('signing', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
it(
|
|
|
|
'wraps tinysecp.sign',
|
|
|
|
hoodwink(function(this: any): void {
|
|
|
|
this.mock(
|
|
|
|
tinysecp,
|
|
|
|
'sign',
|
|
|
|
(h: any, d: any) => {
|
|
|
|
assert.strictEqual(h, hash);
|
|
|
|
assert.strictEqual(d, keyPair.privateKey);
|
|
|
|
return signature;
|
|
|
|
},
|
|
|
|
1,
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.strictEqual(keyPair.sign(hash), signature);
|
|
|
|
}),
|
|
|
|
);
|
2014-10-17 04:31:01 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('throws if no private key is found', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
delete (keyPair as any).__D;
|
2014-10-17 04:31:01 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
keyPair.sign(hash);
|
|
|
|
}, /Missing private key/);
|
|
|
|
});
|
|
|
|
});
|
2014-10-17 04:31:01 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('verify', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
it(
|
|
|
|
'wraps tinysecp.verify',
|
|
|
|
hoodwink(function(this: any): void {
|
|
|
|
this.mock(
|
|
|
|
tinysecp,
|
|
|
|
'verify',
|
|
|
|
(h: any, q: any, s: any) => {
|
|
|
|
assert.strictEqual(h, hash);
|
|
|
|
assert.strictEqual(q, keyPair.publicKey);
|
|
|
|
assert.strictEqual(s, signature);
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
1,
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.strictEqual(keyPair.verify(hash, signature), true);
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2019-04-15 08:28:01 +02:00
|
|
|
describe('optional low R signing', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const sig = Buffer.from(
|
|
|
|
'95a6619140fca3366f1d3b013b0367c4f86e39508a50fdce' +
|
|
|
|
'e5245fbb8bd60aa6086449e28cf15387cf9f85100bfd0838624ca96759e59f65c10a00' +
|
|
|
|
'16b86f5229',
|
|
|
|
'hex',
|
|
|
|
);
|
|
|
|
const sigLowR = Buffer.from(
|
|
|
|
'6a2660c226e8055afad317eeba918a304be79208d505' +
|
|
|
|
'3bc5ea4a5e4c5892b4a061c717c5284ae5202d721c0e49b4717b79966280906b1d3b52' +
|
|
|
|
'95d1fdde963c35',
|
|
|
|
'hex',
|
|
|
|
);
|
|
|
|
const lowRKeyPair = ECPair.fromWIF(
|
|
|
|
'L3nThUzbAwpUiBAjR5zCu66ybXSPMr2zZ3ikp' + 'ScpTPiYTxBynfZu',
|
|
|
|
);
|
|
|
|
const dataToSign = Buffer.from(
|
|
|
|
'b6c5c548a7f6164c8aa7af5350901626ebd69f9ae' + '2c1ecf8871f5088ec204cfe',
|
|
|
|
'hex',
|
|
|
|
);
|
2019-04-15 08:28:01 +02:00
|
|
|
|
|
|
|
it('signs with normal R by default', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const signed = lowRKeyPair.sign(dataToSign);
|
|
|
|
assert.deepStrictEqual(sig, signed);
|
|
|
|
});
|
2019-04-15 08:28:01 +02:00
|
|
|
|
|
|
|
it('signs with low R when true is passed', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const signed = lowRKeyPair.sign(dataToSign, true);
|
|
|
|
assert.deepStrictEqual(sigLowR, signed);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|