AI Response

Is there an easy way to get the miner reward address from the block header.powSolutions.pk ?
1 Reply
Glasgow
Glasgow3y ago
Yes, it's possible to get the miner reward address from the powSolutions.pk field of a block header. To do this, you would need to decode the pk using the GroupElementSerializer and then generate an address from the resulting GroupElement. Here is an example of how to get the reward address from a block header:
import scorex.crypto.hash.Blake2b256
import sigmastate.basics.DLogProtocol.DLogProverInput
import sigmastate.eval.CSigmaProp
import sigmastate.serialization.GroupElementSerializer
import special.collection.Coll
import special.sigma.GroupElement

val pkBytes: Coll[Byte] = ???
val decodedPk: GroupElement = GroupElementSerializer.parse(pkBytes)

val blockHeader: ErgoFullBlock = ???
val powSolution: PowSolution = blockHeader.header.powSolution
val pk: Coll[Byte] = powSolution.pk
val decodedPk: GroupElement = GroupElementSerializer.parse(pk)
val minerRewardOutput = blockHeader.blockTransactions.txs.flatMap(tx =>
tx.outputs.filter(o => o.proposition == decodedPk.propositionBytes)).headOption
val minerAddress = minerRewardOutput.map(o => getAddressFromProposition(o.proposition))
import scorex.crypto.hash.Blake2b256
import sigmastate.basics.DLogProtocol.DLogProverInput
import sigmastate.eval.CSigmaProp
import sigmastate.serialization.GroupElementSerializer
import special.collection.Coll
import special.sigma.GroupElement

val pkBytes: Coll[Byte] = ???
val decodedPk: GroupElement = GroupElementSerializer.parse(pkBytes)

val blockHeader: ErgoFullBlock = ???
val powSolution: PowSolution = blockHeader.header.powSolution
val pk: Coll[Byte] = powSolution.pk
val decodedPk: GroupElement = GroupElementSerializer.parse(pk)
val minerRewardOutput = blockHeader.blockTransactions.txs.flatMap(tx =>
tx.outputs.filter(o => o.proposition == decodedPk.propositionBytes)).headOption
val minerAddress = minerRewardOutput.map(o => getAddressFromProposition(o.proposition))
This code snippet assumes you have a powSolutions.pk field in bytes and a ErgoFullBlock object to work with. It uses the GroupElementSerializer to decode the pk into a GroupElement, then looks for an output in the corresponding miner reward transaction where the proposition matches the decoded pk. Finally, it passes this proposition to getAddressFromProposition to generate an address. Regarding getting an ErgoTree out of the powSolutions.pk, there is no direct way to do this since a pk is a GroupElement, not an ErgoTree. However, you can use the pk to derive a public key script by creating a DLogProverInput and then using CSigmaProp to create an ErgoTree. Here is an example of how to do this:
import sigmastate.basics.DLogProtocol.DLogProverInput
import sigmastate.eval.CSigmaProp
import special.sigma.GroupElement

val pkBytes: Coll[Byte] = ???
val pk: GroupElement = GroupElementSerializer.parse(pkBytes)

val dlogProverInput = DLogProverInput(pk.value)
val ergoTree = CSigmaProp(dlogProverInput).toSigmaProp.tree
import sigmastate.basics.DLogProtocol.DLogProverInput
import sigmastate.eval.CSigmaProp
import special.sigma.GroupElement

val pkBytes: Coll[Byte] = ???
val pk: GroupElement = GroupElementSerializer.parse(pkBytes)

val dlogProverInput = DLogProverInput(pk.value)
val ergoTree = CSigmaProp(dlogProverInput).toSigmaProp.tree
This code block uses the DLogProverInput generated from the pk to create a CSigmaProp, which can then be converted to an ErgoTree using toSigmaProp.tree. Note that the resulting ErgoTree will be a sigmaProp tree since CSigmaProp is represented as a sigmaProp in Ergo.

Did you find this page helpful?