Browse Source

Stealth Addresses: Table stealth addresses until a later date.

patch-2
Braydon Fuller 10 years ago
parent
commit
16120c1148
  1. 127
      lib/expmt/stealthaddress.js
  2. 88
      lib/expmt/stealthkey.js
  3. 85
      lib/expmt/stealthmessage.js
  4. 69
      lib/expmt/stealthtx.js
  5. 149
      test/stealthaddress.js
  6. 138
      test/stealthkey.js
  7. 139
      test/stealthmessage.js
  8. 68
      test/stealthtx.js

127
lib/expmt/stealthaddress.js

@ -1,127 +0,0 @@
var Stealthkey = require('./stealthkey');
var Base58check = require('../base58check');
var Pubkey = require('../pubkey');
var KDF = require('../kdf');
var BufferWriter = require('../bufferwriter');
var BufferReader = require('../bufferreader');
var StealthAddress = function StealthAddress(addrstr) {
if (!(this instanceof StealthAddress))
return new StealthAddress(addrstr);
if (typeof addrstr === 'string') {
this.fromString(addrstr)
}
else if (Buffer.isBuffer(addrstr)) {
var buf = addrstr;
this.fromBuffer(buf);
}
else if (addrstr) {
var obj = addrstr;
this.set(obj);
}
};
StealthAddress.mainver = 42;
StealthAddress.testver = 43;
StealthAddress.prototype.set = function(obj) {
this.payloadPubkey = obj.payloadPubkey || this.payloadPubkey;
this.scanPubkey = obj.scanPubkey || this.scanPubkey;
return this;
};
StealthAddress.prototype.fromJSON = function(json) {
this.fromString(json);
return this;
};
StealthAddress.prototype.toJSON = function() {
return this.toString();
};
StealthAddress.prototype.fromStealthkey = function(stealthkey) {
this.set({
payloadPubkey: stealthkey.payloadKeypair.pubkey,
scanPubkey: stealthkey.scanKeypair.pubkey
});
return this;
};
StealthAddress.prototype.fromBuffer = function(buf) {
var parsed = StealthAddress.parseDWBuffer(buf);
if ((parsed.version !== StealthAddress.mainver) && (parsed.version !== StealthAddress.testver))
throw new Error('Invalid version');
if (parsed.options !== 0)
throw new Error('Invalid options');
if (!parsed.scanPubkey)
throw new Error('Invalid scanPubkey');
if (parsed.payloadPubkeys.length !== 1)
throw new Error('Must have exactly one payloadPubkey');
if (parsed.nSigs !== 1)
throw new Error('Must require exactly one signature');
if (parsed.prefix.toString() !== "")
throw new Error('Only blank prefixes supported');
this.scanPubkey = parsed.scanPubkey;
this.payloadPubkey = parsed.payloadPubkeys[0];
return this;
};
StealthAddress.prototype.fromString = function(str) {
return this.fromBuffer(Base58check(str).toBuffer());
};
StealthAddress.prototype.getSharedKeypair = function(senderKeypair) {
var sharedSecretPoint = this.scanPubkey.point.mul(senderKeypair.privkey.bn);
var sharedSecretPubkey = Pubkey(sharedSecretPoint);
var buf = sharedSecretPubkey.toDER(true);
var sharedKeypair = KDF.sha256hmac2keypair(buf);
return sharedKeypair;
};
StealthAddress.prototype.getReceivePubkey = function(senderKeypair) {
var sharedKeypair = this.getSharedKeypair(senderKeypair);
var pubkey = Pubkey(this.payloadPubkey.point.add(sharedKeypair.pubkey.point));
return pubkey;
};
StealthAddress.prototype.toBuffer = function(networkstr) {
if (networkstr === 'testnet')
var version = StealthAddress.testver;
else
var version = StealthAddress.mainver;
var bw = new BufferWriter();
bw.writeUInt8(version);
bw.writeUInt8(0); //options
bw.write(this.scanPubkey.toDER(true));
bw.writeUInt8(1); //number of payload keys - we only support 1 (not multisig)
bw.write(this.payloadPubkey.toDER(true));
bw.writeUInt8(1); //number of signatures - we only support 1 (not multisig)
bw.writeUInt8(0); //prefix length - we do not support prefix yet
var buf = bw.concat();
return buf;
};
StealthAddress.prototype.toString = function(networkstr) {
return Base58check(this.toBuffer(networkstr)).toString();
};
StealthAddress.parseDWBuffer = function(buf) {
var br = new BufferReader(buf);
var parsed = {};
parsed.version = br.readUInt8();
parsed.options = br.readUInt8();
parsed.scanPubkey = Pubkey().fromBuffer(br.read(33));
parsed.nPayloadPubkeys = br.readUInt8();
parsed.payloadPubkeys = [];
for (var i = 0; i < parsed.nPayloadPubkeys; i++)
parsed.payloadPubkeys.push(Pubkey().fromBuffer(br.read(33)));
parsed.nSigs = br.readUInt8();
parsed.nPrefix = br.readUInt8();
parsed.prefix = br.read(parsed.nPrefix / 8);
return parsed;
};
module.exports = StealthAddress;

