Manuel Araoz
10 years ago
6 changed files with 26 additions and 520 deletions
@ -1,123 +0,0 @@ |
|||
'use strict'; |
|||
|
|||
var base58check = require('../protocol/base58check'); |
|||
var constants = require('./constants'); |
|||
var Hash = require('../crypto/hash'); |
|||
|
|||
|
|||
/** |
|||
* See https://en.bitcoin.it/wiki/Identity_protocol_v1#Type_2_.28ephemeral.29
|
|||
*/ |
|||
function Identity(buf) { |
|||
// TODO: instantiate identities without providing any configuration
|
|||
if (!(this instanceof Identity)) return new Identity(buf); |
|||
|
|||
if (Buffer.isBuffer(buf)) { |
|||
this.fromBuffer(buf); |
|||
} else if (typeof buf === 'string') { |
|||
var str = buf; |
|||
this.fromString(str); |
|||
} else if (buf) { |
|||
var obj = buf; |
|||
this.set(obj); |
|||
} |
|||
}; |
|||
|
|||
Identity.prototype.set = function(obj) { |
|||
this.hashbuf = obj.hashbuf || this.hashbuf || null; |
|||
this.networkstr = obj.networkstr || this.networkstr || 'ephemeral'; |
|||
this.typestr = obj.typestr || this.typestr || 'identity'; |
|||
return this; |
|||
}; |
|||
|
|||
Identity.prototype.fromBuffer = function(buf) { |
|||
// Identities are prefix + type + key
|
|||
if (buf.length !== 1 + 1 + 20) |
|||
throw new Error('Identity buffers must be exactly 22 bytes (was '+buf.length+')'); |
|||
|
|||
var prefix = buf[0]; |
|||
var version = buf[1]; |
|||
|
|||
if (version === constants['ephemeral']['identity']) { |
|||
this.networkstr = 'ephemeral'; |
|||
this.typestr = 'identity'; |
|||
} else if (version === constants['mainnet']['identity']) { |
|||
this.networkstr = 'mainnet'; |
|||
this.typestr = 'identity'; |
|||
} else if (version === constants['testnet']['identity']) { |
|||
this.networkstr = 'testnet'; |
|||
this.typestr = 'identity'; |
|||
} else { |
|||
this.networkstr = 'unknown'; |
|||
this.typestr = 'unknown'; |
|||
} |
|||
|
|||
if (prefix !== constants['ephemeral']['prefix']) |
|||
throw new Error('Identity buffers must contain an identity prefix ('+constants['ephemeral']['prefix']+', was '+ prefix.toString() + ')'); |
|||
|
|||
this.hashbuf = buf.slice( 2 ); |
|||
|
|||
return this; |
|||
}; |
|||
|
|||
Identity.prototype.fromHashbuf = function(hashbuf, networkstr, typestr) { |
|||
if (hashbuf.length !== 20) |
|||
throw new Error('hashbuf must be exactly 20 bytes'); |
|||
this.hashbuf = hashbuf; |
|||
this.networkstr = networkstr || 'ephemeral'; |
|||
this.typestr = typestr || 'identity'; |
|||
return this; |
|||
}; |
|||
|
|||
Identity.prototype.fromPubkey = function(pubkey, networkstr) { |
|||
this.hashbuf = Hash.sha256ripemd160( pubkey.toBuffer() ); |
|||
this.networkstr = networkstr || 'ephemeral'; |
|||
this.typestr = 'identity'; |
|||
return this; |
|||
}; |
|||
|
|||
Identity.prototype.fromString = function(str) { |
|||
var buf = base58check.decode(str); |
|||
return this.fromBuffer(buf); |
|||
} |
|||
|
|||
Identity.isValid = function(addrstr) { |
|||
try { |
|||
var address = new Identity().fromString( addrstr ); |
|||
} catch (e) { |
|||
return false; |
|||
} |
|||
return address.isValid(); |
|||
}; |
|||
|
|||
Identity.prototype.isValid = function() { |
|||
try { |
|||
this.validate(); |
|||
return true; |
|||
} catch (e) { |
|||
return false; |
|||
} |
|||
}; |
|||
|
|||
Identity.prototype.toBuffer = function() { |
|||
var prefix = new Buffer([ constants[ this.networkstr ][ 'prefix' ] ]) |
|||
var version = new Buffer([ constants[ this.networkstr ][ this.typestr ] ]);; |
|||
var buf = Buffer.concat([ prefix , version, this.hashbuf ]); |
|||
return buf; |
|||
}; |
|||
|
|||
Identity.prototype.toString = function() { |
|||
return base58check.encode( this.toBuffer() ); |
|||
}; |
|||
|
|||
Identity.prototype.validate = function() { |
|||
if (!Buffer.isBuffer(this.hashbuf) || this.hashbuf.length !== 20) |
|||
throw new Error('hash must be a buffer of 20 bytes'); |
|||
if (['ephemeral', 'mainnet', 'testnet'].indexOf( this.networkstr )) |
|||
throw new Error('networkstr must be "ephemeral", "mainnet", or "testnet"'); |
|||
if (this.typestr !== 'identity') |
|||
throw new Error('typestr must be "identity"'); |
|||
return this; |
|||
}; |
|||
|
|||
module.exports = Identity; |
@ -1,100 +0,0 @@ |
|||
'use strict'; |
|||
|
|||
var ECDSA = require('./ecdsa'); |
|||
var Keypair = require('../keypair'); |
|||
var BufferWriter = require('../protocol/bufferwriter'); |
|||
var Hash = require('../crypto/hash'); |
|||
var Address = require('../address'); |
|||
var Signature = require('../signature'); |
|||
|
|||
var Message = function Message(obj) { |
|||
if (!(this instanceof Message)) |
|||
return new Message(obj); |
|||
if (obj) |
|||
this.set(obj); |
|||
}; |
|||
|
|||
Message.prototype.set = function(obj) { |
|||
this.messagebuf = obj.messagebuf || this.messagebuf; |
|||
this.keypair = obj.keypair || this.keypair; |
|||
this.sig = obj.sig || this.sig; |
|||
this.address = obj.address || this.address; |
|||
this.verified = typeof obj.verified !== 'undefined' ? obj.verified : this.verified; |
|||
return this; |
|||
}; |
|||
|
|||
Message.magicBytes = new Buffer('Bitcoin Signed Message:\n'); |
|||
|
|||
Message.magicHash = function(messagebuf) { |
|||
if (!Buffer.isBuffer(messagebuf)) |
|||
throw new Error('messagebuf must be a buffer'); |
|||
var bw = new BufferWriter(); |
|||
bw.writeVarintNum(Message.magicBytes.length); |
|||
bw.write(Message.magicBytes); |
|||
bw.writeVarintNum(messagebuf.length); |
|||
bw.write(messagebuf); |
|||
var buf = bw.concat(); |
|||
|
|||
var hashbuf = Hash.sha256sha256(buf); |
|||
|
|||
return hashbuf; |
|||
}; |
|||
|
|||
Message.sign = function(messagebuf, keypair) { |
|||
var m = Message({ |
|||
messagebuf: messagebuf, |
|||
keypair: keypair |
|||
}); |
|||
m.sign(); |
|||
var sigbuf = m.sig.toCompact(); |
|||
var sigstr = sigbuf.toString('base64'); |
|||
return sigstr; |
|||
}; |
|||
|
|||
Message.verify = function(messagebuf, sigstr, address) { |
|||
var sigbuf = new Buffer(sigstr, 'base64'); |
|||
var message = new Message(); |
|||
message.messagebuf = messagebuf; |
|||
message.sig = Signature().fromCompact(sigbuf); |
|||
message.address = address; |
|||
|
|||
return message.verify().verified; |
|||
}; |
|||
|
|||
Message.prototype.sign = function() { |
|||
var hashbuf = Message.magicHash(this.messagebuf); |
|||
var ecdsa = ECDSA({ |
|||
hashbuf: hashbuf, |
|||
keypair: this.keypair |
|||
}); |
|||
ecdsa.signRandomK(); |
|||
ecdsa.calci(); |
|||
this.sig = ecdsa.sig; |
|||
return this; |
|||
}; |
|||
|
|||
Message.prototype.verify = function() { |
|||
var hashbuf = Message.magicHash(this.messagebuf); |
|||
|
|||
var ecdsa = new ECDSA(); |
|||
ecdsa.hashbuf = hashbuf; |
|||
ecdsa.sig = this.sig; |
|||
ecdsa.keypair = new Keypair(); |
|||
ecdsa.keypair.pubkey = ecdsa.sig2pubkey(); |
|||
|
|||
if (!ecdsa.verify()) { |
|||
this.verified = false; |
|||
return this; |
|||
} |
|||
|
|||
var address = Address().fromPubkey(ecdsa.keypair.pubkey, undefined, this.sig.compressed); |
|||
//TODO: what if livenet/testnet mismatch?
|
|||
if (address.hashbuf.toString('hex') === this.address.hashbuf.toString('hex')) |
|||
this.verified = true; |
|||
else |
|||
this.verified = false; |
|||
|
|||
return this; |
|||
}; |
|||
|
|||
module.exports = Message; |
@ -1,171 +0,0 @@ |
|||
var should = require('chai').should(); |
|||
var constants = require('../lib/constants'); |
|||
var PubKey = require('../lib/pubkey'); |
|||
var Identity = require('../lib/identity'); |
|||
|
|||
describe('Identity', function() { |
|||
|
|||
var knownPrivKey = 'L3e3ZneXzGw2wyyRoUxKGGrHCBhBE3uPMvQDXPaJTom4d4ogRxvC'; |
|||
var knownPubKey = '02ff0c643214634691e6f1c5044df79f7002c404407c8db1897484017e1082f182'; |
|||
var knownPubKeyHash = 'bceb8b52237d7a6c09e9aaedcf26cf387530d23e'; |
|||
var knownIdent = 'TfEmMAA5PSQRRJgiZka8y6B5x1pABHe6BVv'; |
|||
var knownIdentComp = 'TfDBCwB4ciatE4Kx3r1TK5kfCTxrrpG1H8J'; |
|||
|
|||
var pubkeyhash = new Buffer( knownPubKeyHash , 'hex'); |
|||
|
|||
//var buf = Buffer.concat([ new Buffer([0]), new Buffer([0]), pubkeyhash ]);
|
|||
// note: key is wrong string until I figure out how to duplicate the generation of short keys
|
|||
//var buf = Buffer.concat([ new Buffer( 0x0f ) , new Buffer( 0x02 ) , pubkeyhash ])
|
|||
var buf = Buffer.concat([ new Buffer([0x0f]) , new Buffer([0x02]) , pubkeyhash ]) |
|||
var str = knownIdent; |
|||
|
|||
it('should create a new identity object', function() { |
|||
var identity = new Identity(); |
|||
should.exist(identity); |
|||
identity = Identity(buf); |
|||
should.exist(identity); |
|||
identity = Identity(str); |
|||
should.exist(identity); |
|||
}); |
|||
|
|||
describe('@isValid', function() { |
|||
|
|||
it('should validate this valid identity string', function() { |
|||
Identity.isValid( str ).should.equal( true ); |
|||
}); |
|||
|
|||
it('should invalidate this valid identity string', function() { |
|||
Identity.isValid(str.substr(1)).should.equal(false); |
|||
}); |
|||
|
|||
}); |
|||
|
|||
describe('#fromBuffer', function() { |
|||
|
|||
it('should make an identity from a buffer', function() { |
|||
Identity().fromBuffer(buf).toString().should.equal(str); |
|||
}); |
|||
|
|||
}); |
|||
|
|||
describe('#fromHashbuf', function() { |
|||
|
|||
it('should make an identity from a hashbuf', function() { |
|||
Identity().fromHashbuf(pubkeyhash).toString().should.equal(str); |
|||
var a = Identity().fromHashbuf(pubkeyhash, 'testnet', 'scripthash'); |
|||
a.networkstr.should.equal('testnet'); |
|||
a.typestr.should.equal('scripthash'); |
|||
}); |
|||
|
|||
it('should throw an error for invalid length hashbuf', function() { |
|||
(function() { |
|||
Identity().fromHashbuf(buf); |
|||
}).should.throw('hashbuf must be exactly 20 bytes'); |
|||
}); |
|||
|
|||
}); |
|||
|
|||
describe('#fromPubkey', function() { |
|||
|
|||
it('should make this identity from a compressed pubkey', function() { |
|||
var pubkey = new PubKey(); |
|||
pubkey.fromDER(new Buffer( knownPubKey , 'hex')); |
|||
var identity = new Identity(); |
|||
identity.fromPubkey(pubkey); |
|||
identity.toString().should.equal( knownIdent ); |
|||
}); |
|||
|
|||
it('should make this identity from an uncompressed pubkey', function() { |
|||
var pubkey = new PubKey(); |
|||
pubkey.fromDER(new Buffer( knownPubKey , 'hex')); |
|||
var identity = new Identity(); |
|||
pubkey.compressed = false; |
|||
identity.fromPubkey(pubkey, 'ephemeral'); |
|||
identity.toString().should.equal( knownIdentComp ); |
|||
}); |
|||
|
|||
}); |
|||
|
|||
describe('#fromString', function() { |
|||
|
|||
it('should derive from this known ephemeral identity string', function() { |
|||
var identity = new Identity(); |
|||
identity.fromString( str ); |
|||
identity.toBuffer().slice(2).toString('hex').should.equal(pubkeyhash.toString('hex')); |
|||
}); |
|||
|
|||
}); |
|||
|
|||
describe('#isValid', function() { |
|||
|
|||
it('should describe this valid identity as valid', function() { |
|||
var identity = new Identity(); |
|||
identity.fromString( knownIdent ); |
|||
identity.isValid().should.equal(true); |
|||
}); |
|||
|
|||
it('should describe this identity with unknown network as invalid', function() { |
|||
var identity = new Identity(); |
|||
identity.fromString( knownIdent ); |
|||
identity.networkstr = 'unknown'; |
|||
identity.isValid().should.equal(false); |
|||
}); |
|||
|
|||
it('should describe this identity with unknown type as invalid', function() { |
|||
var identity = new Identity(); |
|||
identity.fromString( knownIdent ); |
|||
identity.typestr = 'unknown'; |
|||
identity.isValid().should.equal(false); |
|||
}); |
|||
|
|||
}); |
|||
|
|||
describe('#toBuffer', function() { |
|||
|
|||
it('should output this known hash', function() { |
|||
var identity = new Identity(); |
|||
identity.fromString(str); |
|||
identity.toBuffer().slice(2).toString('hex').should.equal(pubkeyhash.toString('hex')); |
|||
}); |
|||
|
|||
}); |
|||
|
|||
describe('#toString', function() { |
|||
|
|||
it('should output the same thing that was input', function() { |
|||
var identity = new Identity(); |
|||
identity.fromString(str); |
|||
identity.toString().should.equal(str); |
|||
}); |
|||
|
|||
}); |
|||
|
|||
describe('#validate', function() { |
|||
|
|||
it('should not throw an error on this valid identity', function() { |
|||
var identity = new Identity(); |
|||
identity.fromString(str); |
|||
should.exist(identity.validate()); |
|||
}); |
|||
|
|||
it('should throw an error on this invalid network', function() { |
|||
var identity = new Identity(); |
|||
identity.fromString(str); |
|||
identity.networkstr = 'unknown'; |
|||
(function() { |
|||
identity.validate(); |
|||
}).should.throw('networkstr must be "ephemeral", "mainnet", or "testnet"'); |
|||
}); |
|||
|
|||
it('should throw an error on this invalid type', function() { |
|||
var identity = new Identity(); |
|||
identity.fromString(str); |
|||
identity.typestr = 'unknown'; |
|||
(function() { |
|||
identity.validate(); |
|||
}).should.throw('typestr must be "identity"'); |
|||
}); |
|||
|
|||
}); |
|||
|
|||
}); |
@ -1,107 +0,0 @@ |
|||
var Address = require('../lib/address'); |
|||
var Message = require('../lib/message'); |
|||
var Keypair = require('../lib/keypair'); |
|||
var should = require('chai').should(); |
|||
|
|||
describe('Message', function() { |
|||
|
|||
it('should make a new message', function() { |
|||
var message = new Message(); |
|||
should.exist(message); |
|||
}); |
|||
|
|||
it('should make a new message when called without "new"', function() { |
|||
var message = Message(); |
|||
should.exist(message); |
|||
}); |
|||
|
|||
describe('#set', function() { |
|||
|
|||
it('should set the messagebuf', function() { |
|||
var messagebuf = new Buffer('message'); |
|||
should.exist(Message().set({messagebuf: messagebuf}).messagebuf); |
|||
}); |
|||
|
|||
}); |
|||
|
|||
describe('@sign', function() { |
|||
var messagebuf = new Buffer('this is my message'); |
|||
var keypair = Keypair().fromRandom(); |
|||
|
|||
it('should return a base64 string', function() { |
|||
var sigstr = Message.sign(messagebuf, keypair); |
|||
var sigbuf = new Buffer(sigstr, 'base64'); |
|||
sigbuf.length.should.equal(1 + 32 + 32); |
|||
}); |
|||
|
|||
it('should sign with a compressed pubkey', function() { |
|||
var keypair = Keypair().fromRandom(); |
|||
keypair.pubkey.compressed = true; |
|||
var sigstr = Message.sign(messagebuf, keypair); |
|||
var sigbuf = new Buffer(sigstr, 'base64'); |
|||
sigbuf[0].should.be.above(27 + 4 - 1); |
|||
sigbuf[0].should.be.below(27 + 4 + 4 - 1); |
|||
}); |
|||
|
|||
it('should sign with an uncompressed pubkey', function() { |
|||
var keypair = Keypair().fromRandom(); |
|||
keypair.pubkey.compressed = false; |
|||
var sigstr = Message.sign(messagebuf, keypair); |
|||
var sigbuf = new Buffer(sigstr, 'base64'); |
|||
sigbuf[0].should.be.above(27 - 1); |
|||
sigbuf[0].should.be.below(27 + 4 - 1); |
|||
}); |
|||
|
|||
}); |
|||
|
|||
describe('@verify', function() { |
|||
var messagebuf = new Buffer('this is my message'); |
|||
var keypair = Keypair().fromRandom(); |
|||
|
|||
it('should verify a signed message', function() { |
|||
var sigstr = Message.sign(messagebuf, keypair); |
|||
var addr = Address().fromPubkey(keypair.pubkey); |
|||
Message.verify(messagebuf, sigstr, addr).should.equal(true); |
|||
}); |
|||
|
|||
it('should verify this known good signature', function() { |
|||
var addrstr = '1CKTmxj6DjGrGTfbZzVxnY4Besbv8oxSZb'; |
|||
var address = Address().fromString(addrstr); |
|||
var sigstr = 'IOrTlbNBI0QO990xOw4HAjnvRl/1zR+oBMS6HOjJgfJqXp/1EnFrcJly0UcNelqJNIAH4f0abxOZiSpYmenMH4M='; |
|||
Message.verify(messagebuf, sigstr, address); |
|||
}); |
|||
|
|||
}); |
|||
|
|||
describe('#sign', function() { |
|||
var messagebuf = new Buffer('this is my message'); |
|||
var keypair = Keypair().fromRandom(); |
|||
|
|||
it('should sign a message', function() { |
|||
var message = new Message(); |
|||
message.messagebuf = messagebuf; |
|||
message.keypair = keypair; |
|||
message.sign(); |
|||
var sig = message.sig; |
|||
should.exist(sig); |
|||
}); |
|||
|
|||
}); |
|||
|
|||
describe('#verify', function() { |
|||
var messagebuf = new Buffer('this is my message'); |
|||
var keypair = Keypair().fromRandom(); |
|||
|
|||
it('should verify a message that was just signed', function() { |
|||
var message = new Message(); |
|||
message.messagebuf = messagebuf; |
|||
message.keypair = keypair; |
|||
message.address = Address().fromPubkey(keypair.pubkey); |
|||
message.sign(); |
|||
message.verify(); |
|||
message.verified.should.equal(true); |
|||
}); |
|||
|
|||
}); |
|||
|
|||
}); |
Loading…
Reference in new issue