2014-07-10 11:55:32 -07:00
|
|
|
var base58 = require('../lib/Base58').base58Check;
|
|
|
|
var util = require('util');
|
|
|
|
var EncodedData = require('./EncodedData');
|
2013-07-10 10:03:05 -07:00
|
|
|
|
2014-03-05 12:37:16 -08:00
|
|
|
|
2014-03-05 11:11:16 -08:00
|
|
|
function VersionedData(version, payload) {
|
2014-07-10 13:17:24 -07:00
|
|
|
VersionedData.super_.call(this, version, payload);
|
2014-06-23 10:57:02 -07:00
|
|
|
if (typeof version != 'number') {
|
2014-03-05 11:11:16 -08:00
|
|
|
return;
|
2013-07-10 10:03:05 -07:00
|
|
|
};
|
2014-03-05 11:11:16 -08:00
|
|
|
this.data = new Buffer(payload.length + 1);
|
2014-07-15 17:14:04 -07:00
|
|
|
this.encoding('binary');
|
2014-03-05 11:11:16 -08:00
|
|
|
this.version(version);
|
|
|
|
this.payload(payload);
|
|
|
|
};
|
2013-07-10 10:03:05 -07:00
|
|
|
|
2014-07-10 11:55:32 -07:00
|
|
|
util.inherits(VersionedData, EncodedData);
|
|
|
|
EncodedData.applyEncodingsTo(VersionedData);
|
2013-07-10 10:03:05 -07:00
|
|
|
|
2014-03-05 11:11:16 -08:00
|
|
|
// get or set the version data (the first byte of the address)
|
|
|
|
VersionedData.prototype.version = function(num) {
|
2014-06-23 10:57:02 -07:00
|
|
|
if (num || (num === 0)) {
|
|
|
|
this.doAsBinary(function() {
|
|
|
|
this.data.writeUInt8(num, 0);
|
|
|
|
});
|
2014-03-05 11:11:16 -08:00
|
|
|
return num;
|
|
|
|
}
|
|
|
|
return this.as('binary').readUInt8(0);
|
|
|
|
};
|
2013-07-10 10:03:05 -07:00
|
|
|
|
2014-03-05 11:11:16 -08:00
|
|
|
// get or set the payload data (as a Buffer object)
|
|
|
|
VersionedData.prototype.payload = function(data) {
|
2014-06-23 10:57:02 -07:00
|
|
|
if (data) {
|
|
|
|
this.doAsBinary(function() {
|
|
|
|
data.copy(this.data, 1);
|
|
|
|
});
|
2014-03-05 11:11:16 -08:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
return this.as('binary').slice(1);
|
2013-07-10 10:03:05 -07:00
|
|
|
};
|
2014-03-05 11:11:16 -08:00
|
|
|
|
2014-07-12 03:14:56 -07:00
|
|
|
module.exports = VersionedData;
|