From edbded4dcea3dd912cdd097eb4def1c180074f97 Mon Sep 17 00:00:00 2001 From: sital999 Date: Thu, 21 Nov 2024 14:23:41 +0545 Subject: [PATCH] Add util fn to convert bech32 to hex for treasury withdrawal --- agent-node/src/functions/treasuryWithdrawal.ts | 13 ++++++++++++- agent-node/src/utils/cardano.ts | 6 ++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/agent-node/src/functions/treasuryWithdrawal.ts b/agent-node/src/functions/treasuryWithdrawal.ts index d053f4175..dbda5b810 100644 --- a/agent-node/src/functions/treasuryWithdrawal.ts +++ b/agent-node/src/functions/treasuryWithdrawal.ts @@ -1,4 +1,5 @@ import { FunctionContext } from '../executor/BaseFunction' +import { bech32toHex } from '../utils/cardano' export default async function handler( context: FunctionContext, @@ -8,6 +9,16 @@ export default async function handler( const { dataHash, url } = await context.builtins.saveMetadata( context.helpers.generateProposalMetadataContent() ) + let updatedWithdrawal = withdrawal + if (withdrawal) { + const stakeKey = Object.keys(withdrawal)[0] + if (stakeKey.startsWith('stake')) { + const statkeKeyHex = bech32toHex(stakeKey) + updatedWithdrawal = { + ['e0'+statkeKeyHex]: withdrawal[stakeKey], + } + } + } const anchorData = anchor && anchor['url'] && anchor['dataHash'] ? anchor @@ -17,7 +28,7 @@ export default async function handler( { anchor: anchorData, refundAccount: context.wallet.rewardAddress, - withdraw: withdrawal, + withdraw: updatedWithdrawal, script: { type: 'PlutusScriptV3', description: '', diff --git a/agent-node/src/utils/cardano.ts b/agent-node/src/utils/cardano.ts index c687521df..c84c4feca 100644 --- a/agent-node/src/utils/cardano.ts +++ b/agent-node/src/utils/cardano.ts @@ -33,3 +33,9 @@ export function loadRootKeyFromBuffer() { } return HdKey.fromBytes(rootKeyBuffer) } + +export function bech32toHex(bechAddress: string) { + const decodedId = bech32.decode(bechAddress, 100) + const data = bech32.fromWords(decodedId.words) + return Buffer.from(data).toString('hex') +}