Skip to content

Commit

Permalink
Merge pull request #142 from morpho-org/refactor/keccak256-noble
Browse files Browse the repository at this point in the history
build(blue-sdk): replace keccak256 with noble hashes
  • Loading branch information
Rubilmax authored Oct 22, 2024
2 parents 71f1cfd + 04f5acf commit 30fa804
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 25 deletions.
2 changes: 1 addition & 1 deletion packages/blue-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"test": "vitest"
},
"dependencies": {
"keccak256": "^1.0.6"
"@noble/hashes": "^1.5.0"
},
"peerDependencies": {
"@morpho-org/morpho-ts": "workspace:^"
Expand Down
30 changes: 18 additions & 12 deletions packages/blue-sdk/src/market/MarketUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import keccak256 from "keccak256";

import { keccak_256 } from "@noble/hashes/sha3";
import { bytesToHex, hexToBytes } from "@noble/hashes/utils";
import {
LIQUIDATION_CURSOR,
MAX_LIQUIDATION_INCENTIVE_FACTOR,
Expand All @@ -8,7 +8,6 @@ import {
} from "../constants.js";
import { MathLib, type RoundingDirection, SharesMath } from "../math/index.js";
import type { BigIntish, MarketId } from "../types.js";

import type { InputMarketParams } from "./MarketParams.js";

/**
Expand All @@ -20,15 +19,22 @@ export namespace MarketUtils {
* @param market The market params.
*/
export function getMarketId(market: InputMarketParams) {
return `0x${keccak256(
`0x${
market.loanToken.substring(2).toLowerCase().padStart(64, "0") +
market.collateralToken.substring(2).toLowerCase().padStart(64, "0") +
market.oracle.substring(2).padStart(64, "0") +
market.irm.substring(2).toLowerCase().padStart(64, "0") +
BigInt(market.lltv).toString(16).padStart(64, "0")
}`,
).toString("hex")}` as MarketId;
return `0x${bytesToHex(
keccak_256(
hexToBytes(
`${
market.loanToken.substring(2).toLowerCase().padStart(64, "0") +
market.collateralToken
.substring(2)
.toLowerCase()
.padStart(64, "0") +
market.oracle.substring(2).padStart(64, "0") +
market.irm.substring(2).toLowerCase().padStart(64, "0") +
BigInt(market.lltv).toString(16).padStart(64, "0")
}`,
),
),
)}` as MarketId;
}

/**
Expand Down
15 changes: 3 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 30fa804

Please sign in to comment.