From 20eaad0c3f6e2a1e6a055d2e3eeb72a54a763999 Mon Sep 17 00:00:00 2001 From: Chris Gianelloni Date: Wed, 5 Jun 2024 18:31:11 -0400 Subject: [PATCH] refactor(ledger): move stakecredential functions to its own file (#652) Signed-off-by: Chris Gianelloni --- ledger/certs.go | 27 ----------------- ledger/credentials.go | 67 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 27 deletions(-) create mode 100644 ledger/credentials.go diff --git a/ledger/certs.go b/ledger/certs.go index 16ac9ed8..bd6832a3 100644 --- a/ledger/certs.go +++ b/ledger/certs.go @@ -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 diff --git a/ledger/credentials.go b/ledger/credentials.go new file mode 100644 index 00000000..3dc172ad --- /dev/null +++ b/ledger/credentials.go @@ -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 +}