Skip to content

Commit e112ec0

Browse files
authored
feat: asset unlock transaction payload (#293)
* feat: add assetunlockpayload.js * test: validation
1 parent 1abe3dc commit e112ec0

File tree

9 files changed

+655
-2
lines changed

9 files changed

+655
-2
lines changed

index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ export { ProRegTxPayload } from './typings/transaction/payload/ProRegTxPayload';
6464
export { ProUpRegTxPayload } from './typings/transaction/payload/ProUpRegTxPayload';
6565
export { ProUpRevTxPayload } from './typings/transaction/payload/ProUpRevTxPayload';
6666
export { ProUpServTxPayload } from './typings/transaction/payload/ProUpServTxPayload';
67+
export { AssetLockPayload } from './typings/transaction/payload/AssetLockPayload';
68+
export { AssetUnlockPayload } from './typings/transaction/payload/AssetUnlockPayload';
6769

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

@@ -362,4 +364,4 @@ export function traverseAndBuildPartialTree(
362364
position: number,
363365
hashes: Buffer[],
364366
matches: boolean[]
365-
): any;
367+
): any;

lib/constants/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ module.exports = {
2525
TRANSACTION_COINBASE: 5,
2626
TRANSACTION_QUORUM_COMMITMENT: 6,
2727
TRANSACTION_ASSET_LOCK: 8,
28+
TRANSACTION_ASSET_UNLOCK: 9,
2829
},
2930
EMPTY_SIGNATURE_SIZE: 0,
3031
primitives: {
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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;

lib/transaction/payload/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ Payload.CoinbasePayload = require('./coinbasepayload');
1111
Payload.constants = require('../../constants');
1212
Payload.CommitmentTxPayload = require('./commitmenttxpayload');
1313
Payload.AssetLockPayload = require('./assetlockpayload');
14+
Payload.AssetUnlockPayload = require('./assetunlockpayload');
1415

1516
module.exports = Payload;

lib/transaction/payload/payload.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var ProTxUpServPayload = require('./proupservtxpayload');
1111
var ProUpRegTxPayload = require('./proupregtxpayload');
1212
var ProUpRevTxPayload = require('./prouprevtxpayload');
1313
var AssetLockPayload = require('./assetlockpayload');
14+
var AssetUnlockPayload = require('./assetunlockpayload');
1415

1516
var PayloadClasses = {};
1617
PayloadClasses[RegisteredPayloadTypes.TRANSACTION_COINBASE] = CoinbasePayload;
@@ -26,6 +27,8 @@ PayloadClasses[RegisteredPayloadTypes.TRANSACTION_PROVIDER_UPDATE_REVOKE] =
2627
ProUpRevTxPayload;
2728
PayloadClasses[RegisteredPayloadTypes.TRANSACTION_ASSET_LOCK] =
2829
AssetLockPayload;
30+
PayloadClasses[RegisteredPayloadTypes.TRANSACTION_ASSET_UNLOCK] =
31+
AssetUnlockPayload;
2932

3033
/**
3134
*

test-d/index.test-d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ import type {
3737
MultiSigInput,
3838
MultiSigScriptHashInput,
3939
AbstractPayload,
40+
AssetLockPayload,
41+
AssetUnlockPayload,
4042
CoinbasePayload,
4143
CommitmentTxPayload,
4244
ProRegTxPayload,

0 commit comments

Comments
 (0)