88
lib/expmt/stealthkey.js

@ -1,88 +0,0 @@
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.fromJSON = function(json) {
this.set({
payloadKeypair: Keypair().fromJSON(json.payloadKeypair),
scanKeypair: Keypair().fromJSON(json.scanKeypair)
});
return this;
};
Stealthkey.prototype.toJSON = function() {
return {
payloadKeypair: this.payloadKeypair.toJSON(),
scanKeypair: this.scanKeypair.toJSON()
};
};
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;

85
lib/expmt/stealthmessage.js

@ -1,85 +0,0 @@
var Stealthkey = require('./stealthkey');
var StealthAddress = require('./stealthaddress');
var ECIES = require('./ecies');
var Message = require('../message');
var Keypair = require('../keypair');
var Address = require('../address');
var Pubkey = require('../pubkey');
var StealthMessage = function StealthMessage(obj) {
if (!(this instanceof StealthMessage))
return new StealthMessage(obj);
if (obj)
this.set(obj);
};
StealthMessage.prototype.set = function(obj) {
this.messagebuf = obj.messagebuf || this.messagebuf;
this.encbuf = obj.encbuf || this.encbuf;
this.toStealthAddress = obj.toStealthAddress || this.toStealthAddress;
this.toStealthkey = obj.toStealthkey || this.toStealthkey;
this.fromKeypair = obj.fromKeypair || this.fromKeypair;
this.receiveAddress = obj.receiveAddress || this.receiveAddress;
return this;
};
StealthMessage.encrypt = function(messagebuf, toStealthAddress, fromKeypair, ivbuf) {
var sm = StealthMessage().set({
messagebuf: messagebuf,
toStealthAddress: toStealthAddress,
fromKeypair: fromKeypair
});
sm.encrypt(ivbuf);
var buf = Buffer.concat([
sm.receiveAddress.hashbuf,
sm.fromKeypair.pubkey.toDER(true),
sm.encbuf
]);
return buf;
};
StealthMessage.decrypt = function(buf, toStealthkey) {
var sm = StealthMessage().set({
toStealthkey: toStealthkey,
receiveAddress: Address().set({hashbuf: buf.slice(0, 20)}),
fromKeypair: Keypair().set({pubkey: Pubkey().fromDER(buf.slice(20, 20 + 33))}),
encbuf: buf.slice(20 + 33)
});
return sm.decrypt().messagebuf;
};
StealthMessage.isForMe = function(buf, toStealthkey) {
var sm = StealthMessage().set({
toStealthkey: toStealthkey,
receiveAddress: Address().set({hashbuf: buf.slice(0, 20)}),
fromKeypair: Keypair().set({pubkey: Pubkey().fromDER(buf.slice(20, 20 + 33))}),
encbuf: buf.slice(20 + 33)
});
return sm.isForMe();
};
StealthMessage.prototype.encrypt = function(ivbuf) {
if (!this.fromKeypair)
this.fromKeypair = Keypair().fromRandom();
var receivePubkey = this.toStealthAddress.getReceivePubkey(this.fromKeypair);
this.receiveAddress = Address().fromPubkey(receivePubkey);
this.encbuf = ECIES.encrypt(this.messagebuf, receivePubkey, this.fromKeypair, ivbuf);
return this;
};
StealthMessage.prototype.decrypt = function() {
var receiveKeypair = this.toStealthkey.getReceiveKeypair(this.fromKeypair.pubkey);
this.messagebuf = ECIES.decrypt(this.encbuf, receiveKeypair.privkey);
return this;
};
StealthMessage.prototype.isForMe = function() {
var receivePubkey = this.toStealthkey.getReceivePubkey(this.fromKeypair.pubkey);
var receiveAddress = Address().fromPubkey(receivePubkey);
if (receiveAddress.toString('hex') === this.receiveAddress.toString('hex'))
return true;
else
return false;
};
module.exports = StealthMessage;

