github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/crypto/crypto.go (about) 1 package crypto 2 3 import ( 4 "bytes" 5 "fmt" 6 7 "github.com/gnolang/gno/tm2/pkg/bech32" 8 "github.com/gnolang/gno/tm2/pkg/crypto/tmhash" 9 ) 10 11 // ---------------------------------------- 12 // Bech32Address 13 14 type Bech32Address string 15 16 func (b32 Bech32Address) String() string { 17 return string(b32) 18 } 19 20 // ---------------------------------------- 21 // Address 22 23 const ( 24 // AddressSize is the size of a pubkey address. 25 AddressSize = tmhash.TruncatedSize 26 ) 27 28 // (truncated) hash of some preimage (typically of a pubkey). 29 type Address [AddressSize]byte 30 31 func AddressFromString(str string) (addr Address, err error) { 32 err = addr.DecodeString(str) 33 return 34 } 35 36 func MustAddressFromString(str string) (addr Address) { 37 err := addr.DecodeString(str) 38 if err != nil { 39 panic(fmt.Errorf("invalid address string representation: %v, error: %w", str, err)) 40 } 41 return 42 } 43 44 func AddressFromPreimage(bz []byte) Address { 45 return AddressFromBytes(tmhash.SumTruncated(bz)) 46 } 47 48 func AddressFromBytes(bz []byte) (ret Address) { 49 if len(bz) != AddressSize { 50 panic(fmt.Errorf("unexpected address byte length. expected %v, got %v", AddressSize, len(bz))) 51 } 52 copy(ret[:], bz) 53 return 54 } 55 56 func (addr Address) MarshalAmino() (string, error) { 57 return AddressToBech32(addr), nil 58 } 59 60 func (addr *Address) UnmarshalAmino(b32str string) (err error) { 61 if b32str == "" { 62 return nil // leave addr as zero. 63 } 64 addr2, err := AddressFromBech32(b32str) 65 if err != nil { 66 return err 67 } 68 copy(addr[:], addr2[:]) 69 return nil 70 } 71 72 func (addr Address) Compare(other Address) int { 73 bz1 := make([]byte, len(addr)) 74 bz2 := make([]byte, len(other)) 75 copy(bz1, addr[:]) 76 copy(bz2, other[:]) 77 return bytes.Compare(bz1, bz2) 78 } 79 80 func (addr Address) IsZero() bool { 81 return addr == Address{} 82 } 83 84 func (addr Address) String() string { 85 return AddressToBech32(addr) 86 } 87 88 func (addr Address) Bech32() Bech32Address { 89 return Bech32Address(AddressToBech32(addr)) 90 } 91 92 func (addr Address) Bytes() []byte { 93 res := make([]byte, AddressSize) 94 copy(res, addr[:]) 95 return res 96 } 97 98 func (addr *Address) DecodeString(str string) error { 99 pre, bz, err := bech32.Decode(str) 100 if err != nil { 101 return err 102 } 103 if pre != Bech32AddrPrefix { 104 return fmt.Errorf("unexpected bech32 prefix for address. expected %q, got %q", Bech32AddrPrefix, pre) 105 } 106 if len(bz) != AddressSize { 107 return fmt.Errorf("unexpected address byte length. expected %v, got %v", AddressSize, len(bz)) 108 } 109 copy((*addr)[:], bz) 110 return nil 111 } 112 113 // ---------------------------------------- 114 // ID 115 116 // The bech32 representation w/ bech32 prefix. 117 type ID string 118 119 func (id ID) IsZero() bool { 120 return id == "" 121 } 122 123 func (id ID) String() string { 124 return string(id) 125 } 126 127 func (id ID) Validate() error { 128 if id.IsZero() { 129 return fmt.Errorf("zero ID is invalid") 130 } 131 var addr Address 132 err := addr.DecodeID(id) 133 return err 134 } 135 136 func AddressFromID(id ID) (addr Address, err error) { 137 err = addr.DecodeString(string(id)) 138 return 139 } 140 141 func (addr Address) ID() ID { 142 return ID(addr.String()) 143 } 144 145 func (addr *Address) DecodeID(id ID) error { 146 return addr.DecodeString(string(id)) 147 } 148 149 // ---------------------------------------- 150 // PubKey 151 152 // All operations must be deterministic. 153 type PubKey interface { 154 // Stable 155 Address() Address 156 Bytes() []byte 157 VerifyBytes(msg []byte, sig []byte) bool 158 Equals(PubKey) bool 159 String() string 160 } 161 162 // ---------------------------------------- 163 // PrivKey 164 165 // All operations must be deterministic. 166 type PrivKey interface { 167 // Stable 168 Bytes() []byte 169 Sign(msg []byte) ([]byte, error) 170 PubKey() PubKey 171 Equals(PrivKey) bool 172 } 173 174 // ---------------------------------------- 175 // Symmetric 176 177 type Symmetric interface { 178 Keygen() []byte 179 Encrypt(plaintext []byte, secret []byte) (ciphertext []byte) 180 Decrypt(ciphertext []byte, secret []byte) (plaintext []byte, err error) 181 }