From dd72e6698f493d5e67528c251156a7e429034921 Mon Sep 17 00:00:00 2001 From: Rigidity Date: Wed, 28 Aug 2024 15:27:45 -0400 Subject: [PATCH] Reproduce actual behavior --- package.json | 1 + pnpm-lock.yaml | 11 ++++++++ src/components/Main.jsx | 58 +++++++++++++++++++++++++++++++++++++++-- 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 0d2638c..1aab113 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "astro": "^4.14.6", "bech32": "^2.0.0", "chia-bls": "^1.0.2", + "clvm-lib": "^1.0.1", "react": "^18.3.1", "react-dom": "^18.3.1" } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 884029e..d74a8e2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -26,6 +26,9 @@ importers: chia-bls: specifier: ^1.0.2 version: 1.0.2 + clvm-lib: + specifier: ^1.0.1 + version: 1.0.1 react: specifier: ^18.3.1 version: 18.3.1 @@ -720,6 +723,9 @@ packages: resolution: {integrity: sha512-eYm0QWBtUrBWZWG0d386OGAw16Z995PiOVo2B7bjWSbHedGl5e0ZWaq65kOGgUSNesEIDkB9ISbTg/JK9dhCZA==} engines: {node: '>=6'} + clvm-lib@1.0.1: + resolution: {integrity: sha512-dZaQgIx8t/ozK0FmIaZGvO5oFKhm/vADtjS4GiokW+Fmian8TWDiRx5j51Gip8NapsBJQjLYhBf1g8SuQ338fg==} + color-convert@1.9.3: resolution: {integrity: sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==} @@ -2442,6 +2448,11 @@ snapshots: clsx@2.1.1: {} + clvm-lib@1.0.1: + dependencies: + chai: 4.5.0 + chia-bls: 1.0.2 + color-convert@1.9.3: dependencies: color-name: 1.1.3 diff --git a/src/components/Main.jsx b/src/components/Main.jsx index 2020b73..1240580 100644 --- a/src/components/Main.jsx +++ b/src/components/Main.jsx @@ -1,8 +1,10 @@ import { bech32m } from "bech32"; import bls from "chia-bls"; +import clvm from "clvm-lib"; import { useState } from "react"; -const { AugSchemeMPL, JacobianPoint } = bls; +const { AugSchemeMPL, JacobianPoint, PrivateKey, bigIntToBytes, hash256 } = bls; +const { Program } = clvm; export default function Main() { const [m, setM] = useState(""); @@ -27,11 +29,63 @@ export default function Main() { ); } +function pathInto(pk, nums) { + for (const num of nums) { + pk = AugSchemeMPL.deriveChildPkUnhardened(pk, num); + } + return pk; +} + +const groupOrder = + 0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001n; + +const defaultHidden = Program.deserializeHex("ff0980").hash(); + +export function calculateSyntheticPublicKey(publicKey, hiddenPuzzleHash) { + return JacobianPoint.fromBytes( + Program.deserializeHex("ff1dff02ffff1effff0bff02ff05808080").run( + Program.fromList([ + Program.fromJacobianPoint(publicKey), + Program.fromBytes(hiddenPuzzleHash), + ]) + ).value.atom, + false + ); +} + +export function calculateSyntheticPrivateKey(privateKey, hiddenPuzzleHash) { + const privateExponent = bytesToBigInt(privateKey.toBytes(), "big"); + const publicKey = privateKey.getG1(); + const syntheticOffset = calculateSyntheticOffset(publicKey, hiddenPuzzleHash); + const syntheticPrivateExponent = mod( + privateExponent + syntheticOffset, + groupOrder + ); + const blob = bigIntToBytes(syntheticPrivateExponent, 32, "big"); + return PrivateKey.fromBytes(blob); +} + +export function calculateSyntheticOffset(publicKey, hiddenPuzzleHash) { + const blob = hash256(concatBytes(publicKey.toBytes(), hiddenPuzzleHash)); + return mod(decodeBigInt(blob), groupOrder); +} + function calc(hex) { try { const key = JacobianPoint.fromHexG1(hex.replace("0x", "")); const address = toAddress( - AugSchemeMPL.deriveChildPkUnhardened(key, 0).toBytes(), + Program.deserializeHex( + "ff02ffff01ff02ffff03ff0bffff01ff02ffff03ffff09ff05ffff1dff0bffff1effff0bff0bffff02ff06ffff04ff02ffff04ff17ff8080808080808080ffff01ff02ff17ff2f80ffff01ff088080ff0180ffff01ff04ffff04ff04ffff04ff05ffff04ffff02ff06ffff04ff02ffff04ff17ff80808080ff80808080ffff02ff17ff2f808080ff0180ffff04ffff01ff32ff02ffff03ffff07ff0580ffff01ff0bffff0102ffff02ff06ffff04ff02ffff04ff09ff80808080ffff02ff06ffff04ff02ffff04ff0dff8080808080ffff01ff0bffff0101ff058080ff0180ff018080" + ) + .curry([ + Program.fromJacobianPoint( + calculateSyntheticPublicKey( + pathInto(key, [12381, 8444, 2, 0]), + defaultHidden + ) + ), + ]) + .hash(), "bls1238" ); return address;