ripple-binary-codec 2.8.0-smartcontract.0 → 2.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/enums/definitions.json +3261 -302
- package/dist/enums/src/enums/definitions.json +3261 -307
- package/dist/serdes/binary-serializer.d.ts +1 -1
- package/dist/serdes/binary-serializer.js +2 -2
- package/dist/serdes/binary-serializer.js.map +1 -1
- package/dist/src/enums/definitions.json +3261 -302
- package/dist/src/serdes/binary-serializer.d.ts +1 -1
- package/dist/src/serdes/binary-serializer.js +2 -2
- package/dist/src/serdes/binary-serializer.js.map +1 -1
- package/dist/src/types/account-id.d.ts +0 -2
- package/dist/src/types/account-id.js +0 -4
- package/dist/src/types/account-id.js.map +1 -1
- package/dist/src/types/amount.d.ts +1 -2
- package/dist/src/types/amount.js +0 -3
- package/dist/src/types/amount.js.map +1 -1
- package/dist/src/types/blob.d.ts +1 -2
- package/dist/src/types/blob.js +0 -3
- package/dist/src/types/blob.js.map +1 -1
- package/dist/src/types/currency.d.ts +0 -2
- package/dist/src/types/currency.js +0 -4
- package/dist/src/types/currency.js.map +1 -1
- package/dist/src/types/hash-128.d.ts +0 -2
- package/dist/src/types/hash-128.js +0 -4
- package/dist/src/types/hash-128.js.map +1 -1
- package/dist/src/types/hash-160.d.ts +0 -2
- package/dist/src/types/hash-160.js +0 -4
- package/dist/src/types/hash-160.js.map +1 -1
- package/dist/src/types/hash-192.d.ts +0 -2
- package/dist/src/types/hash-192.js +0 -4
- package/dist/src/types/hash-192.js.map +1 -1
- package/dist/src/types/hash-256.d.ts +0 -2
- package/dist/src/types/hash-256.js +0 -4
- package/dist/src/types/hash-256.js.map +1 -1
- package/dist/src/types/index.d.ts +1 -3
- package/dist/src/types/index.js +1 -7
- package/dist/src/types/index.js.map +1 -1
- package/dist/src/types/issue.d.ts +1 -2
- package/dist/src/types/issue.js +0 -3
- package/dist/src/types/issue.js.map +1 -1
- package/dist/src/types/path-set.d.ts +1 -2
- package/dist/src/types/path-set.js +0 -3
- package/dist/src/types/path-set.js.map +1 -1
- package/dist/src/types/serialized-type.d.ts +0 -43
- package/dist/src/types/serialized-type.js +1 -63
- package/dist/src/types/serialized-type.js.map +1 -1
- package/dist/src/types/st-array.d.ts +1 -2
- package/dist/src/types/st-array.js +0 -3
- package/dist/src/types/st-array.js.map +1 -1
- package/dist/src/types/st-number.d.ts +1 -2
- package/dist/src/types/st-number.js +0 -3
- package/dist/src/types/st-number.js.map +1 -1
- package/dist/src/types/st-object.d.ts +1 -2
- package/dist/src/types/st-object.js +1 -9
- package/dist/src/types/st-object.js.map +1 -1
- package/dist/src/types/uint-16.d.ts +0 -2
- package/dist/src/types/uint-16.js +0 -4
- package/dist/src/types/uint-16.js.map +1 -1
- package/dist/src/types/uint-32.d.ts +0 -2
- package/dist/src/types/uint-32.js +0 -4
- package/dist/src/types/uint-32.js.map +1 -1
- package/dist/src/types/uint-64.d.ts +0 -2
- package/dist/src/types/uint-64.js +0 -4
- package/dist/src/types/uint-64.js.map +1 -1
- package/dist/src/types/uint-8.d.ts +0 -2
- package/dist/src/types/uint-8.js +0 -4
- package/dist/src/types/uint-8.js.map +1 -1
- package/dist/src/types/vector-256.d.ts +1 -2
- package/dist/src/types/vector-256.js +0 -3
- package/dist/src/types/vector-256.js.map +1 -1
- package/dist/src/types/xchain-bridge.d.ts +1 -2
- package/dist/src/types/xchain-bridge.js +0 -3
- package/dist/src/types/xchain-bridge.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types/account-id.d.ts +0 -2
- package/dist/types/account-id.js +0 -4
- package/dist/types/account-id.js.map +1 -1
- package/dist/types/amount.d.ts +1 -2
- package/dist/types/amount.js +0 -3
- package/dist/types/amount.js.map +1 -1
- package/dist/types/blob.d.ts +1 -2
- package/dist/types/blob.js +0 -3
- package/dist/types/blob.js.map +1 -1
- package/dist/types/currency.d.ts +0 -2
- package/dist/types/currency.js +0 -4
- package/dist/types/currency.js.map +1 -1
- package/dist/types/hash-128.d.ts +0 -2
- package/dist/types/hash-128.js +0 -4
- package/dist/types/hash-128.js.map +1 -1
- package/dist/types/hash-160.d.ts +0 -2
- package/dist/types/hash-160.js +0 -4
- package/dist/types/hash-160.js.map +1 -1
- package/dist/types/hash-192.d.ts +0 -2
- package/dist/types/hash-192.js +0 -4
- package/dist/types/hash-192.js.map +1 -1
- package/dist/types/hash-256.d.ts +0 -2
- package/dist/types/hash-256.js +0 -4
- package/dist/types/hash-256.js.map +1 -1
- package/dist/types/index.d.ts +1 -3
- package/dist/types/index.js +1 -7
- package/dist/types/index.js.map +1 -1
- package/dist/types/issue.d.ts +1 -2
- package/dist/types/issue.js +0 -3
- package/dist/types/issue.js.map +1 -1
- package/dist/types/path-set.d.ts +1 -2
- package/dist/types/path-set.js +0 -3
- package/dist/types/path-set.js.map +1 -1
- package/dist/types/serialized-type.d.ts +0 -43
- package/dist/types/serialized-type.js +1 -63
- package/dist/types/serialized-type.js.map +1 -1
- package/dist/types/st-array.d.ts +1 -2
- package/dist/types/st-array.js +0 -3
- package/dist/types/st-array.js.map +1 -1
- package/dist/types/st-number.d.ts +1 -2
- package/dist/types/st-number.js +0 -3
- package/dist/types/st-number.js.map +1 -1
- package/dist/types/st-object.d.ts +1 -2
- package/dist/types/st-object.js +1 -9
- package/dist/types/st-object.js.map +1 -1
- package/dist/types/uint-16.d.ts +0 -2
- package/dist/types/uint-16.js +0 -4
- package/dist/types/uint-16.js.map +1 -1
- package/dist/types/uint-32.d.ts +0 -2
- package/dist/types/uint-32.js +0 -4
- package/dist/types/uint-32.js.map +1 -1
- package/dist/types/uint-64.d.ts +0 -2
- package/dist/types/uint-64.js +0 -4
- package/dist/types/uint-64.js.map +1 -1
- package/dist/types/uint-8.d.ts +0 -2
- package/dist/types/uint-8.js +0 -4
- package/dist/types/uint-8.js.map +1 -1
- package/dist/types/vector-256.d.ts +1 -2
- package/dist/types/vector-256.js +0 -3
- package/dist/types/vector-256.js.map +1 -1
- package/dist/types/xchain-bridge.d.ts +1 -2
- package/dist/types/xchain-bridge.js +0 -3
- package/dist/types/xchain-bridge.js.map +1 -1
- package/package.json +3 -3
- package/src/enums/definitions.json +3261 -307
- package/src/serdes/binary-serializer.ts +2 -2
- package/src/types/account-id.ts +0 -5
- package/src/types/amount.ts +1 -5
- package/src/types/blob.ts +1 -5
- package/src/types/currency.ts +0 -5
- package/src/types/hash-128.ts +0 -5
- package/src/types/hash-160.ts +0 -5
- package/src/types/hash-192.ts +0 -5
- package/src/types/hash-256.ts +0 -5
- package/src/types/index.ts +0 -6
- package/src/types/issue.ts +1 -5
- package/src/types/path-set.ts +1 -5
- package/src/types/serialized-type.ts +0 -67
- package/src/types/st-array.ts +1 -5
- package/src/types/st-number.ts +1 -5
- package/src/types/st-object.ts +2 -12
- package/src/types/uint-16.ts +0 -5
- package/src/types/uint-32.ts +0 -5
- package/src/types/uint-64.ts +0 -5
- package/src/types/uint-8.ts +0 -5
- package/src/types/vector-256.ts +1 -5
- package/src/types/xchain-bridge.ts +1 -5
- package/dist/src/types/data.d.ts +0 -86
- package/dist/src/types/data.js +0 -252
- package/dist/src/types/data.js.map +0 -1
- package/dist/src/types/dataType.d.ts +0 -94
- package/dist/src/types/dataType.js +0 -145
- package/dist/src/types/dataType.js.map +0 -1
- package/dist/src/types/json.d.ts +0 -173
- package/dist/src/types/json.js +0 -531
- package/dist/src/types/json.js.map +0 -1
- package/dist/types/data.d.ts +0 -86
- package/dist/types/data.js +0 -252
- package/dist/types/data.js.map +0 -1
- package/dist/types/dataType.d.ts +0 -94
- package/dist/types/dataType.js +0 -145
- package/dist/types/dataType.js.map +0 -1
- package/dist/types/json.d.ts +0 -173
- package/dist/types/json.js +0 -531
- package/dist/types/json.js.map +0 -1
- package/src/types/data.ts +0 -294
- package/src/types/dataType.ts +0 -178
- package/src/types/json.ts +0 -650
package/src/types/data.ts
DELETED
|
@@ -1,294 +0,0 @@
|
|
|
1
|
-
import { BinaryParser } from '../serdes/binary-parser'
|
|
2
|
-
import {
|
|
3
|
-
JsonObject,
|
|
4
|
-
SerializedType,
|
|
5
|
-
SerializedTypeID,
|
|
6
|
-
TYPE_ID_TO_STRING,
|
|
7
|
-
TYPE_STRING_TO_ID,
|
|
8
|
-
TYPE_NUMBER_TO_ID,
|
|
9
|
-
} from './serialized-type'
|
|
10
|
-
import { readUInt16BE, writeUInt16BE } from '../utils'
|
|
11
|
-
import { bytesToHex, concat } from '@xrplf/isomorphic/utils'
|
|
12
|
-
import { Hash128 } from './hash-128'
|
|
13
|
-
import { Hash160 } from './hash-160'
|
|
14
|
-
import { Hash192 } from './hash-192'
|
|
15
|
-
import { Hash256 } from './hash-256'
|
|
16
|
-
import { AccountID } from './account-id'
|
|
17
|
-
import { Amount } from './amount'
|
|
18
|
-
import { Blob } from './blob'
|
|
19
|
-
import { Currency } from './currency'
|
|
20
|
-
import { STNumber } from './st-number'
|
|
21
|
-
import { Issue } from './issue'
|
|
22
|
-
import { UInt8 } from './uint-8'
|
|
23
|
-
import { UInt16 } from './uint-16'
|
|
24
|
-
import { UInt32 } from './uint-32'
|
|
25
|
-
import { UInt64 } from './uint-64'
|
|
26
|
-
import { BinarySerializer } from '../binary'
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Interface for Data JSON representation
|
|
30
|
-
*/
|
|
31
|
-
interface DataJSON extends JsonObject {
|
|
32
|
-
type: string
|
|
33
|
-
value: string | number | JsonObject
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Map from SerializedTypeID to the corresponding type class.
|
|
38
|
-
* Types listed here use standard from()/fromParser()/toBytes() without
|
|
39
|
-
* any extra framing (unlike VL and Account which need length prefixes).
|
|
40
|
-
*/
|
|
41
|
-
const SIMPLE_TYPE_MAP: Partial<
|
|
42
|
-
Record<SerializedTypeID, typeof SerializedType>
|
|
43
|
-
> = {
|
|
44
|
-
[SerializedTypeID.STI_UINT8]: UInt8,
|
|
45
|
-
[SerializedTypeID.STI_UINT16]: UInt16,
|
|
46
|
-
[SerializedTypeID.STI_UINT32]: UInt32,
|
|
47
|
-
[SerializedTypeID.STI_UINT64]: UInt64,
|
|
48
|
-
[SerializedTypeID.STI_UINT128]: Hash128,
|
|
49
|
-
[SerializedTypeID.STI_UINT160]: Hash160,
|
|
50
|
-
[SerializedTypeID.STI_UINT192]: Hash192,
|
|
51
|
-
[SerializedTypeID.STI_UINT256]: Hash256,
|
|
52
|
-
[SerializedTypeID.STI_AMOUNT]: Amount,
|
|
53
|
-
[SerializedTypeID.STI_ISSUE]: Issue,
|
|
54
|
-
[SerializedTypeID.STI_CURRENCY]: Currency,
|
|
55
|
-
[SerializedTypeID.STI_NUMBER]: STNumber,
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Types whose from() method expects a numeric argument.
|
|
60
|
-
* For these, json.value is coerced to a number before calling from().
|
|
61
|
-
*/
|
|
62
|
-
const NUMERIC_TYPES = new Set<SerializedTypeID>([
|
|
63
|
-
SerializedTypeID.STI_UINT8,
|
|
64
|
-
SerializedTypeID.STI_UINT16,
|
|
65
|
-
SerializedTypeID.STI_UINT32,
|
|
66
|
-
])
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* STData: Encodes XRPL's "Data" type.
|
|
70
|
-
*
|
|
71
|
-
* This type wraps both a SerializedTypeID and the actual data value.
|
|
72
|
-
* It's encoded as a 2-byte type ID followed by the serialized data.
|
|
73
|
-
*
|
|
74
|
-
* Usage:
|
|
75
|
-
* Data.from({ type: "Amount", value: "1000000" })
|
|
76
|
-
* Data.from({ type: "UInt64", value: "123456789" })
|
|
77
|
-
* Data.fromParser(parser)
|
|
78
|
-
*/
|
|
79
|
-
class Data extends SerializedType {
|
|
80
|
-
static readonly ZERO_DATA: Data = new Data(
|
|
81
|
-
concat([
|
|
82
|
-
new Uint8Array([0x00, 0x01]), // Type ID for UINT16 (SerializedTypeID.STI_UINT16 = 1) as uint16
|
|
83
|
-
new Uint8Array([0x00, 0x00]), // Value: two zero bytes for UINT16
|
|
84
|
-
]),
|
|
85
|
-
)
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Construct Data from bytes
|
|
89
|
-
* @param bytes - Uint8Array containing type ID and data
|
|
90
|
-
*/
|
|
91
|
-
constructor(bytes: Uint8Array) {
|
|
92
|
-
super(bytes ?? Data.ZERO_DATA.bytes)
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Create Data from various input types
|
|
97
|
-
*
|
|
98
|
-
* @param value - Can be:
|
|
99
|
-
* - Data instance (returns as-is)
|
|
100
|
-
* - DataJSON object with 'type' and 'value' fields
|
|
101
|
-
* @returns Data instance
|
|
102
|
-
* @throws Error if value type is not supported
|
|
103
|
-
*/
|
|
104
|
-
static from(value: unknown): Data {
|
|
105
|
-
if (value instanceof Data) {
|
|
106
|
-
return value
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
if (
|
|
110
|
-
typeof value === 'object' &&
|
|
111
|
-
value !== null &&
|
|
112
|
-
'type' in value &&
|
|
113
|
-
'value' in value
|
|
114
|
-
) {
|
|
115
|
-
const json = value as DataJSON
|
|
116
|
-
return Data.fromJSON(json)
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
throw new Error('Data.from: value must be Data instance or DataJSON object')
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Create Data from JSON representation
|
|
124
|
-
*
|
|
125
|
-
* @param json - Object with 'type' and 'value' fields
|
|
126
|
-
* @returns Data instance
|
|
127
|
-
* @throws Error if type is not supported
|
|
128
|
-
*/
|
|
129
|
-
static fromJSON(json: DataJSON): Data {
|
|
130
|
-
const typeId = TYPE_STRING_TO_ID[json.type]
|
|
131
|
-
if (typeId === undefined) {
|
|
132
|
-
throw new Error(`Data: unsupported type string: ${json.type}`)
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
let dataBytes: Uint8Array
|
|
136
|
-
|
|
137
|
-
const TypeClass = SIMPLE_TYPE_MAP[typeId]
|
|
138
|
-
if (TypeClass) {
|
|
139
|
-
// For UInt8/16/32, coerce value to number; all others pass through
|
|
140
|
-
const coercedValue = NUMERIC_TYPES.has(typeId)
|
|
141
|
-
? typeof json.value === 'string'
|
|
142
|
-
? parseInt(json.value, 10)
|
|
143
|
-
: Number(json.value)
|
|
144
|
-
: json.value
|
|
145
|
-
dataBytes = TypeClass.from(coercedValue).toBytes()
|
|
146
|
-
} else if (typeId === SerializedTypeID.STI_VL) {
|
|
147
|
-
const val =
|
|
148
|
-
typeof json.value === 'string' ? json.value : json.value.toString()
|
|
149
|
-
dataBytes = Blob.from(val).toBytes()
|
|
150
|
-
dataBytes = concat([
|
|
151
|
-
BinarySerializer.encodeVariableLength(dataBytes.length),
|
|
152
|
-
dataBytes,
|
|
153
|
-
])
|
|
154
|
-
} else if (typeId === SerializedTypeID.STI_ACCOUNT) {
|
|
155
|
-
const val =
|
|
156
|
-
typeof json.value === 'string' ? json.value : json.value.toString()
|
|
157
|
-
dataBytes = concat([
|
|
158
|
-
new Uint8Array([0x14]),
|
|
159
|
-
AccountID.from(val).toBytes(),
|
|
160
|
-
])
|
|
161
|
-
} else {
|
|
162
|
-
throw new Error(`Data.fromJSON(): unsupported type ID: ${typeId}`)
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// Combine type header with data bytes
|
|
166
|
-
const typeBytes = new Uint8Array(2)
|
|
167
|
-
writeUInt16BE(typeBytes, typeId, 0)
|
|
168
|
-
return new Data(concat([typeBytes, dataBytes]))
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* Read Data from a BinaryParser stream
|
|
173
|
-
*
|
|
174
|
-
* @param parser - BinaryParser positioned at the start of Data
|
|
175
|
-
* @returns Data instance
|
|
176
|
-
*/
|
|
177
|
-
static fromParser(parser: BinaryParser): Data {
|
|
178
|
-
// Read the 2-byte type ID
|
|
179
|
-
const typeBytes = parser.read(2)
|
|
180
|
-
const typeId = TYPE_NUMBER_TO_ID[readUInt16BE(typeBytes, 0)]
|
|
181
|
-
|
|
182
|
-
let dataBytes: Uint8Array
|
|
183
|
-
|
|
184
|
-
const TypeClass = SIMPLE_TYPE_MAP[typeId]
|
|
185
|
-
if (TypeClass) {
|
|
186
|
-
dataBytes = TypeClass.fromParser(parser).toBytes()
|
|
187
|
-
} else if (typeId === SerializedTypeID.STI_VL) {
|
|
188
|
-
const valueVL = parser.readVariableLength()
|
|
189
|
-
dataBytes = concat([
|
|
190
|
-
BinarySerializer.encodeVariableLength(valueVL.length),
|
|
191
|
-
valueVL,
|
|
192
|
-
])
|
|
193
|
-
} else if (typeId === SerializedTypeID.STI_ACCOUNT) {
|
|
194
|
-
parser.skip(1)
|
|
195
|
-
dataBytes = concat([
|
|
196
|
-
new Uint8Array([0x14]),
|
|
197
|
-
AccountID.fromParser(parser).toBytes(),
|
|
198
|
-
])
|
|
199
|
-
} else {
|
|
200
|
-
throw new Error(`Data: unsupported type ID when parsing: ${typeId}`)
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
return new Data(concat([typeBytes, dataBytes]))
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* Get the inner SerializedTypeID
|
|
208
|
-
*
|
|
209
|
-
* @returns The inner type ID
|
|
210
|
-
*/
|
|
211
|
-
getInnerType(): SerializedTypeID {
|
|
212
|
-
return TYPE_NUMBER_TO_ID[readUInt16BE(this.bytes, 0)]
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* Get the string representation of the inner type
|
|
217
|
-
*
|
|
218
|
-
* @returns String name of the type
|
|
219
|
-
*/
|
|
220
|
-
getInnerTypeString(): string {
|
|
221
|
-
const innerType = this.getInnerType()
|
|
222
|
-
return TYPE_ID_TO_STRING[innerType] || innerType.toString()
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
/**
|
|
226
|
-
* Get the data value
|
|
227
|
-
*
|
|
228
|
-
* @returns The stored data value
|
|
229
|
-
*/
|
|
230
|
-
getValue(): SerializedType {
|
|
231
|
-
const innerType = this.getInnerType()
|
|
232
|
-
const parser = new BinaryParser(bytesToHex(this.bytes.slice(2)))
|
|
233
|
-
|
|
234
|
-
const TypeClass = SIMPLE_TYPE_MAP[innerType]
|
|
235
|
-
if (TypeClass) {
|
|
236
|
-
return TypeClass.fromParser(parser)
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
if (innerType === SerializedTypeID.STI_VL) {
|
|
240
|
-
const vlLength = parser.readVariableLengthLength()
|
|
241
|
-
return Blob.fromParser(parser, vlLength)
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
if (innerType === SerializedTypeID.STI_ACCOUNT) {
|
|
245
|
-
parser.skip(1)
|
|
246
|
-
return AccountID.fromParser(parser)
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
throw new Error(`Data.getValue(): unsupported type ID: ${typeof innerType}`)
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
/**
|
|
253
|
-
* Convert to JSON representation
|
|
254
|
-
*
|
|
255
|
-
* @returns JSON object with 'type' and 'value' fields
|
|
256
|
-
*/
|
|
257
|
-
toJSON(): DataJSON {
|
|
258
|
-
return {
|
|
259
|
-
type: this.getInnerTypeString(),
|
|
260
|
-
value: this.getValue().toJSON() as string | number | JsonObject,
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
/**
|
|
265
|
-
* Compare with another Data for equality
|
|
266
|
-
*
|
|
267
|
-
* @param other - Another Data to compare with
|
|
268
|
-
* @returns true if both have the same inner type and data
|
|
269
|
-
*/
|
|
270
|
-
equals(other: Data): boolean {
|
|
271
|
-
if (!(other instanceof Data)) {
|
|
272
|
-
return false
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
// Compare bytes directly
|
|
276
|
-
if (this.bytes.length !== other.bytes.length) {
|
|
277
|
-
return false
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
for (let i = 0; i < this.bytes.length; i++) {
|
|
281
|
-
if (this.bytes[i] !== other.bytes[i]) {
|
|
282
|
-
return false
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
return true
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
getSType(): SerializedTypeID {
|
|
290
|
-
return SerializedTypeID.STI_DATA
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
export { Data }
|
package/src/types/dataType.ts
DELETED
|
@@ -1,178 +0,0 @@
|
|
|
1
|
-
import { BinaryParser } from '../serdes/binary-parser'
|
|
2
|
-
import {
|
|
3
|
-
JsonObject,
|
|
4
|
-
SerializedType,
|
|
5
|
-
SerializedTypeID,
|
|
6
|
-
TYPE_ID_TO_STRING,
|
|
7
|
-
TYPE_STRING_TO_ID,
|
|
8
|
-
} from './serialized-type'
|
|
9
|
-
import { readUInt16BE, writeUInt16BE } from '../utils'
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Interface for DataType JSON representation
|
|
13
|
-
*/
|
|
14
|
-
interface DataTypeJSON extends JsonObject {
|
|
15
|
-
type: string
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* STDataType: Encodes XRPL's "DataType" type.
|
|
20
|
-
*
|
|
21
|
-
* This type wraps an inner SerializedTypeID to indicate what type of data
|
|
22
|
-
* a field contains. It's encoded as a 2-byte unsigned integer representing
|
|
23
|
-
* the inner type.
|
|
24
|
-
*
|
|
25
|
-
* Usage:
|
|
26
|
-
* DataType.from({ type: "Amount" })
|
|
27
|
-
* DataType.from("UInt64")
|
|
28
|
-
* DataType.fromParser(parser)
|
|
29
|
-
*/
|
|
30
|
-
class DataType extends SerializedType {
|
|
31
|
-
private innerType: SerializedTypeID
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Default bytes for DataType (STI_NOTPRESENT)
|
|
35
|
-
*/
|
|
36
|
-
static readonly defaultBytes = new Uint8Array([0x00, 0x01])
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Construct a DataType from bytes
|
|
40
|
-
* @param bytes - 2-byte Uint8Array containing the inner type ID
|
|
41
|
-
* @param innerType - Optional explicit inner type (used when constructing from value)
|
|
42
|
-
* @throws Error if bytes is not a 2-byte Uint8Array
|
|
43
|
-
*/
|
|
44
|
-
constructor(bytes?: Uint8Array, innerType?: SerializedTypeID) {
|
|
45
|
-
const used = bytes ?? DataType.defaultBytes
|
|
46
|
-
if (!(used instanceof Uint8Array) || used.length !== 2) {
|
|
47
|
-
throw new Error(
|
|
48
|
-
`DataType must be constructed from a 2-byte Uint8Array, got ${used?.length} bytes`,
|
|
49
|
-
)
|
|
50
|
-
}
|
|
51
|
-
super(used)
|
|
52
|
-
|
|
53
|
-
// If innerType is explicitly provided, use it; otherwise read from bytes
|
|
54
|
-
if (innerType !== undefined) {
|
|
55
|
-
this.innerType = innerType
|
|
56
|
-
} else {
|
|
57
|
-
this.innerType = readUInt16BE(used, 0) as unknown as SerializedTypeID
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Construct from various input types
|
|
63
|
-
*
|
|
64
|
-
* @param value - Can be:
|
|
65
|
-
* - DataType instance (returns as-is)
|
|
66
|
-
* - DataTypeJSON object with 'type' field
|
|
67
|
-
* - String type name (e.g., "Amount", "UInt64")
|
|
68
|
-
* - SerializedTypeID enum value
|
|
69
|
-
* @returns DataType instance
|
|
70
|
-
* @throws Error if value type is not supported or type string is unknown
|
|
71
|
-
*/
|
|
72
|
-
static from(value: unknown): DataType {
|
|
73
|
-
if (value instanceof DataType) {
|
|
74
|
-
return value
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (typeof value === 'object' && value !== null && 'type' in value) {
|
|
78
|
-
const json = value as DataTypeJSON
|
|
79
|
-
return DataType.fromTypeString(json.type)
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (typeof value === 'string') {
|
|
83
|
-
return DataType.fromTypeString(value)
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
if (typeof value === 'number') {
|
|
87
|
-
return DataType.fromTypeId(value as SerializedTypeID)
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
throw new Error(
|
|
91
|
-
'DataType.from: value must be DataType, DataTypeJSON, string, or SerializedTypeID',
|
|
92
|
-
)
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Construct from a type string
|
|
97
|
-
*
|
|
98
|
-
* @param typeStr - Type string like "Amount", "UInt64", etc.
|
|
99
|
-
* @returns DataType instance
|
|
100
|
-
* @throws Error if type string is not recognized
|
|
101
|
-
*/
|
|
102
|
-
static fromTypeString(typeStr: string): DataType {
|
|
103
|
-
const typeId = TYPE_STRING_TO_ID[typeStr]
|
|
104
|
-
if (typeId === undefined) {
|
|
105
|
-
throw new Error(`DataType: unsupported type string: ${typeStr}`)
|
|
106
|
-
}
|
|
107
|
-
return DataType.fromTypeId(typeId)
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Construct from a SerializedTypeID
|
|
112
|
-
*
|
|
113
|
-
* @param typeId - The SerializedTypeID enum value
|
|
114
|
-
* @returns DataType instance
|
|
115
|
-
*/
|
|
116
|
-
static fromTypeId(typeId: SerializedTypeID): DataType {
|
|
117
|
-
const bytes = new Uint8Array(2)
|
|
118
|
-
writeUInt16BE(bytes, typeId, 0)
|
|
119
|
-
return new DataType(bytes, typeId)
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Read a DataType from a BinaryParser stream (2 bytes)
|
|
124
|
-
*
|
|
125
|
-
* @param parser - BinaryParser positioned at the start of a DataType
|
|
126
|
-
* @returns DataType instance
|
|
127
|
-
*/
|
|
128
|
-
static fromParser(parser: BinaryParser): DataType {
|
|
129
|
-
const bytes = parser.read(2)
|
|
130
|
-
return new DataType(bytes)
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Get the inner SerializedTypeID
|
|
135
|
-
*
|
|
136
|
-
* @returns The inner type ID
|
|
137
|
-
*/
|
|
138
|
-
getInnerType(): SerializedTypeID {
|
|
139
|
-
return this.innerType
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* Set the inner SerializedTypeID
|
|
144
|
-
*
|
|
145
|
-
* @param typeId - The new inner type ID
|
|
146
|
-
*/
|
|
147
|
-
setInnerType(typeId: SerializedTypeID): void {
|
|
148
|
-
this.innerType = typeId
|
|
149
|
-
writeUInt16BE(this.bytes, typeId, 0)
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Get the string representation of the inner type
|
|
154
|
-
*
|
|
155
|
-
* @returns String name of the type, or numeric string if unknown
|
|
156
|
-
*/
|
|
157
|
-
getInnerTypeString(): string {
|
|
158
|
-
return TYPE_ID_TO_STRING[this.innerType] || this.innerType.toString()
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* Convert to JSON representation
|
|
163
|
-
*
|
|
164
|
-
* @returns JSON object with 'type' field
|
|
165
|
-
*/
|
|
166
|
-
toJSON(): DataTypeJSON {
|
|
167
|
-
return {
|
|
168
|
-
type: this.getInnerTypeString(),
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
getSType(): SerializedTypeID {
|
|
173
|
-
return SerializedTypeID.STI_DATATYPE
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// Export the DataType class for external use
|
|
178
|
-
export { DataType }
|