You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
2.2 KiB
73 lines
2.2 KiB
var Keypair = require('../keypair');
|
|
var Privkey = require('../privkey');
|
|
var Pubkey = require('../pubkey');
|
|
var Point = require('../point');
|
|
var Hash = require('../hash');
|
|
var KDF = require('../kdf');
|
|
|
|
var Stealthkey = function Stealthkey(payloadKeypair, scanKeypair) {
|
|
if (!(this instanceof Stealthkey))
|
|
return new Stealthkey(payloadKeypair, scanKeypair);
|
|
|
|
if (payloadKeypair instanceof Keypair) {
|
|
this.set({
|
|
payloadKeypair: payloadKeypair,
|
|
scanKeypair: scanKeypair
|
|
});
|
|
}
|
|
else if (payloadKeypair) {
|
|
var obj = payloadKeypair;
|
|
this.set(obj);
|
|
}
|
|
};
|
|
|
|
Stealthkey.prototype.set = function(obj) {
|
|
this.payloadKeypair = obj.payloadKeypair || this.payloadKeypair;
|
|
this.scanKeypair = obj.scanKeypair || this.scanKeypair;
|
|
return this;
|
|
};
|
|
|
|
Stealthkey.prototype.fromRandom = function() {
|
|
this.payloadKeypair = Keypair().fromRandom();
|
|
this.scanKeypair = Keypair().fromRandom();
|
|
|
|
return this;
|
|
};
|
|
|
|
Stealthkey.prototype.getSharedKeypair = function(senderPubkey) {
|
|
var sharedSecretPoint = senderPubkey.point.mul(this.scanKeypair.privkey.bn);
|
|
var sharedSecretPubkey = Pubkey({point: sharedSecretPoint});
|
|
var buf = sharedSecretPubkey.toDER(true);
|
|
var sharedKeypair = KDF.sha256hmac2keypair(buf);
|
|
|
|
return sharedKeypair;
|
|
};
|
|
|
|
Stealthkey.prototype.getReceivePubkey = function(senderPubkey) {
|
|
var sharedKeypair = this.getSharedKeypair(senderPubkey);
|
|
var pubkey = Pubkey({point: this.payloadKeypair.pubkey.point.add(sharedKeypair.pubkey.point)});
|
|
|
|
return pubkey;
|
|
};
|
|
|
|
Stealthkey.prototype.getReceiveKeypair = function(senderPubkey) {
|
|
var sharedKeypair = this.getSharedKeypair(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;
|
|
};
|
|
|
|
Stealthkey.prototype.isForMe = function(senderPubkey, myPossiblePubkeyhashbuf) {
|
|
var pubkey = this.getReceivePubkey(senderPubkey);
|
|
var pubkeybuf = pubkey.toDER(true);
|
|
var pubkeyhash = Hash.sha256ripemd160(pubkeybuf);
|
|
|
|
if (pubkeyhash.toString('hex') === myPossiblePubkeyhashbuf.toString('hex'))
|
|
return true;
|
|
else
|
|
return false;
|
|
};
|
|
|
|
module.exports = Stealthkey;
|
|
|