-
Notifications
You must be signed in to change notification settings - Fork 541
/
Copy pathbinary.ts
235 lines (213 loc) · 6.56 KB
/
binary.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
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
/* eslint-disable func-style */
import { bytesToHex } from '@xrplf/isomorphic/utils'
import { coreTypes } from './types'
import { BinaryParser } from './serdes/binary-parser'
import { AccountID } from './types/account-id'
import { HashPrefix } from './hash-prefixes'
import { BinarySerializer, BytesList } from './serdes/binary-serializer'
import { sha512Half, transactionID } from './hashes'
import {
type XrplDefinitionsBase,
DEFAULT_DEFINITIONS,
type FieldInstance,
} from './enums'
import { STObject } from './types/st-object'
import { JsonObject } from './types/serialized-type'
/**
* Construct a BinaryParser
*
* @param bytes hex-string or Uint8Array to construct BinaryParser from
* @param definitions rippled definitions used to parse the values of transaction types and such.
* Can be customized for sidechains and amendments.
* @returns BinaryParser
*/
const makeParser = (
bytes: string | Uint8Array,
definitions?: XrplDefinitionsBase,
): BinaryParser =>
new BinaryParser(
bytes instanceof Uint8Array ? bytesToHex(bytes) : bytes,
definitions,
)
/**
* Parse BinaryParser into JSON
*
* @param parser BinaryParser object
* @param definitions rippled definitions used to parse the values of transaction types and such.
* Can be customized for sidechains and amendments.
* @returns JSON for the bytes in the BinaryParser
*/
const readJSON = (
parser: BinaryParser,
definitions: XrplDefinitionsBase = DEFAULT_DEFINITIONS,
): JsonObject =>
(parser.readType(coreTypes.STObject) as STObject).toJSON(definitions)
/**
* Parse a hex-string into its JSON interpretation
*
* @param bytes hex-string to parse into JSON
* @param definitions rippled definitions used to parse the values of transaction types and such.
* Can be customized for sidechains and amendments.
* @returns JSON
*/
const binaryToJSON = (
bytes: string,
definitions?: XrplDefinitionsBase,
): JsonObject => readJSON(makeParser(bytes, definitions), definitions)
/**
* Interface for passing parameters to SerializeObject
*
* @field set signingFieldOnly to true if you want to serialize only signing fields
*/
interface OptionObject {
prefix?: Uint8Array
suffix?: Uint8Array
signingFieldsOnly?: boolean
definitions?: XrplDefinitionsBase
}
/**
* Function to serialize JSON object representing a transaction
*
* @param object JSON object to serialize
* @param opts options for serializing, including optional prefix, suffix, signingFieldOnly, and definitions
* @returns A Uint8Array containing the serialized object
*/
function serializeObject(
object: JsonObject,
opts: OptionObject = {},
): Uint8Array {
const { prefix, suffix, signingFieldsOnly = false, definitions } = opts
const bytesList = new BytesList()
if (prefix) {
bytesList.put(prefix)
}
const filter = signingFieldsOnly
? (f: FieldInstance): boolean => f.isSigningField
: undefined
;(coreTypes.STObject as typeof STObject)
.from(object, filter, definitions)
.toBytesSink(bytesList)
if (suffix) {
bytesList.put(suffix)
}
return bytesList.toBytes()
}
/**
* Serialize an object for signing
*
* @param transaction Transaction to serialize
* @param prefix Prefix bytes to put before the serialized object
* @param opts.definitions Custom rippled types to use instead of the default. Used for sidechains and amendments.
* @returns A Uint8Array with the serialized object
*/
function signingData(
transaction: JsonObject,
prefix: Uint8Array = HashPrefix.transactionSig,
opts: { definitions?: XrplDefinitionsBase } = {},
): Uint8Array {
return serializeObject(transaction, {
prefix,
signingFieldsOnly: true,
definitions: opts.definitions,
})
}
/**
* Interface describing fields required for a Claim
*/
interface ClaimObject extends JsonObject {
channel: string
amount: string | number
}
/**
* Serialize a signingClaim
*
* @param claim A claim object to serialize
* @param opts.definitions Custom rippled types to use instead of the default. Used for sidechains and amendments.
* @returns the serialized object with appropriate prefix
*/
function signingClaimData(claim: ClaimObject): Uint8Array {
const num = BigInt(String(claim.amount))
const prefix = HashPrefix.paymentChannelClaim
const channel = coreTypes.Hash256.from(claim.channel).toBytes()
const amount = coreTypes.UInt64.from(num).toBytes()
const bytesList = new BytesList()
bytesList.put(prefix)
bytesList.put(channel)
bytesList.put(amount)
return bytesList.toBytes()
}
/**
* Serialize a transaction object for multiSigning
*
* @param transaction transaction to serialize
* @param signingAccount Account to sign the transaction with
* @param opts.definitions Custom rippled types to use instead of the default. Used for sidechains and amendments.
* @returns serialized transaction with appropriate prefix and suffix
*/
function multiSigningData(
transaction: JsonObject,
signingAccount: string | AccountID,
opts: { definitions: XrplDefinitionsBase } = {
definitions: DEFAULT_DEFINITIONS,
},
): Uint8Array {
const prefix = HashPrefix.transactionMultiSig
const suffix = coreTypes.AccountID.from(signingAccount).toBytes()
return serializeObject(transaction, {
prefix,
suffix,
signingFieldsOnly: true,
definitions: opts.definitions,
})
}
/**
* Interface describing fields required for a Batch signer
* @property flags - Flags indicating Batch transaction properties
* @property txIDs - Array of transaction IDs included in the Batch
*/
interface BatchObject extends JsonObject {
flags: number
txIDs: string[]
}
/**
* Serialize a signingClaim
*
* @param batch A Batch object to serialize.
* @returns the serialized object with appropriate prefix
*/
function signingBatchData(batch: BatchObject): Uint8Array {
if (batch.flags == null) {
throw Error("No field `flags'")
}
if (batch.txIDs == null) {
throw Error('No field `txIDs`')
}
const prefix = HashPrefix.batch
const flags = coreTypes.UInt32.from(batch.flags).toBytes()
const txIDsLength = coreTypes.UInt32.from(batch.txIDs.length).toBytes()
const bytesList = new BytesList()
bytesList.put(prefix)
bytesList.put(flags)
bytesList.put(txIDsLength)
batch.txIDs.forEach((txID: string) => {
bytesList.put(coreTypes.Hash256.from(txID).toBytes())
})
return bytesList.toBytes()
}
export {
BinaryParser,
BinarySerializer,
BytesList,
ClaimObject,
BatchObject,
makeParser,
serializeObject,
readJSON,
multiSigningData,
signingData,
signingClaimData,
binaryToJSON,
sha512Half,
transactionID,
signingBatchData,
}