Skip to content

Commit 20eaad0

Browse files
authored
refactor(ledger): move stakecredential functions to its own file (#652)
Signed-off-by: Chris Gianelloni <[email protected]>
1 parent d072f88 commit 20eaad0

File tree

2 files changed

+67
-27
lines changed

2 files changed

+67
-27
lines changed

ledger/certs.go

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -117,33 +117,6 @@ type Certificate interface {
117117
Utxorpc() *utxorpc.Certificate
118118
}
119119

120-
const (
121-
StakeCredentialTypeAddrKeyHash = 0
122-
StakeCredentialTypeScriptHash = 1
123-
)
124-
125-
type StakeCredential struct {
126-
cbor.StructAsArray
127-
cbor.DecodeStoreCbor
128-
CredType uint
129-
Credential []byte
130-
}
131-
132-
func (c *StakeCredential) Utxorpc() *utxorpc.StakeCredential {
133-
ret := &utxorpc.StakeCredential{}
134-
switch c.CredType {
135-
case StakeCredentialTypeAddrKeyHash:
136-
ret.StakeCredential = &utxorpc.StakeCredential_AddrKeyHash{
137-
AddrKeyHash: c.Credential[:],
138-
}
139-
case StakeCredentialTypeScriptHash:
140-
ret.StakeCredential = &utxorpc.StakeCredential_ScriptHash{
141-
ScriptHash: c.Credential[:],
142-
}
143-
}
144-
return ret
145-
}
146-
147120
const (
148121
DrepTypeAddrKeyHash = 0
149122
DrepTypeScriptHash = 1

ledger/credentials.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2024 Blink Labs Software
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package ledger
16+
17+
import (
18+
"fmt"
19+
20+
"github.com/blinklabs-io/gouroboros/cbor"
21+
22+
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
23+
"golang.org/x/crypto/blake2b"
24+
)
25+
26+
const (
27+
StakeCredentialTypeAddrKeyHash = 0
28+
StakeCredentialTypeScriptHash = 1
29+
)
30+
31+
type StakeCredential struct {
32+
cbor.StructAsArray
33+
cbor.DecodeStoreCbor
34+
CredType uint
35+
Credential []byte
36+
}
37+
38+
func (c *StakeCredential) Hash() Blake2b224 {
39+
hash, err := blake2b.New(28, nil)
40+
if err != nil {
41+
panic(
42+
fmt.Sprintf(
43+
"unexpected error creating empty blake2b hash: %s",
44+
err,
45+
),
46+
)
47+
}
48+
if c != nil {
49+
hash.Write(c.Credential[:])
50+
}
51+
return Blake2b224(hash.Sum(nil))
52+
}
53+
54+
func (c *StakeCredential) Utxorpc() *utxorpc.StakeCredential {
55+
ret := &utxorpc.StakeCredential{}
56+
switch c.CredType {
57+
case StakeCredentialTypeAddrKeyHash:
58+
ret.StakeCredential = &utxorpc.StakeCredential_AddrKeyHash{
59+
AddrKeyHash: c.Credential[:],
60+
}
61+
case StakeCredentialTypeScriptHash:
62+
ret.StakeCredential = &utxorpc.StakeCredential_ScriptHash{
63+
ScriptHash: c.Credential[:],
64+
}
65+
}
66+
return ret
67+
}

0 commit comments

Comments
 (0)