69
lib/expmt/stealthtx.js

@ -1,69 +0,0 @@
var StealthAddress = require('./stealthaddress');
var StealthKey = require('./stealthkey');
var Transaction = require('../transaction');
var Pubkey = require('../pubkey');
var StealthTx = function StealthTx(tx, sa, sk) {
if (!(this instanceof StealthTx))
return new StealthTx(tx, sa, sk);
if (tx instanceof Transaction) {
this.tx = tx;
this.sa = sa;
this.sk = sk;
} else if (tx) {
var obj = tx;
this.set(obj);
}
};
StealthTx.prototype.set = function(obj) {
this.sk = obj.sk || this.sk;
this.sa = obj.sa || this.sa;
this.tx = obj.tx || this.tx;
return this;
};
StealthTx.prototype.isForMe = function() {
if (!this.notMine())
return true;
else
return false;
};
StealthTx.prototype.notMine = function() {
var err;
if (err = this.notStealth())
return "Not stealth: " + err;
var txopbuf = this.tx.txouts[0].script.chunks[1].buf;
var parsed = StealthTx.parseOpReturnData(txopbuf);
var pubkey = parsed.pubkey;
var pubkeyhashbuf = this.tx.txouts[1].script.chunks[2].buf;
var sk = this.sk;
if (sk.isForMe(pubkey, pubkeyhashbuf)) {
return false;
} else {
return "StealthTx not mine";
}
};
//For now, we only support a very limited variety of stealth tx
StealthTx.prototype.notStealth = function() {
var txouts = this.tx.txouts;
if (!(txouts.length >= 2))
return "Not enough txouts";
if (!txouts[0].script.isOpReturn())
return "First txout is not OP_RETURN";
if (!txouts[1].script.isPubkeyhashOut())
return "Second txout is not pubkeyhash";
return false;
};
StealthTx.parseOpReturnData = function(buf) {
var parsed = {};
parsed.version = buf[0];
parsed.noncebuf = buf.slice(1, 5);
parsed.pubkey = Pubkey().fromBuffer(buf.slice(5, 5 + 33));
return parsed;
};
module.exports = StealthTx;

149
test/stealthaddress.js

