bitcoinjs-lib/src/bigi.js

41 lines
986 B
JavaScript
Raw Normal View History

2014-04-22 02:19:30 +10:00
var assert = require('assert')
var BigInteger = require('bigi')
2014-04-24 07:22:10 +10:00
// Import operations
BigInteger.fromHex = function(hex) {
var buffer = new Buffer(hex, 'hex')
assert.equal(buffer.length, Buffer.byteLength(hex) / 2)
return BigInteger.fromBuffer(buffer)
}
2014-04-22 02:19:30 +10:00
BigInteger.fromBuffer = function(buffer) {
2014-04-24 07:22:10 +10:00
assert(Array.isArray(buffer) || Buffer.isBuffer(buffer)) // FIXME: Transitionary
2014-04-22 02:19:30 +10:00
// FIXME: Transitionary
if (Buffer.isBuffer(buffer)) {
buffer = Array.prototype.slice.call(buffer)
}
return BigInteger.fromByteArrayUnsigned(buffer)
}
2014-04-24 07:22:10 +10:00
// Export operations
BigInteger.prototype.toBuffer = function() {
return new Buffer(this.toByteArrayUnsigned())
}
BigInteger.prototype.toHex = function() {
return this.toBuffer().toString('hex')
}
BigInteger.prototype.toPaddedBuffer = function(s) {
var buffer = this.toBuffer()
var padded = new Buffer(s - buffer.length)
padded.fill(0)
return Buffer.concat([padded, buffer], s)
}
2014-04-22 02:19:30 +10:00
module.exports = BigInteger