commit
4a710e8fa6
|
@ -0,0 +1,20 @@
|
|||
var Identity = require('../lib/identity');
|
||||
var KeyPair = require('../lib/keypair');
|
||||
var Hash = require('../lib/hash');
|
||||
|
||||
var keypair = new KeyPair().fromRandom();
|
||||
|
||||
console.log( 'keypair:' , keypair );
|
||||
console.log( 'private key:' , keypair.privkey.toString('hex') );
|
||||
console.log( 'public key:' , keypair.pubkey.toString('hex') );
|
||||
console.log( 'public hash:' , Hash.sha256ripemd160( keypair.pubkey.toBuffer() ).toString('hex') );
|
||||
|
||||
|
||||
var identity = new Identity().fromPubkey( keypair.pubkey );
|
||||
|
||||
keypair.pubkey.compressed = false;
|
||||
var identityComp = new Identity().fromPubkey( keypair.pubkey );
|
||||
|
||||
console.log( 'identity:' , identity );
|
||||
console.log( 'identity string:' , identity.toString() );
|
||||
console.log( 'identity string, compressed:' , identityComp.toString() );
|
1
index.js
1
index.js
|
@ -13,6 +13,7 @@ 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');
|
||||
|
|
|
@ -98,7 +98,7 @@ Address.prototype.isValid = function() {
|
|||
};
|
||||
|
||||
Address.prototype.toBuffer = function() {
|
||||
version = new Buffer([constants[this.networkstr][this.typestr]]);
|
||||
var version = new Buffer([constants[this.networkstr][this.typestr]]);
|
||||
var buf = Buffer.concat([version, this.hashbuf]);
|
||||
return buf;
|
||||
};
|
||||
|
|
|
@ -1,15 +1,26 @@
|
|||
exports.mainnet = {
|
||||
pubkeyhash: 0x00,
|
||||
privkey: 0x80,
|
||||
scripthash: 0x05,
|
||||
bip32pubkey: 0x0488b21e,
|
||||
pubkeyhash: 0x00,
|
||||
identity: 0x0f,
|
||||
identephem: 0x02,
|
||||
identpersist: 0x01,
|
||||
privkey: 0x80,
|
||||
scripthash: 0x05,
|
||||
bip32pubkey: 0x0488b21e,
|
||||
bip32privkey: 0x0488ade4,
|
||||
};
|
||||
|
||||
exports.testnet = {
|
||||
pubkeyhash: 0x6f,
|
||||
privkey: 0xef,
|
||||
scripthash: 0xc4,
|
||||
bip32pubkey: 0x043587cf,
|
||||
pubkeyhash: 0x6f,
|
||||
identity: 0x0f,
|
||||
identephem: 0x02,
|
||||
identpersist: 0x11,
|
||||
privkey: 0xef,
|
||||
scripthash: 0xc4,
|
||||
bip32pubkey: 0x043587cf,
|
||||
bip32privkey: 0x04358394,
|
||||
};
|
||||
|
||||
exports.ephemeral = {
|
||||
prefix: 0x0f,
|
||||
identity: 0x02
|
||||
}
|
||||
|
|
|
@ -0,0 +1,120 @@
|
|||
var base58check = require('./base58check');
|
||||
var constants = require('./constants');
|
||||
var Hash = require('./hash');
|
||||
var Pubkey = require('./pubkey');
|
||||
var Script = require('./script');
|
||||
var Random = require('./random');
|
||||
|
||||
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;
|
|
@ -3,16 +3,24 @@ var Pubkey = require('./pubkey');
|
|||
var BN = require('./bn');
|
||||
var point = require('./point');
|
||||
|
||||
var Keypair = function Keypair(obj) {
|
||||
var Keypair = function Keypair(obj) {
|
||||
if (!(this instanceof Keypair))
|
||||
return new Keypair(obj);
|
||||
|
||||
// breaks some tests
|
||||
// TODO: allow keys to be created with simply `new Keypair()` (random gen.)
|
||||
/*if (!obj) {
|
||||
var privkey = Privkey().fromRandom();
|
||||
var obj = this.fromPrivkey( privkey );
|
||||
}*/
|
||||
|
||||
if (obj)
|
||||
this.set(obj);
|
||||
};
|
||||
|
||||
Keypair.prototype.set = function(obj) {
|
||||
this.privkey = obj.privkey || this.privkey || undefined;
|
||||
this.pubkey = obj.pubkey || this.pubkey || undefined;
|
||||
this.pubkey = obj.pubkey || this.pubkey || undefined;
|
||||
return this;
|
||||
};
|
||||
|
||||
|
|
|
@ -68,8 +68,9 @@ Pubkey.prototype.fromDER = function(buf) {
|
|||
return this;
|
||||
};
|
||||
|
||||
Pubkey.prototype.fromString = function(str) {
|
||||
this.fromDER(new Buffer(str, 'hex'));
|
||||
Pubkey.prototype.fromString = function( str , encoding ) {
|
||||
var encoding = encoding || 'hex';
|
||||
this.fromDER( new Buffer(str, encoding ) );
|
||||
};
|
||||
|
||||
Pubkey.prototype.fromX = function(odd, x) {
|
||||
|
|
|
@ -5,7 +5,8 @@
|
|||
"author": "BitPay <dev@bitpay.com>",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "mocha"
|
||||
"test": "mocha",
|
||||
"coverage": "istanbul cover _mocha"
|
||||
},
|
||||
"contributors": [
|
||||
{
|
||||
|
|
|
@ -0,0 +1,171 @@
|
|||
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"');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
Loading…
Reference in New Issue