@ -1,149 +0,0 @@
var StealthAddress = require('../lib/expmt/stealthaddress');
var should = require('chai').should();
var Stealthkey = require('../lib/expmt/stealthkey');
var Keypair = require('../lib/keypair');
var Privkey = require('../lib/privkey');
var Pubkey = require('../lib/pubkey');
var BN = require('../lib/bn');
var Hash = require('../lib/hash');
var Base58check = require('../lib/base58check');
describe('StealthAddress', function() {
var stealthkey = Stealthkey();
stealthkey.payloadKeypair = Keypair();
stealthkey.payloadKeypair.privkey = Privkey();
stealthkey.payloadKeypair.privkey.bn = BN().fromBuffer(Hash.sha256(new Buffer('test 1')));
stealthkey.payloadKeypair.privkey2pubkey();
stealthkey.scanKeypair = Keypair();
stealthkey.scanKeypair.privkey = Privkey();
stealthkey.scanKeypair.privkey.bn = BN().fromBuffer(Hash.sha256(new Buffer('test 2')));
stealthkey.scanKeypair.privkey2pubkey();
var senderKeypair = Keypair();
senderKeypair.privkey = Privkey();
senderKeypair.privkey.bn = BN().fromBuffer(Hash.sha256(new Buffer('test 3')));
senderKeypair.privkey2pubkey();
var addressString = 'vJmtuUb8ysKiM1HtHQF23FGfjGAKu5sM94UyyjknqhJHNdj5CZzwtpGzeyaATQ2HvuzomNVtiwsTJSWzzCBgCTtUZbRFpzKVq9MAUr';
var dwhex = '2a0002697763d7e9becb0c180083738c32c05b0e2fee26d6278020c06bbb04d5f66b32010362408459041e0473298af3824dbabe4d2b7f846825ed4d1c2e2c670c07cb275d0100';
var dwbuf = new Buffer(dwhex, 'hex');
it('should make a new stealth address', function() {
var sa = new StealthAddress();
should.exist(sa);
sa = StealthAddress();
should.exist(sa);
sa = StealthAddress(addressString);
should.exist(sa);
sa = StealthAddress(Base58check.decode(addressString));
should.exist(sa);
});
describe('#fromJSON', function() {
it('should give a stealthkey address with the right pubkeys', function() {
var sa = new StealthAddress();
sa.fromJSON(addressString);
sa.payloadPubkey.toString().should.equal(stealthkey.payloadKeypair.pubkey.toString());
sa.scanPubkey.toString().should.equal(stealthkey.scanKeypair.pubkey.toString());
});
});
describe('#toJSON', function() {
it('should return this known address string', function() {
StealthAddress().fromJSON(addressString).toJSON().should.equal(addressString);
});
});
describe('#fromBuffer', function() {
it('should parse this DW buffer', function() {
StealthAddress().fromBuffer(new Buffer(dwhex, 'hex')).toBuffer().toString('hex').should.equal(dwhex);
});
});
describe('#fromString', function() {
it('should parse this DW buffer', function() {
StealthAddress().fromString(Base58check(new Buffer(dwhex, 'hex')).toString()).toBuffer().toString('hex').should.equal(dwhex);
});
});
describe('#getSharedKeypair', function() {
it('should return a key', function() {
var sa = new StealthAddress();
sa.payloadPubkey = stealthkey.payloadKeypair.pubkey;
sa.scanPubkey = stealthkey.scanKeypair.pubkey;
var key = sa.getSharedKeypair(senderKeypair);
(key instanceof Keypair).should.equal(true);
});
it('should return the same key as Stealthkey.prototype.getSharedKeypair', function() {
var sa = new StealthAddress();
sa.payloadPubkey = stealthkey.payloadKeypair.pubkey;
sa.scanPubkey = stealthkey.scanKeypair.pubkey;
var key = sa.getSharedKeypair(senderKeypair);
var key2 = stealthkey.getSharedKeypair(senderKeypair.pubkey);
key.toString().should.equal(key2.toString());
});
});
describe('#getReceivePubkey', function() {
it('should return a pubkey', function() {
var pubkey = StealthAddress().fromStealthkey(stealthkey).getReceivePubkey(senderKeypair);
(pubkey instanceof Pubkey).should.equal(true);
});
it('should return the same pubkey as getReceivePubkey', function() {
var pubkey = StealthAddress().fromStealthkey(stealthkey).getReceivePubkey(senderKeypair);
var pubkey2 = stealthkey.getReceivePubkey(senderKeypair.pubkey);
pubkey2.toString().should.equal(pubkey.toString());
});
});
describe('#toBuffer', function() {
it('should return this known address buffer', function() {
var buf = Base58check.decode(addressString);
StealthAddress().fromBuffer(dwbuf).toBuffer().toString('hex').should.equal(dwhex);
});
});
describe('#toString', function() {
it('should return this known address buffer', function() {
var buf = Base58check.decode(addressString);
StealthAddress().fromBuffer(buf).toString().should.equal(Base58check(new Buffer(dwhex, 'hex')).toString());
});
});
describe('@parseDWBuffer', function() {
it('should parse this known DW buffer', function() {
var buf = new Buffer(dwhex, 'hex');
var parsed = StealthAddress.parseDWBuffer(buf);
parsed.version.should.equal(42);
parsed.options.should.equal(0);
parsed.scanPubkey.toString().should.equal('02697763d7e9becb0c180083738c32c05b0e2fee26d6278020c06bbb04d5f66b32');
parsed.nPayloadPubkeys.should.equal(1);
parsed.payloadPubkeys[0].toString().should.equal('0362408459041e0473298af3824dbabe4d2b7f846825ed4d1c2e2c670c07cb275d');
parsed.nSigs.should.equal(1);
parsed.prefix.toString().should.equal('');
});
});
});

