Browse Source

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).
patch-2
Ryan X. Charles 11 years ago
parent
commit
32fabd0c0f
  1. 2
      index.js
  2. 14
      lib/bip32.js
  3. 4
      lib/ecdsa.js
  4. 16
      lib/expmt/ecies.js
  5. 52
      lib/expmt/stealth.js
  6. 13
      lib/kdf.js
  7. 0
      lib/keypair.js
  8. 4
      lib/message.js
  9. 8
      test/ecdsa.js
  10. 6
      test/ecies.js
  11. 16
      test/kdf.js
  12. 30
      test/keypair.js
  13. 14
      test/message.js
  14. 100
      test/stealth.js

2
index.js

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

14
lib/bip32.js

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

4
lib/ecdsa.js

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

16
lib/expmt/ecies.js

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

52
lib/expmt/stealth.js

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

13
lib/kdf.js

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

0
lib/key.js → lib/keypair.js

4
lib/message.js

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

8
test/ecdsa.js

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

6
test/ecies.js

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

16
test/kdf.js

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

30
test/key.js → test/keypair.js

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

14
test/message.js

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

100
test/stealth.js

@ -1,6 +1,6 @@
var should = require('chai').should();
var Stealth = require('../lib/expmt/stealth');
var Key = require('../lib/key');
var Keypair = require('../lib/keypair');
var Privkey = require('../lib/privkey');
var Pubkey = require('../lib/pubkey');
var BN = require('../lib/bn');
@ -10,19 +10,19 @@ var base58check = require('../lib/base58check');
describe('Stealth', function() {
var stealth = Stealth();
stealth.payloadKey = Key();
stealth.payloadKey.privkey = Privkey();
stealth.payloadKey.privkey.bn = BN().fromBuffer(Hash.sha256(new Buffer('test 1')));
stealth.payloadKey.privkey2pubkey();
stealth.scanKey = Key();
stealth.scanKey.privkey = Privkey();
stealth.scanKey.privkey.bn = BN().fromBuffer(Hash.sha256(new Buffer('test 2')));
stealth.scanKey.privkey2pubkey();
var senderKey = Key();
senderKey.privkey = Privkey();
senderKey.privkey.bn = BN().fromBuffer(Hash.sha256(new Buffer('test 3')));
senderKey.privkey2pubkey();
stealth.payloadKeypair = Keypair();
stealth.payloadKeypair.privkey = Privkey();
stealth.payloadKeypair.privkey.bn = BN().fromBuffer(Hash.sha256(new Buffer('test 1')));
stealth.payloadKeypair.privkey2pubkey();
stealth.scanKeypair = Keypair();
stealth.scanKeypair.privkey = Privkey();
stealth.scanKeypair.privkey.bn = BN().fromBuffer(Hash.sha256(new Buffer('test 2')));
stealth.scanKeypair.privkey2pubkey();
var senderKeypair = Keypair();
senderKeypair.privkey = Privkey();
senderKeypair.privkey.bn = BN().fromBuffer(Hash.sha256(new Buffer('test 3')));
senderKeypair.privkey2pubkey();
var addressString = '9dDbC9FzZ74r8njQkXD6W27gtrxLiWaeFPHxeo1fynQRXPicqxVt7u95ozbwoVVMXyrzaHKN9owsteg63FgwDfrxWx82SAW';
@ -39,7 +39,7 @@ describe('Stealth', function() {
describe('#set', 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 buf = base58check.decode(addressString);
stealth2.fromAddressBuffer(buf);
stealth2.payloadKey.pubkey.toString().should.equal(stealth.payloadKey.pubkey.toString());
stealth2.scanKey.pubkey.toString().should.equal(stealth.scanKey.pubkey.toString());
stealth2.payloadKeypair.pubkey.toString().should.equal(stealth.payloadKeypair.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() {
var stealth2 = new Stealth();
stealth2.fromAddressString(addressString);
stealth2.payloadKey.pubkey.toString().should.equal(stealth.payloadKey.pubkey.toString());
stealth2.scanKey.pubkey.toString().should.equal(stealth.scanKey.pubkey.toString());
stealth2.payloadKeypair.pubkey.toString().should.equal(stealth.payloadKeypair.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() {
var stealth = Stealth().fromRandom();
should.exist(stealth.payloadKey.privkey.bn.gt(0));
should.exist(stealth.scanKey.privkey.bn.gt(0));
should.exist(stealth.payloadKeypair.privkey.bn.gt(0));
should.exist(stealth.scanKeypair.privkey.bn.gt(0));
});
});
describe('#getSharedKeyAsReceiver', function() {
describe('#getSharedKeypairAsReceiver', function() {
it('should return a key', function() {
var key = stealth.getSharedKeyAsReceiver(senderKey.pubkey);
(key instanceof Key).should.equal(true);
var key = stealth.getSharedKeypairAsReceiver(senderKeypair.pubkey);
(key instanceof Keypair).should.equal(true);
});
});
describe('#getSharedKeyAsSender', function() {
describe('#getSharedKeypairAsSender', function() {
it('should return a key', function() {
var stealth2 = new Stealth();
stealth2.payloadKey = new Key();
stealth2.payloadKey.pubkey = stealth.payloadKey.pubkey;
stealth2.scanKey = new Key();
stealth2.scanKey.pubkey = stealth.scanKey.pubkey;
var key = stealth2.getSharedKeyAsSender(senderKey);
(key instanceof Key).should.equal(true);
stealth2.payloadKeypair = new Keypair();
stealth2.payloadKeypair.pubkey = stealth.payloadKeypair.pubkey;
stealth2.scanKeypair = new Keypair();
stealth2.scanKeypair.pubkey = stealth.scanKeypair.pubkey;
var key = stealth2.getSharedKeypairAsSender(senderKeypair);
(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();
stealth2.payloadKey = new Key();
stealth2.payloadKey.pubkey = stealth.payloadKey.pubkey;
stealth2.scanKey = new Key();
stealth2.scanKey.pubkey = stealth.scanKey.pubkey;
var key = stealth2.getSharedKeyAsSender(senderKey);
stealth2.payloadKeypair = new Keypair();
stealth2.payloadKeypair.pubkey = stealth.payloadKeypair.pubkey;
stealth2.scanKeypair = new Keypair();
stealth2.scanKeypair.pubkey = stealth.scanKeypair.pubkey;
var key = stealth2.getSharedKeypairAsSender(senderKeypair);
var key2 = stealth.getSharedKeyAsReceiver(senderKey.pubkey);
var key2 = stealth.getSharedKeypairAsReceiver(senderKeypair.pubkey);
key.toString().should.equal(key2.toString());
});
@ -115,7 +115,7 @@ describe('Stealth', function() {
describe('#getReceivePubkeyAsReceiver', 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);
});
@ -124,33 +124,33 @@ describe('Stealth', function() {
describe('#getReceivePubkeyAsSender', function() {
it('should return a pubkey', function() {
var pubkey = stealth.getReceivePubkeyAsSender(senderKey);
var pubkey = stealth.getReceivePubkeyAsSender(senderKeypair);
(pubkey instanceof Pubkey).should.equal(true);
});
it('should return the same pubkey as getReceivePubkeyAsReceiver', function() {
var pubkey = stealth.getReceivePubkeyAsSender(senderKey);
var pubkey2 = stealth.getReceivePubkeyAsReceiver(senderKey.pubkey);
var pubkey = stealth.getReceivePubkeyAsSender(senderKeypair);
var pubkey2 = stealth.getReceivePubkeyAsReceiver(senderKeypair.pubkey);
pubkey2.toString().should.equal(pubkey.toString());
});
});
describe('#getReceiveKey', function() {
describe('#getReceiveKeypair', function() {
it('should return a key', function() {
var key = stealth.getReceiveKey(senderKey.pubkey);
(key instanceof Key).should.equal(true);
var key = stealth.getReceiveKeypair(senderKeypair.pubkey);
(key instanceof Keypair).should.equal(true);
});
it('should return a key with the same pubkey as getReceivePubkeyAsReceiver', function() {
var key = stealth.getReceiveKey(senderKey.pubkey);
var pubkey = stealth.getReceivePubkeyAsReceiver(senderKey.pubkey);
var key = stealth.getReceiveKeypair(senderKeypair.pubkey);
var pubkey = stealth.getReceivePubkeyAsReceiver(senderKeypair.pubkey);
key.pubkey.toString().should.equal(pubkey.toString());
});
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);
});
@ -160,12 +160,12 @@ describe('Stealth', function() {
it('should return true if it (the transaction or message) is for me', function() {
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() {
var pubkeyhash = new Buffer('00b64fa6ee9b3e8754e3e2bd033bf61048604a99', 'hex');
stealth.isForMe(senderKey.pubkey, pubkeyhash).should.equal(false);
stealth.isForMe(senderKeypair.pubkey, pubkeyhash).should.equal(false);
});
});

Loading…
Cancel
Save