Browse Source
...will be useful in transactions. Note that we already have a primitive understanding of Varints in the BufferReader and BufferWriter classes. However, the new Varint class is a varint object which actually depends on BufferReader and BufferWriter for reading and writing varints. This class is for keeping track of the raw buffer that is read in from a buffer.patch-2
2 changed files with 126 additions and 0 deletions
@ -0,0 +1,47 @@ |
|||||
|
var BufferWriter = require('./bufferwriter'); |
||||
|
var BufferReader = require('./bufferreader'); |
||||
|
var BN = require('./bn'); |
||||
|
|
||||
|
var Varint = function Varint(buf) { |
||||
|
if (!(this instanceof Varint)) |
||||
|
return new Varint(buf); |
||||
|
if (Buffer.isBuffer(buf)) { |
||||
|
this.buf = buf; |
||||
|
} else if (typeof buf === 'number') { |
||||
|
var num = buf; |
||||
|
this.fromNumber(num); |
||||
|
} else if (buf) { |
||||
|
var obj = buf; |
||||
|
this.set(obj); |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
Varint.prototype.set = function(obj) { |
||||
|
this.buf = obj.buf || this.buf; |
||||
|
return this; |
||||
|
}; |
||||
|
|
||||
|
Varint.prototype.fromBuffer = function(buf) { |
||||
|
this.buf = buf; |
||||
|
return this; |
||||
|
}; |
||||
|
|
||||
|
Varint.prototype.fromBufferReader = function(br) { |
||||
|
this.buf = br.readVarintBuf(); |
||||
|
return this; |
||||
|
}; |
||||
|
|
||||
|
Varint.prototype.fromNumber = function(num) { |
||||
|
this.buf = BufferWriter().writeVarintNum(num).concat(); |
||||
|
return this; |
||||
|
}; |
||||
|
|
||||
|
Varint.prototype.toBuffer = function() { |
||||
|
return this.buf; |
||||
|
}; |
||||
|
|
||||
|
Varint.prototype.toNumber = function() { |
||||
|
return BufferReader(this.buf).readVarintNum(); |
||||
|
}; |
||||
|
|
||||
|
module.exports = Varint; |
@ -0,0 +1,79 @@ |
|||||
|
var should = require('chai').should(); |
||||
|
var BufferReader = require('../lib/bufferreader'); |
||||
|
var BufferWriter = require('../lib/bufferwriter'); |
||||
|
var Varint = require('../lib/varint'); |
||||
|
|
||||
|
describe('Varint', function() { |
||||
|
|
||||
|
it('should make a new varint', function() { |
||||
|
var buf = new Buffer('00', 'hex'); |
||||
|
var varint = new Varint(buf); |
||||
|
should.exist(varint); |
||||
|
varint.buf.toString('hex').should.equal('00'); |
||||
|
varint = Varint(buf); |
||||
|
should.exist(varint); |
||||
|
varint.buf.toString('hex').should.equal('00'); |
||||
|
}); |
||||
|
|
||||
|
describe('#set', function() { |
||||
|
|
||||
|
it('should set a buffer', function() { |
||||
|
var buf = new Buffer('00', 'hex'); |
||||
|
var varint = Varint().set({buf: buf}); |
||||
|
varint.buf.toString('hex').should.equal('00'); |
||||
|
varint.set({}); |
||||
|
varint.buf.toString('hex').should.equal('00'); |
||||
|
}); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
describe('#fromBuffer', function() { |
||||
|
|
||||
|
it('should set a buffer', function() { |
||||
|
var buf = BufferWriter().writeVarintNum(5).concat(); |
||||
|
var varint = Varint().fromBuffer(buf); |
||||
|
varint.toNumber().should.equal(5); |
||||
|
}); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
describe('#fromBufferReader', function() { |
||||
|
|
||||
|
it('should set a buffer reader', function() { |
||||
|
var buf = BufferWriter().writeVarintNum(5).concat(); |
||||
|
var br = BufferReader(buf); |
||||
|
var varint = Varint().fromBufferReader(br); |
||||
|
varint.toNumber().should.equal(5); |
||||
|
}); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
describe('#fromNumber', function() { |
||||
|
|
||||
|
it('should set a number', function() { |
||||
|
var varint = Varint().fromNumber(5); |
||||
|
varint.toNumber().should.equal(5); |
||||
|
}); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
describe('#toBuffer', function() { |
||||
|
|
||||
|
it('should return a buffer', function() { |
||||
|
buf = BufferWriter().writeVarintNum(5).concat(); |
||||
|
var varint = Varint(buf); |
||||
|
varint.toBuffer().toString('hex').should.equal(buf.toString('hex')); |
||||
|
}); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
describe('#toNumber', function() { |
||||
|
|
||||
|
it('should return a buffer', function() { |
||||
|
var varint = Varint(5); |
||||
|
varint.toNumber().should.equal(5); |
||||
|
}); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
}); |
Loading…
Reference in new issue