StealthMessage example
This commit is contained in:
parent
5a86a1a5c6
commit
88f3690ef6
|
@ -0,0 +1,53 @@
|
|||
var Pubkey = require('../lib/pubkey');
|
||||
var Address = require('../lib/address');
|
||||
var Stealthkey = require('../lib/expmt/stealthkey');
|
||||
var StealthAddress = require('../lib/expmt/stealthaddress');
|
||||
var StealthMessage = require('../lib/expmt/stealthmessage');
|
||||
var Keypair = require('../lib/keypair')
|
||||
|
||||
//First, the person receiving must make a stealth key.
|
||||
|
||||
var sk = Stealthkey().fromRandom();
|
||||
|
||||
//It has an associated stealth address.
|
||||
|
||||
var sa = StealthAddress().fromStealthkey(sk);
|
||||
|
||||
console.log('Stealth address: ' + sa);
|
||||
|
||||
//The person sending must have a keypair.
|
||||
//It is best to make a new one for each message sent.
|
||||
|
||||
var keypair = Keypair().fromRandom();
|
||||
|
||||
//Now make a message.
|
||||
|
||||
var messagebuf = new Buffer('Hello there. Only you know this message is to, and only you know what it says.');
|
||||
|
||||
//Encrypt the message with the stealth address.
|
||||
|
||||
var encbuf = StealthMessage.encrypt(messagebuf, sa);
|
||||
|
||||
console.log('Hex of the encrypted message: ' + encbuf.toString('hex'));
|
||||
|
||||
//Note that the first 20 bytes are a pubkeyhash, which may be interpreted as a bitcoin address.
|
||||
//This address has never been seen before in public.
|
||||
|
||||
var address = Address().set({hashbuf: encbuf.slice(0, 20)});
|
||||
|
||||
console.log('The randomly generated address the message is to: ' + address);
|
||||
|
||||
//And the next 33 bytes are a nonce public key, which the message is "from".
|
||||
//It has never been seen before in public.
|
||||
|
||||
var pubkey = Pubkey().fromDER(encbuf.slice(20, 20 + 33));
|
||||
|
||||
//The owner of the stealth key can check to see if it is for them.
|
||||
|
||||
console.log('Is the message for me? ' + (StealthMessage.isForMe(encbuf, sk) ? "yes" : "no"));
|
||||
|
||||
//The owner can decrypt it.
|
||||
|
||||
var messagebuf2 = StealthMessage.decrypt(encbuf, sk);
|
||||
|
||||
console.log('Decrypted message: ' + messagebuf2.toString());
|
|
@ -0,0 +1,29 @@
|
|||
var should = require('chai').should();
|
||||
var fs = require('fs');
|
||||
|
||||
describe('Examples', function() {
|
||||
|
||||
var filenames = fs.readdirSync(__dirname + '/../examples/');
|
||||
|
||||
filenames.forEach(function(filename) {
|
||||
|
||||
if (filename.slice(filename.length - 3) === '.js') {
|
||||
|
||||
describe(filename, function() {
|
||||
|
||||
it('should not throw any errors', function() {
|
||||
(function() {
|
||||
var save = console.log;
|
||||
console.log = function() {};
|
||||
require('../examples/' + filename);
|
||||
console.log = save;
|
||||
}).should.not.throw();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
});
|
Loading…
Reference in New Issue