Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions ledger/common/gov.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
package common

import (
"fmt"
"math/big"

"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/plutigo/data"
"github.com/btcsuite/btcd/btcutil/bech32"
)

// VotingProcedures is a convenience type to avoid needing to duplicate the full type definition everywhere
Expand All @@ -38,6 +40,78 @@ type Voter struct {
Hash [28]byte
}

const (
// CIP-129 key type identifiers and CIP-129 credential types mirror address.
cip129KeyTypeConstitutionalCommitteeHot uint8 = 0
cip129KeyTypeDRep uint8 = 2
cip129CredentialTypeKeyHash uint8 = 0x02
cip129CredentialTypeScriptHash uint8 = 0x03
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These values are the same as the existing constants at the top of this file, and the existing ones should be used instead


func encodeCip129Voter(
prefix string,
keyType uint8,
credentialType uint8,
hash []byte,
) string {
header := byte((keyType << 4) | (credentialType & 0x0f))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could use a comment about what's actually happening here

data := make([]byte, 1+len(hash))
data[0] = header
copy(data[1:], hash)
return encodeVoterBech32(prefix, data)
}

func encodeVoterBech32(prefix string, data []byte) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is only ever called by the function above and could easily be merged into it.

convData, err := bech32.ConvertBits(data, 8, 5, true)
if err != nil {
panic(fmt.Sprintf("unexpected error converting voter data to base32: %s", err))
}
encoded, err := bech32.Encode(prefix, convData)
if err != nil {
panic(fmt.Sprintf("unexpected error encoding voter data as bech32: %s", err))
}
return encoded
}

// Generates bech32-encoded identifier for the voter credential.
func (v Voter) String() string {
switch v.Type {
case VoterTypeConstitutionalCommitteeHotKeyHash:
return encodeCip129Voter(
"cc_hot",
cip129KeyTypeConstitutionalCommitteeHot,
cip129CredentialTypeKeyHash,
v.Hash[:],
)
case VoterTypeConstitutionalCommitteeHotScriptHash:
return encodeCip129Voter(
"cc_hot",
cip129KeyTypeConstitutionalCommitteeHot,
cip129CredentialTypeScriptHash,
v.Hash[:],
)
case VoterTypeDRepKeyHash:
return encodeCip129Voter(
"drep",
cip129KeyTypeDRep,
cip129CredentialTypeKeyHash,
v.Hash[:],
)
case VoterTypeDRepScriptHash:
return encodeCip129Voter(
"drep",
cip129KeyTypeDRep,
cip129CredentialTypeScriptHash,
v.Hash[:],
)
case VoterTypeStakingPoolKeyHash:
poolId := PoolId(v.Hash)
return poolId.String()
default:
panic(fmt.Sprintf("unknown voter type: %d", v.Type))
}
}

func (v Voter) ToPlutusData() data.PlutusData {
switch v.Type {
case VoterTypeConstitutionalCommitteeHotScriptHash:
Expand Down
62 changes: 62 additions & 0 deletions ledger/common/gov_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,68 @@ func TestVoterToPlutusData(t *testing.T) {
}
}

func TestVoterString(t *testing.T) {
var zeroHash [28]byte
var sequentialHash [28]byte
for i := range sequentialHash {
sequentialHash[i] = byte(i)
}
testCases := []struct {
name string
voter Voter
want string
}{
{
name: "CIP129CcHotKeyHash",
voter: Voter{
Type: VoterTypeConstitutionalCommitteeHotKeyHash,
Hash: zeroHash,
},
want: "cc_hot1qgqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqvcdjk7",
},
{
name: "CIP129CcHotScriptHash",
voter: Voter{
Type: VoterTypeConstitutionalCommitteeHotScriptHash,
Hash: zeroHash,
},
want: "cc_hot1qvqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqv2arke",
},
{
name: "CIP129DRepKeyHash",
voter: Voter{
Type: VoterTypeDRepKeyHash,
Hash: zeroHash,
},
want: "drep1ygqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq7vlc9n",
},
{
name: "CIP129DRepScriptHash",
voter: Voter{
Type: VoterTypeDRepScriptHash,
Hash: zeroHash,
},
want: "drep1yvqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq770f95",
},
{
name: "StakingPoolKeyHash",
voter: Voter{
Type: VoterTypeStakingPoolKeyHash,
Hash: sequentialHash,
},
want: "pool1qqqsyqcyq5rqwzqfpg9scrgwpugpzysnzs23v9ccrydpk35lkuk",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.want, tc.voter.String())
})
}
assert.Panics(t, func() {
_ = Voter{Type: 99}.String()
})
}

// Tests the ToPlutusData method for Vote types
func TestVoteToPlutusData(t *testing.T) {
testCases := []struct {
Expand Down