Skip to content

Commit

Permalink
feat: asset lock special transaction (dashpay#288)
Browse files Browse the repository at this point in the history
* feat: asset lock tx payload

* refactor: get rid of SubTxRegisterPayload

* refactor: removed rest legacy platform special transactions

* refactor: unused code
  • Loading branch information
markin-io authored Oct 19, 2023
1 parent b98bf01 commit 2acc455
Show file tree
Hide file tree
Showing 29 changed files with 527 additions and 3,999 deletions.
8 changes: 0 additions & 8 deletions docs/usage/transaction.md
Original file line number Diff line number Diff line change
Expand Up @@ -444,14 +444,6 @@ The argument function MUST NOT modify the order of the original array
**Parameters**: None.
**Returns**: Array

## .canHaveNoUtxo()

**Description**:

**Parameters**: None.

**Returns**: Boolean

## .toObject() / .toJSON()

**Description**: Will return an object representation of the transaction
Expand Down
5 changes: 0 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,6 @@ 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 { SubTxCloseAccountPayload } from './typings/transaction/payload/SubTxCloseAccountPayload';
export { SubTxRegisterPayload } from './typings/transaction/payload/SubTxRegisterPayload';
export { SubTxResetKeyPayload } from './typings/transaction/payload/SubTxResetKeyPayload';
export { SubTxTopupPayload } from './typings/transaction/payload/SubTxTopupPayload';
export { SubTxTransitionPayload } from './typings/transaction/payload/SubTxTransitionPayload';

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

Expand Down
6 changes: 1 addition & 5 deletions lib/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ module.exports = {
TRANSACTION_PROVIDER_UPDATE_REVOKE: 4,
TRANSACTION_COINBASE: 5,
TRANSACTION_QUORUM_COMMITMENT: 6,
TRANSACTION_SUBTX_REGISTER: 8,
TRANSACTION_SUBTX_TOPUP: 9,
TRANSACTION_SUBTX_RESETKEY: 10,
TRANSACTION_SUBTX_CLOSEACCOUNT: 11,
TRANSACTION_SUBTX_TRANSITION: 12,
TRANSACTION_ASSET_LOCK: 8,
},
EMPTY_SIGNATURE_SIZE: 0,
primitives: {
Expand Down
172 changes: 172 additions & 0 deletions lib/transaction/payload/assetlockpayload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/* 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 Output = require('../output');

var isUnsignedInteger = utils.isUnsignedInteger;

var CURRENT_PAYLOAD_VERSION = 1;

/**
* @typedef {Object} AssetLockPayloadJSON
* @property {number} version
* @property {object} creditOutputs
*/

/**
* @class AssetLockPayload
* @property {Output[]} creditOutputs
*/
function AssetLockPayload() {
AbstractPayload.call(this);
this.version = CURRENT_PAYLOAD_VERSION;
this.creditOutputs = [];
}

AssetLockPayload.prototype = Object.create(AbstractPayload.prototype);
AssetLockPayload.prototype.constructor = AbstractPayload;

/* Static methods */

/**
* Parse raw transition payload
* @param {Buffer} rawPayload
* @return {AssetLockPayload}
*/
AssetLockPayload.fromBuffer = function (rawPayload) {
var payloadBufferReader = new BufferReader(rawPayload);
var payload = new AssetLockPayload();
payload.version = payloadBufferReader.readUInt8();
var numCreditOutputs = payloadBufferReader.readVarintNum();
for (var i = 0; i < numCreditOutputs; i++) {
payload.creditOutputs.push(Output.fromBufferReader(payloadBufferReader));
}

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|AssetLockPayloadJSON} payloadJson
* @return {AssetLockPayload}
*/
AssetLockPayload.fromJSON = function fromJSON(payloadJson) {
var payload = new AssetLockPayload();
payload.version = payloadJson.version;

var creditOutputs = [];
_.each(payloadJson.creditOutputs, function (output) {
creditOutputs.push(new Output(output));
});

payload.creditOutputs = creditOutputs;

payload.validate();
return payload;
};

/* Instance methods */

/**
* Validates payload data
* @return {boolean}
*/
AssetLockPayload.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(
this.creditOutputs.length > 0,
'Empty credit outputs'
);

_.each(this.creditOutputs, function (output, index) {
Preconditions.checkArgument(
output instanceof Output,
'Credit output ' + index + ' is not an instance of Output'
);
});

_.each(this.creditOutputs, function (output, index) {
Preconditions.checkArgument(
output.script.isPublicKeyHashOut(),
'Credit output ' + index + ' is not P2PKH'
);
});

_.each(this.creditOutputs, function (output, index) {
Preconditions.checkArgument(
output.script.isPublicKeyHashOut(),
'Credit output ' + index + ' is not P2PKH'
);
});

return true;
};

/**
* Serializes payload to JSON
* @return {AssetLockPayloadJSON}
*/
AssetLockPayload.prototype.toJSON = function toJSON() {
this.validate();
const creditOutputs = [];
_.each(this.creditOutputs, function (output) {
creditOutputs.push(output.toJSON());
});
var json = {
version: this.version,
creditOutputs
};

return json;
};

/**
* Serialize payload to buffer
* @return {Buffer}
*/
AssetLockPayload.prototype.toBuffer = function toBuffer() {
this.validate();
var payloadBufferWriter = new BufferWriter();

payloadBufferWriter
.writeUInt8(this.version)
.writeVarintNum(this.creditOutputs.length);

_.each(this.creditOutputs, function (output) {
output.toBufferWriter(payloadBufferWriter);
});

return payloadBufferWriter.toBuffer();
};

/**
* Copy payload instance
* @return {AssetLockPayload}
*/
AssetLockPayload.prototype.copy = function copy() {
return AssetLockPayload.fromJSON(this.toJSON());
};

module.exports = AssetLockPayload;
6 changes: 1 addition & 5 deletions lib/transaction/payload/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@ Payload.ProRegTxPayload = require('./proregtxpayload');
Payload.ProUpRegTxPayload = require('./proupregtxpayload');
Payload.ProUpRevTxPayload = require('./prouprevtxpayload');
Payload.ProTxUpServPayload = require('./proupservtxpayload');
Payload.SubTxCloseAccountPayload = require('./subtxcloseaccountpayload');
Payload.SubTxRegisterPayload = require('./subtxregisterpayload');
Payload.SubTxResetKeyPayload = require('./subtxresetkeypayload');
Payload.SubTxTopupPayload = require('./subtxtopuppayload');
Payload.SubTxTransitionPayload = require('./subtxtransitionpayload');
Payload.CoinbasePayload = require('./coinbasepayload');
Payload.constants = require('../../constants');
Payload.CommitmentTxPayload = require('./commitmenttxpayload');
Payload.AssetLockPayload = require('./assetlockpayload');

module.exports = Payload;
18 changes: 3 additions & 15 deletions lib/transaction/payload/payload.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,15 @@
var RegisteredPayloadTypes =
require('../../constants').registeredTransactionTypes;
var AbstractPayload = require('./abstractpayload');
var SubTxCloseAccountPayload = require('./subtxcloseaccountpayload');
var SubTxRegisterPayload = require('./subtxregisterpayload');
var SubTxResetKeyPayload = require('./subtxresetkeypayload');
var SubTxTopUpPayload = require('./subtxtopuppayload');
var SubTxTransitionPayload = require('./subtxtransitionpayload');
var CoinbasePayload = require('./coinbasepayload');
var CommitmentTxPayload = require('./commitmenttxpayload');
var ProRegTxPayload = require('./proregtxpayload');
var ProTxUpServPayload = require('./proupservtxpayload');
var ProUpRegTxPayload = require('./proupregtxpayload');
var ProUpRevTxPayload = require('./prouprevtxpayload');
var AssetLockPayload = require('./assetlockpayload');

var PayloadClasses = {};
PayloadClasses[RegisteredPayloadTypes.TRANSACTION_SUBTX_CLOSEACCOUNT] =
SubTxCloseAccountPayload;
PayloadClasses[RegisteredPayloadTypes.TRANSACTION_SUBTX_REGISTER] =
SubTxRegisterPayload;
PayloadClasses[RegisteredPayloadTypes.TRANSACTION_SUBTX_RESETKEY] =
SubTxResetKeyPayload;
PayloadClasses[RegisteredPayloadTypes.TRANSACTION_SUBTX_TOPUP] =
SubTxTopUpPayload;
PayloadClasses[RegisteredPayloadTypes.TRANSACTION_SUBTX_TRANSITION] =
SubTxTransitionPayload;
PayloadClasses[RegisteredPayloadTypes.TRANSACTION_COINBASE] = CoinbasePayload;
PayloadClasses[RegisteredPayloadTypes.TRANSACTION_QUORUM_COMMITMENT] =
CommitmentTxPayload;
Expand All @@ -38,6 +24,8 @@ PayloadClasses[RegisteredPayloadTypes.TRANSACTION_PROVIDER_UPDATE_REGISTRAR] =
ProUpRegTxPayload;
PayloadClasses[RegisteredPayloadTypes.TRANSACTION_PROVIDER_UPDATE_REVOKE] =
ProUpRevTxPayload;
PayloadClasses[RegisteredPayloadTypes.TRANSACTION_ASSET_LOCK] =
AssetLockPayload;

/**
*
Expand Down
Loading

0 comments on commit 2acc455

Please sign in to comment.