138
test/stealthkey.js

@ -1,138 +0,0 @@
var should = require('chai').should();
var Stealthkey = require('../lib/expmt/stealthkey');
var Keypair = require('../lib/keypair');
var Privkey = require('../lib/privkey');
var Pubkey = require('../lib/pubkey');
var BN = require('../lib/bn');
var Hash = require('../lib/hash');
describe('Stealthkey', function() {
var stealthkey = Stealthkey();
stealthkey.payloadKeypair = Keypair();
stealthkey.payloadKeypair.privkey = Privkey();
stealthkey.payloadKeypair.privkey.bn = BN().fromBuffer(Hash.sha256(new Buffer('test 1')));
stealthkey.payloadKeypair.privkey2pubkey();
stealthkey.scanKeypair = Keypair();
stealthkey.scanKeypair.privkey = Privkey();
stealthkey.scanKeypair.privkey.bn = BN().fromBuffer(Hash.sha256(new Buffer('test 2')));
stealthkey.scanKeypair.privkey2pubkey();
var senderKeypair = Keypair();
senderKeypair.privkey = Privkey();
senderKeypair.privkey.bn = BN().fromBuffer(Hash.sha256(new Buffer('test 3')));
senderKeypair.privkey2pubkey();
it('should create a new stealthkey', function() {
var stealthkey = new Stealthkey();
should.exist(stealthkey);
});
it('should create a new stealthkey without using "new"', function() {
var stealthkey = Stealthkey();
should.exist(stealthkey);
});
it('should create a new stealthkey with both keypairs in the constructor', function() {
var keypair1 = Keypair();
var keypair2 = Keypair();
var stealthkey = Stealthkey(keypair1, keypair2);
should.exist(stealthkey.payloadKeypair);
should.exist(stealthkey.scanKeypair);
});
describe('#set', function() {
it('should set payload key', function() {
should.exist(Stealthkey().set({payloadKeypair: stealthkey.payloadKeypair}).payloadKeypair);
});
});
describe('#fromJSON', function() {
it('should make a stealthkey from this JSON', function() {
var sk = Stealthkey().fromJSON({
payloadKeypair: stealthkey.payloadKeypair.toJSON(),
scanKeypair: stealthkey.scanKeypair.toJSON()
});
sk.payloadKeypair.toString().should.equal(stealthkey.payloadKeypair.toString());
sk.scanKeypair.toString().should.equal(stealthkey.scanKeypair.toString());
});
});
describe('#toJSON', function() {
it('should convert this stealthkey to json', function() {
var json = stealthkey.toJSON()
var json2 = Stealthkey().fromJSON(json).toJSON();
json.payloadKeypair.privkey.should.equal(json2.payloadKeypair.privkey);
json.scanKeypair.privkey.should.equal(json2.scanKeypair.privkey);
});
});
describe('#fromRandom', function() {
it('should create a new stealthkey from random', function() {
var stealthkey = Stealthkey().fromRandom();
should.exist(stealthkey.payloadKeypair.privkey.bn.gt(0));
should.exist(stealthkey.scanKeypair.privkey.bn.gt(0));
});
});
describe('#getSharedKeypair', function() {
it('should return a key', function() {
var key = stealthkey.getSharedKeypair(senderKeypair.pubkey);
(key instanceof Keypair).should.equal(true);
});
});
describe('#getReceivePubkey', function() {
it('should return a pubkey', function() {
var pubkey = stealthkey.getReceivePubkey(senderKeypair.pubkey);
(pubkey instanceof Pubkey).should.equal(true);
});
});
describe('#getReceiveKeypair', function() {
it('should return a key', function() {
var key = stealthkey.getReceiveKeypair(senderKeypair.pubkey);
(key instanceof Keypair).should.equal(true);
});
it('should return a key with the same pubkey as getReceivePubkey', function() {
var key = stealthkey.getReceiveKeypair(senderKeypair.pubkey);
var pubkey = stealthkey.getReceivePubkey(senderKeypair.pubkey);
key.pubkey.toString().should.equal(pubkey.toString());
});
it('should return private key with length 32 or less', function() {
var key = stealthkey.getReceiveKeypair(senderKeypair.pubkey);
key.privkey.bn.toBuffer().length.should.be.below(33);
});
});
describe('#isForMe', function() {
it('should return true if it (the transaction or message) is for me', function() {
var pubkeyhash = new Buffer('3cb64fa6ee9b3e8754e3e2bd033bf61048604a99', 'hex');
stealthkey.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');
stealthkey.isForMe(senderKeypair.pubkey, pubkeyhash).should.equal(false);
});
});
});

