2015-02-04 09:09:58 -08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var chai = require('chai');
|
|
|
|
var expect = chai.expect;
|
|
|
|
var should = chai.should();
|
|
|
|
|
|
|
|
var bitcore = require('bitcore');
|
2015-02-05 06:09:30 -08:00
|
|
|
var Address = bitcore.Address;
|
2015-02-04 09:09:58 -08:00
|
|
|
var Signature = bitcore.crypto.Signature;
|
|
|
|
var Message = require('../');
|
|
|
|
|
|
|
|
describe('Message', function() {
|
|
|
|
|
|
|
|
var address = 'n1ZCYg9YXtB5XCZazLxSmPDa8iwJRZHhGx';
|
|
|
|
var badAddress = 'mmRcrB5fTwgxaFJmVLNtaG8SV454y1E3kC';
|
|
|
|
var privateKey = bitcore.PrivateKey.fromWIF('cPBn5A4ikZvBTQ8D7NnvHZYCAxzDZ5Z2TSGW2LkyPiLxqYaJPBW4');
|
|
|
|
var text = 'hello, world';
|
|
|
|
var signatureString = 'H/DIn8uA1scAuKLlCx+/9LnAcJtwQQ0PmcPrJUq90aboLv3fH5fFvY+vmbfOSFEtGarznYli6ShPr9RXwY9UrIY=';
|
|
|
|
|
|
|
|
var badSignatureString = 'H69qZ4mbZCcvXk7CWjptD5ypnYVLvQ3eMXLM8+1gX21SLH/GaFnAjQrDn37+TDw79i9zHhbiMMwhtvTwnPigZ6k=';
|
|
|
|
|
|
|
|
var signature = Signature.fromCompact(new Buffer(signatureString, 'base64'));
|
|
|
|
var badSignature = Signature.fromCompact(new Buffer(badSignatureString, 'base64'));
|
|
|
|
|
|
|
|
var publicKey = privateKey.toPublicKey();
|
|
|
|
|
|
|
|
it('will error with incorrect message type', function() {
|
2015-02-05 06:09:30 -08:00
|
|
|
expect(function() {
|
2015-02-04 09:09:58 -08:00
|
|
|
return new Message(new Date());
|
2015-02-05 06:06:32 -08:00
|
|
|
}).to.throw('First argument should be a string');
|
2015-02-04 09:09:58 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('will instantiate without "new"', function() {
|
|
|
|
var message = Message(text);
|
|
|
|
should.exist(message);
|
|
|
|
});
|
|
|
|
|
|
|
|
var signature2;
|
|
|
|
var signature3;
|
|
|
|
|
|
|
|
it('can sign a message', function() {
|
|
|
|
var message2 = new Message(text);
|
|
|
|
signature2 = message2._sign(privateKey);
|
|
|
|
signature3 = Message(text).sign(privateKey);
|
|
|
|
should.exist(signature2);
|
|
|
|
should.exist(signature3);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sign will error with incorrect private key argument', function() {
|
2015-02-05 06:09:30 -08:00
|
|
|
expect(function() {
|
2015-02-04 09:09:58 -08:00
|
|
|
var message3 = new Message(text);
|
|
|
|
return message3.sign('not a private key');
|
2015-02-05 06:06:32 -08:00
|
|
|
}).to.throw('First argument should be an instance of PrivateKey');
|
2015-02-04 09:09:58 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('can verify a message with signature', function() {
|
|
|
|
var message4 = new Message(text);
|
|
|
|
var verified = message4._verify(publicKey, signature2);
|
|
|
|
verified.should.equal(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can verify a message with existing signature', function() {
|
|
|
|
var message5 = new Message(text);
|
|
|
|
var verified = message5._verify(publicKey, signature);
|
|
|
|
verified.should.equal(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('verify will error with incorrect public key argument', function() {
|
2015-02-05 06:09:30 -08:00
|
|
|
expect(function() {
|
2015-02-04 09:09:58 -08:00
|
|
|
var message6 = new Message(text);
|
|
|
|
return message6._verify('not a public key', signature);
|
2015-02-05 06:53:13 -08:00
|
|
|
}).to.throw('First argument should be an instance of PublicKey');
|
2015-02-04 09:09:58 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('verify will error with incorrect signature argument', function() {
|
2015-02-05 06:09:30 -08:00
|
|
|
expect(function() {
|
2015-02-04 09:09:58 -08:00
|
|
|
var message7 = new Message(text);
|
|
|
|
return message7._verify(publicKey, 'not a signature');
|
2015-02-05 06:53:13 -08:00
|
|
|
}).to.throw('Second argument should be an instance of Signature');
|
2015-02-04 09:09:58 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('verify will correctly identify a bad signature', function() {
|
|
|
|
var message8 = new Message(text);
|
|
|
|
var verified = message8._verify(publicKey, badSignature);
|
|
|
|
should.exist(message8.error);
|
|
|
|
verified.should.equal(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can verify a message with address and generated signature string', function() {
|
|
|
|
var message9 = new Message(text);
|
|
|
|
var verified = message9.verify(address, signature3);
|
|
|
|
should.not.exist(message9.error);
|
|
|
|
verified.should.equal(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('will not verify with address mismatch', function() {
|
|
|
|
var message10 = new Message(text);
|
|
|
|
var verified = message10.verify(badAddress, signatureString);
|
|
|
|
should.exist(message10.error);
|
|
|
|
verified.should.equal(false);
|
|
|
|
});
|
|
|
|
|
2015-05-25 20:14:46 -07:00
|
|
|
it('will verify with an uncompressed pubkey', function() {
|
|
|
|
var privateKey = new bitcore.PrivateKey('5KYZdUEo39z3FPrtuX2QbbwGnNP5zTd7yyr2SC1j299sBCnWjss');
|
|
|
|
var message = new Message('This is an example of a signed message.');
|
|
|
|
var signature = message.sign(privateKey);
|
|
|
|
var verified = message.verify(privateKey.toAddress(), signature);
|
|
|
|
verified.should.equal(true);
|
|
|
|
});
|
|
|
|
|
2015-02-04 09:09:58 -08:00
|
|
|
it('can chain methods', function() {
|
|
|
|
var verified = Message(text).verify(address, signatureString);
|
|
|
|
verified.should.equal(true);
|
|
|
|
});
|
|
|
|
|
2015-02-08 01:16:18 -08:00
|
|
|
describe('#json', function() {
|
|
|
|
|
|
|
|
it('roundtrip to-from-to', function() {
|
|
|
|
var json = new Message(text).toJSON();
|
|
|
|
var message = Message.fromJSON(json);
|
2015-02-09 10:00:35 -08:00
|
|
|
message.toString().should.equal(text);
|
2015-02-08 01:16:18 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('checks that the string parameter is valid JSON', function() {
|
|
|
|
expect(function() {
|
|
|
|
return Message.fromJSON('¹');
|
|
|
|
}).to.throw();
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#toString', function() {
|
|
|
|
|
|
|
|
it('message string', function() {
|
|
|
|
var message = new Message(text);
|
|
|
|
message.toString().should.equal(text);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('roundtrip to-from-to', function() {
|
|
|
|
var str = new Message(text).toString();
|
|
|
|
var message = Message.fromString(str);
|
2015-02-09 10:00:35 -08:00
|
|
|
message.toString().should.equal(text);
|
2015-02-08 01:16:18 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#inspect', function() {
|
|
|
|
|
|
|
|
it('should output formatted output correctly', function() {
|
|
|
|
var message = new Message(text);
|
|
|
|
var output = '<Message: '+text+'>';
|
|
|
|
message.inspect().should.equal(output);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2015-02-05 06:09:30 -08:00
|
|
|
it('accepts Address for verification', function() {
|
|
|
|
var verified = Message(text)
|
|
|
|
.verify(new Address(address), signatureString);
|
|
|
|
verified.should.equal(true);
|
|
|
|
});
|
2015-02-08 01:16:18 -08:00
|
|
|
|
2015-02-04 09:09:58 -08:00
|
|
|
});
|