github.com/cosmos/cosmos-sdk@v0.50.1/crypto/keyring/legacy_info.go (about) 1 package keyring 2 3 import ( 4 "fmt" 5 6 "github.com/cosmos/cosmos-sdk/codec/legacy" 7 codectypes "github.com/cosmos/cosmos-sdk/codec/types" 8 "github.com/cosmos/cosmos-sdk/crypto/hd" 9 "github.com/cosmos/cosmos-sdk/crypto/keys/multisig" 10 cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" 11 sdk "github.com/cosmos/cosmos-sdk/types" 12 ) 13 14 // Deprecated: LegacyInfo is the publicly exposed information about a keypair 15 type LegacyInfo interface { 16 // Human-readable type for key listing 17 GetType() KeyType 18 // Name of the key 19 GetName() string 20 // Public key 21 GetPubKey() cryptotypes.PubKey 22 // Address 23 GetAddress() sdk.AccAddress 24 // Bip44 Path 25 GetPath() (*hd.BIP44Params, error) 26 // Algo 27 GetAlgo() hd.PubKeyType 28 } 29 30 var ( 31 _ LegacyInfo = &legacyLocalInfo{} 32 _ LegacyInfo = &legacyLedgerInfo{} 33 _ LegacyInfo = &legacyOfflineInfo{} 34 _ LegacyInfo = &LegacyMultiInfo{} 35 ) 36 37 // legacyLocalInfo is the public information about a locally stored key 38 // Note: Algo must be last field in struct for backwards amino compatibility 39 type legacyLocalInfo struct { 40 Name string `json:"name"` 41 PubKey cryptotypes.PubKey `json:"pubkey"` 42 PrivKeyArmor string `json:"privkey.armor"` 43 Algo hd.PubKeyType `json:"algo"` 44 } 45 46 func infoKey(name string) string { return fmt.Sprintf("%s.%s", name, infoSuffix) } 47 48 // GetType implements Info interface 49 func (i legacyLocalInfo) GetType() KeyType { 50 return TypeLocal 51 } 52 53 // GetType implements Info interface 54 func (i legacyLocalInfo) GetName() string { 55 return i.Name 56 } 57 58 // GetType implements Info interface 59 func (i legacyLocalInfo) GetPubKey() cryptotypes.PubKey { 60 return i.PubKey 61 } 62 63 // GetType implements Info interface 64 func (i legacyLocalInfo) GetAddress() sdk.AccAddress { 65 return i.PubKey.Address().Bytes() 66 } 67 68 // GetPrivKeyArmor 69 func (i legacyLocalInfo) GetPrivKeyArmor() string { 70 return i.PrivKeyArmor 71 } 72 73 // GetType implements Info interface 74 func (i legacyLocalInfo) GetAlgo() hd.PubKeyType { 75 return i.Algo 76 } 77 78 // GetType implements Info interface 79 func (i legacyLocalInfo) GetPath() (*hd.BIP44Params, error) { 80 return nil, fmt.Errorf("BIP44 Paths are not available for this type") 81 } 82 83 // legacyLedgerInfo is the public information about a Ledger key 84 // Note: Algo must be last field in struct for backwards amino compatibility 85 type legacyLedgerInfo struct { 86 Name string `json:"name"` 87 PubKey cryptotypes.PubKey `json:"pubkey"` 88 Path hd.BIP44Params `json:"path"` 89 Algo hd.PubKeyType `json:"algo"` 90 } 91 92 // GetType implements Info interface 93 func (i legacyLedgerInfo) GetType() KeyType { 94 return TypeLedger 95 } 96 97 // GetName implements Info interface 98 func (i legacyLedgerInfo) GetName() string { 99 return i.Name 100 } 101 102 // GetPubKey implements Info interface 103 func (i legacyLedgerInfo) GetPubKey() cryptotypes.PubKey { 104 return i.PubKey 105 } 106 107 // GetAddress implements Info interface 108 func (i legacyLedgerInfo) GetAddress() sdk.AccAddress { 109 return i.PubKey.Address().Bytes() 110 } 111 112 // GetPath implements Info interface 113 func (i legacyLedgerInfo) GetAlgo() hd.PubKeyType { 114 return i.Algo 115 } 116 117 // GetPath implements Info interface 118 func (i legacyLedgerInfo) GetPath() (*hd.BIP44Params, error) { 119 tmp := i.Path 120 return &tmp, nil 121 } 122 123 // legacyOfflineInfo is the public information about an offline key 124 // Note: Algo must be last field in struct for backwards amino compatibility 125 type legacyOfflineInfo struct { 126 Name string `json:"name"` 127 PubKey cryptotypes.PubKey `json:"pubkey"` 128 Algo hd.PubKeyType `json:"algo"` 129 } 130 131 // GetType implements Info interface 132 func (i legacyOfflineInfo) GetType() KeyType { 133 return TypeOffline 134 } 135 136 // GetName implements Info interface 137 func (i legacyOfflineInfo) GetName() string { 138 return i.Name 139 } 140 141 // GetPubKey implements Info interface 142 func (i legacyOfflineInfo) GetPubKey() cryptotypes.PubKey { 143 return i.PubKey 144 } 145 146 // GetAlgo returns the signing algorithm for the key 147 func (i legacyOfflineInfo) GetAlgo() hd.PubKeyType { 148 return i.Algo 149 } 150 151 // GetAddress implements Info interface 152 func (i legacyOfflineInfo) GetAddress() sdk.AccAddress { 153 return i.PubKey.Address().Bytes() 154 } 155 156 // GetPath implements Info interface 157 func (i legacyOfflineInfo) GetPath() (*hd.BIP44Params, error) { 158 return nil, fmt.Errorf("BIP44 Paths are not available for this type") 159 } 160 161 // Deprecated: this structure is not used anymore and it's here only to allow 162 // decoding old multiInfo records from keyring. 163 // The problem with legacy.Cdc.UnmarshalLengthPrefixed - the legacy codec doesn't 164 // tolerate extensibility. 165 type multisigPubKeyInfo struct { 166 PubKey cryptotypes.PubKey `json:"pubkey"` 167 Weight uint `json:"weight"` 168 } 169 170 // multiInfo is the public information about a multisig key 171 type LegacyMultiInfo struct { 172 Name string `json:"name"` 173 PubKey cryptotypes.PubKey `json:"pubkey"` 174 Threshold uint `json:"threshold"` 175 PubKeys []multisigPubKeyInfo `json:"pubkeys"` 176 } 177 178 // NewLegacyMultiInfo creates a new legacyMultiInfo instance 179 func NewLegacyMultiInfo(name string, pub cryptotypes.PubKey) (LegacyInfo, error) { 180 if _, ok := pub.(*multisig.LegacyAminoPubKey); !ok { 181 return nil, fmt.Errorf("MultiInfo supports only multisig.LegacyAminoPubKey, got %T", pub) 182 } 183 return &LegacyMultiInfo{ 184 Name: name, 185 PubKey: pub, 186 }, nil 187 } 188 189 // GetType implements Info interface 190 func (i LegacyMultiInfo) GetType() KeyType { 191 return TypeMulti 192 } 193 194 // GetName implements Info interface 195 func (i LegacyMultiInfo) GetName() string { 196 return i.Name 197 } 198 199 // GetPubKey implements Info interface 200 func (i LegacyMultiInfo) GetPubKey() cryptotypes.PubKey { 201 return i.PubKey 202 } 203 204 // GetAddress implements Info interface 205 func (i LegacyMultiInfo) GetAddress() sdk.AccAddress { 206 return i.PubKey.Address().Bytes() 207 } 208 209 // GetPath implements Info interface 210 func (i LegacyMultiInfo) GetAlgo() hd.PubKeyType { 211 return hd.MultiType 212 } 213 214 // GetPath implements Info interface 215 func (i LegacyMultiInfo) GetPath() (*hd.BIP44Params, error) { 216 return nil, fmt.Errorf("BIP44 Paths are not available for this type") 217 } 218 219 // UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces 220 func (i LegacyMultiInfo) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error { 221 multiPK := i.PubKey.(*multisig.LegacyAminoPubKey) 222 223 return codectypes.UnpackInterfaces(multiPK, unpacker) 224 } 225 226 // encoding info 227 func MarshalInfo(i LegacyInfo) []byte { 228 return legacy.Cdc.MustMarshalLengthPrefixed(i) 229 } 230 231 // decoding info 232 func unMarshalLegacyInfo(bz []byte) (info LegacyInfo, err error) { 233 err = legacy.Cdc.UnmarshalLengthPrefixed(bz, &info) 234 if err != nil { 235 return nil, err 236 } 237 238 // After unmarshalling into &info, if we notice that the info is a 239 // multiInfo, then we unmarshal again, explicitly in a multiInfo this time. 240 // Since multiInfo implements UnpackInterfacesMessage, this will correctly 241 // unpack the underlying anys inside the multiInfo. 242 // 243 // This is a workaround, as go cannot check that an interface (Info) 244 // implements another interface (UnpackInterfacesMessage). 245 _, ok := info.(LegacyMultiInfo) 246 if ok { 247 var multi LegacyMultiInfo 248 err = legacy.Cdc.UnmarshalLengthPrefixed(bz, &multi) 249 250 return multi, err 251 } 252 253 return 254 } 255 256 // privKeyFromLegacyInfo exports a private key from LegacyInfo 257 func privKeyFromLegacyInfo(info LegacyInfo) (cryptotypes.PrivKey, error) { 258 switch linfo := info.(type) { 259 case legacyLocalInfo: 260 if linfo.PrivKeyArmor == "" { 261 return nil, fmt.Errorf("private key not available") 262 } 263 priv, err := legacy.PrivKeyFromBytes([]byte(linfo.PrivKeyArmor)) 264 if err != nil { 265 return nil, err 266 } 267 268 return priv, nil 269 // case legacyLedgerInfo, legacyOfflineInfo, LegacyMultiInfo: 270 default: 271 return nil, fmt.Errorf("only works on local private keys, provided %s", linfo) 272 } 273 }