Files
xahau.js/src/common/hashes/README.md
Elliot Lee 3c13da66b3 Export hashing functions (#1275)
The now-deprecated ripple-hashes library provided a number of hash
functions for users to compute the hashes/IDs for various XRP Ledger
objects:

* Transactions (to generate transaction hashes, also known as IDs)
* Transaction signing hashes (to sign transactions)
* Ledger Object IDs (to look up specific ledger objects in a ledger's
  state tree; see https://xrpl.org/ledger-object-ids.html)

This commit exports these utility methods from ripple-lib as static
methods. Access them on the RippleAPI class. Example:

import {RippleAPI} from 'ripple-lib'
const hash = RippleAPI.computeBinaryTransactionHash(...)
2020-04-28 14:19:36 -07:00

3.3 KiB

XRP Ledger Hashes

Methods to hash XRP Ledger objects

Computing a transaction hash (ID)

computeBinaryTransactionHash = (txBlobHex: string): string

Compute the hash of a binary transaction blob.

computeTransactionHash = (txJSON: any): string

Compute the hash of a transaction in txJSON format.

Hash Prefixes

In many cases, the XRP Ledger prefixes an object's binary data with a 4-byte code before calculating its hash, so that objects of different types have different hashes even if the binary data is the same. The existing 4-byte codes are structured as 3 alphabetic characters, encoded as ASCII, followed by a zero byte.

Some types of hashes appear in API requests and responses. Others are only calculated as the first step of signing a certain type of data, or calculating a higher-level hash. Some of following methods internally use some of the 4-byte hash prefixes in order to calculate the appropriate hash.

computeBinaryTransactionSigningHash = (txBlobHex: string): string

In order to single-sign a transaction, you must perform these steps:

  1. Assuming the transaction is in JSON format (txJSON), encode the transaction in the XRP Ledger's binary format.
  2. Hash the data with the appropriate prefix (0x53545800 if single-signing, or 0x534D5400 if multi-signing).
  3. After signing, you must re-serialize the transaction with the TxnSignature field included.

The computeBinaryTransactionSigningHash helps with step 2, automatically using the 0x53545800 prefix needed for single-signing a transaction.

For details, see Serialization Format.

Removed: computeTransactionSigningHash, which took txJSON as a parameter. It was part of the deprecated ripple-hashes library. If you have txJSON, encode it with ripple-binary-codec first. Example: return computeBinaryTransactionSigningHash(encode(txJSON))

computeAccountLedgerObjectID = (address: string): string

Compute the hash of an account, given the account's classic address (starting with r).

computeSignerListLedgerObjectID = (address: string): string

Compute the hash of an account's SignerList.

computeOrderID = (address: string, sequence: number): string

Compute the hash of an order, given the owner's classic address (starting with r) and the account sequence number of the OfferCreate order transaction.

computeTrustlineHash = (address1: string, address2: string, currency: string): string

Compute the hash of a trustline, given the two parties' classic addresses (starting with r) and the currency code.

computeTransactionTreeHash = (transactions: any[]): string

computeStateTreeHash = (entries: any[]): string

computeLedgerHash = (ledgerHeader): string

Compute the hash of a ledger.

computeEscrowHash = (address, sequence): string

Compute the hash of an escrow, given the owner's classic address (starting with r) and the account sequence number of the EscrowCreate escrow transaction.

computePaymentChannelHash = (address, dstAddress, sequence): string

Compute the hash of a payment channel, given the owner's classic address (starting with r), the classic address of the destination, and the account sequence number of the PaymentChannelCreate payment channel transaction.