Skip to content

Commit 8a0fc62

Browse files
Merge pull request #85 from IntersectMBO/feat/hash-plutus-data-options
feat/hash plutus data options
2 parents 8195065 + 5ee95bc commit 8a0fc62

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

.changeset/seven-parts-warn.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
"@evolution-sdk/evolution": patch
3+
---
4+
5+
## hashPlutusData encoding options
6+
7+
Add optional CBOR encoding options parameter to `hashPlutusData` function. This allows controlling how Plutus data is encoded before hashing, which affects the resulting datum hash.
8+
9+
**Before:**
10+
11+
```typescript
12+
import { hashPlutusData } from "@evolution-sdk/evolution/utils/Hash"
13+
14+
// Always uses indefinite-length encoding (CML_DATA_DEFAULT_OPTIONS)
15+
const hash = hashPlutusData(data)
16+
```
17+
18+
**After:**
19+
20+
```typescript
21+
import { Core } from "@evolution-sdk/evolution"
22+
import { hashPlutusData } from "@evolution-sdk/evolution/utils/Hash"
23+
24+
const cborHex = "d87983486c6f76656c6163655820c3e43c6b8fb46068d4ef9746a934eba534873db0aacebdaf369c78ab23cb57751a004c4b40"
25+
const decoded = Core.Data.fromCBORHex(cborHex)
26+
27+
// Indefinite-length (SDK default for Data)
28+
const indefiniteHash = hashPlutusData(decoded, Core.CBOR.CML_DATA_DEFAULT_OPTIONS)
29+
console.log("Hash:", Core.Bytes.toHex(indefiniteHash.hash))
30+
// b67b6e7d2497d4e87a240a080a109a905f73527a244775cc1e2a43f48202700f
31+
32+
// Definite-length encoding
33+
const definiteHash = hashPlutusData(decoded, Core.CBOR.CML_DEFAULT_OPTIONS)
34+
console.log("Hash:", Core.Bytes.toHex(definiteHash.hash))
35+
// bc7eea92ba15710926e99904e746e5da739d77085b6192ddd87a0e7b4298e0c0
36+
37+
// Aiken-compatible encoding
38+
const aikenHash = hashPlutusData(decoded, Core.CBOR.AIKEN_DEFAULT_OPTIONS)
39+
console.log("Hash:", Core.Bytes.toHex(aikenHash.hash))
40+
// b67b6e7d2497d4e87a240a080a109a905f73527a244775cc1e2a43f48202700f
41+
```

packages/evolution/src/utils/Hash.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,14 @@ export const hashAuxiliaryData = (aux: AuxiliaryData.AuxiliaryData): AuxiliaryDa
160160
}
161161

162162
/**
163-
* Compute hash of plutus data using default Data encoding.
163+
* Compute hash of plutus data using specified CBOR encoding options.
164+
* Defaults to CML_DATA_DEFAULT_OPTIONS (indefinite-length arrays/maps).
164165
*/
165-
export const hashPlutusData = (pd: Data.Data): DatumOption.DatumHash => {
166-
const bytes = Data.toCBORBytes(pd)
166+
export const hashPlutusData = (
167+
pd: Data.Data,
168+
options: CBOR.CodecOptions = CBOR.CML_DATA_DEFAULT_OPTIONS
169+
): DatumOption.DatumHash => {
170+
const bytes = Data.toCBORBytes(pd, options)
167171
const digest = blake2b(bytes, { dkLen: 32 })
168172
return new DatumOption.DatumHash({ hash: digest })
169173
}

0 commit comments

Comments
 (0)