Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
65 changes: 65 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,69 @@ type Voter struct {
Hash [28]byte
}

func encodeCip129Voter(
prefix string,
keyType uint8,
credentialType uint8,
hash []byte,
) string {
// Header packs the 4-bit key type (per CIP-129) in the high nibble and the credential semantics in the low nibble.
// Since CIP-129 reserves values 0 and 1,we offset the existing credential constants (0 = key hash, 1 = script hash) by 2
// so the output nibble matches the spec's 0x2/0x3 tags.
header := byte((keyType << 4) | ((credentialType + 2) & 0x0f))
data := make([]byte, 1+len(hash))
data[0] = header
copy(data[1:], hash)
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",
VoterTypeConstitutionalCommitteeHotKeyHash,
CredentialTypeAddrKeyHash,
v.Hash[:],
)
case VoterTypeConstitutionalCommitteeHotScriptHash:
return encodeCip129Voter(
"cc_hot",
VoterTypeConstitutionalCommitteeHotKeyHash,
CredentialTypeScriptHash,
v.Hash[:],
)
case VoterTypeDRepKeyHash:
return encodeCip129Voter(
"drep",
VoterTypeDRepKeyHash,
CredentialTypeAddrKeyHash,
v.Hash[:],
)
case VoterTypeDRepScriptHash:
return encodeCip129Voter(
"drep",
VoterTypeDRepKeyHash,
CredentialTypeScriptHash,
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