Skip to content

Commit

Permalink
refactor(ledger): move stakecredential functions to its own file (#652)
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Gianelloni <wolf31o2@blinklabs.io>
  • Loading branch information
wolf31o2 authored Jun 5, 2024
1 parent d072f88 commit 20eaad0
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 27 deletions.
27 changes: 0 additions & 27 deletions ledger/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,33 +117,6 @@ type Certificate interface {
Utxorpc() *utxorpc.Certificate
}

const (
StakeCredentialTypeAddrKeyHash = 0
StakeCredentialTypeScriptHash = 1
)

type StakeCredential struct {
cbor.StructAsArray
cbor.DecodeStoreCbor
CredType uint
Credential []byte
}

func (c *StakeCredential) Utxorpc() *utxorpc.StakeCredential {
ret := &utxorpc.StakeCredential{}
switch c.CredType {
case StakeCredentialTypeAddrKeyHash:
ret.StakeCredential = &utxorpc.StakeCredential_AddrKeyHash{
AddrKeyHash: c.Credential[:],
}
case StakeCredentialTypeScriptHash:
ret.StakeCredential = &utxorpc.StakeCredential_ScriptHash{
ScriptHash: c.Credential[:],
}
}
return ret
}

const (
DrepTypeAddrKeyHash = 0
DrepTypeScriptHash = 1
Expand Down
67 changes: 67 additions & 0 deletions ledger/credentials.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2024 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ledger

import (
"fmt"

"github.com/blinklabs-io/gouroboros/cbor"

utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
"golang.org/x/crypto/blake2b"
)

const (
StakeCredentialTypeAddrKeyHash = 0
StakeCredentialTypeScriptHash = 1
)

type StakeCredential struct {
cbor.StructAsArray
cbor.DecodeStoreCbor
CredType uint
Credential []byte
}

func (c *StakeCredential) Hash() Blake2b224 {
hash, err := blake2b.New(28, nil)
if err != nil {
panic(
fmt.Sprintf(
"unexpected error creating empty blake2b hash: %s",
err,
),
)
}
if c != nil {
hash.Write(c.Credential[:])
}
return Blake2b224(hash.Sum(nil))
}

func (c *StakeCredential) Utxorpc() *utxorpc.StakeCredential {
ret := &utxorpc.StakeCredential{}
switch c.CredType {
case StakeCredentialTypeAddrKeyHash:
ret.StakeCredential = &utxorpc.StakeCredential_AddrKeyHash{
AddrKeyHash: c.Credential[:],
}
case StakeCredentialTypeScriptHash:
ret.StakeCredential = &utxorpc.StakeCredential_ScriptHash{
ScriptHash: c.Credential[:],
}
}
return ret
}

0 comments on commit 20eaad0

Please sign in to comment.