Browse Source

remove extras

patch-2
Manuel Araoz 10 years ago
parent
commit
a9d029341d
  1. 31
      index.js
  2. 123
      lib/extra/identity.js
  3. 100
      lib/extra/message.js
  4. 14
      test/address.js
  5. 171
      test/identity.js
  6. 107
      test/message.js

31
index.js

@ -1,27 +1,29 @@
var bitcore = module.exports;
//main bitcoin library
// protocol
bitcore.Constants = require('./lib/protocol/constants');
bitcore.Base58 = require('./lib/protocol/base58');
bitcore.Base58Check = require('./lib/protocol/base58check');
bitcore.BufferReader = require('./lib/protocol/bufferreader');
bitcore.BufferWriter = require('./lib/protocol/bufferwriter');
// crypto
bitcore.BN = require('./lib/crypto/bn');
bitcore.KDF = require('./lib/crypto/kdf');
bitcore.ECDSA = require('./lib/crypto/ecdsa');
bitcore.Hash = require('./lib/crypto/hash');
bitcore.Random = require('./lib/crypto/random');
bitcore.Point = require('./lib/crypto/point');
// main bitcoin library
bitcore.Address = require('./lib/address');
bitcore.Base58 = require('./lib/base58');
bitcore.Base58Check = require('./lib/base58check');
bitcore.BIP32 = require('./lib/bip32');
bitcore.Block = require('./lib/block');
bitcore.Blockheader = require('./lib/blockheader');
bitcore.BN = require('./lib/bn');
bitcore.BufferReader = require('./lib/bufferreader');
bitcore.BufferWriter = require('./lib/bufferwriter');
bitcore.Constants = require('./lib/constants');
bitcore.ECDSA = require('./lib/ecdsa');
bitcore.Hash = require('./lib/hash');
bitcore.Identity = require('./lib/identity');
bitcore.KDF = require('./lib/kdf');
bitcore.Keypair = require('./lib/keypair');
bitcore.Message = require('./lib/message');
bitcore.Opcode = require('./lib/opcode');
bitcore.Point = require('./lib/point');
bitcore.Privkey = require('./lib/privkey');
bitcore.Pubkey = require('./lib/pubkey');
bitcore.Random = require('./lib/random');
bitcore.Script = require('./lib/script');
bitcore.Signature = require('./lib/signature');
bitcore.Transaction = require('./lib/transaction');
@ -29,6 +31,7 @@ bitcore.Txin = require('./lib/txin');
bitcore.Txout = require('./lib/txout');
bitcore.Varint = require('./lib/varint');
//dependencies, subject to change
bitcore.deps = {};
bitcore.deps.aes = require('aes');

123
lib/extra/identity.js

@ -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;

100
lib/extra/message.js

@ -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;

14
test/address.js

@ -1,7 +1,11 @@
'use strict';
var should = require('chai').should();
var Pubkey = require('../lib/pubkey');
var Address = require('../lib/address');
var Script = require('../lib/script');
var bitcore = require('..');
var Pubkey = bitcore.Pubkey;
var Address = bitcore.Address;
var Script = bitcore.Script;
describe('Address', function() {
var pubkeyhash = new Buffer('3c3fa3d4adcaf8f52d5b1843975e122548269937', 'hex');
@ -11,9 +15,9 @@ describe('Address', function() {
it('should create a new address object', function() {
var address = new Address();
should.exist(address);
address = Address(buf);
address = new Address(buf);
should.exist(address);
address = Address(str);
address = new Address(str);
should.exist(address);
});

171
test/identity.js

@ -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"');
});
});
});

107
test/message.js

@ -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…
Cancel
Save