Skip to content

Commit

Permalink
port changes to BCH
Browse files Browse the repository at this point in the history
  • Loading branch information
matiu committed Apr 4, 2020
1 parent 65e6036 commit 48e5e19
Show file tree
Hide file tree
Showing 12 changed files with 4,133 additions and 495 deletions.
41 changes: 30 additions & 11 deletions packages/bitcore-build/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion packages/bitcore-lib-cash/lib/errors/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,14 @@ module.exports = [{
}, {
name: 'MissingPreviousOutput',
message: 'No previous output information.'
}]
}, {
name: 'BlockHeightOutOfRange',
message: 'Block Height can only be between 0 and 65535'
} , {
name: 'LockTimeRange',
message: 'Seconds needs to be more that 0 and less that 33553920'
}
]
}, {
name: 'NeedMoreInfo',
message: '{0}'
Expand Down
72 changes: 72 additions & 0 deletions packages/bitcore-lib-cash/lib/transaction/input/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ var MAXINT = 0xffffffff; // Math.pow(2, 32) - 1;
var DEFAULT_RBF_SEQNUMBER = MAXINT - 2;
var DEFAULT_SEQNUMBER = MAXINT;
var DEFAULT_LOCKTIME_SEQNUMBER = MAXINT - 1;
const SEQUENCE_LOCKTIME_DISABLE_FLAG = Math.pow(2,31); // (1 << 31);
const SEQUENCE_LOCKTIME_TYPE_FLAG = Math.pow(2,22); // (1 << 22);
const SEQUENCE_LOCKTIME_MASK = 0xffff;
const SEQUENCE_LOCKTIME_GRANULARITY = 512; // 512 seconds
const SEQUENCE_BLOCKDIFF_LIMIT = Math.pow(2,16)-1; // 16 bits




function Input(params) {
if (!(this instanceof Input)) {
Expand All @@ -29,6 +37,7 @@ Input.MAXINT = MAXINT;
Input.DEFAULT_SEQNUMBER = DEFAULT_SEQNUMBER;
Input.DEFAULT_LOCKTIME_SEQNUMBER = DEFAULT_LOCKTIME_SEQNUMBER;
Input.DEFAULT_RBF_SEQNUMBER = DEFAULT_RBF_SEQNUMBER;
Input.SEQUENCE_LOCKTIME_TYPE_FLAG = SEQUENCE_LOCKTIME_TYPE_FLAG;

Object.defineProperty(Input.prototype, 'script', {
configurable: false,
Expand Down Expand Up @@ -201,4 +210,67 @@ Input.prototype._estimateSize = function() {
return this.toBufferWriter().toBuffer().length;
};


/**
* Sets sequence number so that transaction is not valid until the desired seconds
* since the transaction is mined
*
* @param {Number} time in seconds
* @return {Transaction} this
*/
Input.prototype.lockForSeconds = function(seconds) {
$.checkArgument(_.isNumber(seconds));
if (seconds < 0 || seconds >= SEQUENCE_LOCKTIME_GRANULARITY * SEQUENCE_LOCKTIME_MASK) {
throw new errors.Transaction.Input.LockTimeRange();
}
seconds = parseInt(Math.floor(seconds / SEQUENCE_LOCKTIME_GRANULARITY));

// SEQUENCE_LOCKTIME_DISABLE_FLAG = 1
this.sequenceNumber = seconds | SEQUENCE_LOCKTIME_TYPE_FLAG ;
return this;
};

/**
* Sets sequence number so that transaction is not valid until the desired block height differnece since the tx is mined
*
* @param {Number} height
* @return {Transaction} this
*/
Input.prototype.lockUntilBlockHeight = function(heightDiff) {
$.checkArgument(_.isNumber(heightDiff));
if (heightDiff < 0 || heightDiff >= SEQUENCE_BLOCKDIFF_LIMIT) {
throw new errors.Transaction.Input.BlockHeightOutOfRange();
}
// SEQUENCE_LOCKTIME_TYPE_FLAG = 0
// SEQUENCE_LOCKTIME_DISABLE_FLAG = 0
this.sequenceNumber = heightDiff ;
return this;
};


/**
* Returns a semantic version of the input's sequence nLockTime.
* @return {Number|Date}
* If sequence lock is disabled it returns null,
* if is set to block height lock, returns a block height (number)
* else it returns a Date object.
*/
Input.prototype.getLockTime = function() {
if (this.sequenceNumber & SEQUENCE_LOCKTIME_DISABLE_FLAG) {
return null;
}

if (this.sequenceNumber & SEQUENCE_LOCKTIME_TYPE_FLAG) {
var seconds = SEQUENCE_LOCKTIME_GRANULARITY * (this.sequenceNumber & SEQUENCE_LOCKTIME_MASK);
return seconds;
return s;
} else {
var blockHeight = this.sequenceNumber & SEQUENCE_LOCKTIME_MASK;
return blockHeight;
}
};




module.exports = Input;
12 changes: 11 additions & 1 deletion packages/bitcore-lib-cash/lib/transaction/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function Transaction(serialized) {
}
}

var CURRENT_VERSION = 1;
var CURRENT_VERSION = 2;
var DEFAULT_NLOCKTIME = 0;
var MAX_BLOCK_SIZE = 1000000;

Expand Down Expand Up @@ -1227,4 +1227,14 @@ Transaction.prototype.isCoinbase = function() {
return (this.inputs.length === 1 && this.inputs[0].isNull());
};


Transaction.prototype.setVersion = function(version) {
$.checkArgument(
JSUtil.isNaturalNumber(version) && version <= CURRENT_VERSION,
'Wrong version number');
this.version = version;
return this;
};


module.exports = Transaction;
Loading

0 comments on commit 48e5e19

Please sign in to comment.