-
Notifications
You must be signed in to change notification settings - Fork 540
/
Copy pathindex.ts
166 lines (156 loc) · 4.38 KB
/
index.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
import { quality, binary, HashPrefix } from './coretypes'
import { decodeLedgerData } from './ledger-hashes'
import { ClaimObject, BatchObject } from './binary'
import { JsonObject } from './types/serialized-type'
import {
XrplDefinitionsBase,
TRANSACTION_TYPES,
DEFAULT_DEFINITIONS,
} from './enums'
import { XrplDefinitions } from './enums/xrpl-definitions'
import { coreTypes } from './types'
import { bytesToHex } from '@xrplf/isomorphic/utils'
const {
signingData,
signingClaimData,
multiSigningData,
signingBatchData,
binaryToJSON,
serializeObject,
} = binary
/**
* Decode a transaction
*
* @param binary hex-string of the encoded transaction
* @param definitions Custom rippled types to use instead of the default. Used for sidechains and amendments.
* @returns the JSON representation of the transaction
*/
function decode(binary: string, definitions?: XrplDefinitionsBase): JsonObject {
if (typeof binary !== 'string') {
throw new Error('binary must be a hex string')
}
return binaryToJSON(binary, definitions)
}
/**
* Encode a transaction
*
* @param json The JSON representation of a transaction
* @param definitions Custom rippled types to use instead of the default. Used for sidechains and amendments.
*
* @returns A hex-string of the encoded transaction
*/
function encode(json: object, definitions?: XrplDefinitionsBase): string {
if (typeof json !== 'object') {
throw new Error()
}
return bytesToHex(serializeObject(json as JsonObject, { definitions }))
}
/**
* Encode a transaction and prepare for signing
*
* @param json JSON object representing the transaction
* @param signer string representing the account to sign the transaction with
* @param definitions Custom rippled types to use instead of the default. Used for sidechains and amendments.
* @returns a hex string of the encoded transaction
*/
function encodeForSigning(
json: object,
definitions?: XrplDefinitionsBase,
): string {
if (typeof json !== 'object') {
throw new Error()
}
return bytesToHex(
signingData(json as JsonObject, HashPrefix.transactionSig, {
definitions,
}),
)
}
/**
* Encode a payment channel claim for signing.
*
* @param json JSON object representing the claim.
* @returns a hex string of the encoded claim.
*/
function encodeForSigningClaim(json: object): string {
if (typeof json !== 'object') {
throw new Error()
}
return bytesToHex(signingClaimData(json as ClaimObject))
}
/**
* Encode a transaction and prepare for multi-signing.
*
* @param json JSON object representing the transaction.
* @param signer string representing the account to sign the transaction with.
* @param definitions Custom rippled types to use instead of the default. Used for sidechains and amendments.
* @returns a hex string of the encoded transaction.
*/
function encodeForMultisigning(
json: object,
signer: string,
definitions?: XrplDefinitionsBase,
): string {
if (typeof json !== 'object') {
throw new Error()
}
if (json['SigningPubKey'] !== '') {
throw new Error()
}
const definitionsOpt = definitions ? { definitions } : undefined
return bytesToHex(
multiSigningData(json as JsonObject, signer, definitionsOpt),
)
}
/**
* Encode a Batch transaction for signing.
*
* @param json JSON object representing the transaction.
* @returns a hex string of the encoded transaction.
*/
function encodeForSigningBatch(json: object): string {
if (typeof json !== 'object') {
throw new Error('Need an object to encode a Batch transaction')
}
return bytesToHex(signingBatchData(json as BatchObject))
}
/**
* Encode a quality value
*
* @param value string representation of a number
* @returns a hex-string representing the quality
*/
function encodeQuality(value: string): string {
if (typeof value !== 'string') {
throw new Error()
}
return bytesToHex(quality.encode(value))
}
/**
* Decode a quality value
*
* @param value hex-string of a quality
* @returns a string representing the quality
*/
function decodeQuality(value: string): string {
if (typeof value !== 'string') {
throw new Error()
}
return quality.decode(value).toString()
}
export {
decode,
encode,
encodeForSigning,
encodeForSigningClaim,
encodeForMultisigning,
encodeForSigningBatch,
encodeQuality,
decodeQuality,
decodeLedgerData,
TRANSACTION_TYPES,
XrplDefinitions,
XrplDefinitionsBase,
DEFAULT_DEFINITIONS,
coreTypes,
}