This repository has been archived by the owner on Jun 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 272
/
object.ts
117 lines (103 loc) · 3.11 KB
/
object.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
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
import assert from 'assert'
import * as ethjsUtil from 'ethjs-util'
import * as rlp from 'rlp'
import { toBuffer, baToJSON, unpadBuffer } from './bytes'
/**
* Defines properties on a `Object`. It make the assumption that underlying data is binary.
* @param self the `Object` to define properties on
* @param fields an array fields to define. Fields can contain:
* * `name` - the name of the properties
* * `length` - the number of bytes the field can have
* * `allowLess` - if the field can be less than the length
* * `allowEmpty`
* @param data data to be validated against the definitions
* @deprecated
*/
export const defineProperties = function(self: any, fields: any, data?: any) {
self.raw = []
self._fields = []
// attach the `toJSON`
self.toJSON = function(label: boolean = false) {
if (label) {
type Dict = { [key: string]: string }
const obj: Dict = {}
self._fields.forEach((field: string) => {
obj[field] = `0x${self[field].toString('hex')}`
})
return obj
}
return baToJSON(self.raw)
}
self.serialize = function serialize() {
return rlp.encode(self.raw)
}
fields.forEach((field: any, i: number) => {
self._fields.push(field.name)
function getter() {
return self.raw[i]
}
function setter(v: any) {
v = toBuffer(v)
if (v.toString('hex') === '00' && !field.allowZero) {
v = Buffer.allocUnsafe(0)
}
if (field.allowLess && field.length) {
v = unpadBuffer(v)
assert(
field.length >= v.length,
`The field ${field.name} must not have more ${field.length} bytes`
)
} else if (!(field.allowZero && v.length === 0) && field.length) {
assert(
field.length === v.length,
`The field ${field.name} must have byte length of ${field.length}`
)
}
self.raw[i] = v
}
Object.defineProperty(self, field.name, {
enumerable: true,
configurable: true,
get: getter,
set: setter
})
if (field.default) {
self[field.name] = field.default
}
// attach alias
if (field.alias) {
Object.defineProperty(self, field.alias, {
enumerable: false,
configurable: true,
set: setter,
get: getter
})
}
})
// if the constuctor is passed data
if (data) {
if (typeof data === 'string') {
data = Buffer.from(ethjsUtil.stripHexPrefix(data), 'hex')
}
if (Buffer.isBuffer(data)) {
data = rlp.decode(data)
}
if (Array.isArray(data)) {
if (data.length > self._fields.length) {
throw new Error('wrong number of fields in data')
}
// make sure all the items are buffers
data.forEach((d, i) => {
self[self._fields[i]] = toBuffer(d)
})
} else if (typeof data === 'object') {
const keys = Object.keys(data)
fields.forEach((field: any) => {
if (keys.indexOf(field.name) !== -1) self[field.name] = data[field.name]
if (keys.indexOf(field.alias) !== -1) self[field.alias] = data[field.alias]
})
} else {
throw new Error('invalid data')
}
}
}