forked from harmony-one/horizon
/
Receipt.js
42 lines (36 loc) · 1.21 KB
/
Receipt.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
const { Receipt: LegacyReceipt } = require('eth-object')
const { decode } = require('eth-util-lite')
class Receipt extends LegacyReceipt {
type = 0
static fromRpc(rpcResult) {
const leagcyReceipt = LegacyReceipt.fromRpc(rpcResult)
const receipt = new Receipt(leagcyReceipt.raw)
receipt.type = Number(rpcResult.type || 0)
return receipt
}
static fromBuffer(buf) {
if (!buf) return new Receipt()
if (buf[0] < 0x7f) {
const receipt = new Receipt(decode(buf.slice(1)))
receipt.type = buf[0]
return receipt
}
return new Receipt(decode(buf))
}
static fromHex(hex = '') {
const buffer = Buffer.from(hex.startsWith('0x') ? hex.slice(2) : hex, 'hex')
return Receipt.fromBuffer(buffer)
}
serialize() {
const serialized = super.serialize()
// EIP2718: Receipt is either TransactionType || ReceiptPayload or LegacyReceipt
return this.type ? Buffer.concat([Buffer.from([this.type]), serialized]) : serialized
}
toBuffer() {
return this.serialize()
}
toHex() {
return '0x' + this.toBuffer().toString('hex')
}
}
module.exports = Receipt