script: improve new scriptnum.
This commit is contained in:
parent
ad3bc3c8d6
commit
a91bd5c958
@ -1,6 +1,6 @@
|
|||||||
/*!
|
/*!
|
||||||
* scriptnum.js - script number for bcoin
|
* scriptnum.js - script number object for bcoin.
|
||||||
* Copyright (c) 2014-2017, Christopher Jeffrey (MIT License).
|
* Copyright (c) 2017, Christopher Jeffrey (MIT License).
|
||||||
* https://github.com/bcoin-org/bcoin
|
* https://github.com/bcoin-org/bcoin
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -9,18 +9,40 @@
|
|||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const {I64} = require('../utils/int64');
|
const {I64} = require('../utils/int64');
|
||||||
const {ScriptError} = require('./common');
|
const {ScriptError} = require('./common');
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Constants
|
||||||
|
*/
|
||||||
|
|
||||||
const EMPTY_ARRAY = Buffer.alloc(0);
|
const EMPTY_ARRAY = Buffer.alloc(0);
|
||||||
|
|
||||||
function ScriptNum(num, base, limit) {
|
/**
|
||||||
if (!(this instanceof ScriptNum))
|
* Script Number
|
||||||
return new ScriptNum(num, base, limit);
|
* @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, I64);
|
||||||
Object.setPrototypeOf(ScriptNum.prototype, I64.prototype);
|
Object.setPrototypeOf(ScriptNum.prototype, I64.prototype);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cast to int32.
|
||||||
|
* @returns {Number}
|
||||||
|
*/
|
||||||
|
|
||||||
ScriptNum.prototype.toInt = function toInt() {
|
ScriptNum.prototype.toInt = function toInt() {
|
||||||
if (this.lt(I64.UINT32_MIN))
|
if (this.lt(I64.UINT32_MIN))
|
||||||
return I64.LONG_MIN;
|
return I64.LONG_MIN;
|
||||||
@ -31,6 +53,11 @@ ScriptNum.prototype.toInt = function toInt() {
|
|||||||
return this.lo;
|
return this.lo;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Serialize script number.
|
||||||
|
* @returns {Buffer}
|
||||||
|
*/
|
||||||
|
|
||||||
ScriptNum.prototype.toRaw = function toRaw() {
|
ScriptNum.prototype.toRaw = function toRaw() {
|
||||||
let num = this;
|
let num = this;
|
||||||
|
|
||||||
@ -92,6 +119,13 @@ ScriptNum.prototype.toRaw = function toRaw() {
|
|||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantiate script number from serialized data.
|
||||||
|
* @private
|
||||||
|
* @param {Buffer} data
|
||||||
|
* @returns {ScriptNum}
|
||||||
|
*/
|
||||||
|
|
||||||
ScriptNum.prototype.fromRaw = function fromRaw(data) {
|
ScriptNum.prototype.fromRaw = function fromRaw(data) {
|
||||||
assert(Buffer.isBuffer(data));
|
assert(Buffer.isBuffer(data));
|
||||||
|
|
||||||
@ -132,6 +166,24 @@ ScriptNum.prototype.fromRaw = function fromRaw(data) {
|
|||||||
return this;
|
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) {
|
ScriptNum.prototype.decode = function decode(data, minimal, limit) {
|
||||||
assert(Buffer.isBuffer(data));
|
assert(Buffer.isBuffer(data));
|
||||||
|
|
||||||
@ -155,10 +207,22 @@ ScriptNum.prototype.decode = function decode(data, minimal, limit) {
|
|||||||
return this.fromRaw(data);
|
return this.fromRaw(data);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inspect script number.
|
||||||
|
* @returns {String}
|
||||||
|
*/
|
||||||
|
|
||||||
ScriptNum.prototype.inspect = function inspect() {
|
ScriptNum.prototype.inspect = function inspect() {
|
||||||
return `<ScriptNum: ${this.toString(10)}>`;
|
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) {
|
ScriptNum.isMinimal = function isMinimal(data) {
|
||||||
assert(Buffer.isBuffer(data));
|
assert(Buffer.isBuffer(data));
|
||||||
|
|
||||||
@ -176,10 +240,30 @@ ScriptNum.isMinimal = function isMinimal(data) {
|
|||||||
return true;
|
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) {
|
ScriptNum.decode = function decode(data, minimal, limit) {
|
||||||
return new ScriptNum().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) {
|
ScriptNum.isScriptNum = function isScriptNum(obj) {
|
||||||
return obj instanceof ScriptNum;
|
return obj instanceof ScriptNum;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Expose
|
||||||
|
*/
|
||||||
|
|
||||||
|
module.exports = ScriptNum;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user