Skip to content

Commit

Permalink
feat: address hash function
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Gianelloni <[email protected]>
  • Loading branch information
wolf31o2 committed Jun 6, 2024
1 parent 20eaad0 commit a31c7b8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
38 changes: 38 additions & 0 deletions ledger/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -43,6 +45,8 @@ const (
AddressTypeNoneScript = 0b1111
)

type AddrKeyHash Blake2b224

type Address struct {
addressType uint8
networkId uint8
Expand Down Expand Up @@ -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
Expand All @@ -212,6 +233,23 @@ func (a Address) StakeAddress() *Address {
return newAddr
}

// StakeKeyHash returns a new Blake2b224 hash of the stake key
func (a *Address) StakeKeyHash() 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 ||
Expand Down
1 change: 0 additions & 1 deletion ledger/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ func (c *StakeDelegationCertificate) Utxorpc() *utxorpc.Certificate {
}
}

type AddrKeyHash Blake2b224
type PoolKeyHash Blake2b224
type PoolMetadataHash Blake2b256
type VrfKeyHash Blake2b256
Expand Down

0 comments on commit a31c7b8

Please sign in to comment.