Key -> Keypair

"Keypair" is a more explanatory name, and also should be less confused with
other kinds of keys (particularly "cipher keys", which are the keys used in
symmetric block ciphers, especially AES).
This commit is contained in:
Ryan X. Charles 2014-08-29 14:18:56 -07:00
parent 40a2007e18
commit 32fabd0c0f
14 changed files with 138 additions and 139 deletions

View File

@ -12,7 +12,7 @@ bitcore.Constants = require('./lib/constants');
bitcore.ECDSA = require('./lib/ecdsa'); bitcore.ECDSA = require('./lib/ecdsa');
bitcore.Hash = require('./lib/hash'); bitcore.Hash = require('./lib/hash');
bitcore.KDF = require('./lib/kdf'); bitcore.KDF = require('./lib/kdf');
bitcore.Key = require('./lib/key'); bitcore.Keypair = require('./lib/keypair');
bitcore.Message = require('./lib/message'); bitcore.Message = require('./lib/message');
bitcore.Point = require('./lib/point'); bitcore.Point = require('./lib/point');
bitcore.Privkey = require('./lib/privkey'); bitcore.Privkey = require('./lib/privkey');

View File

@ -1,6 +1,6 @@
var base58 = require('./base58'); var base58 = require('./base58');
var Hash = require('./hash'); var Hash = require('./hash');
var Key = require('./key'); var Keypair = require('./keypair');
var Pubkey = require('./pubkey'); var Pubkey = require('./pubkey');
var Privkey = require('./privkey'); var Privkey = require('./privkey');
var Point = require('./point'); var Point = require('./point');
@ -21,7 +21,7 @@ BIP32.prototype.fromRandom = function(networkstr) {
this.parentFingerprint = new Buffer([0, 0, 0, 0]); this.parentFingerprint = new Buffer([0, 0, 0, 0]);
this.childIndex = new Buffer([0, 0, 0, 0]); this.childIndex = new Buffer([0, 0, 0, 0]);
this.chainCode = Random.getRandomBuffer(32); this.chainCode = Random.getRandomBuffer(32);
this.key = (new Key()).fromRandom(); this.key = (new Keypair()).fromRandom();
this.hasPrivateKey = true; this.hasPrivateKey = true;
this.pubKeyHash = Hash.sha256ripemd160(this.key.pubkey.toBuffer()); this.pubKeyHash = Hash.sha256ripemd160(this.key.pubkey.toBuffer());
this.buildExtendedPublicKey(); this.buildExtendedPublicKey();
@ -63,7 +63,7 @@ BIP32.prototype.fromSeed = function(bytes, networkstr) {
this.childIndex = new Buffer([0, 0, 0, 0]); this.childIndex = new Buffer([0, 0, 0, 0]);
this.chainCode = hash.slice(32, 64); this.chainCode = hash.slice(32, 64);
this.version = constants[networkstr].bip32privkey; this.version = constants[networkstr].bip32privkey;
this.key = new Key(); this.key = new Keypair();
this.key.privkey = new Privkey({bn: bn.fromBuffer(hash.slice(0, 32))}); this.key.privkey = new Privkey({bn: bn.fromBuffer(hash.slice(0, 32))});
this.key.privkey2pubkey(); this.key.privkey2pubkey();
this.hasPrivateKey = true; this.hasPrivateKey = true;
@ -97,13 +97,13 @@ BIP32.prototype.initFromBytes = function(bytes) {
this.version == constants.testnet.bip32pubkey); this.version == constants.testnet.bip32pubkey);
if (isPrivate && keyBytes[0] == 0) { if (isPrivate && keyBytes[0] == 0) {
this.key = new Key(); this.key = new Keypair();
this.key.privkey = new Privkey({bn: bn.fromBuffer(keyBytes.slice(1, 33))}); this.key.privkey = new Privkey({bn: bn.fromBuffer(keyBytes.slice(1, 33))});
this.key.privkey2pubkey(); this.key.privkey2pubkey();
this.pubKeyHash = Hash.sha256ripemd160(this.key.pubkey.toBuffer()); this.pubKeyHash = Hash.sha256ripemd160(this.key.pubkey.toBuffer());
this.hasPrivateKey = true; this.hasPrivateKey = true;
} else if (isPublic && (keyBytes[0] == 0x02 || keyBytes[0] == 0x03)) { } else if (isPublic && (keyBytes[0] == 0x02 || keyBytes[0] == 0x03)) {
this.key = new Key(); this.key = new Keypair();
this.key.pubkey = (new Pubkey()).fromDER(keyBytes); this.key.pubkey = (new Pubkey()).fromDER(keyBytes);
this.pubKeyHash = Hash.sha256ripemd160(this.key.pubkey.toBuffer()); this.pubKeyHash = Hash.sha256ripemd160(this.key.pubkey.toBuffer());
this.hasPrivateKey = false; this.hasPrivateKey = false;
@ -272,7 +272,7 @@ BIP32.prototype.deriveChild = function(i) {
ret = new BIP32(); ret = new BIP32();
ret.chainCode = ir; ret.chainCode = ir;
ret.key = new Key(); ret.key = new Keypair();
ret.key.privkey = new Privkey({bn: k}); ret.key.privkey = new Privkey({bn: k});
ret.key.privkey2pubkey(); ret.key.privkey2pubkey();
ret.hasPrivateKey = true; ret.hasPrivateKey = true;
@ -293,7 +293,7 @@ BIP32.prototype.deriveChild = function(i) {
ret = new BIP32(); ret = new BIP32();
ret.chainCode = ir; ret.chainCode = ir;
var key = new Key(); var key = new Keypair();
key.pubkey = newpub; key.pubkey = newpub;
ret.key = key; ret.key = key;
ret.hasPrivateKey = false; ret.hasPrivateKey = false;

View File

@ -1,7 +1,7 @@
var BN = require('./bn'); var BN = require('./bn');
var Point = require('./point'); var Point = require('./point');
var Signature = require('./signature'); var Signature = require('./signature');
var Key = require('./key'); var Keypair = require('./keypair');
var Pubkey = require('./pubkey'); var Pubkey = require('./pubkey');
var Random = require('./random'); var Random = require('./random');
@ -45,7 +45,7 @@ ECDSA.prototype.fromString = function(str) {
if (obj.hashbuf) if (obj.hashbuf)
this.hashbuf = new Buffer(obj.hashbuf, 'hex'); this.hashbuf = new Buffer(obj.hashbuf, 'hex');
if (obj.key) if (obj.key)
this.key = Key().fromString(obj.key); this.key = Keypair().fromString(obj.key);
if (obj.sig) if (obj.sig)
this.sig = Signature().fromString(obj.sig); this.sig = Signature().fromString(obj.sig);
if (obj.k) if (obj.k)

View File

@ -1,5 +1,5 @@
var AESCBC = require('./aescbc'); var AESCBC = require('./aescbc');
var Key = require('../key'); var Keypair = require('../keypair');
var Point = require('../point'); var Point = require('../point');
var Hash = require('../hash'); var Hash = require('../hash');
var Pubkey = require('../pubkey'); var Pubkey = require('../pubkey');
@ -10,12 +10,12 @@ var ECIES = function ECIES() {
return new ECIES(); return new ECIES();
}; };
ECIES.encrypt = function(messagebuf, tokey, fromkey, ivbuf) { ECIES.encrypt = function(messagebuf, tokeypair, fromkeypair, ivbuf) {
var r = fromkey.privkey.bn; var r = fromkeypair.privkey.bn;
var R = fromkey.pubkey.point; var R = fromkeypair.pubkey.point;
var Rpubkey = fromkey.pubkey; var Rpubkey = fromkeypair.pubkey;
var Rbuf = Rpubkey.toDER(true); var Rbuf = Rpubkey.toDER(true);
var KB = tokey.pubkey.point; var KB = tokeypair.pubkey.point;
var P = KB.mul(r); var P = KB.mul(r);
var S = P.getX(); var S = P.getX();
var Sbuf = S.toBuffer({size: 32}); var Sbuf = S.toBuffer({size: 32});
@ -28,8 +28,8 @@ ECIES.encrypt = function(messagebuf, tokey, fromkey, ivbuf) {
return encbuf; return encbuf;
}; };
ECIES.decrypt = function(encbuf, tokey) { ECIES.decrypt = function(encbuf, tokeypair) {
var kB = tokey.privkey.bn; var kB = tokeypair.privkey.bn;
var frompubkey = Pubkey().fromDER(encbuf.slice(0, 33)); var frompubkey = Pubkey().fromDER(encbuf.slice(0, 33));
var R = frompubkey.point; var R = frompubkey.point;
var P = R.mul(kB); var P = R.mul(kB);

View File

@ -1,4 +1,4 @@
var Key = require('../key'); var Keypair = require('../keypair');
var Privkey = require('../privkey'); var Privkey = require('../privkey');
var Pubkey = require('../pubkey'); var Pubkey = require('../pubkey');
var Point = require('../point'); var Point = require('../point');
@ -14,8 +14,8 @@ var Stealth = function Stealth(obj) {
}; };
Stealth.prototype.set = function(obj) { Stealth.prototype.set = function(obj) {
this.payloadKey = obj.payloadKey || this.payloadKey; this.payloadKeypair = obj.payloadKeypair || this.payloadKeypair;
this.scanKey = obj.scanKey || this.scanKey; this.scanKeypair = obj.scanKeypair || this.scanKeypair;
return this; return this;
}; };
@ -27,9 +27,9 @@ Stealth.prototype.fromAddressBuffer = function(buf) {
var sPubBuf = buf.slice(33, 66); var sPubBuf = buf.slice(33, 66);
var payloadPubkey = Pubkey().fromDER(pPubBuf); var payloadPubkey = Pubkey().fromDER(pPubBuf);
this.payloadKey = Key({pubkey: payloadPubkey}); this.payloadKeypair = Keypair({pubkey: payloadPubkey});
var scanPubkey = Pubkey().fromDER(sPubBuf); var scanPubkey = Pubkey().fromDER(sPubBuf);
this.scanKey = Key({pubkey: scanPubkey}); this.scanKeypair = Keypair({pubkey: scanPubkey});
return this; return this;
}; };
@ -42,48 +42,48 @@ Stealth.prototype.fromAddressString = function(str) {
}; };
Stealth.prototype.fromRandom = function() { Stealth.prototype.fromRandom = function() {
this.payloadKey = Key().fromRandom(); this.payloadKeypair = Keypair().fromRandom();
this.scanKey = Key().fromRandom(); this.scanKeypair = Keypair().fromRandom();
return this; return this;
}; };
Stealth.prototype.getSharedKeyAsReceiver = function(senderPubkey) { Stealth.prototype.getSharedKeypairAsReceiver = function(senderPubkey) {
var sharedSecretPoint = senderPubkey.point.mul(this.scanKey.privkey.bn); var sharedSecretPoint = senderPubkey.point.mul(this.scanKeypair.privkey.bn);
var sharedSecretPubkey = Pubkey({point: sharedSecretPoint}); var sharedSecretPubkey = Pubkey({point: sharedSecretPoint});
var buf = sharedSecretPubkey.toDER(true); var buf = sharedSecretPubkey.toDER(true);
var sharedKey = KDF.sha256hmac2key(buf); var sharedKeypair = KDF.sha256hmac2keypair(buf);
return sharedKey; return sharedKeypair;
}; };
Stealth.prototype.getSharedKeyAsSender = function(senderKey) { Stealth.prototype.getSharedKeypairAsSender = function(senderKeypair) {
var sharedSecretPoint = this.scanKey.pubkey.point.mul(senderKey.privkey.bn); var sharedSecretPoint = this.scanKeypair.pubkey.point.mul(senderKeypair.privkey.bn);
var sharedSecretPubkey = Pubkey({point: sharedSecretPoint}); var sharedSecretPubkey = Pubkey({point: sharedSecretPoint});
var buf = sharedSecretPubkey.toDER(true); var buf = sharedSecretPubkey.toDER(true);
var sharedKey = KDF.sha256hmac2key(buf); var sharedKeypair = KDF.sha256hmac2keypair(buf);
return sharedKey; return sharedKeypair;
}; };
Stealth.prototype.getReceivePubkeyAsReceiver = function(senderPubkey) { Stealth.prototype.getReceivePubkeyAsReceiver = function(senderPubkey) {
var sharedKey = this.getSharedKeyAsReceiver(senderPubkey); var sharedKeypair = this.getSharedKeypairAsReceiver(senderPubkey);
var pubkey = Pubkey({point: this.payloadKey.pubkey.point.add(sharedKey.pubkey.point)}); var pubkey = Pubkey({point: this.payloadKeypair.pubkey.point.add(sharedKeypair.pubkey.point)});
return pubkey; return pubkey;
}; };
Stealth.prototype.getReceivePubkeyAsSender = function(senderKey) { Stealth.prototype.getReceivePubkeyAsSender = function(senderKeypair) {
var sharedKey = this.getSharedKeyAsSender(senderKey); var sharedKeypair = this.getSharedKeypairAsSender(senderKeypair);
var pubkey = Pubkey({point: this.payloadKey.pubkey.point.add(sharedKey.pubkey.point)}); var pubkey = Pubkey({point: this.payloadKeypair.pubkey.point.add(sharedKeypair.pubkey.point)});
return pubkey; return pubkey;
}; };
Stealth.prototype.getReceiveKey = function(senderPubkey) { Stealth.prototype.getReceiveKeypair = function(senderPubkey) {
var sharedKey = this.getSharedKeyAsReceiver(senderPubkey); var sharedKeypair = this.getSharedKeypairAsReceiver(senderPubkey);
var privkey = Privkey({bn: this.payloadKey.privkey.bn.add(sharedKey.privkey.bn).mod(Point.getN())}); var privkey = Privkey({bn: this.payloadKeypair.privkey.bn.add(sharedKeypair.privkey.bn).mod(Point.getN())});
var key = Key({privkey: privkey}); var key = Keypair({privkey: privkey});
key.privkey2pubkey(); key.privkey2pubkey();
return key; return key;
@ -101,8 +101,8 @@ Stealth.prototype.isForMe = function(senderPubkey, myPossiblePubkeyhash) {
}; };
Stealth.prototype.toAddressBuffer = function() { Stealth.prototype.toAddressBuffer = function() {
var pBuf = this.payloadKey.pubkey.toDER(true); var pBuf = this.payloadKeypair.pubkey.toDER(true);
var sBuf = this.scanKey.pubkey.toDER(true); var sBuf = this.scanKeypair.pubkey.toDER(true);
return Buffer.concat([pBuf, sBuf]); return Buffer.concat([pBuf, sBuf]);
}; };

View File

@ -2,21 +2,20 @@ var Bn = require('./bn');
var Privkey = require('./privkey'); var Privkey = require('./privkey');
var Point = require('./point'); var Point = require('./point');
var Pubkey = require('./pubkey'); var Pubkey = require('./pubkey');
var Key = require('./key'); var Keypair = require('./keypair');
var Hash = require('./hash'); var Hash = require('./hash');
function KDF() { function KDF() {
}; };
KDF.buf2key = function(buf) { KDF.buf2keypair = function(buf) {
return KDF.sha256hmac2key(buf); return KDF.sha256hmac2keypair(buf);
}; };
KDF.sha256hmac2key = function(buf) { KDF.sha256hmac2keypair = function(buf) {
var privkey = KDF.sha256hmac2privkey(buf); var privkey = KDF.sha256hmac2privkey(buf);
var key = new Key({privkey: privkey}); var keypair = Keypair().fromPrivkey(privkey);
key.privkey2pubkey(); return keypair;
return key;
}; };
KDF.sha256hmac2privkey = function(buf) { KDF.sha256hmac2privkey = function(buf) {

View File

@ -1,5 +1,5 @@
var ECDSA = require('./ecdsa'); var ECDSA = require('./ecdsa');
var Key = require('./key'); var Keypair = require('./keypair');
var Privkey = require('./privkey'); var Privkey = require('./privkey');
var Pubkey = require('./pubkey'); var Pubkey = require('./pubkey');
var BufferWriter = require('./bufferwriter'); var BufferWriter = require('./bufferwriter');
@ -73,7 +73,7 @@ Message.prototype.verify = function() {
var ecdsa = new ECDSA(); var ecdsa = new ECDSA();
ecdsa.hashbuf = hashbuf; ecdsa.hashbuf = hashbuf;
ecdsa.sig = this.sig; ecdsa.sig = this.sig;
ecdsa.key = new Key(); ecdsa.key = new Keypair();
ecdsa.key.pubkey = ecdsa.sig2pubkey(); ecdsa.key.pubkey = ecdsa.sig2pubkey();
if (!ecdsa.verify()) { if (!ecdsa.verify()) {

View File

@ -1,6 +1,6 @@
var ECDSA = require('../lib/ecdsa'); var ECDSA = require('../lib/ecdsa');
var Hash = require('../lib/hash'); var Hash = require('../lib/hash');
var Key = require('../lib/key'); var Keypair = require('../lib/keypair');
var Privkey = require('../lib/privkey'); var Privkey = require('../lib/privkey');
var Pubkey = require('../lib/pubkey'); var Pubkey = require('../lib/pubkey');
var Signature = require('../lib/signature'); var Signature = require('../lib/signature');
@ -16,7 +16,7 @@ describe("ECDSA", function() {
var ecdsa = new ECDSA(); var ecdsa = new ECDSA();
ecdsa.hashbuf = Hash.sha256(new Buffer('test data')); ecdsa.hashbuf = Hash.sha256(new Buffer('test data'));
ecdsa.key = new Key(); ecdsa.key = new Keypair();
ecdsa.key.privkey = new Privkey({bn: BN().fromBuffer(new Buffer('fee0a1f7afebf9d2a5a80c0c98a31c709681cce195cbcd06342b517970c0be1e', 'hex'))}); ecdsa.key.privkey = new Privkey({bn: BN().fromBuffer(new Buffer('fee0a1f7afebf9d2a5a80c0c98a31c709681cce195cbcd06342b517970c0be1e', 'hex'))});
ecdsa.key.pubkey = new Pubkey({ ecdsa.key.pubkey = new Pubkey({
point: point(BN().fromBuffer(new Buffer('ac242d242d23be966085a2b2b893d989f824e06c9ad0395a8a52f055ba39abb2', 'hex')), point: point(BN().fromBuffer(new Buffer('ac242d242d23be966085a2b2b893d989f824e06c9ad0395a8a52f055ba39abb2', 'hex')),
@ -45,7 +45,7 @@ describe("ECDSA", function() {
var r = BN('71706645040721865894779025947914615666559616020894583599959600180037551395766', 10); var r = BN('71706645040721865894779025947914615666559616020894583599959600180037551395766', 10);
var s = BN('109412465507152403114191008482955798903072313614214706891149785278625167723646', 10); var s = BN('109412465507152403114191008482955798903072313614214706891149785278625167723646', 10);
var ecdsa = new ECDSA(); var ecdsa = new ECDSA();
ecdsa.key = new Key(); ecdsa.key = new Keypair();
ecdsa.key.privkey = Privkey(); ecdsa.key.privkey = Privkey();
ecdsa.key.privkey.bn = BN().fromBuffer(Hash.sha256(new Buffer('test'))); ecdsa.key.privkey.bn = BN().fromBuffer(Hash.sha256(new Buffer('test')));
ecdsa.key.privkey2pubkey(); ecdsa.key.privkey2pubkey();
@ -119,7 +119,7 @@ describe("ECDSA", function() {
ecdsa.hashbuf = Hash.sha256(new Buffer('test')); ecdsa.hashbuf = Hash.sha256(new Buffer('test'));
var pk = new Pubkey(); var pk = new Pubkey();
pk.fromDER(new Buffer('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341', 'hex')); pk.fromDER(new Buffer('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341', 'hex'));
ecdsa.key = new Key(); ecdsa.key = new Keypair();
ecdsa.key.pubkey = pk; ecdsa.key.pubkey = pk;
ecdsa.sig = new Signature(); ecdsa.sig = new Signature();
ecdsa.sig.r = BN(0); ecdsa.sig.r = BN(0);

View File

@ -1,6 +1,6 @@
var ECIES = require('../lib/expmt/ecies'); var ECIES = require('../lib/expmt/ecies');
var should = require('chai').should(); var should = require('chai').should();
var Key = require('../lib/key'); var Keypair = require('../lib/keypair');
var Hash = require('../lib/hash'); var Hash = require('../lib/hash');
describe('#ECIES', function() { describe('#ECIES', function() {
@ -15,8 +15,8 @@ describe('#ECIES', function() {
should.exist(ecies); should.exist(ecies);
}); });
var fromkey = Key().fromRandom(); var fromkey = Keypair().fromRandom();
var tokey = Key().fromRandom(); var tokey = Keypair().fromRandom();
var messagebuf = Hash.sha256(new Buffer('my message is the hash of this string')); var messagebuf = Hash.sha256(new Buffer('my message is the hash of this string'));
describe('@encrypt', function() { describe('@encrypt', function() {

View File

@ -4,24 +4,24 @@ var Hash = require('../lib/hash');
describe('KDF', function() { describe('KDF', function() {
describe('#buf2key', function() { describe('#buf2keypair', function() {
it('should compute these known values', function() { it('should compute these known values', function() {
var buf = Hash.sha256(new Buffer('test')); var buf = Hash.sha256(new Buffer('test'));
var key = KDF.buf2key(buf); var keypair = KDF.buf2keypair(buf);
key.privkey.toString().should.equal('KxxVszVMFLGzmxpxR7sMSaWDmqMKLVhKebX5vZbGHyuR8spreQ7V'); keypair.privkey.toString().should.equal('KxxVszVMFLGzmxpxR7sMSaWDmqMKLVhKebX5vZbGHyuR8spreQ7V');
key.pubkey.toString().should.equal('03774f761ae89a0d2fda0d532bad62286ae8fcda9bc38c060036296085592a97c1'); keypair.pubkey.toString().should.equal('03774f761ae89a0d2fda0d532bad62286ae8fcda9bc38c060036296085592a97c1');
}); });
}); });
describe('#sha256hmac2key', function() { describe('#sha256hmac2keypair', function() {
it('should compute these known values', function() { it('should compute these known values', function() {
var buf = Hash.sha256(new Buffer('test')); var buf = Hash.sha256(new Buffer('test'));
var key = KDF.sha256hmac2key(buf); var keypair = KDF.sha256hmac2keypair(buf);
key.privkey.toString().should.equal('KxxVszVMFLGzmxpxR7sMSaWDmqMKLVhKebX5vZbGHyuR8spreQ7V'); keypair.privkey.toString().should.equal('KxxVszVMFLGzmxpxR7sMSaWDmqMKLVhKebX5vZbGHyuR8spreQ7V');
key.pubkey.toString().should.equal('03774f761ae89a0d2fda0d532bad62286ae8fcda9bc38c060036296085592a97c1'); keypair.pubkey.toString().should.equal('03774f761ae89a0d2fda0d532bad62286ae8fcda9bc38c060036296085592a97c1');
}); });
}); });

View File

@ -4,19 +4,19 @@ var point = require('../lib/point');
var Address = require('../lib/address'); var Address = require('../lib/address');
var Privkey = require('../lib/privkey'); var Privkey = require('../lib/privkey');
var Pubkey = require('../lib/pubkey'); var Pubkey = require('../lib/pubkey');
var Key = require('../lib/key'); var Keypair = require('../lib/keypair');
describe('Key', function() { describe('Keypair', function() {
it('should make a blank key', function() { it('should make a blank key', function() {
var key = new Key(); var key = new Keypair();
should.exist(key); should.exist(key);
}); });
it('should make a key with a priv and pub', function() { it('should make a key with a priv and pub', function() {
var priv = new Privkey(); var priv = new Privkey();
var pub = new Pubkey(); var pub = new Pubkey();
var key = new Key({privkey: priv, pubkey: pub}); var key = new Keypair({privkey: priv, pubkey: pub});
should.exist(key); should.exist(key);
should.exist(key.privkey); should.exist(key.privkey);
should.exist(key.pubkey); should.exist(key.pubkey);
@ -25,7 +25,7 @@ describe('Key', function() {
describe("#set", function() { describe("#set", function() {
it('should make a new priv and pub', function() { it('should make a new priv and pub', function() {
should.exist(Key().set({privkey: Privkey()}).privkey); should.exist(Keypair().set({privkey: Privkey()}).privkey);
}); });
}); });
@ -33,7 +33,7 @@ describe('Key', function() {
describe("#fromPrivkey", function() { describe("#fromPrivkey", function() {
it('should make a new key from a privkey', function() { it('should make a new key from a privkey', function() {
should.exist(Key().fromPrivkey(Privkey().fromRandom()).pubkey); should.exist(Keypair().fromPrivkey(Privkey().fromRandom()).pubkey);
}); });
}); });
@ -41,7 +41,7 @@ describe('Key', function() {
describe("#fromRandom", function() { describe("#fromRandom", function() {
it('should make a new priv and pub, should be compressed, mainnet', function() { it('should make a new priv and pub, should be compressed, mainnet', function() {
var key = new Key(); var key = new Keypair();
key.fromRandom(); key.fromRandom();
should.exist(key.privkey); should.exist(key.privkey);
should.exist(key.pubkey); should.exist(key.pubkey);
@ -58,7 +58,7 @@ describe('Key', function() {
describe("#fromString()", function() { describe("#fromString()", function() {
it('should recover a key creating with toString', function() { it('should recover a key creating with toString', function() {
var key = new Key(); var key = new Keypair();
key.fromRandom(); key.fromRandom();
var priv = key.privkey; var priv = key.privkey;
var pub = key.pubkey; var pub = key.pubkey;
@ -71,7 +71,7 @@ describe('Key', function() {
}); });
it('should work with only Privkey set', function() { it('should work with only Privkey set', function() {
var key = new Key(); var key = new Keypair();
key.fromRandom(); key.fromRandom();
key.pubkey = undefined; key.pubkey = undefined;
var priv = key.privkey; var priv = key.privkey;
@ -82,7 +82,7 @@ describe('Key', function() {
}); });
it('should work with only Pubkey set', function() { it('should work with only Pubkey set', function() {
var key = new Key(); var key = new Keypair();
key.fromRandom(); key.fromRandom();
key.privkey = undefined; key.privkey = undefined;
var pub = key.pubkey; var pub = key.pubkey;
@ -99,7 +99,7 @@ describe('Key', function() {
it('should return an address', function() { it('should return an address', function() {
var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff'; var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff';
var pubhex = '02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc'; var pubhex = '02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc';
var key = new Key(); var key = new Keypair();
key.privkey = new Privkey({bn: bn(new Buffer(privhex, 'hex'))}); key.privkey = new Privkey({bn: bn(new Buffer(privhex, 'hex'))});
key.privkey2pubkey(); key.privkey2pubkey();
key.getAddress().toString().should.equal((new Address()).fromPubkey(key.pubkey).toString()); key.getAddress().toString().should.equal((new Address()).fromPubkey(key.pubkey).toString());
@ -112,7 +112,7 @@ describe('Key', function() {
it('should convert this known Privkey to known Pubkey', function() { it('should convert this known Privkey to known Pubkey', function() {
var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff'; var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff';
var pubhex = '02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc'; var pubhex = '02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc';
var key = new Key(); var key = new Keypair();
key.privkey = new Privkey({bn: bn(new Buffer(privhex, 'hex'))}); key.privkey = new Privkey({bn: bn(new Buffer(privhex, 'hex'))});
key.privkey2pubkey(); key.privkey2pubkey();
key.pubkey.toString().should.equal(pubhex); key.pubkey.toString().should.equal(pubhex);
@ -120,7 +120,7 @@ describe('Key', function() {
it('should convert this known Privkey to known Pubkey and preserve compressed=true', function() { it('should convert this known Privkey to known Pubkey and preserve compressed=true', function() {
var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff'; var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff';
var key = new Key(); var key = new Keypair();
key.privkey = new Privkey({bn: bn(new Buffer(privhex, 'hex'))}); key.privkey = new Privkey({bn: bn(new Buffer(privhex, 'hex'))});
key.privkey.compressed = true; key.privkey.compressed = true;
key.privkey2pubkey(); key.privkey2pubkey();
@ -129,7 +129,7 @@ describe('Key', function() {
it('should convert this known Privkey to known Pubkey and preserve compressed=true', function() { it('should convert this known Privkey to known Pubkey and preserve compressed=true', function() {
var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff'; var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff';
var key = new Key(); var key = new Keypair();
key.privkey = new Privkey({bn: bn(new Buffer(privhex, 'hex'))}); key.privkey = new Privkey({bn: bn(new Buffer(privhex, 'hex'))});
key.privkey.compressed = false; key.privkey.compressed = false;
key.privkey2pubkey(); key.privkey2pubkey();
@ -141,7 +141,7 @@ describe('Key', function() {
describe("#toString()", function() { describe("#toString()", function() {
it('should exist', function() { it('should exist', function() {
var key = new Key(); var key = new Keypair();
key.fromRandom(); key.fromRandom();
should.exist(key.toString()); should.exist(key.toString());
}); });

View File

@ -1,6 +1,6 @@
var Address = require('../lib/address'); var Address = require('../lib/address');
var Message = require('../lib/message'); var Message = require('../lib/message');
var Key = require('../lib/key'); var Keypair = require('../lib/keypair');
var should = require('chai').should(); var should = require('chai').should();
describe('Message', function() { describe('Message', function() {
@ -26,7 +26,7 @@ describe('Message', function() {
describe('@sign', function() { describe('@sign', function() {
var messagebuf = new Buffer('this is my message'); var messagebuf = new Buffer('this is my message');
var key = Key().fromRandom(); var key = Keypair().fromRandom();
it('should return a base64 string', function() { it('should return a base64 string', function() {
var sigstr = Message.sign(messagebuf, key); var sigstr = Message.sign(messagebuf, key);
@ -35,7 +35,7 @@ describe('Message', function() {
}); });
it('should sign with a compressed pubkey', function() { it('should sign with a compressed pubkey', function() {
var key = Key().fromRandom(); var key = Keypair().fromRandom();
key.pubkey.compressed = true; key.pubkey.compressed = true;
var sigstr = Message.sign(messagebuf, key); var sigstr = Message.sign(messagebuf, key);
var sigbuf = new Buffer(sigstr, 'base64'); var sigbuf = new Buffer(sigstr, 'base64');
@ -44,7 +44,7 @@ describe('Message', function() {
}); });
it('should sign with an uncompressed pubkey', function() { it('should sign with an uncompressed pubkey', function() {
var key = Key().fromRandom(); var key = Keypair().fromRandom();
key.pubkey.compressed = false; key.pubkey.compressed = false;
var sigstr = Message.sign(messagebuf, key); var sigstr = Message.sign(messagebuf, key);
var sigbuf = new Buffer(sigstr, 'base64'); var sigbuf = new Buffer(sigstr, 'base64');
@ -56,7 +56,7 @@ describe('Message', function() {
describe('@verify', function() { describe('@verify', function() {
var messagebuf = new Buffer('this is my message'); var messagebuf = new Buffer('this is my message');
var key = Key().fromRandom(); var key = Keypair().fromRandom();
it('should verify a signed message', function() { it('should verify a signed message', function() {
var sigstr = Message.sign(messagebuf, key); var sigstr = Message.sign(messagebuf, key);
@ -75,7 +75,7 @@ describe('Message', function() {
describe('#sign', function() { describe('#sign', function() {
var messagebuf = new Buffer('this is my message'); var messagebuf = new Buffer('this is my message');
var key = Key().fromRandom(); var key = Keypair().fromRandom();
it('should sign a message', function() { it('should sign a message', function() {
var message = new Message(); var message = new Message();
@ -90,7 +90,7 @@ describe('Message', function() {
describe('#verify', function() { describe('#verify', function() {
var messagebuf = new Buffer('this is my message'); var messagebuf = new Buffer('this is my message');
var key = Key().fromRandom(); var key = Keypair().fromRandom();
it('should verify a message that was just signed', function() { it('should verify a message that was just signed', function() {
var message = new Message(); var message = new Message();

View File

@ -1,6 +1,6 @@
var should = require('chai').should(); var should = require('chai').should();
var Stealth = require('../lib/expmt/stealth'); var Stealth = require('../lib/expmt/stealth');
var Key = require('../lib/key'); var Keypair = require('../lib/keypair');
var Privkey = require('../lib/privkey'); var Privkey = require('../lib/privkey');
var Pubkey = require('../lib/pubkey'); var Pubkey = require('../lib/pubkey');
var BN = require('../lib/bn'); var BN = require('../lib/bn');
@ -10,19 +10,19 @@ var base58check = require('../lib/base58check');
describe('Stealth', function() { describe('Stealth', function() {
var stealth = Stealth(); var stealth = Stealth();
stealth.payloadKey = Key(); stealth.payloadKeypair = Keypair();
stealth.payloadKey.privkey = Privkey(); stealth.payloadKeypair.privkey = Privkey();
stealth.payloadKey.privkey.bn = BN().fromBuffer(Hash.sha256(new Buffer('test 1'))); stealth.payloadKeypair.privkey.bn = BN().fromBuffer(Hash.sha256(new Buffer('test 1')));
stealth.payloadKey.privkey2pubkey(); stealth.payloadKeypair.privkey2pubkey();
stealth.scanKey = Key(); stealth.scanKeypair = Keypair();
stealth.scanKey.privkey = Privkey(); stealth.scanKeypair.privkey = Privkey();
stealth.scanKey.privkey.bn = BN().fromBuffer(Hash.sha256(new Buffer('test 2'))); stealth.scanKeypair.privkey.bn = BN().fromBuffer(Hash.sha256(new Buffer('test 2')));
stealth.scanKey.privkey2pubkey(); stealth.scanKeypair.privkey2pubkey();
var senderKey = Key(); var senderKeypair = Keypair();
senderKey.privkey = Privkey(); senderKeypair.privkey = Privkey();
senderKey.privkey.bn = BN().fromBuffer(Hash.sha256(new Buffer('test 3'))); senderKeypair.privkey.bn = BN().fromBuffer(Hash.sha256(new Buffer('test 3')));
senderKey.privkey2pubkey(); senderKeypair.privkey2pubkey();
var addressString = '9dDbC9FzZ74r8njQkXD6W27gtrxLiWaeFPHxeo1fynQRXPicqxVt7u95ozbwoVVMXyrzaHKN9owsteg63FgwDfrxWx82SAW'; var addressString = '9dDbC9FzZ74r8njQkXD6W27gtrxLiWaeFPHxeo1fynQRXPicqxVt7u95ozbwoVVMXyrzaHKN9owsteg63FgwDfrxWx82SAW';
@ -39,7 +39,7 @@ describe('Stealth', function() {
describe('#set', function() { describe('#set', function() {
it('should set payload key', function() { it('should set payload key', function() {
should.exist(Stealth().set({payloadKey: stealth.payloadKey}).payloadKey); should.exist(Stealth().set({payloadKeypair: stealth.payloadKeypair}).payloadKeypair);
}); });
}); });
@ -50,8 +50,8 @@ describe('Stealth', function() {
var stealth2 = new Stealth(); var stealth2 = new Stealth();
var buf = base58check.decode(addressString); var buf = base58check.decode(addressString);
stealth2.fromAddressBuffer(buf); stealth2.fromAddressBuffer(buf);
stealth2.payloadKey.pubkey.toString().should.equal(stealth.payloadKey.pubkey.toString()); stealth2.payloadKeypair.pubkey.toString().should.equal(stealth.payloadKeypair.pubkey.toString());
stealth2.scanKey.pubkey.toString().should.equal(stealth.scanKey.pubkey.toString()); stealth2.scanKeypair.pubkey.toString().should.equal(stealth.scanKeypair.pubkey.toString());
}); });
}); });
@ -61,8 +61,8 @@ describe('Stealth', function() {
it('should give a stealth address with the right pubkeys', function() { it('should give a stealth address with the right pubkeys', function() {
var stealth2 = new Stealth(); var stealth2 = new Stealth();
stealth2.fromAddressString(addressString); stealth2.fromAddressString(addressString);
stealth2.payloadKey.pubkey.toString().should.equal(stealth.payloadKey.pubkey.toString()); stealth2.payloadKeypair.pubkey.toString().should.equal(stealth.payloadKeypair.pubkey.toString());
stealth2.scanKey.pubkey.toString().should.equal(stealth.scanKey.pubkey.toString()); stealth2.scanKeypair.pubkey.toString().should.equal(stealth.scanKeypair.pubkey.toString());
}); });
}); });
@ -71,42 +71,42 @@ describe('Stealth', function() {
it('should create a new stealth from random', function() { it('should create a new stealth from random', function() {
var stealth = Stealth().fromRandom(); var stealth = Stealth().fromRandom();
should.exist(stealth.payloadKey.privkey.bn.gt(0)); should.exist(stealth.payloadKeypair.privkey.bn.gt(0));
should.exist(stealth.scanKey.privkey.bn.gt(0)); should.exist(stealth.scanKeypair.privkey.bn.gt(0));
}); });
}); });
describe('#getSharedKeyAsReceiver', function() { describe('#getSharedKeypairAsReceiver', function() {
it('should return a key', function() { it('should return a key', function() {
var key = stealth.getSharedKeyAsReceiver(senderKey.pubkey); var key = stealth.getSharedKeypairAsReceiver(senderKeypair.pubkey);
(key instanceof Key).should.equal(true); (key instanceof Keypair).should.equal(true);
}); });
}); });
describe('#getSharedKeyAsSender', function() { describe('#getSharedKeypairAsSender', function() {
it('should return a key', function() { it('should return a key', function() {
var stealth2 = new Stealth(); var stealth2 = new Stealth();
stealth2.payloadKey = new Key(); stealth2.payloadKeypair = new Keypair();
stealth2.payloadKey.pubkey = stealth.payloadKey.pubkey; stealth2.payloadKeypair.pubkey = stealth.payloadKeypair.pubkey;
stealth2.scanKey = new Key(); stealth2.scanKeypair = new Keypair();
stealth2.scanKey.pubkey = stealth.scanKey.pubkey; stealth2.scanKeypair.pubkey = stealth.scanKeypair.pubkey;
var key = stealth2.getSharedKeyAsSender(senderKey); var key = stealth2.getSharedKeypairAsSender(senderKeypair);
(key instanceof Key).should.equal(true); (key instanceof Keypair).should.equal(true);
}); });
it('should return the same key as getSharedKeyAsReceiver', function() { it('should return the same key as getSharedKeypairAsReceiver', function() {
var stealth2 = new Stealth(); var stealth2 = new Stealth();
stealth2.payloadKey = new Key(); stealth2.payloadKeypair = new Keypair();
stealth2.payloadKey.pubkey = stealth.payloadKey.pubkey; stealth2.payloadKeypair.pubkey = stealth.payloadKeypair.pubkey;
stealth2.scanKey = new Key(); stealth2.scanKeypair = new Keypair();
stealth2.scanKey.pubkey = stealth.scanKey.pubkey; stealth2.scanKeypair.pubkey = stealth.scanKeypair.pubkey;
var key = stealth2.getSharedKeyAsSender(senderKey); var key = stealth2.getSharedKeypairAsSender(senderKeypair);
var key2 = stealth.getSharedKeyAsReceiver(senderKey.pubkey); var key2 = stealth.getSharedKeypairAsReceiver(senderKeypair.pubkey);
key.toString().should.equal(key2.toString()); key.toString().should.equal(key2.toString());
}); });
@ -115,7 +115,7 @@ describe('Stealth', function() {
describe('#getReceivePubkeyAsReceiver', function() { describe('#getReceivePubkeyAsReceiver', function() {
it('should return a pubkey', function() { it('should return a pubkey', function() {
var pubkey = stealth.getReceivePubkeyAsReceiver(senderKey.pubkey); var pubkey = stealth.getReceivePubkeyAsReceiver(senderKeypair.pubkey);
(pubkey instanceof Pubkey).should.equal(true); (pubkey instanceof Pubkey).should.equal(true);
}); });
@ -124,33 +124,33 @@ describe('Stealth', function() {
describe('#getReceivePubkeyAsSender', function() { describe('#getReceivePubkeyAsSender', function() {
it('should return a pubkey', function() { it('should return a pubkey', function() {
var pubkey = stealth.getReceivePubkeyAsSender(senderKey); var pubkey = stealth.getReceivePubkeyAsSender(senderKeypair);
(pubkey instanceof Pubkey).should.equal(true); (pubkey instanceof Pubkey).should.equal(true);
}); });
it('should return the same pubkey as getReceivePubkeyAsReceiver', function() { it('should return the same pubkey as getReceivePubkeyAsReceiver', function() {
var pubkey = stealth.getReceivePubkeyAsSender(senderKey); var pubkey = stealth.getReceivePubkeyAsSender(senderKeypair);
var pubkey2 = stealth.getReceivePubkeyAsReceiver(senderKey.pubkey); var pubkey2 = stealth.getReceivePubkeyAsReceiver(senderKeypair.pubkey);
pubkey2.toString().should.equal(pubkey.toString()); pubkey2.toString().should.equal(pubkey.toString());
}); });
}); });
describe('#getReceiveKey', function() { describe('#getReceiveKeypair', function() {
it('should return a key', function() { it('should return a key', function() {
var key = stealth.getReceiveKey(senderKey.pubkey); var key = stealth.getReceiveKeypair(senderKeypair.pubkey);
(key instanceof Key).should.equal(true); (key instanceof Keypair).should.equal(true);
}); });
it('should return a key with the same pubkey as getReceivePubkeyAsReceiver', function() { it('should return a key with the same pubkey as getReceivePubkeyAsReceiver', function() {
var key = stealth.getReceiveKey(senderKey.pubkey); var key = stealth.getReceiveKeypair(senderKeypair.pubkey);
var pubkey = stealth.getReceivePubkeyAsReceiver(senderKey.pubkey); var pubkey = stealth.getReceivePubkeyAsReceiver(senderKeypair.pubkey);
key.pubkey.toString().should.equal(pubkey.toString()); key.pubkey.toString().should.equal(pubkey.toString());
}); });
it('should return private key with length 32 or less', function() { it('should return private key with length 32 or less', function() {
var key = stealth.getReceiveKey(senderKey.pubkey); var key = stealth.getReceiveKeypair(senderKeypair.pubkey);
key.privkey.bn.toBuffer().length.should.be.below(33); key.privkey.bn.toBuffer().length.should.be.below(33);
}); });
@ -160,12 +160,12 @@ describe('Stealth', function() {
it('should return true if it (the transaction or message) is for me', function() { it('should return true if it (the transaction or message) is for me', function() {
var pubkeyhash = new Buffer('3cb64fa6ee9b3e8754e3e2bd033bf61048604a99', 'hex'); var pubkeyhash = new Buffer('3cb64fa6ee9b3e8754e3e2bd033bf61048604a99', 'hex');
stealth.isForMe(senderKey.pubkey, pubkeyhash).should.equal(true); stealth.isForMe(senderKeypair.pubkey, pubkeyhash).should.equal(true);
}); });
it('should return false if it (the transaction or message) is not for me', function() { it('should return false if it (the transaction or message) is not for me', function() {
var pubkeyhash = new Buffer('00b64fa6ee9b3e8754e3e2bd033bf61048604a99', 'hex'); var pubkeyhash = new Buffer('00b64fa6ee9b3e8754e3e2bd033bf61048604a99', 'hex');
stealth.isForMe(senderKey.pubkey, pubkeyhash).should.equal(false); stealth.isForMe(senderKeypair.pubkey, pubkeyhash).should.equal(false);
}); });
}); });