Skip to content

Commit f9d33d6

Browse files
committed
Add hashOracle utility hash function
1 parent 0649abe commit f9d33d6

File tree

5 files changed

+34
-0
lines changed

5 files changed

+34
-0
lines changed

packages/xrpl/src/utils/hashes/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ Compute the hash of a ledger.
6060

6161
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.
6262

63+
### hashOracle = (address, documentId): string
64+
65+
Compute the hash of an oracle, given the owner's classic address (starting with `r`) and the OracleDocumentID of the `OracleSet` transaction.
66+
6367
### hashPaymentChannel = (address, dstAddress, sequence): string
6468

6569
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.

packages/xrpl/src/utils/hashes/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,22 @@ export function hashEscrow(address: string, sequence: number): string {
163163
)
164164
}
165165

166+
/**
167+
* Compute the Hash of an Oracle LedgerEntry.
168+
*
169+
* @param address - Address of the owner of the Oracle.
170+
* @param documentId - OracleDocumentID of the Oracle.
171+
* @returns The hash of the Oracle LedgerEntry.
172+
* @category Utilities
173+
*/
174+
export function hashOracle(address: string, documentId: number): string {
175+
return sha512Half(
176+
ledgerSpaceHex('oracle') +
177+
addressToHex(address) +
178+
documentId.toString(HEX).padStart(BYTE_LENGTH * 2, '0'),
179+
)
180+
}
181+
166182
/**
167183
* Compute the hash of a Payment Channel.
168184
*

packages/xrpl/src/utils/hashes/ledgerSpaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const ledgerSpaces = {
1717
offer: 'o',
1818
// Directory of things owned by an account.
1919
ownerDir: 'O',
20+
oracle: 'R',
2021
// Directory of order books.
2122
bookDir: 'B',
2223
contract: 'c',

packages/xrpl/src/utils/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import {
4545
hashLedger,
4646
hashLedgerHeader,
4747
hashEscrow,
48+
hashOracle,
4849
hashPaymentChannel,
4950
} from './hashes'
5051
import parseNFTokenID from './parseNFTokenID'
@@ -178,6 +179,7 @@ const hashes = {
178179
hashLedger,
179180
hashLedgerHeader,
180181
hashEscrow,
182+
hashOracle,
181183
hashPaymentChannel,
182184
}
183185

packages/xrpl/test/utils/hashes.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
hashTxTree,
1717
hashTrustline,
1818
hashEscrow,
19+
hashOracle,
1920
hashPaymentChannel,
2021
hashSignedTx,
2122
hashAccountRoot,
@@ -138,6 +139,16 @@ describe('Hashes', function () {
138139
assert.equal(actualEntryHash, expectedEntryHash)
139140
})
140141

142+
it('calcOracleEntryHash', function () {
143+
const account = 'rsNvoAZ9MquZSRhu4cEY9wTv1VqHXpVPPt'
144+
const documentId = 1
145+
const expectedEntryHash =
146+
'D463D13ACF77206306BE891DF410655213FBD2C1F2B58A492E7F556A41A44338'
147+
const actualEntryHash = hashOracle(account, documentId)
148+
149+
assert.equal(actualEntryHash, expectedEntryHash)
150+
})
151+
141152
it('calcPaymentChannelEntryHash', function () {
142153
const account = 'rDx69ebzbowuqztksVDmZXjizTd12BVr4x'
143154
const dstAccount = 'rLFtVprxUEfsH54eCWKsZrEQzMDsx1wqso'

0 commit comments

Comments
 (0)