From dc09dae85178f1ec15f551011d6dae822b238f1a Mon Sep 17 00:00:00 2001 From: Denis Angell Date: Fri, 23 Jun 2023 15:33:20 +0000 Subject: [PATCH] add `hashURIToken` --- 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 +++++++++++ 4 files changed, 30 insertions(+) diff --git a/packages/xrpl/src/utils/hashes/index.ts b/packages/xrpl/src/utils/hashes/index.ts index 2ed7c923..8b817280 100644 --- a/packages/xrpl/src/utils/hashes/index.ts +++ b/packages/xrpl/src/utils/hashes/index.ts @@ -6,6 +6,8 @@ import { decodeAccountID } from '@transia/ripple-address-codec' import BigNumber from 'bignumber.js' +import { convertStringToHex } from '../stringConversion' + import hashLedger, { hashLedgerHeader, hashSignedTx, @@ -184,4 +186,18 @@ export function hashPaymentChannel( ) } +/** + * Compute the Hash of an URIToken LedgerEntry. + * + * @param issuer - Address of the issuer of the URIToken. + * @param uri - string uri of the URIToken (not the hex). + * @returns The hash of the URIToken LedgerEntry. + * @category Utilities + */ +export function hashURIToken(issuer: string, uri: string): string { + return sha512Half( + ledgerSpaceHex('uriToken') + addressToHex(issuer) + convertStringToHex(uri), + ) +} + export { hashLedgerHeader, hashSignedTx, hashLedger, hashStateTree, hashTxTree } diff --git a/packages/xrpl/src/utils/hashes/ledgerSpaces.ts b/packages/xrpl/src/utils/hashes/ledgerSpaces.ts index e2af0c6a..89dc08a2 100644 --- a/packages/xrpl/src/utils/hashes/ledgerSpaces.ts +++ b/packages/xrpl/src/utils/hashes/ledgerSpaces.ts @@ -28,6 +28,7 @@ const ledgerSpaces = { signerList: 'S', paychan: 'x', check: 'C', + uriToken: 'U', depositPreauth: 'p', } diff --git a/packages/xrpl/src/utils/index.ts b/packages/xrpl/src/utils/index.ts index 078d9d1e..6c96ad53 100644 --- a/packages/xrpl/src/utils/index.ts +++ b/packages/xrpl/src/utils/index.ts @@ -39,6 +39,7 @@ import { hashLedgerHeader, hashEscrow, hashPaymentChannel, + hashURIToken, } from './hashes' import { calculateHookOn, hexHookParameters, TTS } from './hooks' import parseNFTokenID from './parseNFTokenID' @@ -173,6 +174,7 @@ const hashes = { hashLedgerHeader, hashEscrow, hashPaymentChannel, + hashURIToken, } export { diff --git a/packages/xrpl/test/utils/hashes.test.ts b/packages/xrpl/test/utils/hashes.test.ts index 4c96dd6f..7340ffff 100644 --- a/packages/xrpl/test/utils/hashes.test.ts +++ b/packages/xrpl/test/utils/hashes.test.ts @@ -15,6 +15,7 @@ import { hashAccountRoot, hashOfferId, hashSignerListId, + hashURIToken, } from '../../src/utils/hashes' import fixtures from '../fixtures/rippled' import { assertResultMatch } from '../testUtils' @@ -143,6 +144,16 @@ describe('Hashes', function () { assert.equal(actualEntryHash, expectedEntryHash) }) + it('calcURITokenEntryHash', function () { + const issuer = 'rDx69ebzbowuqztksVDmZXjizTd12BVr4x' + const uri = 'ipfs://cid' + const expectedEntryHash = + 'AFC4233E5C4094952DEF5483DC41488C8744D1268F897C0CB25DE66399591399' + const actualEntryHash = hashURIToken(issuer, uri) + + assert.equal(actualEntryHash, expectedEntryHash) + }) + it('Hash a signed transaction correctly', function () { const expected_hash = '458101D51051230B1D56E9ACAFAA34451BF65FA000F95DF6F0FF5B3A62D83FC2'