|
| 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