-
Notifications
You must be signed in to change notification settings - Fork 1
/
caver-util.js
310 lines (287 loc) · 13.2 KB
/
caver-util.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
const utils = require('caver-js/packages/caver-utils')
const helpers = require('caver-js/packages/caver-core-helpers')
const Hash = require('eth-lib/lib/hash')
const RLP = require('eth-lib/lib/rlp')
const Bytes = require('eth-lib/lib/bytes')
const {
FEE_DELEGATED_VALUE_TRANSFER_TYPE_TAG,
FEE_DELEGATED_VALUE_TRANSFER_MEMO_TYPE_TAG,
FEE_DELEGATED_VALUE_TRANSFER_MEMO_WITH_RATIO_TYPE_TAG,
FEE_DELEGATED_VALUE_TRANSFER_WITH_RATIO_TYPE_TAG,
FEE_DELEGATED_SMART_CONTRACT_DEPLOY_TYPE_TAG,
FEE_DELEGATED_SMART_CONTRACT_DEPLOY_WITH_RATIO_TYPE_TAG,
FEE_DELEGATED_SMART_CONTRACT_EXECUTION_TYPE_TAG,
FEE_DELEGATED_SMART_CONTRACT_EXECUTION_WITH_RATIO_TYPE_TAG,
FEE_DELEGATED_CANCEL_TYPE_TAG,
FEE_DELEGATED_CANCEL_WITH_RATIO_TYPE_TAG,
} = helpers.constants
const {
encodeRLPByTxType,
makeRawTransaction,
getSenderTxHash,
splitFeePayer,
} = require('caver-js/packages/caver-klay/caver-klay-accounts/src/makeRawTransaction')
function inputCallFormatter (tx) {
return helpers.formatters.inputCallFormatter(tx)
}
function coverInitialTxValue (tx) {
if (typeof tx !== 'object') throw new Error('Invalid transaction')
if (!tx.senderRawTransaction && (!tx.type || tx.type === 'LEGACY' || tx.type.includes('SMART_CONTRACT_DEPLOY'))) {
tx.to = tx.to || '0x'
tx.data = utils.addHexPrefix(tx.data || '0x')
}
tx.chainId = utils.numberToHex(tx.chainId)
return tx
}
function decodeRawTransaction (transaction) {
if (!transaction.senderRawTransaction) {
return {}
}
// fee payer rlp encoding.
const typeDetacehdRawTransaction = `0x${transaction.senderRawTransaction.slice(4)}`
if (transaction.type === 'FEE_DELEGATED_VALUE_TRANSFER') {
// eslint-disable-next-line no-unused-vars
const [nonce, gasPrice, gas, to, value, from, [[v, r, s]]] = utils.rlpDecode(typeDetacehdRawTransaction)
return { nonce, gasPrice, gas, to, value, from }
} else if (transaction.type === 'FEE_DELEGATED_VALUE_TRANSFER_WITH_RATIO') {
// eslint-disable-next-line no-unused-vars
const [nonce, gasPrice, gas, to, value, from, feeRatio, [[v, r, s]]] = utils.rlpDecode(typeDetacehdRawTransaction)
return { nonce, gasPrice, gas, to, value, from, feeRatio }
} else if (transaction.type === 'FEE_DELEGATED_VALUE_TRANSFER_MEMO') {
// eslint-disable-next-line no-unused-vars
const [nonce, gasPrice, gas, to, value, from, data, [[v, r, s]]] = utils.rlpDecode(typeDetacehdRawTransaction)
return { nonce, gasPrice, gas, to, value, from, data }
} else if (transaction.type === 'FEE_DELEGATED_VALUE_TRANSFER_MEMO_WITH_RATIO') {
// eslint-disable-next-line no-unused-vars
const [nonce, gasPrice, gas, to, value, from, data, feeRatio, [[v, r, s]]] = utils.rlpDecode(typeDetacehdRawTransaction)
return { nonce, gasPrice, gas, to, value, from, data, feeRatio }
} else if (transaction.type === 'FEE_DELEGATED_SMART_CONTRACT_EXECUTION') {
// eslint-disable-next-line no-unused-vars
const [nonce, gasPrice, gas, to, value, from, data, [[v, r, s]]] = utils.rlpDecode(typeDetacehdRawTransaction)
return { nonce, gasPrice, gas, to, value, from, data }
} else if (transaction.type === 'FEE_DELEGATED_SMART_CONTRACT_EXECUTION_WITH_RATIO') {
// eslint-disable-next-line no-unused-vars
const [nonce, gasPrice, gas, to, value, from, data, feeRatio, [[v, r, s]]] = utils.rlpDecode(typeDetacehdRawTransaction)
return { nonce, gasPrice, gas, to, value, from, data, feeRatio }
} else if (transaction.type === 'CANCEL') {
} else if (transaction.type === 'FEE_DELEGATED_CANCEL') {
// eslint-disable-next-line no-unused-vars
const [nonce, gasPrice, gas, from, [[v, r, s]]] = utils.rlpDecode(typeDetacehdRawTransaction)
return { nonce, gasPrice, gas, from }
} else if (transaction.type === 'FEE_DELEGATED_CANCEL_WITH_RATIO') {
// eslint-disable-next-line no-unused-vars
const [nonce, gasPrice, gas, from, feeRatio, [[v, r, s]]] = utils.rlpDecode(typeDetacehdRawTransaction)
return { nonce, gasPrice, gas, from, feeRatio }
} else if (transaction.type === 'FEE_DELEGATED_SMART_CONTRACT_DEPLOY') {
// eslint-disable-next-line no-unused-vars
const [nonce, gasPrice, gas, to, value, from, data, humanReadable, codeFormat, [[v, r, s]]] = utils.rlpDecode(typeDetacehdRawTransaction)
return { nonce, gasPrice, gas, to, value, from, data, humanReadable, codeFormat }
} else if (transaction.type === 'FEE_DELEGATED_SMART_CONTRACT_DEPLOY_WITH_RATIO') {
// eslint-disable-next-line no-unused-vars
const [nonce, gasPrice, gas, to, value, from, data, humanReadable, feeRatio, codeFormat, [[v, r, s]]] = utils.rlpDecode(typeDetacehdRawTransaction)
return { nonce, gasPrice, gas, to, value, from, data, humanReadable, feeRatio, codeFormat }
} else {
return {}
}
}
function getRlpData (type, values) {
switch (type) {
case 'FEE_DELEGATED_VALUE_TRANSFER':
return RLP.encode([
FEE_DELEGATED_VALUE_TRANSFER_TYPE_TAG,
Bytes.fromNat(values.nonce),
Bytes.fromNat(values.gasPrice),
Bytes.fromNat(values.gas),
values.to.toLowerCase(),
Bytes.fromNat(values.value),
values.from.toLowerCase(),
])
case 'FEE_DELEGATED_VALUE_TRANSFER_WITH_RATIO':
return RLP.encode([
FEE_DELEGATED_VALUE_TRANSFER_WITH_RATIO_TYPE_TAG,
Bytes.fromNat(values.nonce),
Bytes.fromNat(values.gasPrice),
Bytes.fromNat(values.gas),
values.to.toLowerCase(),
Bytes.fromNat(values.value),
values.from.toLowerCase(),
values.feeRatio,
])
case 'FEE_DELEGATED_VALUE_TRANSFER_MEMO':
return RLP.encode([
FEE_DELEGATED_VALUE_TRANSFER_MEMO_TYPE_TAG,
Bytes.fromNat(values.nonce),
Bytes.fromNat(values.gasPrice),
Bytes.fromNat(values.gas),
values.to.toLowerCase(),
Bytes.fromNat(values.value),
values.from.toLowerCase(),
values.data,
])
case 'FEE_DELEGATED_VALUE_TRANSFER_MEMO_WITH_RATIO':
return RLP.encode([
FEE_DELEGATED_VALUE_TRANSFER_MEMO_WITH_RATIO_TYPE_TAG,
Bytes.fromNat(values.nonce),
Bytes.fromNat(values.gasPrice),
Bytes.fromNat(values.gas),
values.to.toLowerCase(),
Bytes.fromNat(values.value),
values.from.toLowerCase(),
values.data,
Bytes.fromNat(values.feeRatio),
])
case 'FEE_DELEGATED_SMART_CONTRACT_EXECUTION':
return RLP.encode([
FEE_DELEGATED_SMART_CONTRACT_EXECUTION_TYPE_TAG,
Bytes.fromNat(values.nonce),
Bytes.fromNat(values.gasPrice),
Bytes.fromNat(values.gas),
values.to.toLowerCase(),
Bytes.fromNat(values.value),
values.from.toLowerCase(),
values.data,
])
case 'FEE_DELEGATED_SMART_CONTRACT_EXECUTION_WITH_RATIO':
return RLP.encode([
FEE_DELEGATED_SMART_CONTRACT_EXECUTION_WITH_RATIO_TYPE_TAG,
Bytes.fromNat(values.nonce),
Bytes.fromNat(values.gasPrice),
Bytes.fromNat(values.gas),
values.to.toLowerCase(),
Bytes.fromNat(values.value),
values.from.toLowerCase(),
values.data,
Bytes.fromNat(values.feeRatio),
])
case 'FEE_DELEGATED_CANCEL' :
return RLP.encode([
FEE_DELEGATED_CANCEL_TYPE_TAG,
Bytes.fromNat(values.nonce),
Bytes.fromNat(values.gasPrice),
Bytes.fromNat(values.gas),
values.from.toLowerCase(),
])
case 'FEE_DELEGATED_CANCEL_WITH_RATIO':
return RLP.encode([
FEE_DELEGATED_CANCEL_WITH_RATIO_TYPE_TAG,
Bytes.fromNat(values.nonce),
Bytes.fromNat(values.gasPrice),
Bytes.fromNat(values.gas),
values.from.toLowerCase(),
Bytes.fromNat(values.feeRatio),
])
case 'FEE_DELEGATED_SMART_CONTRACT_DEPLOY':
return RLP.encode([
FEE_DELEGATED_SMART_CONTRACT_DEPLOY_TYPE_TAG,
Bytes.fromNat(values.nonce),
Bytes.fromNat(values.gasPrice),
Bytes.fromNat(values.gas),
values.to.toLowerCase(),
Bytes.fromNat(values.value),
values.from.toLowerCase(),
values.data,
values.humanReadable,
values.codeFormat,
])
case 'FEE_DELEGATED_SMART_CONTRACT_DEPLOY_WITH_RATIO':
return RLP.encode([
FEE_DELEGATED_SMART_CONTRACT_DEPLOY_WITH_RATIO_TYPE_TAG,
Bytes.fromNat(values.nonce),
Bytes.fromNat(values.gasPrice),
Bytes.fromNat(values.gas),
values.to.toLowerCase(),
Bytes.fromNat(values.value),
values.from.toLowerCase(),
values.data,
values.humanReadable,
Bytes.fromNat(values.feeRatio),
values.codeFormat,
])
default:
return {}
}
}
function generateTxObject (tx) {
let isLegacy = false
let isFeePayer = false
let existedFeePayerSignatures = []
let existedSenderSignatures = []
if (tx.senderRawTransaction) {
if (tx.feePayerSignatures) {
existedFeePayerSignatures = existedFeePayerSignatures.concat(tx.feePayerSignatures)
}
try {
// Decode senderRawTransaction to get signatures of fee payer
const { senderRawTransaction, feePayer, feePayerSignatures } = splitFeePayer(tx.senderRawTransaction)
// feePayer !== '0x' means that in senderRawTransaction there are feePayerSignatures
if (feePayer !== '0x' && feePayer !== '0x0000000000000000000000000000000000000000') {
// The feePayer inside the tx object does not match the feePayer information contained in the senderRawTransaction.
if (feePayer.toLowerCase() !== tx.feePayer.toLowerCase()) {
return Promise.reject(`Invalid feePayer: The fee payer(${feePayer}) included in the transaction does not match the fee payer(${tx.feePayer}) you want to sign.`)
}
existedFeePayerSignatures = existedFeePayerSignatures.concat(feePayerSignatures)
}
tx.senderRawTransaction = senderRawTransaction
isFeePayer = true
} catch (e) {
return Promise.reject(e.message || e)
}
} else {
isLegacy = !!(tx.type === undefined || tx.type === 'LEGACY')
if (tx.signatures) {
// if there is existed signatures or feePayerSignatures, those should be preserved.
if (isLegacy) {
return Promise.reject('Legacy transaction cannot be signed with multiple keys.')
}
existedSenderSignatures = existedSenderSignatures.concat(tx.signatures)
}
}
tx = inputCallFormatter(tx)
const transaction = coverInitialTxValue(tx)
const rlpEncoded = encodeRLPByTxType(transaction)
const txObj = Object.assign({}, tx)
const decoded = decodeRawTransaction(tx)
let rlpData
if (isFeePayer) {
rlpData = getRlpData(tx.type, decoded)
} else {
rlpData = tx.data
}
txObj.nonce = tx.nonce || '0'
txObj.nonce = (txObj.nonce === '0x') ? '0x0' : txObj.nonce
txObj.gasPrice = tx.gasPrice || decoded.gasPrice || '0x00'
txObj.gas = tx.gas || decoded.gas || '0x00'
txObj.to = tx.to || ''
txObj.value = tx.value || '0x00'
txObj.from = isFeePayer ? tx.feePayer : tx.from
txObj.data = rlpData || '0x'
txObj.feeRatio = utils.hexToNumber(tx.feeRatio) || utils.hexToNumber(decoded.feeRatio) || null
txObj.contract = tx.contract || null
txObj.chainId = utils.hexToNumber(tx.chainId)
//
txObj.ref = { isFeePayer, transaction, rlpEncoded, existedFeePayerSignatures, existedSenderSignatures }
return txObj
}
//
function getTransactionResult (isFeePayer, transaction, rlpEncoded, sigs) {
const { rawTransaction, signatures, feePayerSignatures } = makeRawTransaction(rlpEncoded, sigs, transaction)
const result = {
// messageHash: Hash.keccak256(rlpEncoded),
v: sigs[0][0],
r: sigs[0][1],
s: sigs[0][2],
rawTransaction,
txHash: Hash.keccak256(rawTransaction),
senderTxHash: getSenderTxHash(rawTransaction),
}
if (isFeePayer) {
result.feePayerSignatures = feePayerSignatures
} else {
result.signatures = signatures
}
return result
}
module.exports = {
getTransactionResult,
generateTxObject,
}