forked from bitpay/bitcore-lib
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: asset unlock transaction payload (#293)
* feat: add assetunlockpayload.js * test: validation
- Loading branch information
Showing
9 changed files
with
655 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
/* eslint-disable */ | ||
// TODO: Remove previous line and work through linting issues at next edit | ||
|
||
var Preconditions = require('../../util/preconditions'); | ||
var BufferWriter = require('../../encoding/bufferwriter'); | ||
var BufferReader = require('../../encoding/bufferreader'); | ||
var AbstractPayload = require('./abstractpayload'); | ||
var utils = require('../../util/js'); | ||
const _ = require('lodash'); | ||
const BN = require('../../crypto/bn'); | ||
const constants = require('../../constants'); | ||
|
||
var isUnsignedInteger = utils.isUnsignedInteger; | ||
|
||
var CURRENT_PAYLOAD_VERSION = 1; | ||
|
||
/** | ||
* @typedef {Object} AssetUnlockPayloadJSON | ||
* @property {number} version | ||
* @property {object} creditOutputs | ||
*/ | ||
|
||
/** | ||
* @class AssetUnlockPayload | ||
* @property {Output[]} creditOutputs | ||
*/ | ||
function AssetUnlockPayload() { | ||
AbstractPayload.call(this); | ||
this.version = CURRENT_PAYLOAD_VERSION; | ||
} | ||
|
||
AssetUnlockPayload.prototype = Object.create(AbstractPayload.prototype); | ||
AssetUnlockPayload.prototype.constructor = AbstractPayload; | ||
|
||
/* Static methods */ | ||
|
||
/** | ||
* Parse raw transition payload | ||
* @param {Buffer} rawPayload | ||
* @return {AssetUnlockPayload} | ||
*/ | ||
AssetUnlockPayload.fromBuffer = function (rawPayload) { | ||
var payloadBufferReader = new BufferReader(rawPayload); | ||
var payload = new AssetUnlockPayload(); | ||
payload.version = payloadBufferReader.readUInt8(); | ||
payload.index = payloadBufferReader.readUInt64LEBN().toNumber(); | ||
payload.fee = payloadBufferReader.readUInt32LE(); | ||
payload.requestHeight = payloadBufferReader.readUInt32LE(); | ||
payload.quorumHash = payloadBufferReader | ||
.read(constants.SHA256_HASH_SIZE) | ||
.toString('hex'); | ||
payload.quorumSig = payloadBufferReader | ||
.read(constants.BLS_SIGNATURE_SIZE) | ||
.toString('hex'); | ||
|
||
if (!payloadBufferReader.finished()) { | ||
throw new Error( | ||
'Failed to parse payload: raw payload is bigger than expected.' | ||
); | ||
} | ||
|
||
payload.validate(); | ||
return payload; | ||
}; | ||
|
||
/** | ||
* Create new instance of payload from JSON | ||
* @param {string|AssetUnlockPayloadJSON} payloadJson | ||
* @return {AssetUnlockPayload} | ||
*/ | ||
AssetUnlockPayload.fromJSON = function fromJSON(payloadJson) { | ||
var payload = new AssetUnlockPayload(); | ||
payload.version = payloadJson.version; | ||
payload.index = payloadJson.index; | ||
payload.fee = payloadJson.fee; | ||
payload.requestHeight = payloadJson.requestHeight; | ||
payload.quorumHash = payloadJson.quorumHash; | ||
payload.quorumSig = payloadJson.quorumSig; | ||
|
||
payload.validate(); | ||
return payload; | ||
}; | ||
|
||
/* Instance methods */ | ||
|
||
/** | ||
* Validates payload data | ||
* @return {boolean} | ||
*/ | ||
AssetUnlockPayload.prototype.validate = function () { | ||
Preconditions.checkArgument( | ||
isUnsignedInteger(this.version), | ||
'Expect version to be an unsigned integer' | ||
); | ||
|
||
Preconditions.checkArgument( | ||
this.version !== 0 && this.version <= CURRENT_PAYLOAD_VERSION, | ||
'Invalid version' | ||
); | ||
|
||
Preconditions.checkArgument( | ||
isUnsignedInteger(this.index), | ||
`Expect index to be an unsigned integer` | ||
); | ||
|
||
Preconditions.checkArgument( | ||
isUnsignedInteger(this.fee), | ||
`Expect fee to be an unsigned integer` | ||
); | ||
|
||
Preconditions.checkArgument( | ||
isUnsignedInteger(this.requestHeight), | ||
`Expect requestHeight to be an unsigned integer` | ||
); | ||
|
||
Preconditions.checkArgument( | ||
utils.isHexaString(this.quorumHash), | ||
'Expect quorumHash to be a hex string' | ||
); | ||
|
||
Preconditions.checkArgument( | ||
utils.isHexaString(this.quorumSig), | ||
'Expect quorumSig to be a hex string' | ||
); | ||
|
||
return true; | ||
}; | ||
|
||
/** | ||
* Serializes payload to JSON | ||
* @return {AssetUnlockPayloadJSON} | ||
*/ | ||
AssetUnlockPayload.prototype.toJSON = function toJSON() { | ||
this.validate(); | ||
var json = { | ||
version: this.version, | ||
index: this.index, | ||
fee: this.fee, | ||
requestHeight: this.requestHeight, | ||
quorumHash: this.quorumHash, | ||
quorumSig: this.quorumSig, | ||
}; | ||
|
||
return json; | ||
}; | ||
|
||
/** | ||
* Serialize payload to buffer | ||
* @return {Buffer} | ||
*/ | ||
AssetUnlockPayload.prototype.toBuffer = function toBuffer() { | ||
this.validate(); | ||
var payloadBufferWriter = new BufferWriter(); | ||
|
||
payloadBufferWriter | ||
.writeUInt8(this.version) | ||
.writeUInt64LEBN(new BN(this.index)) | ||
.writeUInt32LE(this.fee) | ||
.writeUInt32LE(this.requestHeight) | ||
.write(Buffer.from(this.quorumHash, 'hex')) | ||
.write(Buffer.from(this.quorumSig, 'hex')); | ||
|
||
return payloadBufferWriter.toBuffer(); | ||
}; | ||
|
||
/** | ||
* Copy payload instance | ||
* @return {AssetUnlockPayload} | ||
*/ | ||
AssetUnlockPayload.prototype.copy = function copy() { | ||
return AssetUnlockPayload.fromJSON(this.toJSON()); | ||
}; | ||
|
||
module.exports = AssetUnlockPayload; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.