From f9d33d6dbb80251ca4181600a94bea55cad777fb Mon Sep 17 00:00:00 2001 From: Anurag Date: Sat, 20 Sep 2025 01:00:18 +0530 Subject: [PATCH] Add hashOracle utility hash function --- packages/xrpl/src/utils/hashes/README.md | 4 ++++ packages/xrpl/src/utils/hashes/index.ts | 16 ++++++++++++++++ packages/xrpl/src/utils/hashes/ledgerSpaces.ts | 1 + packages/xrpl/src/utils/index.ts | 2 ++ packages/xrpl/test/utils/hashes.test.ts | 11 +++++++++++ 5 files changed, 34 insertions(+) diff --git a/packages/xrpl/src/utils/hashes/README.md b/packages/xrpl/src/utils/hashes/README.md index 89ff79b13e..faa347fdca 100644 --- a/packages/xrpl/src/utils/hashes/README.md +++ b/packages/xrpl/src/utils/hashes/README.md @@ -60,6 +60,10 @@ Compute the hash of a ledger. 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. +### hashOracle = (address, documentId): string + +Compute the hash of an oracle, given the owner's classic address (starting with `r`) and the OracleDocumentID of the `OracleSet` transaction. + ### hashPaymentChannel = (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. diff --git a/packages/xrpl/src/utils/hashes/index.ts b/packages/xrpl/src/utils/hashes/index.ts index 9d32f6bfa1..c91a66d706 100644 --- a/packages/xrpl/src/utils/hashes/index.ts +++ b/packages/xrpl/src/utils/hashes/index.ts @@ -163,6 +163,22 @@ export function hashEscrow(address: string, sequence: number): string { ) } +/** + * Compute the Hash of an Oracle LedgerEntry. + * + * @param address - Address of the owner of the Oracle. + * @param documentId - OracleDocumentID of the Oracle. + * @returns The hash of the Oracle LedgerEntry. + * @category Utilities + */ +export function hashOracle(address: string, documentId: number): string { + return sha512Half( + ledgerSpaceHex('oracle') + + addressToHex(address) + + documentId.toString(HEX).padStart(BYTE_LENGTH * 2, '0'), + ) +} + /** * Compute the hash of a Payment Channel. * diff --git a/packages/xrpl/src/utils/hashes/ledgerSpaces.ts b/packages/xrpl/src/utils/hashes/ledgerSpaces.ts index e2af0c6aae..1f47775b6c 100644 --- a/packages/xrpl/src/utils/hashes/ledgerSpaces.ts +++ b/packages/xrpl/src/utils/hashes/ledgerSpaces.ts @@ -17,6 +17,7 @@ const ledgerSpaces = { offer: 'o', // Directory of things owned by an account. ownerDir: 'O', + oracle: 'R', // Directory of order books. bookDir: 'B', contract: 'c', diff --git a/packages/xrpl/src/utils/index.ts b/packages/xrpl/src/utils/index.ts index 2304384e90..6877611166 100644 --- a/packages/xrpl/src/utils/index.ts +++ b/packages/xrpl/src/utils/index.ts @@ -45,6 +45,7 @@ import { hashLedger, hashLedgerHeader, hashEscrow, + hashOracle, hashPaymentChannel, } from './hashes' import parseNFTokenID from './parseNFTokenID' @@ -178,6 +179,7 @@ const hashes = { hashLedger, hashLedgerHeader, hashEscrow, + hashOracle, hashPaymentChannel, } diff --git a/packages/xrpl/test/utils/hashes.test.ts b/packages/xrpl/test/utils/hashes.test.ts index 6b75989de4..b4cdf14137 100644 --- a/packages/xrpl/test/utils/hashes.test.ts +++ b/packages/xrpl/test/utils/hashes.test.ts @@ -16,6 +16,7 @@ import { hashTxTree, hashTrustline, hashEscrow, + hashOracle, hashPaymentChannel, hashSignedTx, hashAccountRoot, @@ -138,6 +139,16 @@ describe('Hashes', function () { assert.equal(actualEntryHash, expectedEntryHash) }) + it('calcOracleEntryHash', function () { + const account = 'rsNvoAZ9MquZSRhu4cEY9wTv1VqHXpVPPt' + const documentId = 1 + const expectedEntryHash = + 'D463D13ACF77206306BE891DF410655213FBD2C1F2B58A492E7F556A41A44338' + const actualEntryHash = hashOracle(account, documentId) + + assert.equal(actualEntryHash, expectedEntryHash) + }) + it('calcPaymentChannelEntryHash', function () { const account = 'rDx69ebzbowuqztksVDmZXjizTd12BVr4x' const dstAccount = 'rLFtVprxUEfsH54eCWKsZrEQzMDsx1wqso'