forked from stellar/go
/
from_address.go
49 lines (40 loc) · 1.2 KB
/
from_address.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package keypair
import (
"github.com/stellar/go/strkey"
"github.com/stellar/go/xdr"
"golang.org/x/crypto/ed25519"
)
// FromAddress represents a keypair to which only the address is know. This KP
// can verify signatures, but cannot sign them.
//
// NOTE: ensure the address provided is a valid strkey encoded stellar address.
// Some operations will panic otherwise. It's recommended that you create these
// structs through the Parse() method.
type FromAddress struct {
address string
}
func (kp *FromAddress) Address() string {
return kp.address
}
func (kp *FromAddress) Hint() (r [4]byte) {
copy(r[:], kp.publicKey()[28:])
return
}
func (kp *FromAddress) Verify(input []byte, sig []byte) error {
if len(sig) != 64 {
return ErrInvalidSignature
}
if !ed25519.Verify(kp.publicKey(), input, sig) {
return ErrInvalidSignature
}
return nil
}
func (kp *FromAddress) Sign(input []byte) ([]byte, error) {
return nil, ErrCannotSign
}
func (kp *FromAddress) SignDecorated(input []byte) (xdr.DecoratedSignature, error) {
return xdr.DecoratedSignature{}, ErrCannotSign
}
func (kp *FromAddress) publicKey() ed25519.PublicKey {
return ed25519.PublicKey(strkey.MustDecode(strkey.VersionByteAccountID, kp.address))
}