139
test/stealthmessage.js

@ -1,139 +0,0 @@
var Keypair = require('../lib/keypair');
var StealthMessage = require('../lib/expmt/stealthmessage');
var Stealthkey = require('../lib/expmt/stealthkey');
var StealthAddress = require('../lib/expmt/stealthaddress');
var KDF = require('../lib/kdf');
var Hash = require('../lib/hash');
var should = require('chai').should();
var Address = require('../lib/address');
describe('StealthMessage', function() {
var payloadKeypair = KDF.buf2keypair(new Buffer('key1'));
var scanKeypair = KDF.buf2keypair(new Buffer('key2'));
var fromKeypair = KDF.buf2keypair(new Buffer('key3'));
var enchex = 'f557994f16d0d628fa4fdb4ab3d7e0bc5f2754f20381c7831a20c7c9ec88dcf092ea3683261798ccda991ed65a3a54a036d8125dec0381c7831a20c7c9ec88dcf092ea3683261798ccda991ed65a3a54a036d8125dec9f86d081884c7d659a2feaa0c55ad01560ba2904d3bc8395b6c4a6f87648edb33db6a22170e5e26f340c7ba943169210234cd6a753ad13919b0ab7d678b47b5e7d63e452382de2c2590fb57ef048f7b3';
var encbuf = new Buffer(enchex, 'hex');
var ivbuf = Hash.sha256(new Buffer('test')).slice(0, 128 / 8);
var sk = Stealthkey().set({payloadKeypair: payloadKeypair, scanKeypair: scanKeypair});
var sa = StealthAddress().fromStealthkey(sk);
var messagebuf = new Buffer('this is my message');
it('should make a new stealthmessage', function() {
var sm = new StealthMessage();
should.exist(sm);
sm = StealthMessage()
should.exist(sm);
});
it('should allow "set" style syntax', function() {
var encbuf = StealthMessage().set({
messagebuf: messagebuf,
toStealthAddress: sa
}).encrypt().encbuf;
should.exist(encbuf);
encbuf.length.should.equal(113);
});
describe('#set', function() {
it('should set the messagebuf', function() {
var sm = StealthMessage().set({messagebuf: messagebuf});
should.exist(sm.messagebuf);
});
});
describe('@encrypt', function() {
it('should encrypt a message', function() {
var encbuf = StealthMessage.encrypt(messagebuf, sa);
encbuf.length.should.equal(166);
});
it('should encrypt a message with this fromKeypair and ivbuf the same each time', function() {
var encbuf = StealthMessage.encrypt(messagebuf, sa, fromKeypair, ivbuf);
encbuf.length.should.equal(166);
encbuf.toString('hex').should.equal(enchex);
});
});
describe('@decrypt', function() {
it('should decrypt this known message correctly', function() {
var messagebuf2 = StealthMessage.decrypt(encbuf, sk);
messagebuf2.toString('hex').should.equal(messagebuf.toString('hex'));
});
});
describe('@isForMe', function() {
it('should know that this message is for me', function() {
StealthMessage.isForMe(encbuf, sk).should.equal(true);
});
it('should know that this message is for me even if my payloadPrivkey is not present', function() {
var sk2 = new Stealthkey();
sk2.scanKeypair = sk.scanKeypair;
sk2.payloadKeypair = Keypair().set({pubkey: sk.payloadKeypair.pubkey});
should.not.exist(sk2.payloadKeypair.privkey);
StealthMessage.isForMe(encbuf, sk2).should.equal(true);
});
});
describe('#encrypt', function() {
it('should encrypt this message', function() {
var sm = StealthMessage().set({
messagebuf: messagebuf,
toStealthAddress: sa,
fromKeypair: fromKeypair
});
sm.encrypt().encbuf.length.should.equal(113);
});
});
describe('#decrypt', function() {
it('should decrypt that which was encrypted', function() {
var sm = StealthMessage().set({
messagebuf: messagebuf,
toStealthAddress: sa
}).encrypt();
var messagebuf2 = StealthMessage().set({
encbuf: sm.encbuf,
fromKeypair: sm.fromKeypair,
toStealthkey: sk
}).decrypt().messagebuf;
messagebuf2.toString('hex').should.equal(messagebuf.toString('hex'));
});
});
describe('#isForMe', function() {
it('should know that this message is for me', function() {
StealthMessage().set({
encbuf: encbuf,
toStealthkey: sk,
fromKeypair: fromKeypair,
receiveAddress: Address().set({hashbuf: encbuf.slice(0, 20)})
}).isForMe().should.equal(true);
});
it('should know that this message is not for me', function() {
StealthMessage().set({
encbuf: encbuf,
toStealthkey: sk,
fromKeypair: fromKeypair,
receiveAddress: Address().set({hashbuf: encbuf.slice(0, 20)})
}).isForMe().should.equal(true);
});
});
});

