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