| 1 | package identity |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | |
| 6 | "github.com/gosuda/portal-tunnel/v2/types" |
| 7 | ) |
| 8 | |
| 9 | type Authority interface { |
| 10 | Identity() types.Identity |
| 11 | SignEthereumPersonalMessage(message string) (string, error) |
| 12 | SignSHA256Secp256k1(payload []byte) (Secp256k1Signature, error) |
| 13 | } |
| 14 | |
| 15 | type LocalAuthority struct { |
| 16 | identity types.Identity |
| 17 | } |
| 18 | |
| 19 | func NewLocalAuthority(raw types.Identity) (LocalAuthority, error) { |
| 20 | normalized, err := normalizeStoredIdentity(raw) |
| 21 | if err != nil { |
| 22 | return LocalAuthority{}, err |
| 23 | } |
| 24 | if normalized.PrivateKey == "" { |
| 25 | return LocalAuthority{}, errors.New("authority private key is required") |
| 26 | } |
| 27 | if normalized.PublicKey == "" { |
| 28 | return LocalAuthority{}, errors.New("authority public key is required") |
| 29 | } |
| 30 | if normalized.Address == "" { |
| 31 | return LocalAuthority{}, errors.New("authority address is required") |
| 32 | } |
| 33 | return LocalAuthority{identity: normalized}, nil |
| 34 | } |
| 35 | |
| 36 | func (a LocalAuthority) Identity() types.Identity { |
| 37 | identity := a.identity.Copy() |
| 38 | identity.PrivateKey = "" |
| 39 | identity.Mnemonic = "" |
| 40 | identity.DerivationPath = "" |
| 41 | identity.TokenSecret = "" |
| 42 | return identity |
| 43 | } |
| 44 | |
| 45 | func (a LocalAuthority) SignEthereumPersonalMessage(message string) (string, error) { |
| 46 | return signEthereumPersonalMessage(message, a.identity.PrivateKey) |
| 47 | } |
| 48 | |
| 49 | func (a LocalAuthority) SignSHA256Secp256k1(payload []byte) (Secp256k1Signature, error) { |
| 50 | privateKey, _, err := parseSecp256k1PrivateKeyHex(a.identity.PrivateKey, true) |
| 51 | if err != nil { |
| 52 | return Secp256k1Signature{}, err |
| 53 | } |
| 54 | return signSHA256Secp256k1(payload, privateKey) |
| 55 | } |