/
AssetOutPoint.ts
81 lines (75 loc) · 2.59 KB
/
AssetOutPoint.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { H256 } from "../H256";
export type AssetOutPointData = {
transactionHash: H256;
index: number;
assetType: H256;
amount: number;
lockScriptHash?: H256;
parameters?: Buffer[];
};
/**
* AssetOutPoint consists of transactionHash and index, asset type, and amount.
*
* - The transaction that it points to must be either AssetMint or AssetTransfer.
* - Index is what decides which Asset to point to amongst the Asset list that transaction creates.
* - The asset type and amount must be identical to the Asset that it points to.
*/
export class AssetOutPoint {
readonly transactionHash: H256;
readonly index: number;
readonly assetType: H256;
readonly amount: number;
readonly lockScriptHash?: H256;
readonly parameters?: Buffer[];
/**
* @param data.transactionHash A transaction hash that Asset is created at.
* @param data.index An index in the outputs of a transaction.
* @param data.assetType An asset type of an asset that it points.
* @param data.amount Asset amount of an asset that it points.
* @param data.lockScriptHash A lock script hash of an asset.
* @param data.parameters Parameters of an asset.
*/
constructor(data: AssetOutPointData) {
const { transactionHash, index, assetType, amount, lockScriptHash, parameters } = data;
this.transactionHash = transactionHash;
this.index = index;
this.assetType = assetType;
this.amount = amount;
this.lockScriptHash = lockScriptHash;
this.parameters = parameters;
}
/**
* Convert to an object for RLP encoding.
*/
toEncodeObject() {
const { transactionHash, index, assetType, amount } = this;
return [transactionHash.toEncodeObject(), index, assetType.toEncodeObject(), amount];
}
/**
* Create an AssetOutPoint from an AssetOutPoint JSON object.
* @param data An AssetOutPoint JSON object.
* @returns An AssetOutPoint.
*/
static fromJSON(data: any) {
const { transactionHash, index, assetType, amount } = data;
return new this({
transactionHash: new H256(transactionHash),
index,
assetType: new H256(assetType),
amount,
});
}
/**
* Convert to an AssetOutPoint JSON object.
* @returns An AssetOutPoint JSON object.
*/
toJSON() {
const { transactionHash, index, assetType, amount } = this;
return {
transactionHash: transactionHash.value,
index,
assetType: assetType.value,
amount,
};
}
}