bitcore/test/keypair.js

179 lines
5.1 KiB
JavaScript
Raw Normal View History

2014-11-20 13:28:00 -08:00
'use strict';
2014-08-07 21:31:36 -07:00
var should = require('chai').should();
2014-11-20 13:28:00 -08:00
var bitcore = require('..');
2014-11-21 08:26:30 -08:00
var bn = bitcore.crypto.BN;
2014-11-20 13:28:00 -08:00
var Privkey = bitcore.Privkey;
var Pubkey = bitcore.Pubkey;
var Keypair = bitcore.Keypair;
2014-08-07 21:31:36 -07:00
describe('Keypair', function() {
2014-11-20 13:28:00 -08:00
2014-08-07 21:31:36 -07:00
it('should make a blank key', function() {
var key = new Keypair();
2014-08-07 21:31:36 -07:00
should.exist(key);
});
it('should make a key with a priv and pub', function() {
2014-08-09 17:43:24 -07:00
var priv = new Privkey();
var pub = new Pubkey();
2014-11-20 13:28:00 -08:00
var key = new Keypair({
privkey: priv,
pubkey: pub
});
2014-08-07 21:31:36 -07:00
should.exist(key);
2014-08-09 17:43:24 -07:00
should.exist(key.privkey);
should.exist(key.pubkey);
2014-08-07 21:31:36 -07:00
});
2014-11-20 13:28:00 -08:00
describe('#set', function() {
2014-08-28 16:18:36 -07:00
it('should make a new priv and pub', function() {
2014-11-20 13:28:00 -08:00
should.exist(new Keypair().set({
privkey: new Privkey()
}).privkey);
2014-08-28 16:18:36 -07:00
});
});
describe('#fromJSON', function() {
it('should make a keypair from this json', function() {
var privkey = Privkey().fromRandom();
var pubkey = Pubkey().fromPrivkey(privkey);
var keypair = Keypair().fromJSON({
privkey: privkey.toJSON(),
pubkey: pubkey.toJSON()
})
keypair.privkey.toString().should.equal(privkey.toString());
keypair.pubkey.toString().should.equal(pubkey.toString());
});
});
describe('#toJSON', function() {
it('should make json from this keypair', function() {
var json = Keypair().fromRandom().toJSON();
should.exist(json.privkey);
should.exist(json.pubkey);
var keypair = Keypair().fromJSON(json);
keypair.toJSON().privkey.toString().should.equal(json.privkey.toString());
keypair.toJSON().pubkey.toString().should.equal(json.pubkey.toString());
});
});
2014-08-29 12:43:55 -07:00
describe("#fromPrivkey", function() {
2014-11-20 13:28:00 -08:00
2014-08-29 12:43:55 -07:00
it('should make a new key from a privkey', function() {
should.exist(Keypair().fromPrivkey(Privkey().fromRandom()).pubkey);
2014-08-29 12:43:55 -07:00
});
});
2014-08-07 21:31:36 -07:00
describe("#fromRandom", function() {
2014-11-20 13:28:00 -08:00
2014-08-29 12:38:43 -07:00
it('should make a new priv and pub, should be compressed, mainnet', function() {
var key = new Keypair();
2014-08-07 21:31:36 -07:00
key.fromRandom();
2014-08-09 17:43:24 -07:00
should.exist(key.privkey);
should.exist(key.pubkey);
2014-08-13 12:23:06 -07:00
key.privkey.bn.gt(bn(0)).should.equal(true);
key.pubkey.point.getX().gt(bn(0)).should.equal(true);
key.pubkey.point.getY().gt(bn(0)).should.equal(true);
2014-08-29 12:38:43 -07:00
key.privkey.compressed.should.equal(true);
key.privkey.networkstr.should.equal('mainnet');
key.pubkey.compressed.should.equal(true);
2014-08-07 21:31:36 -07:00
});
});
describe("#fromString()", function() {
2014-11-20 13:28:00 -08:00
2014-08-07 21:31:36 -07:00
it('should recover a key creating with toString', function() {
var key = new Keypair();
2014-08-07 21:31:36 -07:00
key.fromRandom();
2014-08-09 17:43:24 -07:00
var priv = key.privkey;
var pub = key.pubkey;
2014-08-07 21:31:36 -07:00
var str = key.toString();
key.fromString(str);
2014-08-09 17:43:24 -07:00
should.exist(key.privkey);
should.exist(key.pubkey);
key.privkey.toString().should.equal(priv.toString());
key.pubkey.toString().should.equal(pub.toString());
2014-08-07 21:31:36 -07:00
});
2014-08-09 17:43:24 -07:00
it('should work with only Privkey set', function() {
var key = new Keypair();
2014-08-07 21:31:36 -07:00
key.fromRandom();
2014-08-09 17:43:24 -07:00
key.pubkey = undefined;
var priv = key.privkey;
2014-08-07 21:31:36 -07:00
var str = key.toString();
key.fromString(str);
2014-08-09 17:43:24 -07:00
should.exist(key.privkey);
key.privkey.toString().should.equal(priv.toString());
2014-08-07 21:31:36 -07:00
});
2014-08-09 17:43:24 -07:00
it('should work with only Pubkey set', function() {
var key = new Keypair();
2014-08-07 21:31:36 -07:00
key.fromRandom();
2014-08-09 17:43:24 -07:00
key.privkey = undefined;
var pub = key.pubkey;
2014-08-07 21:31:36 -07:00
var str = key.toString();
key.fromString(str);
2014-08-09 17:43:24 -07:00
should.exist(key.pubkey);
key.pubkey.toString().should.equal(pub.toString());
2014-08-07 21:31:36 -07:00
});
});
2014-08-09 17:43:24 -07:00
describe("#privkey2pubkey", function() {
2014-11-20 13:28:00 -08:00
2014-08-09 17:43:24 -07:00
it('should convert this known Privkey to known Pubkey', function() {
2014-08-07 21:31:36 -07:00
var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff';
var pubhex = '02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc';
var key = new Keypair();
2014-11-20 13:28:00 -08:00
key.privkey = new Privkey({
bn: bn(new Buffer(privhex, 'hex'))
});
2014-08-09 17:43:24 -07:00
key.privkey2pubkey();
key.pubkey.toString().should.equal(pubhex);
2014-08-07 21:31:36 -07:00
});
it('should convert this known Privkey to known Pubkey and preserve compressed=true', function() {
var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff';
var key = new Keypair();
2014-11-20 13:28:00 -08:00
key.privkey = new Privkey({
bn: bn(new Buffer(privhex, 'hex'))
});
key.privkey.compressed = true;
key.privkey2pubkey();
key.pubkey.compressed.should.equal(true);
});
it('should convert this known Privkey to known Pubkey and preserve compressed=true', function() {
var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff';
var key = new Keypair();
2014-11-20 13:28:00 -08:00
key.privkey = new Privkey({
bn: bn(new Buffer(privhex, 'hex'))
});
key.privkey.compressed = false;
key.privkey2pubkey();
key.pubkey.compressed.should.equal(false);
});
2014-08-07 21:31:36 -07:00
});
describe("#toString()", function() {
2014-11-20 13:28:00 -08:00
2014-08-07 21:31:36 -07:00
it('should exist', function() {
var key = new Keypair();
2014-08-07 21:31:36 -07:00
key.fromRandom();
should.exist(key.toString());
});
});
});