1 | const SECUtil = require('@sec-block/secjs-util')
|
2 | const SECTransactionTxModel = require('../model/transactionchain-trans-model')
|
3 |
|
4 | const TX_VERSION = '0.1'
|
5 |
|
6 | class SECTransactionTx {
|
7 | |
8 |
|
9 |
|
10 |
|
11 | constructor (tx = {}) {
|
12 | this.txBuffer = []
|
13 | this.tx = SECTransactionTxModel
|
14 |
|
15 | if (Object.keys(tx).length !== 0) {
|
16 | this.setTx(tx)
|
17 | }
|
18 | }
|
19 |
|
20 | getTx () {
|
21 | return this.tx
|
22 | }
|
23 |
|
24 | getTxBuffer () {
|
25 | return this.txBuffer
|
26 | }
|
27 |
|
28 | setTx (tx) {
|
29 | if (!(Array.isArray(tx))) {
|
30 |
|
31 | this._setTxFromJson(tx)
|
32 | } else {
|
33 |
|
34 | this._setTxFromBuffer(tx)
|
35 | }
|
36 | }
|
37 |
|
38 | _setTxFromJson (tx) {
|
39 | let self = this
|
40 |
|
41 | this.tx = SECTransactionTxModel
|
42 |
|
43 |
|
44 | Object.keys(tx).forEach(function (key) {
|
45 | if (!(key in tx)) {
|
46 | throw new Error(`key: ${key} is not recognized`)
|
47 | }
|
48 | self.tx.key = tx.key
|
49 | })
|
50 | this.tx.TxHash = this._calculateTxHash()
|
51 |
|
52 |
|
53 | this.txBuffer = [
|
54 | Buffer.from(this.tx.TxHash, 'hex'),
|
55 | Buffer.from(this.tx.TxReceiptStatus),
|
56 | Buffer.from(this.tx.Version),
|
57 | SECUtil.intToBuffer(this.tx.TimeStamp),
|
58 | Buffer.from(this.tx.SellerAddress),
|
59 | Buffer.from(this.tx.BuyerAddress),
|
60 | Buffer.from(this.tx.ShareHash),
|
61 | SECUtil.intToBuffer(this.tx.ShareTimeStamp),
|
62 | Buffer.from(JSON.stringify(this.tx.ProductInfo)),
|
63 | SECUtil.intToBuffer(this.tx.SharedTimes),
|
64 | Buffer.from(this.tx.Status),
|
65 | Buffer.from(this.tx.InputData)
|
66 | ]
|
67 | }
|
68 |
|
69 | _setTxFromBuffer (txBuffer) {
|
70 |
|
71 | this.tx = SECTransactionTxModel
|
72 |
|
73 | if (txBuffer.length !== 12) {
|
74 | throw new Error(`input txBuffer length(${txBuffer.length}) mismatch, its length should be: 12`)
|
75 | }
|
76 |
|
77 |
|
78 | this.tx.TxHash = txBuffer[0].toString('hex')
|
79 | this.tx.TxReceiptStatus = txBuffer[1].toString()
|
80 | this.tx.Version = txBuffer[2].toString()
|
81 | this.tx.TimeStamp = SECUtil.bufferToInt(txBuffer[3])
|
82 | this.tx.SellerAddress = txBuffer[4].toString()
|
83 | this.tx.BuyerAddress = txBuffer[5].toString()
|
84 | this.tx.ShareHash = txBuffer[6].toString()
|
85 | this.tx.ShareTimeStamp = SECUtil.bufferToInt(txBuffer[7])
|
86 | this.tx.ProductInfo = JSON.parse(txBuffer[8].toString())
|
87 | this.tx.SharedTimes = SECUtil.bufferToInt(txBuffer[9])
|
88 | this.tx.Status = txBuffer[10].toString()
|
89 | this.tx.InputData = txBuffer[11].toString()
|
90 |
|
91 |
|
92 | this.txBuffer = txBuffer
|
93 | }
|
94 |
|
95 | _calculateTxHash () {
|
96 | let txHashBuffer = [
|
97 | Buffer.from(TX_VERSION),
|
98 | SECUtil.intToBuffer(this.tx.TimeStamp),
|
99 | Buffer.from(this.tx.SellerAddress, 'hex'),
|
100 | Buffer.from(this.tx.BuyerAddress, 'hex'),
|
101 | Buffer.from(this.tx.ShareHash),
|
102 | Buffer.from(this.tx.ShareTimeStamp),
|
103 | SECUtil.intToBuffer(this.tx.SharedTimes),
|
104 | Buffer.from(this.tx.InputData)
|
105 | ]
|
106 |
|
107 | return SECUtil.rlphash(txHashBuffer).toString('hex')
|
108 | }
|
109 |
|
110 | getTxHash () {
|
111 | if (this.tx.TxHash !== '') {
|
112 | return this.tx.TxHash
|
113 | } else {
|
114 | throw Error('transaction hash not defined')
|
115 | }
|
116 | }
|
117 | }
|
118 |
|
119 | module.exports = SECTransactionTx
|