goat-systems / go-tezos

Go Tezos Is a Go library that exposes and builds upon the Tezos RPC.
MIT License
71 stars 45 forks source link

Unsupported Address Formats #184

Open drush opened 3 years ago

drush commented 3 years ago

getCurveByPrefix checks for various prefixes of different lengths, but calls to getCurveByPrefix always pass the first 5 chars so that only the 5 char prefixes can be matched. A simple error case to demonstrate the bug is to test for an edpk.... address. This ill fail because prefix will be the first 5 characters of the address instead of just 4.

func getCurveByPrefix(prefix string) (iCurve, error) {
    if prefix == "edpk" || prefix == "edsk" || prefix == "tz1" || prefix == "edesk" || prefix == "edsig" {
        return &ed25519Curve{}, nil
    }

https://github.com/goat-systems/go-tezos/blob/519084001470cc6fb17c2d79010bec47da4489ff/keys/curve.go#L41

Example caller:

// FromEncryptedSecret returns a new key from an encrypted private key
func FromEncryptedSecret(esk, passwd string) (*Key, error) {
    curve, err := getCurveByPrefix(esk[:5])
    if err != nil {
        return &Key{}, err
    }

https://github.com/goat-systems/go-tezos/blob/519084001470cc6fb17c2d79010bec47da4489ff/keys/key.go#L76

Weltburger commented 3 years ago

Instead, you can use the following code: key, err := keys.FromBase58("edsk...", keys.Ed25519) if err != nil { fmt.Printf("failed to import keys: %s\n", err.Error()) os.Exit(1) } The second parameter depends on the key prefix.