github.com/deso-protocol/core@v1.2.9/lib/types.go (about) 1 package lib 2 3 import "fmt" 4 5 // A PKID is an ID associated with a public key. In the DB, various fields are 6 // indexed using the PKID rather than the user's public key directly in order to 7 // create one layer of indirection between the public key and the user's data. This 8 // makes it easy for the user to transfer certain data to a new public key. 9 type PKID [33]byte 10 type PublicKey [33]byte 11 12 func NewPKID(pkidBytes []byte) *PKID { 13 if len(pkidBytes) == 0 { 14 return nil 15 } 16 pkid := &PKID{} 17 copy(pkid[:], pkidBytes) 18 return pkid 19 } 20 21 func (pkid *PKID) ToBytes() []byte { 22 return pkid[:] 23 } 24 25 func (pkid *PKID) NewPKID() *PKID { 26 newPkid := &PKID{} 27 copy(newPkid[:], pkid[:]) 28 return newPkid 29 } 30 31 func NewPublicKey(publicKeyBytes []byte) *PublicKey { 32 if len(publicKeyBytes) == 0 { 33 return nil 34 } 35 publicKey := &PublicKey{} 36 copy(publicKey[:], publicKeyBytes) 37 return publicKey 38 } 39 40 func (publicKey *PublicKey) ToBytes() []byte { 41 return publicKey[:] 42 } 43 44 func PublicKeyToPKID(publicKey []byte) *PKID { 45 if len(publicKey) == 0 { 46 return nil 47 } 48 pkid := &PKID{} 49 copy(pkid[:], publicKey) 50 return pkid 51 } 52 53 func PKIDToPublicKey(pkid *PKID) []byte { 54 if pkid == nil { 55 return nil 56 } 57 return pkid[:] 58 } 59 60 const HashSizeBytes = 32 61 62 // BlockHash is a convenient alias for a block hash. 63 type BlockHash [HashSizeBytes]byte 64 65 func NewBlockHash(input []byte) *BlockHash { 66 blockHash := &BlockHash{} 67 copy(blockHash[:], input) 68 return blockHash 69 } 70 71 func (bh *BlockHash) String() string { 72 return fmt.Sprintf("%064x", HashToBigint(bh)) 73 } 74 75 func (bh *BlockHash) ToBytes() []byte { 76 res := make([]byte, HashSizeBytes) 77 copy(res, bh[:]) 78 return res 79 } 80 81 // IsEqual returns true if target is the same as hash. 82 func (bh *BlockHash) IsEqual(target *BlockHash) bool { 83 if bh == nil && target == nil { 84 return true 85 } 86 if bh == nil || target == nil { 87 return false 88 } 89 return *bh == *target 90 } 91 92 func (bh *BlockHash) NewBlockHash() *BlockHash { 93 newBlockhash := &BlockHash{} 94 copy(newBlockhash[:], bh[:]) 95 return newBlockhash 96 }