-
Notifications
You must be signed in to change notification settings - Fork 95
/
block_issuer_key_ed25519_pubkeyhash.go
99 lines (78 loc) · 3.46 KB
/
block_issuer_key_ed25519_pubkeyhash.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package iotago
import (
"bytes"
"cmp"
"context"
"golang.org/x/crypto/blake2b"
hiveEd25519 "github.com/iotaledger/hive.go/crypto/ed25519"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/serializer/v2"
)
// Ed25519PublicKeyHashBytesLength is the length of an Ed25519 public key hash.
const (
Ed25519PublicKeyHashBytesLength = blake2b.Size256
Ed25519PublicKeyHashBlockIssuerKeyLength = serializer.SmallTypeDenotationByteSize + Ed25519PublicKeyHashBytesLength
)
// An Ed25519 Address Block Issuer Key.
type Ed25519PublicKeyHashBlockIssuerKey struct {
PublicKeyHash [Ed25519PublicKeyHashBytesLength]byte `serix:"pubKeyHash"`
}
// Ed25519PublicKeyHashBlockIssuerKeyFromImplicitAccountCreationAddress creates an Ed25519PublicKeyHashBlockIssuerKey from an Ed25519 public key hash.
func Ed25519PublicKeyHashBlockIssuerKeyFromImplicitAccountCreationAddress(address *ImplicitAccountCreationAddress) *Ed25519PublicKeyHashBlockIssuerKey {
cpy := [Ed25519PublicKeyHashBytesLength]byte{}
copy(cpy[:], address[:])
return &Ed25519PublicKeyHashBlockIssuerKey{PublicKeyHash: cpy}
}
// Ed25519PublicKeyHashBlockIssuerKeyFromPublicKey creates an Ed25519PublicKeyHashBlockIssuerKey from an Ed25519 public key.
func Ed25519PublicKeyHashBlockIssuerKeyFromPublicKey(pubKey hiveEd25519.PublicKey) *Ed25519PublicKeyHashBlockIssuerKey {
pubKeyHash := blake2b.Sum256(pubKey[:])
return &Ed25519PublicKeyHashBlockIssuerKey{
PublicKeyHash: pubKeyHash,
}
}
func (key *Ed25519PublicKeyHashBlockIssuerKey) Clone() BlockIssuerKey {
cpy := [Ed25519PublicKeyHashBytesLength]byte{}
copy(cpy[:], key.PublicKeyHash[:])
return &Ed25519PublicKeyHashBlockIssuerKey{
PublicKeyHash: cpy,
}
}
func Ed25519PublicKeyHashBlockIssuerKeyFromBytes(bytes []byte) (*Ed25519PublicKeyHashBlockIssuerKey, int, error) {
blockIssuerKey := &Ed25519PublicKeyHashBlockIssuerKey{}
n, err := CommonSerixAPI().Decode(context.TODO(), bytes, blockIssuerKey)
if err != nil {
return nil, 0, ierrors.Wrap(err, "failed to deserialize Ed25519PublicKeyHashBlockIssuerKey")
}
return blockIssuerKey, n, nil
}
// Bytes returns a byte slice consisting of the type prefix and the raw address.
func (key *Ed25519PublicKeyHashBlockIssuerKey) Bytes() ([]byte, error) {
return CommonSerixAPI().Encode(context.TODO(), key)
}
// Type returns the BlockIssuerKeyType.
func (key *Ed25519PublicKeyHashBlockIssuerKey) Type() BlockIssuerKeyType {
return BlockIssuerKeyPublicKeyHash
}
func (key *Ed25519PublicKeyHashBlockIssuerKey) Equal(other BlockIssuerKey) bool {
otherBlockIssuerKey, is := other.(*Ed25519PublicKeyHashBlockIssuerKey)
if !is {
return false
}
return key.PublicKeyHash == otherBlockIssuerKey.PublicKeyHash
}
func (key *Ed25519PublicKeyHashBlockIssuerKey) Compare(other BlockIssuerKey) int {
typeCompare := cmp.Compare(key.Type(), other.Type())
if typeCompare != 0 {
return typeCompare
}
//nolint:forcetypeassert // we can safely assume that this is an Ed25519PublicKeyHashBlockIssuerKey
otherBlockIssuerKey := other.(*Ed25519PublicKeyHashBlockIssuerKey)
return bytes.Compare(key.PublicKeyHash[:], otherBlockIssuerKey.PublicKeyHash[:])
}
// Size returns the size of the block issuer key when serialized.
func (key *Ed25519PublicKeyHashBlockIssuerKey) Size() int {
return Ed25519PublicKeyHashBlockIssuerKeyLength
}
func (key *Ed25519PublicKeyHashBlockIssuerKey) StorageScore(storageScoreStructure *StorageScoreStructure, _ StorageScoreFunc) StorageScore {
return storageScoreStructure.OffsetEd25519BlockIssuerKey()
}