mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-12-06 17:27:59 +00:00
- Removes need for bundlers to polyfill the `Buffer` class. `UInt8Array` are used instead which are native to the browser and node. - Reduces bundle size 7.1kb gzipped and eliminates 4 runtime dependencies: `base-x`, `base64-js`, `buffer`, and `ieee754`. BREAKING CHANGE: All methods that previously took a `Buffer` now accept a `UInt8Array`. --------- Co-authored-by: Jackson Mills <jmills@ripple.com>
154 lines
4.2 KiB
TypeScript
154 lines
4.2 KiB
TypeScript
import { quality, binary, HashPrefix } from './coretypes'
|
|
import { decodeLedgerData } from './ledger-hashes'
|
|
import { ClaimObject } 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,
|
|
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 transaction and prepare for signing with a claim
|
|
*
|
|
* @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 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 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,
|
|
encodeQuality,
|
|
decodeQuality,
|
|
decodeLedgerData,
|
|
TRANSACTION_TYPES,
|
|
XrplDefinitions,
|
|
XrplDefinitionsBase,
|
|
DEFAULT_DEFINITIONS,
|
|
coreTypes,
|
|
}
|