68
test/stealthtx.js

@ -1,68 +0,0 @@
var should = require('chai').should();
var Txout = require('../lib/txout');
var Stealthkey = require('../lib/expmt/stealthkey');
var StealthTx = require('../lib/expmt/stealthtx');
var Transaction = require('../lib/transaction');
var Varint = require('../lib/varint');
describe('StealthTx', function() {
var txhex = '0100000001c828ccce36eca04f96321ad488528af86c7598e67157c4f8e2f462a9e0e3af5f010000006a47304402204525eef6a56cc57fb184e53efdfdc1086d5265da21480d55c2184536440a64f70220349cdc6c66a8507dde0d172fe64aeb57ae56e014b50315f615086a6b85c5424e012102c0633ddb6bf2a8686e2ba4ce8026c94e1e27ef12e73f8fed6d6d2b97199f9b74ffffffff020000000000000000286a2606deadbeef0365b5a5b0ba059666e907b0b5e07b37fdb162d1399ed829315491fe1f30c87b3f905f0100000000001976a9142042d5e7ef9e82346419fbfe7df5ae52fe4bea3c88ac00000000';
var txbuf = new Buffer(txhex, 'hex');
var txidhex = '66da969fff214c329e27062beaf3baf20ed035801559b31f3e868c2de4cdfc5b';
var tx = Transaction(txbuf);
it('should make a new StealthTx', function() {
var stx = new StealthTx();
should.exist(stx);
stx = StealthTx();
should.exist(stx);
});
describe('#isForMe', function() {
it('should return false for this known tx and random stealthkey', function() {
var sk = Stealthkey().fromRandom();
var stx = StealthTx().set({sk: sk, tx: tx});
stx.isForMe().should.equal(false);
});
});
describe('#notMine', function() {
it('should return true for this known tx and random stealthkey', function() {
var sk = Stealthkey().fromRandom();
var stx = StealthTx().set({sk: sk, tx: tx});
stx.notMine().should.equal("StealthTx not mine");
});
});
describe('#notStealth', function() {
it('should know this is a stealth tx', function() {
var stx = StealthTx().set({tx: tx});
stx.notStealth().should.equal(false);
});
it('should know this is not a stealth tx', function() {
var tx2 = Transaction(tx);
tx2.txouts.pop();
tx2.txoutsvi = Varint(1);
var stx = StealthTx().set({tx: tx2});
stx.notStealth().should.equal("Not enough txouts");
});
});
describe('@parseOpReturnData', function() {
var txout = tx.txouts[0];
var buf = txout.script.chunks[1].buf;
var parsed = StealthTx.parseOpReturnData(buf);
(typeof parsed.version).should.equal('number');
parsed.noncebuf.length.should.be.above(0);
parsed.pubkey.toBuffer().length.should.equal(33);
});
});
Loading…
Cancel
Save