|
| 1 | +/* eslint-disable */ |
| 2 | +// TODO: Remove previous line and work through linting issues at next edit |
| 3 | + |
| 4 | +var Preconditions = require('../../util/preconditions'); |
| 5 | +var BufferWriter = require('../../encoding/bufferwriter'); |
| 6 | +var BufferReader = require('../../encoding/bufferreader'); |
| 7 | +var AbstractPayload = require('./abstractpayload'); |
| 8 | +var utils = require('../../util/js'); |
| 9 | +const _ = require('lodash'); |
| 10 | +const BN = require('../../crypto/bn'); |
| 11 | +const constants = require('../../constants'); |
| 12 | + |
| 13 | +var isUnsignedInteger = utils.isUnsignedInteger; |
| 14 | + |
| 15 | +var CURRENT_PAYLOAD_VERSION = 1; |
| 16 | + |
| 17 | +/** |
| 18 | + * @typedef {Object} AssetUnlockPayloadJSON |
| 19 | + * @property {number} version |
| 20 | + * @property {object} creditOutputs |
| 21 | + */ |
| 22 | + |
| 23 | +/** |
| 24 | + * @class AssetUnlockPayload |
| 25 | + * @property {Output[]} creditOutputs |
| 26 | + */ |
| 27 | +function AssetUnlockPayload() { |
| 28 | + AbstractPayload.call(this); |
| 29 | + this.version = CURRENT_PAYLOAD_VERSION; |
| 30 | +} |
| 31 | + |
| 32 | +AssetUnlockPayload.prototype = Object.create(AbstractPayload.prototype); |
| 33 | +AssetUnlockPayload.prototype.constructor = AbstractPayload; |
| 34 | + |
| 35 | +/* Static methods */ |
| 36 | + |
| 37 | +/** |
| 38 | + * Parse raw transition payload |
| 39 | + * @param {Buffer} rawPayload |
| 40 | + * @return {AssetUnlockPayload} |
| 41 | + */ |
| 42 | +AssetUnlockPayload.fromBuffer = function (rawPayload) { |
| 43 | + var payloadBufferReader = new BufferReader(rawPayload); |
| 44 | + var payload = new AssetUnlockPayload(); |
| 45 | + payload.version = payloadBufferReader.readUInt8(); |
| 46 | + payload.index = payloadBufferReader.readUInt64LEBN().toNumber(); |
| 47 | + payload.fee = payloadBufferReader.readUInt32LE(); |
| 48 | + payload.requestHeight = payloadBufferReader.readUInt32LE(); |
| 49 | + payload.quorumHash = payloadBufferReader |
| 50 | + .read(constants.SHA256_HASH_SIZE) |
| 51 | + .toString('hex'); |
| 52 | + payload.quorumSig = payloadBufferReader |
| 53 | + .read(constants.BLS_SIGNATURE_SIZE) |
| 54 | + .toString('hex'); |
| 55 | + |
| 56 | + if (!payloadBufferReader.finished()) { |
| 57 | + throw new Error( |
| 58 | + 'Failed to parse payload: raw payload is bigger than expected.' |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + payload.validate(); |
| 63 | + return payload; |
| 64 | +}; |
| 65 | + |
| 66 | +/** |
| 67 | + * Create new instance of payload from JSON |
| 68 | + * @param {string|AssetUnlockPayloadJSON} payloadJson |
| 69 | + * @return {AssetUnlockPayload} |
| 70 | + */ |
| 71 | +AssetUnlockPayload.fromJSON = function fromJSON(payloadJson) { |
| 72 | + var payload = new AssetUnlockPayload(); |
| 73 | + payload.version = payloadJson.version; |
| 74 | + payload.index = payloadJson.index; |
| 75 | + payload.fee = payloadJson.fee; |
| 76 | + payload.requestHeight = payloadJson.requestHeight; |
| 77 | + payload.quorumHash = payloadJson.quorumHash; |
| 78 | + payload.quorumSig = payloadJson.quorumSig; |
| 79 | + |
| 80 | + payload.validate(); |
| 81 | + return payload; |
| 82 | +}; |
| 83 | + |
| 84 | +/* Instance methods */ |
| 85 | + |
| 86 | +/** |
| 87 | + * Validates payload data |
| 88 | + * @return {boolean} |
| 89 | + */ |
| 90 | +AssetUnlockPayload.prototype.validate = function () { |
| 91 | + Preconditions.checkArgument( |
| 92 | + isUnsignedInteger(this.version), |
| 93 | + 'Expect version to be an unsigned integer' |
| 94 | + ); |
| 95 | + |
| 96 | + Preconditions.checkArgument( |
| 97 | + this.version !== 0 && this.version <= CURRENT_PAYLOAD_VERSION, |
| 98 | + 'Invalid version' |
| 99 | + ); |
| 100 | + |
| 101 | + Preconditions.checkArgument( |
| 102 | + isUnsignedInteger(this.index), |
| 103 | + `Expect index to be an unsigned integer` |
| 104 | + ); |
| 105 | + |
| 106 | + Preconditions.checkArgument( |
| 107 | + isUnsignedInteger(this.fee), |
| 108 | + `Expect fee to be an unsigned integer` |
| 109 | + ); |
| 110 | + |
| 111 | + Preconditions.checkArgument( |
| 112 | + isUnsignedInteger(this.requestHeight), |
| 113 | + `Expect requestHeight to be an unsigned integer` |
| 114 | + ); |
| 115 | + |
| 116 | + Preconditions.checkArgument( |
| 117 | + utils.isHexaString(this.quorumHash), |
| 118 | + 'Expect quorumHash to be a hex string' |
| 119 | + ); |
| 120 | + |
| 121 | + Preconditions.checkArgument( |
| 122 | + utils.isHexaString(this.quorumSig), |
| 123 | + 'Expect quorumSig to be a hex string' |
| 124 | + ); |
| 125 | + |
| 126 | + return true; |
| 127 | +}; |
| 128 | + |
| 129 | +/** |
| 130 | + * Serializes payload to JSON |
| 131 | + * @return {AssetUnlockPayloadJSON} |
| 132 | + */ |
| 133 | +AssetUnlockPayload.prototype.toJSON = function toJSON() { |
| 134 | + this.validate(); |
| 135 | + var json = { |
| 136 | + version: this.version, |
| 137 | + index: this.index, |
| 138 | + fee: this.fee, |
| 139 | + requestHeight: this.requestHeight, |
| 140 | + quorumHash: this.quorumHash, |
| 141 | + quorumSig: this.quorumSig, |
| 142 | + }; |
| 143 | + |
| 144 | + return json; |
| 145 | +}; |
| 146 | + |
| 147 | +/** |
| 148 | + * Serialize payload to buffer |
| 149 | + * @return {Buffer} |
| 150 | + */ |
| 151 | +AssetUnlockPayload.prototype.toBuffer = function toBuffer() { |
| 152 | + this.validate(); |
| 153 | + var payloadBufferWriter = new BufferWriter(); |
| 154 | + |
| 155 | + payloadBufferWriter |
| 156 | + .writeUInt8(this.version) |
| 157 | + .writeUInt64LEBN(new BN(this.index)) |
| 158 | + .writeUInt32LE(this.fee) |
| 159 | + .writeUInt32LE(this.requestHeight) |
| 160 | + .write(Buffer.from(this.quorumHash, 'hex')) |
| 161 | + .write(Buffer.from(this.quorumSig, 'hex')); |
| 162 | + |
| 163 | + return payloadBufferWriter.toBuffer(); |
| 164 | +}; |
| 165 | + |
| 166 | +/** |
| 167 | + * Copy payload instance |
| 168 | + * @return {AssetUnlockPayload} |
| 169 | + */ |
| 170 | +AssetUnlockPayload.prototype.copy = function copy() { |
| 171 | + return AssetUnlockPayload.fromJSON(this.toJSON()); |
| 172 | +}; |
| 173 | + |
| 174 | +module.exports = AssetUnlockPayload; |
0 commit comments