Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: asset unlock transaction payload #293

Merged
merged 2 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export { ProRegTxPayload } from './typings/transaction/payload/ProRegTxPayload';
export { ProUpRegTxPayload } from './typings/transaction/payload/ProUpRegTxPayload';
export { ProUpRevTxPayload } from './typings/transaction/payload/ProUpRevTxPayload';
export { ProUpServTxPayload } from './typings/transaction/payload/ProUpServTxPayload';
export { AssetLockPayload } from './typings/transaction/payload/AssetLockPayload';
export { AssetUnlockPayload } from './typings/transaction/payload/AssetUnlockPayload';

export { Output } from './typings/transaction/Output';

Expand Down Expand Up @@ -362,4 +364,4 @@ export function traverseAndBuildPartialTree(
position: number,
hashes: Buffer[],
matches: boolean[]
): any;
): any;
1 change: 1 addition & 0 deletions lib/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module.exports = {
TRANSACTION_COINBASE: 5,
TRANSACTION_QUORUM_COMMITMENT: 6,
TRANSACTION_ASSET_LOCK: 8,
TRANSACTION_ASSET_UNLOCK: 9,
},
EMPTY_SIGNATURE_SIZE: 0,
primitives: {
Expand Down
174 changes: 174 additions & 0 deletions lib/transaction/payload/assetunlockpayload.js
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;
1 change: 1 addition & 0 deletions lib/transaction/payload/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ Payload.CoinbasePayload = require('./coinbasepayload');
Payload.constants = require('../../constants');
Payload.CommitmentTxPayload = require('./commitmenttxpayload');
Payload.AssetLockPayload = require('./assetlockpayload');
Payload.AssetUnlockPayload = require('./assetunlockpayload');

module.exports = Payload;
3 changes: 3 additions & 0 deletions lib/transaction/payload/payload.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var ProTxUpServPayload = require('./proupservtxpayload');
var ProUpRegTxPayload = require('./proupregtxpayload');
var ProUpRevTxPayload = require('./prouprevtxpayload');
var AssetLockPayload = require('./assetlockpayload');
var AssetUnlockPayload = require('./assetunlockpayload');

var PayloadClasses = {};
PayloadClasses[RegisteredPayloadTypes.TRANSACTION_COINBASE] = CoinbasePayload;
Expand All @@ -26,6 +27,8 @@ PayloadClasses[RegisteredPayloadTypes.TRANSACTION_PROVIDER_UPDATE_REVOKE] =
ProUpRevTxPayload;
PayloadClasses[RegisteredPayloadTypes.TRANSACTION_ASSET_LOCK] =
AssetLockPayload;
PayloadClasses[RegisteredPayloadTypes.TRANSACTION_ASSET_UNLOCK] =
AssetUnlockPayload;

/**
*
Expand Down
2 changes: 2 additions & 0 deletions test-d/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import type {
MultiSigInput,
MultiSigScriptHashInput,
AbstractPayload,
AssetLockPayload,
AssetUnlockPayload,
CoinbasePayload,
CommitmentTxPayload,
ProRegTxPayload,
Expand Down
Loading
Loading