diff --git a/ledger/address.go b/ledger/address.go index 1c792660..5f1aab90 100644 --- a/ledger/address.go +++ b/ledger/address.go @@ -20,6 +20,8 @@ import ( "github.com/blinklabs-io/gouroboros/base58" "github.com/blinklabs-io/gouroboros/bech32" "github.com/blinklabs-io/gouroboros/cbor" + + "golang.org/x/crypto/blake2b" ) const ( @@ -43,6 +45,8 @@ const ( AddressTypeNoneScript = 0b1111 ) +type AddrKeyHash Blake2b224 + type Address struct { addressType uint8 networkId uint8 @@ -191,6 +195,23 @@ func (a Address) PaymentAddress() *Address { return newAddr } +// PaymentKeyHash returns a new Blake2b224 hash of the payment key +func (a *Address) PaymentKeyHash() Blake2b224 { + hash, err := blake2b.New(28, nil) + if err != nil { + panic( + fmt.Sprintf( + "unexpected error creating empty blake2b hash: %s", + err, + ), + ) + } + if a.paymentAddress != nil { + hash.Write(a.paymentAddress[:]) + } + return Blake2b224(hash.Sum(nil)) +} + // StakeAddress returns a new Address with only the stake key portion. This will return nil if the address is not a payment/staking key pair func (a Address) StakeAddress() *Address { var addrType uint8 @@ -212,6 +233,23 @@ func (a Address) StakeAddress() *Address { return newAddr } +// StakeKeyHash returns a new Blake2b224 hash of the stake key +func (a *Address) PaymentKeyHash() Blake2b224 { + hash, err := blake2b.New(28, nil) + if err != nil { + panic( + fmt.Sprintf( + "unexpected error creating empty blake2b hash: %s", + err, + ), + ) + } + if a.stakingAddress != nil { + hash.Write(a.stakingAddress[:]) + } + return Blake2b224(hash.Sum(nil)) +} + func (a Address) generateHRP() string { var ret string if a.addressType == AddressTypeNoneKey || diff --git a/ledger/certs.go b/ledger/certs.go index bd6832a3..bc0c7d31 100644 --- a/ledger/certs.go +++ b/ledger/certs.go @@ -221,7 +221,6 @@ func (c *StakeDelegationCertificate) Utxorpc() *utxorpc.Certificate { } } -type AddrKeyHash Blake2b224 type PoolKeyHash Blake2b224 type PoolMetadataHash Blake2b256 type VrfKeyHash Blake2b256