script: improve new scriptnum.

This commit is contained in:
Christopher Jeffrey 2017-08-16 20:35:54 -07:00
parent ad3bc3c8d6
commit a91bd5c958
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -1,6 +1,6 @@
/*!
* scriptnum.js - script number for bcoin
* Copyright (c) 2014-2017, Christopher Jeffrey (MIT License).
* scriptnum.js - script number object for bcoin.
* Copyright (c) 2017, Christopher Jeffrey (MIT License).
* https://github.com/bcoin-org/bcoin
*/
@ -9,18 +9,40 @@
const assert = require('assert');
const {I64} = require('../utils/int64');
const {ScriptError} = require('./common');
/*
* Constants
*/
const EMPTY_ARRAY = Buffer.alloc(0);
function ScriptNum(num, base, limit) {
if (!(this instanceof ScriptNum))
return new ScriptNum(num, base, limit);
/**
* Script Number
* @see https://github.com/chjj/n64
* @alias module:script.ScriptNum
* @constructor
* @param {(Number|String|Buffer|Object)?} num
* @param {(String|Number)?} base
* @property {Number} hi
* @property {Number} lo
* @property {Number} sign
*/
I64.call(this, num, base, limit);
function ScriptNum(num, base) {
if (!(this instanceof ScriptNum))
return new ScriptNum(num, base);
I64.call(this, num, base);
}
Object.setPrototypeOf(ScriptNum, I64);
Object.setPrototypeOf(ScriptNum.prototype, I64.prototype);
/**
* Cast to int32.
* @returns {Number}
*/
ScriptNum.prototype.toInt = function toInt() {
if (this.lt(I64.UINT32_MIN))
return I64.LONG_MIN;
@ -31,6 +53,11 @@ ScriptNum.prototype.toInt = function toInt() {
return this.lo;
};
/**
* Serialize script number.
* @returns {Buffer}
*/
ScriptNum.prototype.toRaw = function toRaw() {
let num = this;
@ -92,6 +119,13 @@ ScriptNum.prototype.toRaw = function toRaw() {
return data;
};
/**
* Instantiate script number from serialized data.
* @private
* @param {Buffer} data
* @returns {ScriptNum}
*/
ScriptNum.prototype.fromRaw = function fromRaw(data) {
assert(Buffer.isBuffer(data));
@ -132,6 +166,24 @@ ScriptNum.prototype.fromRaw = function fromRaw(data) {
return this;
};
/**
* Serialize script number.
* @returns {Buffer}
*/
ScriptNum.prototype.encode = function encode() {
return this.toRaw();
};
/**
* Decode and verify script number.
* @private
* @param {Buffer} data
* @param {Boolean?} minimal - Require minimal encoding.
* @param {Number?} limit - Size limit.
* @returns {ScriptNum}
*/
ScriptNum.prototype.decode = function decode(data, minimal, limit) {
assert(Buffer.isBuffer(data));
@ -155,10 +207,22 @@ ScriptNum.prototype.decode = function decode(data, minimal, limit) {
return this.fromRaw(data);
};
/**
* Inspect script number.
* @returns {String}
*/
ScriptNum.prototype.inspect = function inspect() {
return `<ScriptNum: ${this.toString(10)}>`;
};
/**
* Test wether a serialized script
* number is in its most minimal form.
* @param {Buffer} data
* @returns {Boolean}
*/
ScriptNum.isMinimal = function isMinimal(data) {
assert(Buffer.isBuffer(data));
@ -176,10 +240,30 @@ ScriptNum.isMinimal = function isMinimal(data) {
return true;
};
/**
* Decode and verify script number.
* @param {Buffer} data
* @param {Boolean?} minimal - Require minimal encoding.
* @param {Number?} limit - Size limit.
* @returns {ScriptNum}
*/
ScriptNum.decode = function decode(data, minimal, limit) {
return new ScriptNum().decode(data, minimal, limit);
};
/**
* Test whether object is a script number.
* @param {Object} obj
* @returns {Boolean}
*/
ScriptNum.isScriptNum = function isScriptNum(obj) {
return obj instanceof ScriptNum;
};
/*
* Expose
*/
module.exports = ScriptNum;