Clean example for full identities.
This commit is contained in:
parent
4dfe9fd13d
commit
dac2acc743
|
@ -1,14 +1,12 @@
|
|||
var Identity = require('../lib/identity');
|
||||
var Keypair = require('../lib/keypair');
|
||||
var KeyPair = require('../lib/keypair');
|
||||
|
||||
var pubkeyhash = new Buffer('3c3fa3d4adcaf8f52d5b1843975e122548269937', 'hex');
|
||||
var buf = Buffer.concat([new Buffer([0]), pubkeyhash]);
|
||||
var keypair = new KeyPair().fromRandom();
|
||||
|
||||
console.log( 'keypair:' , keypair );
|
||||
console.log( 'public key:' , keypair.pubkey.toString() );
|
||||
|
||||
var keypair = new Keypair();
|
||||
//var identity = new Identity().fromPubkey( keypair.pubkey );
|
||||
var identity = new Identity().fromPubkey( keypair.pubkey );
|
||||
|
||||
console.log( 'pubkey', keypair.pubkey.toString() );
|
||||
console.log( 'privkey', keypair.privkey.toString() );
|
||||
console.log( identity );
|
||||
console.log( identity.toString() );
|
||||
console.log( 'identity:' , identity );
|
||||
console.log( 'identity string:' , identity.toString() );
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
exports.mainnet = {
|
||||
pubkeyhash: 0x00,
|
||||
identpersist: 0x01,
|
||||
identity: 0x0f,
|
||||
identephem: 0x02,
|
||||
identpersist: 0x01,
|
||||
privkey: 0x80,
|
||||
scripthash: 0x05,
|
||||
bip32pubkey: 0x0488b21e,
|
||||
|
@ -10,8 +11,16 @@ exports.mainnet = {
|
|||
|
||||
exports.testnet = {
|
||||
pubkeyhash: 0x6f,
|
||||
identity: 0x0f,
|
||||
identephem: 0x02,
|
||||
identpersist: 0x11,
|
||||
privkey: 0xef,
|
||||
scripthash: 0xc4,
|
||||
bip32pubkey: 0x043587cf,
|
||||
bip32privkey: 0x04358394,
|
||||
};
|
||||
|
||||
exports.ephemeral = {
|
||||
prefix: 0x0f,
|
||||
identity: 0x02
|
||||
}
|
||||
|
|
|
@ -22,28 +22,37 @@ function Identity(buf) {
|
|||
|
||||
Identity.prototype.set = function(obj) {
|
||||
this.hashbuf = obj.hashbuf || this.hashbuf || null;
|
||||
this.networkstr = obj.networkstr || this.networkstr || 'mainnet';
|
||||
this.typestr = obj.typestr || this.typestr || 'identephem';
|
||||
this.networkstr = obj.networkstr || this.networkstr || 'ephemeral';
|
||||
this.typestr = obj.typestr || this.typestr || 'identity';
|
||||
return this;
|
||||
};
|
||||
|
||||
Identity.prototype.fromBuffer = function(buf) {
|
||||
if (buf.length !== 1 + 20)
|
||||
throw new Error('Identity buffers must be exactly 21 bytes');
|
||||
var version = buf[0];
|
||||
// Identities are prefix + type + key
|
||||
if (buf.length !== 1 + 1 + 20)
|
||||
throw new Error('Identity buffers must be exactly 22 bytes');
|
||||
|
||||
var prefix = buf[0];
|
||||
var version = buf[1];
|
||||
|
||||
if (prefix !== constants['ephemeral'][ prefix ])
|
||||
throw new Error('Identity buffers must contain an identity prefix (0x0f)');
|
||||
|
||||
if (version === constants['mainnet']['identephem']) {
|
||||
if (version === constants['ephemeral']['identity']) {
|
||||
this.networkstr = 'ephemeral';
|
||||
this.typestr = 'identity';
|
||||
} else if (version === constants['mainnet']['identity']) {
|
||||
this.networkstr = 'mainnet';
|
||||
this.typestr = 'identephem';
|
||||
} else if (version === constants['mainnet']['identpersist']) {
|
||||
this.networkstr = 'mainnet';
|
||||
this.typestr = 'identpersist';
|
||||
this.typestr = 'identity';
|
||||
} else if (version === constants['testnet']['identity']) {
|
||||
this.networkstr = 'testnet';
|
||||
this.typestr = 'identity';
|
||||
} else {
|
||||
this.networkstr = 'unknown';
|
||||
this.typestr = 'unknown';
|
||||
}
|
||||
|
||||
this.hashbuf = buf.slice(1);
|
||||
this.hashbuf = buf.slice( 2 );
|
||||
|
||||
return this;
|
||||
};
|
||||
|
@ -51,16 +60,16 @@ Identity.prototype.fromBuffer = function(buf) {
|
|||
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 || 'mainnet';
|
||||
this.typestr = typestr || 'identephem';
|
||||
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 || 'mainnet';
|
||||
this.typestr = 'identephem';
|
||||
this.networkstr = networkstr || 'ephemeral';
|
||||
this.typestr = 'identity';
|
||||
return this;
|
||||
};
|
||||
|
||||
|
@ -71,7 +80,7 @@ Identity.prototype.fromString = function(str) {
|
|||
|
||||
Identity.isValid = function(addrstr) {
|
||||
try {
|
||||
var address = new Identity().fromString(addrstr);
|
||||
var address = new Identity().fromString( addrstr );
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
|
@ -88,21 +97,20 @@ Identity.prototype.isValid = function() {
|
|||
};
|
||||
|
||||
Identity.prototype.toBuffer = function() {
|
||||
console.log( this.networkstr );
|
||||
|
||||
version = new Buffer([constants[this.networkstr][this.typestr]]);
|
||||
var buf = Buffer.concat([version, this.hashbuf]);
|
||||
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());
|
||||
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 (this.networkstr !== 'mainnet' && this.networkstr !== 'testnet')
|
||||
if (['ephemeral', 'mainnet', 'testnet'].indexOf( this.networkstr ))
|
||||
throw new Error('networkstr must be "mainnet" or "testnet"');
|
||||
if (this.typestr !== 'identephem' && this.typestr !== 'identpersist')
|
||||
throw new Error('typestr must be "identephem" or "identpersist"');
|
||||
|
|
Loading…
Reference in New Issue