Skip to content

Commit

Permalink
Add util fn to convert bech32 to hex for treasury withdrawal
Browse files Browse the repository at this point in the history
  • Loading branch information
Sital999 committed Nov 21, 2024
1 parent 5896d86 commit edbded4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
13 changes: 12 additions & 1 deletion agent-node/src/functions/treasuryWithdrawal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FunctionContext } from '../executor/BaseFunction'
import { bech32toHex } from '../utils/cardano'

export default async function handler(
context: FunctionContext,
Expand All @@ -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
Expand All @@ -17,7 +28,7 @@ export default async function handler(
{
anchor: anchorData,
refundAccount: context.wallet.rewardAddress,
withdraw: withdrawal,
withdraw: updatedWithdrawal,
script: {
type: 'PlutusScriptV3',
description: '',
Expand Down
6 changes: 6 additions & 0 deletions agent-node/src/utils/cardano.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}

0 comments on commit edbded4

Please sign in to comment.